1 // Created on: 2017-06-16
2 // Created by: Natalia ERMOLAEVA
3 // Copyright (c) 2017 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
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.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
16 #include <inspector/DFBrowser_TreeLevelLine.hxx>
18 #include <inspector/DFBrowser_SearchLine.hxx>
19 #include <inspector/DFBrowser_Window.hxx>
20 #include <inspector/DFBrowser_TreeLevelLineDelegate.hxx>
21 #include <inspector/DFBrowser_TreeLevelLineModel.hxx>
23 #include <inspector/DFBrowserPane_Tools.hxx>
25 #include <inspector/TreeModel_ModelBase.hxx>
26 #include <inspector/TreeModel_Tools.hxx>
28 #include <Standard_WarningsDisable.hxx>
29 #include <QAbstractItemModel>
31 #include <QGridLayout>
32 #include <QHeaderView>
33 #include <QItemSelectionModel>
37 #include <QToolButton>
39 #include <Standard_WarningsRestore.hxx>
41 const int HISTORY_SIZE = 10;
42 const int MARGIN_SIZE = 2;
44 // =======================================================================
45 // function : Constructor
47 // =======================================================================
48 DFBrowser_TreeLevelLine::DFBrowser_TreeLevelLine (QWidget* theParent)
49 : QObject (theParent), mySelectionProcessingBlocked (false), myCurrentHistoryIndex (-1)
51 myMainWindow = new QWidget (theParent);
52 QGridLayout* aLayout = new QGridLayout (myMainWindow);
53 aLayout->setContentsMargins (MARGIN_SIZE, MARGIN_SIZE, MARGIN_SIZE, MARGIN_SIZE);
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);
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);
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);
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);
81 DFBrowser_TreeLevelLineModel* aHModel = new DFBrowser_TreeLevelLineModel (myTableView);
82 myTableView->setModel (aHModel);
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&)));
90 // highlight for items
91 myTableView->viewport()->setAttribute (Qt::WA_Hover);
92 myTableView->setItemDelegate (new DFBrowser_TreeLevelLineDelegate (myTableView));
94 aLayout->setColumnStretch (2, 1);
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);
102 mySearchLine = new DFBrowser_SearchLine (myMainWindow);
103 aLayout->addWidget (mySearchLine, 0, 4);
105 updateActionsState();
108 // =======================================================================
111 // =======================================================================
112 void DFBrowser_TreeLevelLine::ClearHistory()
114 myHistoryIndices.clear();
115 setCurrentHistoryIndex (-1);
118 // =======================================================================
119 // function : onSelectionChanged
121 // =======================================================================
122 void DFBrowser_TreeLevelLine::OnTreeViewSelectionChanged (const QItemSelection& theSelected,
123 const QItemSelection&)
125 QModelIndexList aSelectedIndices = theSelected.indexes();
126 QModelIndex aSelectedIndex = TreeModel_ModelBase::SingleSelected (aSelectedIndices, 0);
128 if (!mySelectionProcessingBlocked) // we're processing action click
129 setForwardIndex (aSelectedIndex);
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();
139 myTableView->scrollTo (myTableView->model()->index (0, myTableView->model()->columnCount()-1));
141 mySelectionProcessingBlocked = isBlocked;
144 // =======================================================================
145 // function : onTableSelectionChanged
147 // =======================================================================
148 void DFBrowser_TreeLevelLine::onTableSelectionChanged (const QItemSelection& theSelected,
149 const QItemSelection&)
151 if (mySelectionProcessingBlocked)
154 DFBrowser_TreeLevelLineModel* aTableModel = dynamic_cast<DFBrowser_TreeLevelLineModel*> (myTableView->model());
158 QModelIndex aSelectedIndex = TreeModel_ModelBase::SingleSelected (theSelected.indexes(), 0, Qt::Vertical);
159 emit indexSelected (aTableModel->GetTreeViewIndex (aSelectedIndex));
162 // =======================================================================
163 // function : onActionClicked
165 // =======================================================================
166 void DFBrowser_TreeLevelLine::onActionClicked()
168 QToolButton* aSender = (QToolButton*)sender();
169 if (aSender == myBackwardButton || aSender == myForwardButton)
171 bool aBlocked = mySelectionProcessingBlocked;
172 mySelectionProcessingBlocked = true;
174 if (aSender == myBackwardButton)
176 anIndex = getBackwardIndex();
177 if (anIndex.isValid())
178 setCurrentHistoryIndex (myCurrentHistoryIndex - 1);
182 anIndex = getForwardIndex();
183 if (anIndex.isValid())
184 setCurrentHistoryIndex (myCurrentHistoryIndex + 1);
186 if (anIndex.isValid())
187 emit indexSelected (anIndex);
188 mySelectionProcessingBlocked = aBlocked;
190 else if (aSender == myUpdateButton)
191 emit updateClicked();
194 // =======================================================================
195 // function : getBackwardIndex
197 // =======================================================================
198 QModelIndex DFBrowser_TreeLevelLine::getBackwardIndex()
200 return myCurrentHistoryIndex > 0 ? myHistoryIndices[myCurrentHistoryIndex-1] : QModelIndex();
203 // =======================================================================
204 // function : getForwardIndex
206 // =======================================================================
207 QModelIndex DFBrowser_TreeLevelLine::getForwardIndex()
209 return (myCurrentHistoryIndex >= 0 && myCurrentHistoryIndex + 1 < myHistoryIndices.count())
210 ? myHistoryIndices[myCurrentHistoryIndex + 1] : QModelIndex();
213 // =======================================================================
214 // function : setForwardIndex
216 // =======================================================================
217 void DFBrowser_TreeLevelLine::setForwardIndex (const QModelIndex& theIndex)
219 while (myCurrentHistoryIndex != myHistoryIndices.count() - 1)
220 myHistoryIndices.removeLast();
222 myHistoryIndices.append (theIndex);
223 if (myHistoryIndices.size() > HISTORY_SIZE)
224 myHistoryIndices.removeFirst();
226 setCurrentHistoryIndex (myHistoryIndices.count() - 1);
229 // =======================================================================
230 // function : setCurrentHistoryIndex
232 // =======================================================================
233 void DFBrowser_TreeLevelLine::setCurrentHistoryIndex (const int theIndexId)
235 myCurrentHistoryIndex = theIndexId;
236 updateActionsState();
239 // =======================================================================
240 // function : updateActionsState
242 // =======================================================================
243 void DFBrowser_TreeLevelLine::updateActionsState()
245 if (myCurrentHistoryIndex < 0)
247 myBackwardButton->setEnabled (false);
248 myForwardButton->setEnabled (false);
252 myBackwardButton->setEnabled (myCurrentHistoryIndex > 0);
253 myForwardButton->setEnabled (myCurrentHistoryIndex < myHistoryIndices.size() - 1);