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 const int COLUMN_NAME_WIDTH = 300;
37 // =======================================================================
38 // function : Constructor
40 // =======================================================================
41 DFBrowser_TreeModel::DFBrowser_TreeModel (QObject* theParent)
42 : TreeModel_ModelBase (theParent)
46 // =======================================================================
47 // function : InitColumns
49 // =======================================================================
50 void DFBrowser_TreeModel::InitColumns()
52 setHeaderItem (0, TreeModel_HeaderSection ("Name"));
55 // =======================================================================
56 // function : SetModule
58 // =======================================================================
59 void DFBrowser_TreeModel::SetModule (DFBrowser_Module* theModule)
61 DFBrowser_ItemApplicationPtr aRootItem = itemDynamicCast<DFBrowser_ItemApplication> (RootItem (0));
62 aRootItem->SetModule (theModule);
65 // =======================================================================
66 // function : createRootItem
68 // =======================================================================
69 TreeModel_ItemBasePtr DFBrowser_TreeModel::createRootItem (const int)
71 return DFBrowser_ItemApplication::CreateItem (TreeModel_ItemBasePtr());
74 // =======================================================================
77 // =======================================================================
78 void DFBrowser_TreeModel::Init (const Handle(TDocStd_Application)& theApplication)
80 DFBrowser_ItemApplicationPtr aRootItem = itemDynamicCast<DFBrowser_ItemApplication> (RootItem (0));
82 aRootItem->SetApplication (theApplication);
86 // =======================================================================
87 // function : GetTDocStdApplication
89 // =======================================================================
90 Handle(TDocStd_Application) DFBrowser_TreeModel::GetTDocStdApplication() const
92 DFBrowser_ItemApplicationPtr aRootItem = itemDynamicCast<DFBrowser_ItemApplication> (RootItem (0));
93 return aRootItem->GetApplication();
96 // =======================================================================
97 // function : FindIndex
99 // =======================================================================
100 QModelIndex DFBrowser_TreeModel::FindIndex (const TDF_Label& theLabel) const
102 TDF_Label aRoot = theLabel.Root();
104 NCollection_List<TDF_Label> aLabels;
105 aLabels.Prepend (theLabel);
106 TDF_Label aFather = theLabel.Father();
107 if (!aFather.IsNull())
109 while (aFather != aRoot)
111 aLabels.Prepend (aFather);
112 aFather = aFather.Father();
115 bool aDocumentItemFound = false;
116 QModelIndex aParentIndex = index (0, 0);
117 TreeModel_ItemBasePtr aParentItem = TreeModel_ModelBase::GetItemByIndex (aParentIndex); // application item
118 // find document, where label of document item is equal to Root label
119 for (int aChildId = 0, aCount = aParentItem->rowCount(); aChildId < aCount; aChildId++)
121 QModelIndex anIndex = index (aChildId, 0, aParentIndex);
122 TreeModel_ItemBasePtr anItemBase = TreeModel_ModelBase::GetItemByIndex (anIndex);
123 DFBrowser_ItemDocumentPtr anItem = itemDynamicCast<DFBrowser_ItemDocument> (anItemBase);
124 if (anItem->GetLabel() == aRoot)
126 aParentItem = anItem;
127 aParentIndex = anIndex;
128 aDocumentItemFound = true;
132 if (!aDocumentItemFound) // document is not found
133 return QModelIndex();
135 for (NCollection_List<TDF_Label>::const_iterator aLabelIt = aLabels.begin(); aLabelIt != aLabels.end()
136 && aParentIndex.isValid(); aLabelIt++)
138 const TDF_Label aLabel = *aLabelIt;
139 for (int aParentChildId = 0, aCount = aParentItem->rowCount(); aParentChildId < aCount; aParentChildId++)
141 QModelIndex anIndex = index (aParentChildId, 0, aParentIndex);
142 DFBrowser_ItemPtr anItem = itemDynamicCast<DFBrowser_Item> (TreeModel_ModelBase::GetItemByIndex (anIndex));
143 if (anItem->HasAttribute())
146 if (anItem->HasLabel() && anItem->GetLabel().IsEqual (aLabel))
148 aParentItem = anItem;
149 aParentIndex = anIndex;
157 // =======================================================================
158 // function : FindIndexByPath
160 // =======================================================================
161 QModelIndex DFBrowser_TreeModel::FindIndexByPath (const QStringList& theLabelEntries, const QString& theValue) const
163 QModelIndex aFoundIndex;
165 QModelIndex aRootIndex = index (0, 0);
166 TreeModel_ItemBasePtr aRootItem = TreeModel_ModelBase::GetItemByIndex (aRootIndex); // application item
167 // find document, where label of document item is equal to Root label
168 for (int aDocItemId = 0, aNbDocItems = aRootItem->rowCount(); aDocItemId < aNbDocItems && !aFoundIndex.isValid(); aDocItemId++)
170 QModelIndex aParentIndex = index (aDocItemId, 0, aRootIndex);
171 if (!aParentIndex.isValid()) // OCAF document for this document item is not found
173 if (theLabelEntries.size() == 0)
175 aFoundIndex = aParentIndex;
178 TreeModel_ItemBasePtr aParentItem = TreeModel_ModelBase::GetItemByIndex (aParentIndex);
179 for (int aPathId = 1, aPathCount = theLabelEntries.size(); aPathId < aPathCount + 1; aPathId++)
182 if (aPathId < aPathCount)
183 anEntry = theLabelEntries[aPathId];
187 bool aFoundEntry = false;
188 for (int aChildId = 0, aNbChildren = aParentItem->rowCount(); aChildId < aNbChildren; aChildId++)
190 QModelIndex anIndex = index (aChildId, 0, aParentIndex);
191 TreeModel_ItemBasePtr anItemBase = TreeModel_ModelBase::GetItemByIndex (anIndex);
192 DFBrowser_ItemPtr anItem = itemDynamicCast<DFBrowser_Item> (anItemBase);
194 if (aPathId == aPathCount && anItem->HasAttribute())
196 // processing attribute in theValue
197 DFBrowser_ItemApplicationPtr aRootAppItem = itemDynamicCast<DFBrowser_ItemApplication>(RootItem (0));
198 QString anAttributeInfo = DFBrowser_Module::GetAttributeInfo (anItem->GetAttribute(), aRootAppItem->GetModule(),
199 Qt::DisplayRole, 0).toString();
200 if (anAttributeInfo == anEntry)
202 aParentItem = anItem;
203 aParentIndex = anIndex;
208 else if (anItem->HasLabel() &&
209 anEntry == QString (DFBrowserPane_Tools::GetEntry (anItem->GetLabel()).ToCString()))
211 aParentItem = anItem;
212 aParentIndex = anIndex;
218 if (!aFoundEntry) // an entry is not found on some level tree, find it in other documents
221 aFoundIndex = aParentIndex;
227 // =======================================================================
228 // function : FindIndexByAttribute
230 // =======================================================================
231 QModelIndex DFBrowser_TreeModel::FindIndexByAttribute (Handle(TDF_Attribute) theAttribute) const
233 QModelIndex aFoundIndex;
234 const TDF_Label aLabel = theAttribute->Label();
236 QModelIndex aParentIndex = FindIndex (aLabel);
237 if (!aParentIndex.isValid())
240 TreeModel_ItemBasePtr aParentItem = TreeModel_ModelBase::GetItemByIndex (aParentIndex);
241 for (int aChildId = 0, aCount = aParentItem->rowCount(); aChildId < aCount; aChildId++)
243 QModelIndex anIndex = index (aChildId, 0, aParentIndex);
244 TreeModel_ItemBasePtr anItemBase = TreeModel_ModelBase::GetItemByIndex (anIndex);
245 DFBrowser_ItemPtr anItem = itemDynamicCast<DFBrowser_Item> (anItemBase);
246 if (anItem->GetAttribute() == theAttribute)
248 aFoundIndex = anIndex;
255 // =======================================================================
256 // function : ConvertToIndices
258 // =======================================================================
259 void DFBrowser_TreeModel::ConvertToIndices (const NCollection_List<TDF_Label>& theReferences,
260 QModelIndexList& theIndices)
262 for (NCollection_List<TDF_Label>::Iterator aLabelItr (theReferences); aLabelItr.More(); aLabelItr.Next())
263 theIndices.append (FindIndex (aLabelItr.Value()));
266 // =======================================================================
267 // function : ConvertToIndices
269 // =======================================================================
270 void DFBrowser_TreeModel::ConvertToIndices (const NCollection_List<Handle(TDF_Attribute)>& theReferences,
271 QModelIndexList& theIndices)
273 for (NCollection_List<Handle(TDF_Attribute)>::Iterator anAttrItr (theReferences); anAttrItr.More(); anAttrItr.Next())
274 theIndices.append(FindIndexByAttribute (anAttrItr.Value()));
277 // =======================================================================
280 // =======================================================================
281 QVariant DFBrowser_TreeModel::data (const QModelIndex& theIndex, int theRole) const
283 if (theRole == Qt::BackgroundRole && myHighlightedIndices.contains (theIndex))
284 return DFBrowserPane_Tools::LightHighlightColor();
285 return TreeModel_ModelBase::data (theIndex, theRole);