14bbbdcb |
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 | |
0cb512c0 |
16 | #include <inspector/DFBrowser_TreeLevelView.hxx> |
14bbbdcb |
17 | |
0cb512c0 |
18 | #include <inspector/DFBrowser_Item.hxx> |
19 | #include <inspector/DFBrowser_TreeLevelViewModel.hxx> |
20 | #include <inspector/DFBrowser_Window.hxx> |
21 | #include <inspector/DFBrowserPane_Tools.hxx> |
22 | #include <inspector/TreeModel_ModelBase.hxx> |
14bbbdcb |
23 | |
24 | #include <QItemSelectionModel> |
25 | |
26 | #include <QGridLayout> |
27 | #include <QHeaderView> |
28 | #include <QTableView> |
29 | #include <QWidget> |
30 | |
d2c90917 |
31 | const int LABEL_OR_ATTRIBUTECOLUMN_WIDTH = 160; |
14bbbdcb |
32 | |
33 | // ======================================================================= |
34 | // function : Constructor |
35 | // purpose : |
36 | // ======================================================================= |
37 | DFBrowser_TreeLevelView::DFBrowser_TreeLevelView (QWidget* theParent) |
38 | : QObject (theParent) |
39 | { |
40 | myMainWindow = new QWidget (theParent); |
41 | QGridLayout* aLayout = new QGridLayout (myMainWindow); |
42 | aLayout->setContentsMargins (0, 0, 0, 0); |
43 | |
44 | myTableView = new QTableView (myMainWindow); |
45 | myTableView->setModel (new DFBrowser_TreeLevelViewModel (myTableView)); |
d2c90917 |
46 | myTableView->setColumnWidth (0, LABEL_OR_ATTRIBUTECOLUMN_WIDTH); |
14bbbdcb |
47 | myTableView->setEditTriggers (QAbstractItemView::DoubleClicked); |
d2c90917 |
48 | myTableView->horizontalHeader()->setVisible (false); |
14bbbdcb |
49 | QHeaderView* aVHeader = myTableView->verticalHeader(); |
50 | aVHeader->setVisible (false); |
d2c90917 |
51 | aVHeader->setDefaultSectionSize (aVHeader->minimumSectionSize()); |
14bbbdcb |
52 | myTableView->horizontalHeader()->setStretchLastSection (true); |
53 | aLayout->addWidget (myTableView); |
54 | |
55 | QItemSelectionModel* aSelectionModel = new QItemSelectionModel (myTableView->model()); |
56 | myTableView->setSelectionMode (QAbstractItemView::SingleSelection); |
57 | myTableView->setSelectionModel (aSelectionModel); |
58 | myTableView->setSelectionBehavior (QAbstractItemView::SelectRows); |
59 | connect (aSelectionModel, SIGNAL (selectionChanged (const QItemSelection&, const QItemSelection&)), |
60 | this, SLOT (onTableSelectionChanged (const QItemSelection&, const QItemSelection&))); |
61 | connect (myTableView, SIGNAL (doubleClicked (const QModelIndex&)), |
62 | this, SLOT (onTableDoubleClicked (const QModelIndex&))); |
63 | |
64 | DFBrowser_Window::SetWhiteBackground (myTableView); |
65 | myTableView->setGridStyle (Qt::NoPen); |
66 | } |
67 | |
68 | // ======================================================================= |
69 | // function : clearSelection |
70 | // purpose : |
71 | // ======================================================================= |
72 | void DFBrowser_TreeLevelView::ClearSelection() |
73 | { |
74 | myTableView->selectionModel()->clearSelection(); |
75 | } |
76 | |
77 | // ======================================================================= |
78 | // function : ProcessItem |
79 | // purpose : |
80 | // ======================================================================= |
81 | bool DFBrowser_TreeLevelView::ProcessItem (const QModelIndex& theIndex) |
82 | { |
83 | bool aResult = false; |
84 | TreeModel_ItemBasePtr anItemBase = TreeModel_ModelBase::GetItemByIndex (theIndex); |
85 | if (anItemBase) { |
86 | // use this view for attribute/document/label items |
87 | DFBrowser_ItemPtr anItem = itemDynamicCast<DFBrowser_Item> (anItemBase); |
88 | if (anItem) |
89 | aResult = anItem && !anItem->HasAttribute(); |
90 | else |
91 | aResult = true; // attribute or document item |
92 | } |
93 | return aResult; |
94 | } |
95 | |
96 | // ======================================================================= |
97 | // function : UpdateByTreeSelectionChanged |
98 | // purpose : |
99 | // ======================================================================= |
100 | void DFBrowser_TreeLevelView::UpdateByTreeSelectionChanged (const QItemSelection& theSelected, |
101 | const QItemSelection&) |
102 | { |
103 | QModelIndexList aSelectedIndices = theSelected.indexes(); |
104 | QModelIndexList aFirstColumnSelectedIndices; |
105 | for (QModelIndexList::const_iterator aSelIt = aSelectedIndices.begin(); aSelIt != aSelectedIndices.end(); aSelIt++) |
106 | { |
107 | QModelIndex anIndex = *aSelIt; |
108 | if (anIndex.column() == 0) |
109 | aFirstColumnSelectedIndices.append (anIndex); |
110 | } |
111 | |
112 | if (aFirstColumnSelectedIndices.size() != 1) |
113 | return; |
114 | |
115 | DFBrowser_TreeLevelViewModel* aModel = dynamic_cast<DFBrowser_TreeLevelViewModel*> (myTableView->model()); |
116 | const QModelIndex& anIndex = aFirstColumnSelectedIndices.first(); |
117 | if (DFBrowser_TreeLevelView::ProcessItem(anIndex)) // to Init |
118 | aModel->Init (anIndex); |
119 | else |
120 | aModel->Reset(); |
121 | } |
122 | |
123 | // ======================================================================= |
124 | // function : onTableSelectionChanged |
125 | // purpose : |
126 | // ======================================================================= |
127 | void DFBrowser_TreeLevelView::onTableSelectionChanged (const QItemSelection& theSelected, |
128 | const QItemSelection&) |
129 | { |
130 | QModelIndexList aSelectedIndices = theSelected.indexes(); |
131 | QModelIndex aSelectedIndex = DFBrowser_Window::SingleSelected (aSelectedIndices, 0); |
132 | |
133 | DFBrowser_TreeLevelViewModel* aTableModel = dynamic_cast<DFBrowser_TreeLevelViewModel*> (myTableView->model()); |
134 | if (aTableModel && aTableModel->IsInitialized()) |
135 | { |
136 | const QModelIndex& aTreeViewIndex = aTableModel->GetTreeViewIndex (aSelectedIndex); |
137 | if (aTreeViewIndex.isValid()) |
138 | emit indexSelected (aTreeViewIndex); |
139 | } |
140 | } |
141 | |
142 | // ======================================================================= |
143 | // function : onTableDoubleClicked |
144 | // purpose : |
145 | // ======================================================================= |
146 | void DFBrowser_TreeLevelView::onTableDoubleClicked (const QModelIndex& theIndex) |
147 | { |
148 | DFBrowser_TreeLevelViewModel* aTableModel = dynamic_cast<DFBrowser_TreeLevelViewModel*> (myTableView->model()); |
149 | if (!aTableModel) |
150 | return; |
151 | |
152 | const QModelIndex& aTreeViewIndex = aTableModel->GetTreeViewIndex (theIndex); |
153 | if (aTreeViewIndex.isValid()) |
154 | emit indexDoubleClicked (aTreeViewIndex); |
155 | } |