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_TreeModel.hxx>
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>
26 #include <TDocStd_Application.hxx>
27 #include <TDocStd_Document.hxx>
28 #include <TNaming_Builder.hxx>
29 #include <TNaming_NamedShape.hxx>
31 #include <Standard_WarningsDisable.hxx>
32 #include <QAbstractItemModel>
33 #include <Standard_WarningsRestore.hxx>
35 // =======================================================================
36 // function : Constructor
38 // =======================================================================
39 DFBrowser_TreeModel::DFBrowser_TreeModel (QObject* theParent)
40 : TreeModel_ModelBase (theParent)
44 // =======================================================================
45 // function : InitColumns
47 // =======================================================================
48 void DFBrowser_TreeModel::InitColumns()
50 setHeaderItem (0, TreeModel_HeaderSection ("Name"));
53 // =======================================================================
54 // function : SetModule
56 // =======================================================================
57 void DFBrowser_TreeModel::SetModule (DFBrowser_Module* theModule)
59 DFBrowser_ItemApplicationPtr aRootItem = itemDynamicCast<DFBrowser_ItemApplication> (RootItem (0));
60 aRootItem->SetModule (theModule);
63 // =======================================================================
64 // function : createRootItem
66 // =======================================================================
67 TreeModel_ItemBasePtr DFBrowser_TreeModel::createRootItem (const int)
69 return DFBrowser_ItemApplication::CreateItem (TreeModel_ItemBasePtr());
72 // =======================================================================
75 // =======================================================================
76 void DFBrowser_TreeModel::Init (const Handle(TDocStd_Application)& theApplication)
78 DFBrowser_ItemApplicationPtr aRootItem = itemDynamicCast<DFBrowser_ItemApplication> (RootItem (0));
80 aRootItem->SetApplication (theApplication);
84 // =======================================================================
85 // function : GetTDocStdApplication
87 // =======================================================================
88 Handle(TDocStd_Application) DFBrowser_TreeModel::GetTDocStdApplication() const
90 DFBrowser_ItemApplicationPtr aRootItem = itemDynamicCast<DFBrowser_ItemApplication> (RootItem (0));
91 return aRootItem->GetApplication();
94 // =======================================================================
95 // function : FindIndex
97 // =======================================================================
98 QModelIndex DFBrowser_TreeModel::FindIndex (const TDF_Label& theLabel) const
100 TDF_Label aRoot = theLabel.Root();
102 NCollection_List<TDF_Label> aLabels;
103 aLabels.Prepend (theLabel);
104 TDF_Label aFather = theLabel.Father();
105 if (!aFather.IsNull())
107 while (aFather != aRoot)
109 aLabels.Prepend (aFather);
110 aFather = aFather.Father();
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++)
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)
124 aParentItem = anItem;
125 aParentIndex = anIndex;
126 aDocumentItemFound = true;
130 if (!aDocumentItemFound) // document is not found
131 return QModelIndex();
133 for (NCollection_List<TDF_Label>::const_iterator aLabelIt = aLabels.begin(); aLabelIt != aLabels.end()
134 && aParentIndex.isValid(); aLabelIt++)
136 const TDF_Label aLabel = *aLabelIt;
137 for (int aParentChildId = 0, aCount = aParentItem->rowCount(); aParentChildId < aCount; aParentChildId++)
139 QModelIndex anIndex = index (aParentChildId, 0, aParentIndex);
140 DFBrowser_ItemPtr anItem = itemDynamicCast<DFBrowser_Item> (TreeModel_ModelBase::GetItemByIndex (anIndex));
141 if (anItem->HasAttribute())
144 if (anItem->HasLabel() && anItem->GetLabel().IsEqual (aLabel))
146 aParentItem = anItem;
147 aParentIndex = anIndex;
155 // =======================================================================
156 // function : FindIndexByPath
158 // =======================================================================
159 QModelIndex DFBrowser_TreeModel::FindIndexByPath (const QStringList& theLabelEntries, const QString& theValue) const
161 QModelIndex aFoundIndex;
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++)
168 QModelIndex aParentIndex = index (aDocItemId, 0, aRootIndex);
169 if (!aParentIndex.isValid()) // OCAF document for this document item is not found
171 if (theLabelEntries.size() == 0)
173 aFoundIndex = aParentIndex;
176 TreeModel_ItemBasePtr aParentItem = TreeModel_ModelBase::GetItemByIndex (aParentIndex);
177 for (int aPathId = 1, aPathCount = theLabelEntries.size(); aPathId < aPathCount + 1; aPathId++)
180 if (aPathId < aPathCount)
181 anEntry = theLabelEntries[aPathId];
185 bool aFoundEntry = false;
186 for (int aChildId = 0, aNbChildren = aParentItem->rowCount(); aChildId < aNbChildren; aChildId++)
188 QModelIndex anIndex = index (aChildId, 0, aParentIndex);
189 TreeModel_ItemBasePtr anItemBase = TreeModel_ModelBase::GetItemByIndex (anIndex);
190 DFBrowser_ItemPtr anItem = itemDynamicCast<DFBrowser_Item> (anItemBase);
192 if (aPathId == aPathCount && anItem->HasAttribute())
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)
200 aParentItem = anItem;
201 aParentIndex = anIndex;
206 else if (anItem->HasLabel() &&
207 anEntry == QString (DFBrowserPane_Tools::GetEntry (anItem->GetLabel()).ToCString()))
209 aParentItem = anItem;
210 aParentIndex = anIndex;
216 if (!aFoundEntry) // an entry is not found on some level tree, find it in other documents
219 aFoundIndex = aParentIndex;
225 // =======================================================================
226 // function : FindIndexByAttribute
228 // =======================================================================
229 QModelIndex DFBrowser_TreeModel::FindIndexByAttribute (Handle(TDF_Attribute) theAttribute) const
231 QModelIndex aFoundIndex;
232 const TDF_Label aLabel = theAttribute->Label();
234 QModelIndex aParentIndex = FindIndex (aLabel);
235 if (!aParentIndex.isValid())
238 TreeModel_ItemBasePtr aParentItem = TreeModel_ModelBase::GetItemByIndex (aParentIndex);
239 for (int aChildId = 0, aCount = aParentItem->rowCount(); aChildId < aCount; aChildId++)
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)
246 aFoundIndex = anIndex;
253 // =======================================================================
254 // function : ConvertToIndices
256 // =======================================================================
257 void DFBrowser_TreeModel::ConvertToIndices (const NCollection_List<TDF_Label>& theReferences,
258 QModelIndexList& theIndices)
260 for (NCollection_List<TDF_Label>::Iterator aLabelItr (theReferences); aLabelItr.More(); aLabelItr.Next())
261 theIndices.append (FindIndex (aLabelItr.Value()));
264 // =======================================================================
265 // function : ConvertToIndices
267 // =======================================================================
268 void DFBrowser_TreeModel::ConvertToIndices (const NCollection_List<Handle(TDF_Attribute)>& theReferences,
269 QModelIndexList& theIndices)
271 for (NCollection_List<Handle(TDF_Attribute)>::Iterator anAttrItr (theReferences); anAttrItr.More(); anAttrItr.Next())
272 theIndices.append(FindIndexByAttribute (anAttrItr.Value()));
275 // =======================================================================
278 // =======================================================================
279 QVariant DFBrowser_TreeModel::data (const QModelIndex& theIndex, int theRole) const
281 if (theRole == Qt::BackgroundRole && myHighlightedIndices.contains (theIndex))
282 return DFBrowserPane_Tools::LightHighlightColor();
283 return TreeModel_ModelBase::data (theIndex, theRole);