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 <DFBrowser_ThreadItemUsedShapesMap.hxx>
18 #include <DFBrowser_Module.hxx>
19 #include <DFBrowser_Tools.hxx>
20 #include <DFBrowser_TreeModel.hxx>
22 #include <DFBrowserPane_TNamingUsedShapes.hxx>
23 #include <DFBrowserPane_Tools.hxx>
25 #include <TNaming_DataMapIteratorOfDataMapOfShapePtrRefShape.hxx>
26 #include <TNaming_PtrRefShape.hxx>
27 #include <TNaming_RefShape.hxx>
28 #include <TNaming_UsedShapes.hxx>
30 #include <Standard_Type.hxx>
31 #include <TDocStd_Document.hxx>
33 // =======================================================================
36 // =======================================================================
37 void DFBrowser_ThreadItemUsedShapesMap::Run()
41 DFBrowser_TreeModel* aDFBrowserModel = dynamic_cast<DFBrowser_TreeModel*> (myModule->GetOCAFViewModel());
45 Handle(TDocStd_Application) anApplication = aDFBrowserModel->GetTDocStdApplication();
46 if (anApplication.IsNull())
49 for (Standard_Integer aDocId = 1, aNbDocuments = anApplication->NbDocuments(); aDocId <= aNbDocuments; aDocId++)
51 Handle(TDocStd_Document) aDocument;
52 anApplication->GetDocument (aDocId, aDocument);
53 if (aDocument.IsNull())
56 TDF_Label aLabel = aDocument->Main().Root();
58 Handle(TNaming_UsedShapes) anAttribute;
59 if (!aLabel.FindAttribute (TNaming_UsedShapes::GetID(), anAttribute))
62 std::list<TCollection_AsciiString> aReferences;
63 findReferences (anAttribute, aReferences);
64 if (!aReferences.empty())
65 myAttributeRefs.Bind (anAttribute, aReferences);
69 // =======================================================================
70 // function : ApplyValues
72 // =======================================================================
73 void DFBrowser_ThreadItemUsedShapesMap::ApplyValues()
75 if (myAttributeRefs.IsEmpty())
78 DFBrowserPane_AttributePaneAPI* aPane = myModule->GetAttributePane (STANDARD_TYPE (TNaming_UsedShapes)->Name());
81 DFBrowserPane_TNamingUsedShapes* aUsedShapesPane = dynamic_cast<DFBrowserPane_TNamingUsedShapes*> (aPane);
82 aUsedShapesPane->SetSortedReferences (myAttributeRefs);
85 DFBrowser_TreeModel* aDFBrowserModel = dynamic_cast<DFBrowser_TreeModel*> (myModule->GetOCAFViewModel());
86 for (NCollection_DataMap<Handle(TDF_Attribute), std::list<TCollection_AsciiString> >::Iterator aRefIt (myAttributeRefs);
87 aRefIt.More(); aRefIt.Next())
89 TreeModel_ItemBasePtr anItemBase = TreeModel_ModelBase::GetItemByIndex
90 (aDFBrowserModel->FindIndexByAttribute (aRefIt.Key()));
95 myAttributeRefs.Clear();
98 // =======================================================================
99 // function : ClearSortedReferences
101 // =======================================================================
102 void DFBrowser_ThreadItemUsedShapesMap::ClearSortedReferences (DFBrowser_Module* theModule)
104 DFBrowserPane_AttributePaneAPI* aPane = theModule->GetAttributePane (STANDARD_TYPE (TNaming_UsedShapes)->Name());
108 DFBrowserPane_TNamingUsedShapes* aUsedShapesPane = dynamic_cast<DFBrowserPane_TNamingUsedShapes*> (aPane);
109 aUsedShapesPane->ClearSortedReferences();
111 // update tree item state
112 DFBrowser_TreeModel* aDFBrowserModel = dynamic_cast<DFBrowser_TreeModel*> (theModule->GetOCAFViewModel());
113 if (!aDFBrowserModel)
115 Handle(TDocStd_Application) anApplication = aDFBrowserModel->GetTDocStdApplication();
116 if (anApplication.IsNull())
119 for (Standard_Integer aDocId = 1, aNbDocuments = anApplication->NbDocuments(); aDocId <= aNbDocuments; aDocId++)
121 Handle(TDocStd_Document) aDocument;
122 anApplication->GetDocument (aDocId, aDocument);
123 if (aDocument.IsNull())
125 TDF_Label aLabel = aDocument->Main().Root();
126 Handle(TNaming_UsedShapes) anAttribute;
127 if (aLabel.FindAttribute (TNaming_UsedShapes::GetID(), anAttribute))
129 TreeModel_ItemBasePtr anItemBase = TreeModel_ModelBase::GetItemByIndex(
130 aDFBrowserModel->FindIndexByAttribute (anAttribute));
137 // =======================================================================
138 // function : isLessThan
140 // =======================================================================
141 bool DFBrowser_ThreadItemUsedShapesMap::isLessThan (const QStringList& theLeft, const QStringList& theRight)
143 int aState = 1; //! where 0 - less, 1 - equal, 2 - larger
144 int aLeftSize = theLeft.size();
145 int aRightSize = theRight.size();
147 for (int anItemId = 0; anItemId < aRightSize && anItemId < aLeftSize && aState == 1; anItemId++)
149 int aRightId = theRight[anItemId].toInt();
150 int aLeftId = theLeft[anItemId].toInt();
151 if (aLeftId == aRightId)
153 else if (aLeftId < aRightId)
155 else if (aLeftId > aRightId)
159 { // equal in similar parts
160 if (aLeftSize < aRightSize)
162 else if (aLeftSize > aRightSize)
168 // =======================================================================
169 // function : addValue
171 // =======================================================================
172 void DFBrowser_ThreadItemUsedShapesMap::addValue (const TCollection_AsciiString& theEntry,
173 QList<QPair<TCollection_AsciiString, QStringList > >& theEntries)
175 QStringList aSplit = QString (theEntry.ToCString()).split (":");
178 bool isInserted = false;
179 // looking for nearest smaller value from the end of the list
180 for (int anEntryId = theEntries.size() - 1; anEntryId >= 0 && !isInserted; anEntryId--)
182 if (isLessThan(aSplit, theEntries[anEntryId].second))
183 aLessIndex = anEntryId;
186 // if less than was found and now, the entry is greater than current entry
187 if (aLessIndex != -1)
189 theEntries.insert (aLessIndex, qMakePair (theEntry, aSplit));
197 if (aLessIndex != -1) // less than all, insert at this position
198 theEntries.insert (aLessIndex, qMakePair (theEntry, aSplit));
200 theEntries.append (qMakePair (theEntry, aSplit));
204 // =======================================================================
205 // function : findReferences
207 // =======================================================================
208 //#define REQUIRE_OCAF_REVIEW:26 : start
209 void DFBrowser_ThreadItemUsedShapesMap::findReferences (Handle(TDF_Attribute) theAttribute,
210 std::list<TCollection_AsciiString>& theReferences)
212 Handle(TNaming_UsedShapes) anAttribute = Handle(TNaming_UsedShapes)::DownCast (theAttribute);
214 QList<QPair<TCollection_AsciiString, QStringList> > anEntries;
215 for (TNaming_DataMapIteratorOfDataMapOfShapePtrRefShape aDataIt(anAttribute->Map()); aDataIt.More(); aDataIt.Next())
216 addValue(DFBrowserPane_Tools::GetEntry (aDataIt.Value()->Label()), anEntries);
218 for (QList<QPair<TCollection_AsciiString, QStringList> >::const_iterator anEntryIt = anEntries.begin();
219 anEntryIt != anEntries.end(); anEntryIt++)
220 theReferences.push_back (anEntryIt->first);
222 //#define REQUIRE_OCAF_REVIEW:26 : end