404c6f4f5f9628dcbc122bef11364f58c5a5c955
[occt.git] / tools / DFBrowser / DFBrowser_Module.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 <DFBrowser_Module.hxx>
17
18 #include <DFBrowser_Item.hxx>
19 #include <DFBrowser_ItemApplication.hxx>
20 #include <DFBrowser_ItemBase.hxx>
21 #include <DFBrowser_ItemRole.hxx>
22 #include <DFBrowser_Tools.hxx>
23 #include <DFBrowser_TreeModel.hxx>
24
25 #include <DFBrowserPane_AttributePane.hxx>
26 #include <DFBrowserPane_AttributePaneCreator.hxx>
27 #include <DFBrowserPane_ItemRole.hxx>
28 #include <DFBrowserPane_Tools.hxx>
29
30 #include <DFBrowserPaneXDE_AttributePaneCreator.hxx>
31 #include <DFBrowserPaneXDE_Tools.hxx>
32
33 #include <XCAFApp_Application.hxx>
34
35 #include <QItemSelectionModel>
36 #include <QMessageBox>
37
38 // =======================================================================
39 // function : Constructor
40 // purpose :
41 // =======================================================================
42 DFBrowser_Module::DFBrowser_Module()
43 : myOCAFViewModel (0)
44 {
45   RegisterPaneCreator (new DFBrowserPane_AttributePaneCreator());
46 }
47
48 // =======================================================================
49 // function : CreateViewModel
50 // purpose :
51 // =======================================================================
52 void DFBrowser_Module::CreateViewModel (void* theParent)
53 {
54   myOCAFViewModel = new DFBrowser_TreeModel ((QWidget*)theParent, this);
55 }
56
57 // =======================================================================
58 // function : SetApplication
59 // purpose :
60 // =======================================================================
61 void DFBrowser_Module::SetApplication (const Handle(TDocStd_Application)& theApplication)
62 {
63   myOCAFViewModel->Init (theApplication);
64
65   myPaneCreators.clear();
66   RegisterPaneCreator (new DFBrowserPane_AttributePaneCreator());
67   if (!theApplication.IsNull() && DFBrowserPaneXDE_Tools::IsXDEApplication (theApplication))
68     RegisterPaneCreator (new DFBrowserPaneXDE_AttributePaneCreator (myPaneCreators[0]));
69 }
70
71 // =======================================================================
72 // function : SetExternalContext
73 // purpose :
74 // =======================================================================
75 void DFBrowser_Module::SetExternalContext (const Handle(Standard_Transient)& theContext)
76 {
77   myExternalContext = Handle(AIS_InteractiveContext)::DownCast (theContext);
78 }
79
80 // =======================================================================
81 // function : GetTDocStdApplication
82 // purpose :
83 // =======================================================================
84 Handle(TDocStd_Application) DFBrowser_Module::GetTDocStdApplication() const
85 {
86   return myOCAFViewModel->GetTDocStdApplication();
87 }
88
89 // =======================================================================
90 // function : UpdateTreeModel
91 // purpose :
92 // =======================================================================
93 void DFBrowser_Module::UpdateTreeModel()
94 {
95   QAbstractItemModel* aModel = GetOCAFViewModel();
96   QItemSelectionModel* aSelectionModel = GetOCAFViewSelectionModel();
97   if (!aModel || !aSelectionModel)
98     return;
99   aSelectionModel->clearSelection();
100
101   emit beforeUpdateTreeModel();
102   myOCAFViewModel->Reset();
103   myOCAFViewModel->EmitLayoutChanged();
104
105   SetInitialTreeViewSelection();
106 }
107
108 // =======================================================================
109 // function : SetInitialTreeViewSelection
110 // purpose :
111 // =======================================================================
112 void DFBrowser_Module::SetInitialTreeViewSelection()
113 {
114   QAbstractItemModel* aModel = GetOCAFViewModel();
115   QItemSelectionModel* aSelectionModel = GetOCAFViewSelectionModel();
116   if (!aModel || !aSelectionModel)
117     return;
118
119   // select a parent(aplication) item
120   aSelectionModel->select (aModel->index (0, 0), QItemSelectionModel::ClearAndSelect);
121 }
122
123 // =======================================================================
124 // function : FindAttribute
125 // purpose :
126 // =======================================================================
127 Handle(TDF_Attribute) DFBrowser_Module::FindAttribute (const QModelIndex& theIndex)
128 {
129   TreeModel_ItemBasePtr anItemBase = TreeModel_ModelBase::GetItemByIndex (theIndex);
130   if (!anItemBase)
131     return Handle(TDF_Attribute)();
132
133   DFBrowser_ItemPtr anItem = itemDynamicCast<DFBrowser_Item> (anItemBase);
134   return (anItem && anItem->HasAttribute()) ? anItem->GetAttribute() : Handle(TDF_Attribute)();
135 }
136
137 // =======================================================================
138 // function : GetAttributePane
139 // purpose :
140 // =======================================================================
141 DFBrowserPane_AttributePaneAPI* DFBrowser_Module::GetAttributePane (Handle(TDF_Attribute) theAttribute)
142 {
143   DFBrowserPane_AttributePaneAPI* aPane = 0;
144   if (theAttribute.IsNull())
145     return aPane;
146   return GetAttributePane (theAttribute->DynamicType()->Name());
147 }
148
149 // =======================================================================
150 // function : GetAttributePane
151 // purpose :
152 // =======================================================================
153 DFBrowserPane_AttributePaneAPI* DFBrowser_Module::GetAttributePane (const Standard_CString& theAttributeName)
154 {
155   DFBrowserPane_AttributePaneAPI* aPane = 0;
156
157   if (!myAttributeTypes.contains (theAttributeName))
158   {
159     aPane = CreateAttributePane (theAttributeName);
160     if (aPane)
161       myAttributeTypes[theAttributeName] = aPane;
162   }
163   else
164     aPane = myAttributeTypes[theAttributeName];
165
166   return aPane;
167 }
168
169 // =======================================================================
170 // function : GetAttributeInfo
171 // purpose :
172 // =======================================================================
173 QVariant DFBrowser_Module::GetAttributeInfo (Handle(TDF_Attribute) theAttribute, DFBrowser_Module* theModule,
174                                              int theRole, int theColumnId)
175 {
176   DFBrowserPane_AttributePane* anAttributePane = 0;
177   if (!theAttribute.IsNull())
178   {
179     DFBrowserPane_AttributePaneAPI* anAPIPane = theModule->GetAttributePane (theAttribute);
180     if (anAPIPane)
181       anAttributePane = dynamic_cast<DFBrowserPane_AttributePane*> (anAPIPane);
182   }
183
184   QVariant aValue;
185   if (anAttributePane)
186     aValue = anAttributePane->GetAttributeInfo (theAttribute,
187                theRole == DFBrowser_ItemRole_AdditionalInfo ? DFBrowserPane_ItemRole_ShortInfo : theRole,
188                theColumnId);
189   else
190     aValue = DFBrowserPane_AttributePane::GetAttributeInfoByType (theAttribute->DynamicType()->Name(), theRole, theColumnId);
191   return aValue;
192 }
193
194 // =======================================================================
195 // function : GetAttributeInfo
196 // purpose :
197 // =======================================================================
198 QVariant DFBrowser_Module::GetAttributeInfo (const Standard_CString& theAttributeName, DFBrowser_Module* theModule,
199                                              int theRole, int theColumnId)
200 {
201   DFBrowserPane_AttributePane* anAttributePane = 0;
202   DFBrowserPane_AttributePaneAPI* anAPIPane = theModule->GetAttributePane (theAttributeName);
203   if (anAPIPane)
204     anAttributePane = dynamic_cast<DFBrowserPane_AttributePane*> (anAPIPane);
205
206   QVariant aValue;
207   if (anAttributePane)
208   {
209     Handle(TDF_Attribute) anAttribute;
210     aValue = anAttributePane->GetAttributeInfo (anAttribute,
211                theRole == DFBrowser_ItemRole_AdditionalInfo ? DFBrowserPane_ItemRole_ShortInfo : theRole, theColumnId);
212   }
213   else
214     aValue = DFBrowserPane_AttributePane::GetAttributeInfoByType (theAttributeName, theRole, theColumnId);
215   return aValue;
216
217 }
218
219 // =======================================================================
220 // function : CreateAttributePane
221 // purpose :
222 // =======================================================================
223 DFBrowserPane_AttributePaneAPI* DFBrowser_Module::CreateAttributePane (const Standard_CString& theAttributeName)
224 {
225   DFBrowserPane_AttributePaneAPI* aPane = 0;
226   // iteration should be performed from the tail of the list, as latest added creator has
227   // larger priority
228   for (int aPaneCreatorId = myPaneCreators.size()-1; aPaneCreatorId >= 0 && !aPane; aPaneCreatorId--)
229     aPane = myPaneCreators[aPaneCreatorId]->CreateAttributePane (theAttributeName);
230   return aPane;
231 }