0029310: Coding - multiple compiler warnings in Inspectors
[occt.git] / tools / DFBrowser / DFBrowser_ThreadItemSearch.cxx
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_ThreadItemSearch.hxx>
17
18 #include <inspector/DFBrowser_Module.hxx>
19 #include <inspector/DFBrowser_Tools.hxx>
20 #include <inspector/DFBrowser_TreeModel.hxx>
21 #include <inspector/DFBrowser_SearchLine.hxx>
22 #include <inspector/DFBrowserPane_Tools.hxx>
23
24 #include <TDataStd_Comment.hxx>
25 #include <TDataStd_Name.hxx>
26 #include <TDataStd_UAttribute.hxx>
27 #include <TDF_AttributeIterator.hxx>
28 #include <TDF_ChildIterator.hxx>
29
30 #include <Standard_WarningsDisable.hxx>
31 #include <QDir>
32 #include <Standard_WarningsRestore.hxx>
33
34 // =======================================================================
35 // function : Run
36 // purpose :
37 // =======================================================================
38 void DFBrowser_ThreadItemSearch::Run()
39 {
40   DFBrowser_TreeModel* aModel = dynamic_cast<DFBrowser_TreeModel*> (mySearchLine->GetModule()->GetOCAFViewModel());
41   Handle(TDocStd_Application) anApplication = aModel->GetTDocStdApplication();
42   if (anApplication.IsNull())
43     return;
44
45   myDocumentValues.clear();
46   myDocumentInfoValues.clear();
47
48   QMap<QString, DFBrowser_SearchItemInfo> anAdditionalValues;
49   QStringList anInfoValues;
50   QStringList aCurrentPath;
51   for (Standard_Integer aDocId = 1, aNbDoc = anApplication->NbDocuments(); aDocId <= aNbDoc; aDocId++)
52   {
53     Handle(TDocStd_Document) aDocument;
54     anApplication->GetDocument (aDocId, aDocument);
55     if (aDocument.IsNull())
56       continue;
57
58     anAdditionalValues.clear();
59     anInfoValues.clear();
60     aCurrentPath.clear();
61     getLabelLines (aDocument->Main().Root(), aCurrentPath, anAdditionalValues, anInfoValues);
62
63     myDocumentValues[aDocId] = anAdditionalValues;
64     myDocumentInfoValues[aDocId] = anInfoValues;
65   }
66 }
67
68 // =======================================================================
69 // function : ApplyValues
70 // purpose :
71 // =======================================================================
72 void DFBrowser_ThreadItemSearch::ApplyValues()
73 {
74   mySearchLine->SetValues (myDocumentValues, myDocumentInfoValues);
75 }
76
77 // =======================================================================
78 // function : ClearValues
79 // purpose :
80 // =======================================================================
81 void DFBrowser_ThreadItemSearch::ClearValues (DFBrowser_SearchLine* theSearchLine)
82 {
83   theSearchLine->ClearValues();
84 }
85
86 // =======================================================================
87 // function : getLabelLines
88 // purpose :
89 // =======================================================================
90 void DFBrowser_ThreadItemSearch::getLabelLines (const TDF_Label& theLabel, QStringList& theCurrentPath,
91                                                 QMap<QString, DFBrowser_SearchItemInfo >& theValues,
92                                                 QStringList& theInfoValues)
93 {
94   addLabel (theLabel, theCurrentPath, theValues, theInfoValues);
95   theCurrentPath.append (DFBrowserPane_Tools::GetEntry (theLabel).ToCString());
96
97   int anId = 0;
98   for (TDF_AttributeIterator anAttrIt (theLabel); anAttrIt.More(); anAttrIt.Next(), anId++)
99     addAttribute(anAttrIt.Value(), theCurrentPath, theValues, theInfoValues);
100
101   for (TDF_ChildIterator aChildIt (theLabel); aChildIt.More(); aChildIt.Next())
102     getLabelLines(aChildIt.Value(), theCurrentPath, theValues, theInfoValues);
103
104   theCurrentPath.removeLast();
105 }
106
107 // =======================================================================
108 // function : addLabel
109 // purpose :
110 // =======================================================================
111 void DFBrowser_ThreadItemSearch::addLabel (const TDF_Label& theLabel, const QStringList& theCurrentPath,
112                                            QMap<QString, DFBrowser_SearchItemInfo>& theValues,
113                                            QStringList& theInfoValues)
114 {
115   QString anEntry = DFBrowserPane_Tools::GetEntry (theLabel).ToCString();
116   if (!theValues.contains (anEntry))
117   {
118     theInfoValues.append (anEntry);
119     theValues[anEntry] = DFBrowser_SearchItemInfo ("40x40_label_icon", anEntry, theCurrentPath, QDir::separator());
120   }
121 }
122
123 // =======================================================================
124 // function : addAttribute
125 // purpose :
126 // =======================================================================
127 void DFBrowser_ThreadItemSearch::addAttribute (const Handle(TDF_Attribute)& theAttribute,
128                                                const QStringList& theCurrentPath,
129                                                QMap<QString, DFBrowser_SearchItemInfo >& theValues,
130                                                QStringList& theInfoValues)
131 {
132   Standard_CString anAttributeKind = theAttribute->DynamicType()->Name();
133   // add element of attribute kind
134   QString anAttributeName = QString ("%1%2%3").arg (anAttributeKind)
135                             .arg (DFBrowser_SearchLineModel::SplitSeparator())
136                             .arg (DFBrowserPane_Tools::GetEntry (theAttribute->Label()).ToCString());
137
138   if (!theInfoValues.contains (anAttributeName))
139   {
140     theInfoValues.append (anAttributeName);
141     theValues[anAttributeName] = DFBrowser_SearchItemInfo (QVariant(), anAttributeName, theCurrentPath,
142                                                            QDir::separator());
143   }
144
145   // add element of attribute value, e.g. Name or Comment string
146   QString anAttributeValue;
147   if (anAttributeKind == STANDARD_TYPE (TDataStd_Name)->Name())
148   {
149     Handle(TDataStd_Name) anAttribute = Handle(TDataStd_Name)::DownCast (theAttribute);
150     anAttributeValue = DFBrowserPane_Tools::ToString (anAttribute->Get());
151   }
152   else if (anAttributeKind == STANDARD_TYPE (TDataStd_Comment)->Name())
153   {
154     Handle(TDataStd_Comment) anAttribute = Handle(TDataStd_Comment)::DownCast (theAttribute);
155     anAttributeValue = DFBrowserPane_Tools::ToString (anAttribute->Get());
156   }
157   else
158     return;
159
160   if (anAttributeValue == "")
161     return;
162
163   // using attribute value in the name
164   QString anAttributeValueExt = QString ("%1%2%3").arg (anAttributeValue)
165                                           .arg (DFBrowser_SearchLineModel::SplitSeparator())
166                                           .arg (DFBrowserPane_Tools::GetEntry (theAttribute->Label()).ToCString());
167   if (!theInfoValues.contains (anAttributeValueExt))
168   {
169     theInfoValues.append (anAttributeValueExt);
170     theValues[anAttributeValueExt] = DFBrowser_SearchItemInfo (QVariant(), anAttributeValueExt,
171                                                                theCurrentPath, QDir::separator());
172   }
173 }