0031939: Coding - correction of spelling errors in comments [part 2]
[occt.git] / tools / DFBrowser / DFBrowser_TreeLevelLine.cxx
1 // Created on: 2017-06-16
2 // Created by: Natalia ERMOLAEVA
3 // Copyright (c) 2017 OPEN CASCADE SAS
4 //
5 // This file is part of Open CASCADE Technology software library.
6 //
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
12 //
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement. 
15
16 #include <inspector/DFBrowser_TreeLevelLine.hxx>
17
18 #include <inspector/DFBrowser_SearchLine.hxx>
19 #include <inspector/DFBrowser_Window.hxx>
20 #include <inspector/DFBrowser_TreeLevelLineDelegate.hxx>
21 #include <inspector/DFBrowser_TreeLevelLineModel.hxx>
22
23 #include <inspector/DFBrowserPane_Tools.hxx>
24
25 #include <inspector/TreeModel_ModelBase.hxx>
26 #include <inspector/TreeModel_Tools.hxx>
27
28 #include <Standard_WarningsDisable.hxx>
29 #include <QAbstractItemModel>
30 #include <QFrame>
31 #include <QGridLayout>
32 #include <QHeaderView>
33 #include <QItemSelectionModel>
34 #include <QPainter>
35 #include <QScrollBar>
36 #include <QTableView>
37 #include <QToolButton>
38 #include <QWidget>
39 #include <Standard_WarningsRestore.hxx>
40
41 const int HISTORY_SIZE = 10;
42 const int MARGIN_SIZE = 2;
43
44 // =======================================================================
45 // function : Constructor
46 // purpose :
47 // =======================================================================
48 DFBrowser_TreeLevelLine::DFBrowser_TreeLevelLine (QWidget* theParent)
49 : QObject (theParent), mySelectionProcessingBlocked (false), myCurrentHistoryIndex (-1)
50 {
51   myMainWindow = new QWidget (theParent);
52   QGridLayout* aLayout = new QGridLayout (myMainWindow);
53   aLayout->setContentsMargins (MARGIN_SIZE, MARGIN_SIZE, MARGIN_SIZE, MARGIN_SIZE);
54
55   myBackwardButton = new QToolButton (myMainWindow);
56   myBackwardButton->setIcon (QIcon (":/icons/treeline_backward.png"));
57   myBackwardButton->setToolTip (tr ("Backward"));
58   connect (myBackwardButton, SIGNAL (clicked()), this, SLOT (onActionClicked()));
59   aLayout->addWidget (myBackwardButton, 0, 0);
60
61   myForwardButton = new QToolButton (myMainWindow);
62   myForwardButton->setIcon (QIcon (":/icons/treeline_forward.png"));
63   myForwardButton->setToolTip (tr ("Forward"));
64   connect (myForwardButton, SIGNAL (clicked()), this, SLOT (onActionClicked()));
65   aLayout->addWidget (myForwardButton, 0, 1);
66
67   myTableView = new QTableView (myMainWindow);
68   myTableView->horizontalHeader()->setVisible (false);
69   QHeaderView* aVHeader = myTableView->verticalHeader();
70   aVHeader->setVisible (false);
71   int aDefCellSize = aVHeader->minimumSectionSize() + TreeModel_Tools::HeaderSectionMargin();
72   aVHeader->setDefaultSectionSize (aDefCellSize);
73   aLayout->addWidget (myTableView, 0, 2);
74
75   myTableView->setFixedHeight (aDefCellSize);
76   myTableView->horizontalHeader()->setMinimumSectionSize (5); // it will be resized by context
77   myTableView->setHorizontalScrollMode (QAbstractItemView::ScrollPerItem);
78   myTableView->setHorizontalScrollBarPolicy (Qt::ScrollBarAlwaysOff); //! TEMPORARY
79   myTableView->setShowGrid (false);
80
81   DFBrowser_TreeLevelLineModel* aHModel = new DFBrowser_TreeLevelLineModel (myTableView);
82   myTableView->setModel (aHModel);
83
84   QItemSelectionModel* aSelectionModel = new QItemSelectionModel (aHModel);
85   myTableView->setSelectionMode (QAbstractItemView::SingleSelection);
86   myTableView->setSelectionModel (aSelectionModel);
87   connect (aSelectionModel, SIGNAL (selectionChanged (const QItemSelection&, const QItemSelection&)),
88            this, SLOT (onTableSelectionChanged (const QItemSelection&, const QItemSelection&)));
89
90   // highlight for items
91   myTableView->viewport()->setAttribute (Qt::WA_Hover);
92   myTableView->setItemDelegate (new DFBrowser_TreeLevelLineDelegate (myTableView));
93
94   aLayout->setColumnStretch (2, 1);
95
96   myUpdateButton = new QToolButton (myMainWindow);
97   myUpdateButton->setIcon (QIcon (":/icons/treeline_update.png"));
98   myUpdateButton->setToolTip (tr ("Update Tree Model"));
99   connect (myUpdateButton, SIGNAL (clicked()), this, SLOT (onActionClicked()));
100   aLayout->addWidget (myUpdateButton, 0, 3);
101
102   mySearchLine = new DFBrowser_SearchLine (myMainWindow);
103   aLayout->addWidget (mySearchLine, 0, 4);
104
105   updateActionsState();
106 }
107
108 // =======================================================================
109 // function : clear
110 // purpose :
111 // =======================================================================
112 void DFBrowser_TreeLevelLine::ClearHistory()
113 {
114   myHistoryIndices.clear();
115   setCurrentHistoryIndex (-1);
116 }
117
118 // =======================================================================
119 // function : onSelectionChanged
120 // purpose :
121 // =======================================================================
122 void DFBrowser_TreeLevelLine::OnTreeViewSelectionChanged (const QItemSelection& theSelected,
123                                                           const QItemSelection&)
124 {
125   QModelIndexList aSelectedIndices = theSelected.indexes();
126   QModelIndex aSelectedIndex = TreeModel_ModelBase::SingleSelected (aSelectedIndices, 0);
127
128   if (!mySelectionProcessingBlocked) // we're processing action click
129     setForwardIndex (aSelectedIndex);
130
131   bool isBlocked = mySelectionProcessingBlocked;
132   // block selection processing in order to avoid circling by processing table selection changing
133   mySelectionProcessingBlocked = true;
134   DFBrowser_TreeLevelLineModel* aModel = dynamic_cast<DFBrowser_TreeLevelLineModel*> (myTableView->model());
135   aModel->Init (aSelectedIndex);
136   myTableView->selectionModel()->clearSelection();
137   myTableView->resizeColumnsToContents();
138
139   myTableView->scrollTo (myTableView->model()->index (0, myTableView->model()->columnCount()-1));
140
141   mySelectionProcessingBlocked = isBlocked;
142 }
143
144 // =======================================================================
145 // function : onTableSelectionChanged
146 // purpose :
147 // =======================================================================
148 void DFBrowser_TreeLevelLine::onTableSelectionChanged (const QItemSelection& theSelected,
149                                                        const QItemSelection&)
150 {
151   if (mySelectionProcessingBlocked)
152     return;
153
154   DFBrowser_TreeLevelLineModel* aTableModel = dynamic_cast<DFBrowser_TreeLevelLineModel*> (myTableView->model());
155   if (!aTableModel)
156     return;
157
158   QModelIndex aSelectedIndex = TreeModel_ModelBase::SingleSelected (theSelected.indexes(), 0, Qt::Vertical);
159   emit indexSelected (aTableModel->GetTreeViewIndex (aSelectedIndex));
160 }
161
162 // =======================================================================
163 // function : onActionClicked
164 // purpose :
165 // =======================================================================
166 void DFBrowser_TreeLevelLine::onActionClicked()
167 {
168   QToolButton* aSender = (QToolButton*)sender();
169   if (aSender == myBackwardButton || aSender == myForwardButton)
170   {
171     bool aBlocked = mySelectionProcessingBlocked;
172     mySelectionProcessingBlocked = true;
173     QModelIndex anIndex;
174     if (aSender == myBackwardButton)
175     {
176       anIndex = getBackwardIndex();
177       if (anIndex.isValid())
178         setCurrentHistoryIndex (myCurrentHistoryIndex - 1);
179     }
180     else
181     {
182       anIndex = getForwardIndex();
183       if (anIndex.isValid())
184         setCurrentHistoryIndex (myCurrentHistoryIndex + 1);
185     }
186     if (anIndex.isValid())
187       emit indexSelected (anIndex);
188     mySelectionProcessingBlocked = aBlocked;
189   }
190   else if (aSender == myUpdateButton)
191     emit updateClicked();
192 }
193
194 // =======================================================================
195 // function : getBackwardIndex
196 // purpose :
197 // =======================================================================
198 QModelIndex DFBrowser_TreeLevelLine::getBackwardIndex()
199 {
200   return myCurrentHistoryIndex > 0 ? myHistoryIndices[myCurrentHistoryIndex-1] : QModelIndex();
201 }
202
203 // =======================================================================
204 // function : getForwardIndex
205 // purpose :
206 // =======================================================================
207 QModelIndex DFBrowser_TreeLevelLine::getForwardIndex()
208 {
209   return (myCurrentHistoryIndex >= 0 && myCurrentHistoryIndex + 1 < myHistoryIndices.count())
210      ? myHistoryIndices[myCurrentHistoryIndex + 1] : QModelIndex();
211 }
212
213 // =======================================================================
214 // function : setForwardIndex
215 // purpose :
216 // =======================================================================
217 void DFBrowser_TreeLevelLine::setForwardIndex (const QModelIndex& theIndex)
218 {
219   while (myCurrentHistoryIndex != myHistoryIndices.count() - 1)
220     myHistoryIndices.removeLast();
221
222   myHistoryIndices.append (theIndex);
223   if (myHistoryIndices.size() > HISTORY_SIZE)
224     myHistoryIndices.removeFirst();
225
226   setCurrentHistoryIndex (myHistoryIndices.count() - 1);
227 }
228
229 // =======================================================================
230 // function : setCurrentHistoryIndex
231 // purpose :
232 // =======================================================================
233 void DFBrowser_TreeLevelLine::setCurrentHistoryIndex (const int theIndexId)
234 {
235   myCurrentHistoryIndex = theIndexId;
236   updateActionsState();
237 }
238
239 // =======================================================================
240 // function : updateActionsState
241 // purpose :
242 // =======================================================================
243 void DFBrowser_TreeLevelLine::updateActionsState()
244 {
245   if (myCurrentHistoryIndex < 0)
246   {
247     myBackwardButton->setEnabled (false);
248     myForwardButton->setEnabled (false);
249   }
250   else
251   {
252     myBackwardButton->setEnabled (myCurrentHistoryIndex > 0);
253     myForwardButton->setEnabled (myCurrentHistoryIndex < myHistoryIndices.size() - 1);
254   }
255 }