0029559: Samples - wrong copyright statement in FuncDemo
[occt.git] / tools / DFBrowser / DFBrowser_ThreadItemUsedShapesMap.cxx
CommitLineData
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_ThreadItemUsedShapesMap.hxx>
14bbbdcb 17
0cb512c0 18#include <inspector/DFBrowser_Module.hxx>
19#include <inspector/DFBrowser_Tools.hxx>
20#include <inspector/DFBrowser_TreeModel.hxx>
14bbbdcb 21
0cb512c0 22#include <inspector/DFBrowserPane_TNamingUsedShapes.hxx>
23#include <inspector/DFBrowserPane_Tools.hxx>
14bbbdcb 24
25#include <TNaming_DataMapIteratorOfDataMapOfShapePtrRefShape.hxx>
26#include <TNaming_PtrRefShape.hxx>
27#include <TNaming_RefShape.hxx>
28#include <TNaming_UsedShapes.hxx>
29
30#include <Standard_Type.hxx>
31#include <TDocStd_Document.hxx>
32
33// =======================================================================
34// function : Run
35// purpose :
36// =======================================================================
37void DFBrowser_ThreadItemUsedShapesMap::Run()
38{
39 if (!myModule)
40 return;
41 DFBrowser_TreeModel* aDFBrowserModel = dynamic_cast<DFBrowser_TreeModel*> (myModule->GetOCAFViewModel());
42 if (!aDFBrowserModel)
43 return;
44
45 Handle(TDocStd_Application) anApplication = aDFBrowserModel->GetTDocStdApplication();
46 if (anApplication.IsNull())
47 return;
48
49 for (Standard_Integer aDocId = 1, aNbDocuments = anApplication->NbDocuments(); aDocId <= aNbDocuments; aDocId++)
50 {
51 Handle(TDocStd_Document) aDocument;
52 anApplication->GetDocument (aDocId, aDocument);
53 if (aDocument.IsNull())
54 continue;
55
56 TDF_Label aLabel = aDocument->Main().Root();
57
58 Handle(TNaming_UsedShapes) anAttribute;
59 if (!aLabel.FindAttribute (TNaming_UsedShapes::GetID(), anAttribute))
60 continue;
61
62 std::list<TCollection_AsciiString> aReferences;
63 findReferences (anAttribute, aReferences);
64 if (!aReferences.empty())
65 myAttributeRefs.Bind (anAttribute, aReferences);
66 }
67}
68
69// =======================================================================
70// function : ApplyValues
71// purpose :
72// =======================================================================
73void DFBrowser_ThreadItemUsedShapesMap::ApplyValues()
74{
75 if (myAttributeRefs.IsEmpty())
76 return;
77
78 DFBrowserPane_AttributePaneAPI* aPane = myModule->GetAttributePane (STANDARD_TYPE (TNaming_UsedShapes)->Name());
79 if (aPane)
80 {
81 DFBrowserPane_TNamingUsedShapes* aUsedShapesPane = dynamic_cast<DFBrowserPane_TNamingUsedShapes*> (aPane);
82 aUsedShapesPane->SetSortedReferences (myAttributeRefs);
83 }
84 // update
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())
88 {
89 TreeModel_ItemBasePtr anItemBase = TreeModel_ModelBase::GetItemByIndex
90 (aDFBrowserModel->FindIndexByAttribute (aRefIt.Key()));
91 if (anItemBase)
0cb512c0 92 {
93 anItemBase->Reset();
14bbbdcb 94 anItemBase->Init();
0cb512c0 95 }
14bbbdcb 96 }
97 // clear cache
98 myAttributeRefs.Clear();
99}
100
101// =======================================================================
102// function : ClearSortedReferences
103// purpose :
104// =======================================================================
105void DFBrowser_ThreadItemUsedShapesMap::ClearSortedReferences (DFBrowser_Module* theModule)
106{
107 DFBrowserPane_AttributePaneAPI* aPane = theModule->GetAttributePane (STANDARD_TYPE (TNaming_UsedShapes)->Name());
108 if (!aPane)
109 return;
110
111 DFBrowserPane_TNamingUsedShapes* aUsedShapesPane = dynamic_cast<DFBrowserPane_TNamingUsedShapes*> (aPane);
112 aUsedShapesPane->ClearSortedReferences();
113
114 // update tree item state
115 DFBrowser_TreeModel* aDFBrowserModel = dynamic_cast<DFBrowser_TreeModel*> (theModule->GetOCAFViewModel());
116 if (!aDFBrowserModel)
117 return;
118 Handle(TDocStd_Application) anApplication = aDFBrowserModel->GetTDocStdApplication();
119 if (anApplication.IsNull())
120 return;
121
122 for (Standard_Integer aDocId = 1, aNbDocuments = anApplication->NbDocuments(); aDocId <= aNbDocuments; aDocId++)
123 {
124 Handle(TDocStd_Document) aDocument;
125 anApplication->GetDocument (aDocId, aDocument);
126 if (aDocument.IsNull())
127 continue;
128 TDF_Label aLabel = aDocument->Main().Root();
129 Handle(TNaming_UsedShapes) anAttribute;
130 if (aLabel.FindAttribute (TNaming_UsedShapes::GetID(), anAttribute))
131 {
132 TreeModel_ItemBasePtr anItemBase = TreeModel_ModelBase::GetItemByIndex(
133 aDFBrowserModel->FindIndexByAttribute (anAttribute));
134 if (anItemBase)
135 anItemBase->Init();
136 }
137 }
138}
139
140// =======================================================================
141// function : isLessThan
142// purpose :
143// =======================================================================
144bool DFBrowser_ThreadItemUsedShapesMap::isLessThan (const QStringList& theLeft, const QStringList& theRight)
145{
146 int aState = 1; //! where 0 - less, 1 - equal, 2 - larger
147 int aLeftSize = theLeft.size();
148 int aRightSize = theRight.size();
149
150 for (int anItemId = 0; anItemId < aRightSize && anItemId < aLeftSize && aState == 1; anItemId++)
151 {
152 int aRightId = theRight[anItemId].toInt();
153 int aLeftId = theLeft[anItemId].toInt();
154 if (aLeftId == aRightId)
155 continue;
156 else if (aLeftId < aRightId)
157 aState = 0; // less
158 else if (aLeftId > aRightId)
159 aState = 2; // less
160 }
161 if (aState == 1)
162 { // equal in similar parts
163 if (aLeftSize < aRightSize)
164 aState = 0;
165 else if (aLeftSize > aRightSize)
166 aState = 2;
167 }
168 return aState == 0;
169}
170
171// =======================================================================
172// function : addValue
173// purpose :
174// =======================================================================
175void DFBrowser_ThreadItemUsedShapesMap::addValue (const TCollection_AsciiString& theEntry,
176 QList<QPair<TCollection_AsciiString, QStringList > >& theEntries)
177{
178 QStringList aSplit = QString (theEntry.ToCString()).split (":");
179
180 int aLessIndex = -1;
181 bool isInserted = false;
182 // looking for nearest smaller value from the end of the list
183 for (int anEntryId = theEntries.size() - 1; anEntryId >= 0 && !isInserted; anEntryId--)
184 {
185 if (isLessThan(aSplit, theEntries[anEntryId].second))
186 aLessIndex = anEntryId;
187 else
188 {
189 // if less than was found and now, the entry is greater than current entry
190 if (aLessIndex != -1)
191 {
192 theEntries.insert (aLessIndex, qMakePair (theEntry, aSplit));
193 isInserted = true;
194 }
195 break;
196 }
197 }
198 if (!isInserted)
199 {
200 if (aLessIndex != -1) // less than all, insert at this position
201 theEntries.insert (aLessIndex, qMakePair (theEntry, aSplit));
202 else
203 theEntries.append (qMakePair (theEntry, aSplit));
204 }
205}
206
207// =======================================================================
208// function : findReferences
209// purpose :
210// =======================================================================
14bbbdcb 211void DFBrowser_ThreadItemUsedShapesMap::findReferences (Handle(TDF_Attribute) theAttribute,
212 std::list<TCollection_AsciiString>& theReferences)
213{
214 Handle(TNaming_UsedShapes) anAttribute = Handle(TNaming_UsedShapes)::DownCast (theAttribute);
215
216 QList<QPair<TCollection_AsciiString, QStringList> > anEntries;
217 for (TNaming_DataMapIteratorOfDataMapOfShapePtrRefShape aDataIt(anAttribute->Map()); aDataIt.More(); aDataIt.Next())
218 addValue(DFBrowserPane_Tools::GetEntry (aDataIt.Value()->Label()), anEntries);
219
220 for (QList<QPair<TCollection_AsciiString, QStringList> >::const_iterator anEntryIt = anEntries.begin();
221 anEntryIt != anEntries.end(); anEntryIt++)
222 theReferences.push_back (anEntryIt->first);
223}