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_TreeModel.hxx>
17
18 #include <inspector/DFBrowser_Item.hxx>
19 #include <inspector/DFBrowser_ItemApplication.hxx>
20 #include <inspector/DFBrowser_ItemDocument.hxx>
21 #include <inspector/DFBrowser_Module.hxx>
22 #include <inspector/DFBrowser_Window.hxx>
23 #include <inspector/DFBrowserPane_Tools.hxx>
24 #include <NCollection_List.hxx>
25
26 #include <TDocStd_Application.hxx>
27 #include <TDocStd_Document.hxx>
28 #include <TNaming_Builder.hxx>
29 #include <TNaming_NamedShape.hxx>
30
31 #include <Standard_WarningsDisable.hxx>
32 #include <QAbstractItemModel>
33 #include <Standard_WarningsRestore.hxx>
34
35 // =======================================================================
36 // function : Constructor
37 // purpose :
38 // =======================================================================
DFBrowser_TreeModel(QObject * theParent)39 DFBrowser_TreeModel::DFBrowser_TreeModel (QObject* theParent)
40 : TreeModel_ModelBase (theParent)
41 {
42 }
43
44 // =======================================================================
45 // function : InitColumns
46 // purpose :
47 // =======================================================================
InitColumns()48 void DFBrowser_TreeModel::InitColumns()
49 {
50 setHeaderItem (0, TreeModel_HeaderSection ("Name"));
51 }
52
53 // =======================================================================
54 // function : SetModule
55 // purpose :
56 // =======================================================================
SetModule(DFBrowser_Module * theModule)57 void DFBrowser_TreeModel::SetModule (DFBrowser_Module* theModule)
58 {
59 DFBrowser_ItemApplicationPtr aRootItem = itemDynamicCast<DFBrowser_ItemApplication> (RootItem (0));
60 aRootItem->SetModule (theModule);
61 }
62
63 // =======================================================================
64 // function : createRootItem
65 // purpose :
66 // =======================================================================
createRootItem(const int)67 TreeModel_ItemBasePtr DFBrowser_TreeModel::createRootItem (const int)
68 {
69 return DFBrowser_ItemApplication::CreateItem (TreeModel_ItemBasePtr());
70 }
71
72 // =======================================================================
73 // function : Init
74 // purpose :
75 // =======================================================================
Init(const Handle (TDocStd_Application)& theApplication)76 void DFBrowser_TreeModel::Init (const Handle(TDocStd_Application)& theApplication)
77 {
78 DFBrowser_ItemApplicationPtr aRootItem = itemDynamicCast<DFBrowser_ItemApplication> (RootItem (0));
79 Reset();
80 aRootItem->SetApplication (theApplication);
81 EmitLayoutChanged();
82 }
83
84 // =======================================================================
85 // function : GetTDocStdApplication
86 // purpose :
87 // =======================================================================
Handle(TDocStd_Application)88 Handle(TDocStd_Application) DFBrowser_TreeModel::GetTDocStdApplication() const
89 {
90 DFBrowser_ItemApplicationPtr aRootItem = itemDynamicCast<DFBrowser_ItemApplication> (RootItem (0));
91 return aRootItem->GetApplication();
92 }
93
94 // =======================================================================
95 // function : FindIndex
96 // purpose :
97 // =======================================================================
FindIndex(const TDF_Label & theLabel) const98 QModelIndex DFBrowser_TreeModel::FindIndex (const TDF_Label& theLabel) const
99 {
100 TDF_Label aRoot = theLabel.Root();
101
102 NCollection_List<TDF_Label> aLabels;
103 aLabels.Prepend (theLabel);
104 TDF_Label aFather = theLabel.Father();
105 if (!aFather.IsNull())
106 {
107 while (aFather != aRoot)
108 {
109 aLabels.Prepend (aFather);
110 aFather = aFather.Father();
111 }
112 }
113 bool aDocumentItemFound = false;
114 QModelIndex aParentIndex = index (0, 0);
115 TreeModel_ItemBasePtr aParentItem = TreeModel_ModelBase::GetItemByIndex (aParentIndex); // application item
116 // find document, where label of document item is equal to Root label
117 for (int aChildId = 0, aCount = aParentItem->rowCount(); aChildId < aCount; aChildId++)
118 {
119 QModelIndex anIndex = index (aChildId, 0, aParentIndex);
120 TreeModel_ItemBasePtr anItemBase = TreeModel_ModelBase::GetItemByIndex (anIndex);
121 DFBrowser_ItemDocumentPtr anItem = itemDynamicCast<DFBrowser_ItemDocument> (anItemBase);
122 if (anItem->GetLabel() == aRoot)
123 {
124 aParentItem = anItem;
125 aParentIndex = anIndex;
126 aDocumentItemFound = true;
127 break;
128 }
129 }
130 if (!aDocumentItemFound) // document is not found
131 return QModelIndex();
132
133 for (NCollection_List<TDF_Label>::const_iterator aLabelIt = aLabels.begin(); aLabelIt != aLabels.end()
134 && aParentIndex.isValid(); aLabelIt++)
135 {
136 const TDF_Label aLabel = *aLabelIt;
137 for (int aParentChildId = 0, aCount = aParentItem->rowCount(); aParentChildId < aCount; aParentChildId++)
138 {
139 QModelIndex anIndex = index (aParentChildId, 0, aParentIndex);
140 DFBrowser_ItemPtr anItem = itemDynamicCast<DFBrowser_Item> (TreeModel_ModelBase::GetItemByIndex (anIndex));
141 if (anItem->HasAttribute())
142 continue;
143
144 if (anItem->HasLabel() && anItem->GetLabel().IsEqual (aLabel))
145 {
146 aParentItem = anItem;
147 aParentIndex = anIndex;
148 break;
149 }
150 }
151 }
152 return aParentIndex;
153 }
154
155 // =======================================================================
156 // function : FindIndexByPath
157 // purpose :
158 // =======================================================================
FindIndexByPath(const QStringList & theLabelEntries,const QString & theValue) const159 QModelIndex DFBrowser_TreeModel::FindIndexByPath (const QStringList& theLabelEntries, const QString& theValue) const
160 {
161 QModelIndex aFoundIndex;
162
163 QModelIndex aRootIndex = index (0, 0);
164 TreeModel_ItemBasePtr aRootItem = TreeModel_ModelBase::GetItemByIndex (aRootIndex); // application item
165 // find document, where label of document item is equal to Root label
166 for (int aDocItemId = 0, aNbDocItems = aRootItem->rowCount(); aDocItemId < aNbDocItems && !aFoundIndex.isValid(); aDocItemId++)
167 {
168 QModelIndex aParentIndex = index (aDocItemId, 0, aRootIndex);
169 if (!aParentIndex.isValid()) // OCAF document for this document item is not found
170 continue;
171 if (theLabelEntries.size() == 0)
172 {
173 aFoundIndex = aParentIndex;
174 break;
175 }
176 TreeModel_ItemBasePtr aParentItem = TreeModel_ModelBase::GetItemByIndex (aParentIndex);
177 for (int aPathId = 1, aPathCount = theLabelEntries.size(); aPathId < aPathCount + 1; aPathId++)
178 {
179 QString anEntry;
180 if (aPathId < aPathCount)
181 anEntry = theLabelEntries[aPathId];
182 else
183 anEntry = theValue;
184
185 bool aFoundEntry = false;
186 for (int aChildId = 0, aNbChildren = aParentItem->rowCount(); aChildId < aNbChildren; aChildId++)
187 {
188 QModelIndex anIndex = index (aChildId, 0, aParentIndex);
189 TreeModel_ItemBasePtr anItemBase = TreeModel_ModelBase::GetItemByIndex (anIndex);
190 DFBrowser_ItemPtr anItem = itemDynamicCast<DFBrowser_Item> (anItemBase);
191
192 if (aPathId == aPathCount && anItem->HasAttribute())
193 {
194 // processing attribute in theValue
195 DFBrowser_ItemApplicationPtr aRootAppItem = itemDynamicCast<DFBrowser_ItemApplication>(RootItem (0));
196 QString anAttributeInfo = DFBrowser_Module::GetAttributeInfo (anItem->GetAttribute(), aRootAppItem->GetModule(),
197 Qt::DisplayRole, 0).toString();
198 if (anAttributeInfo == anEntry)
199 {
200 aParentItem = anItem;
201 aParentIndex = anIndex;
202 aFoundEntry = true;
203 break;
204 }
205 }
206 else if (anItem->HasLabel() &&
207 anEntry == QString (DFBrowserPane_Tools::GetEntry (anItem->GetLabel()).ToCString()))
208 {
209 aParentItem = anItem;
210 aParentIndex = anIndex;
211 aFoundEntry = true;
212 break;
213 }
214
215 }
216 if (!aFoundEntry) // an entry is not found on some level tree, find it in other documents
217 break;
218 else
219 aFoundIndex = aParentIndex;
220 }
221 }
222 return aFoundIndex;
223 }
224
225 // =======================================================================
226 // function : FindIndexByAttribute
227 // purpose :
228 // =======================================================================
FindIndexByAttribute(Handle (TDF_Attribute)theAttribute) const229 QModelIndex DFBrowser_TreeModel::FindIndexByAttribute (Handle(TDF_Attribute) theAttribute) const
230 {
231 QModelIndex aFoundIndex;
232 const TDF_Label aLabel = theAttribute->Label();
233
234 QModelIndex aParentIndex = FindIndex (aLabel);
235 if (!aParentIndex.isValid())
236 return aFoundIndex;
237
238 TreeModel_ItemBasePtr aParentItem = TreeModel_ModelBase::GetItemByIndex (aParentIndex);
239 for (int aChildId = 0, aCount = aParentItem->rowCount(); aChildId < aCount; aChildId++)
240 {
241 QModelIndex anIndex = index (aChildId, 0, aParentIndex);
242 TreeModel_ItemBasePtr anItemBase = TreeModel_ModelBase::GetItemByIndex (anIndex);
243 DFBrowser_ItemPtr anItem = itemDynamicCast<DFBrowser_Item> (anItemBase);
244 if (anItem->GetAttribute() == theAttribute)
245 {
246 aFoundIndex = anIndex;
247 break;
248 }
249 }
250 return aFoundIndex;
251 }
252
253 // =======================================================================
254 // function : ConvertToIndices
255 // purpose :
256 // =======================================================================
ConvertToIndices(const NCollection_List<TDF_Label> & theReferences,QModelIndexList & theIndices)257 void DFBrowser_TreeModel::ConvertToIndices (const NCollection_List<TDF_Label>& theReferences,
258 QModelIndexList& theIndices)
259 {
260 for (NCollection_List<TDF_Label>::Iterator aLabelItr (theReferences); aLabelItr.More(); aLabelItr.Next())
261 theIndices.append (FindIndex (aLabelItr.Value()));
262 }
263
264 // =======================================================================
265 // function : ConvertToIndices
266 // purpose :
267 // =======================================================================
ConvertToIndices(const NCollection_List<Handle (TDF_Attribute)> & theReferences,QModelIndexList & theIndices)268 void DFBrowser_TreeModel::ConvertToIndices (const NCollection_List<Handle(TDF_Attribute)>& theReferences,
269 QModelIndexList& theIndices)
270 {
271 for (NCollection_List<Handle(TDF_Attribute)>::Iterator anAttrItr (theReferences); anAttrItr.More(); anAttrItr.Next())
272 theIndices.append(FindIndexByAttribute (anAttrItr.Value()));
273 }
274
275 // =======================================================================
276 // function : data
277 // purpose :
278 // =======================================================================
data(const QModelIndex & theIndex,int theRole) const279 QVariant DFBrowser_TreeModel::data (const QModelIndex& theIndex, int theRole) const
280 {
281 if (theRole == Qt::BackgroundRole && myHighlightedIndices.contains (theIndex))
282 return DFBrowserPane_Tools::LightHighlightColor();
283 return TreeModel_ModelBase::data (theIndex, theRole);
284 }
285