Salome HOME
Join modifications from branch BR_DEBUG_3_2_0b1
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_Utils.cxx
1 //  Copyright (C) 2003  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
3 //
4 //  This library is free software; you can redistribute it and/or
5 //  modify it under the terms of the GNU Lesser General Public
6 //  License as published by the Free Software Foundation; either
7 //  version 2.1 of the License.
8 //
9 //  This library is distributed in the hope that it will be useful,
10 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
11 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 //  Lesser General Public License for more details.
13 //
14 //  You should have received a copy of the GNU Lesser General Public
15 //  License along with this library; if not, write to the Free Software
16 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 //
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19
20
21 #include <qstring.h>
22
23 #include "SMESHGUI_Utils.h"
24
25 #include "OB_Browser.h"
26
27 #include "SUIT_Desktop.h"
28 #include "SUIT_Application.h"
29 #include "SUIT_Session.h"
30
31 #include "LightApp_SelectionMgr.h"
32 #include "SalomeApp_Application.h"
33 #include "SalomeApp_Module.h"
34 #include "SalomeApp_Study.h"
35
36 #include "SALOME_ListIO.hxx"
37
38 #include "SALOMEconfig.h"
39 #include CORBA_CLIENT_HEADER(GEOM_Gen)
40
41 #include "utilities.h"
42
43 //using namespace std;
44
45 namespace SMESH{
46
47   SUIT_Desktop*
48   GetDesktop(const CAM_Module* theModule)
49   {
50     return theModule->application()->desktop();
51   }
52
53   LightApp_SelectionMgr*
54   GetSelectionMgr(const SalomeApp_Module* theModule)
55   {
56     return theModule->getApp()->selectionMgr();
57   }
58
59   SUIT_ResourceMgr*
60   GetResourceMgr( const SalomeApp_Module* )
61   {
62     return SUIT_Session::session()->resourceMgr();
63   }
64
65   _PTR(Study)
66   GetCStudy(const SalomeApp_Study* theStudy)
67   {
68     return theStudy->studyDS();
69   }
70
71   CORBA::Object_var 
72   DataOwnerToObject(const LightApp_DataOwnerPtr& theOwner)
73   {
74     CORBA::Object_var anObj;
75     if(theOwner){
76       const Handle(SALOME_InteractiveObject)& anIO = theOwner->IO();
77       if(!anIO.IsNull()){
78         if(anIO->hasEntry()){
79           _PTR(Study) aStudy = GetActiveStudyDocument();
80           _PTR(SObject) aSObj = aStudy->FindObjectID(anIO->getEntry());
81           anObj = SObjectToObject(aSObj,aStudy);
82         }
83       }
84     }
85     return anObj;
86   }
87
88
89   SUIT_Study* GetActiveStudy()
90   {
91     SUIT_Application* app = SUIT_Session::session()->activeApplication();
92     if (app)
93       return app->activeStudy();
94     else
95       return NULL;
96   }
97
98   SUIT_ViewWindow* GetActiveWindow()
99   {
100     SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>
101       (SUIT_Session::session()->activeApplication());
102     if (app && app->desktop() )
103       return app->desktop()->activeWindow();
104     else
105       return NULL;
106   }
107
108   _PTR(Study) GetActiveStudyDocument()
109   {
110     SalomeApp_Study* aStudy = dynamic_cast<SalomeApp_Study*>(GetActiveStudy());
111     if (aStudy)
112       return aStudy->studyDS();
113     else
114       return _PTR(Study)();
115   }
116
117   _PTR(SObject) FindSObject (CORBA::Object_ptr theObject)
118   {
119     SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>
120       (SUIT_Session::session()->activeApplication());
121     if (app && !CORBA::is_nil(theObject)) {
122       if(_PTR(Study) aStudy = GetActiveStudyDocument()){
123         CORBA::String_var anIOR = app->orb()->object_to_string(theObject);
124         if (strcmp(anIOR.in(), "") != 0)
125           return aStudy->FindObjectIOR(anIOR.in());
126       }
127     }
128     return _PTR(SObject)();
129   }
130
131   void SetName (_PTR(SObject) theSObject, const char* theName)
132   {
133     _PTR(Study) aStudy = GetActiveStudyDocument();
134     if (aStudy->GetProperties()->IsLocked())
135       return;
136     _PTR(StudyBuilder) aBuilder = aStudy->NewBuilder();
137     _PTR(GenericAttribute) anAttr =
138       aBuilder->FindOrCreateAttribute(theSObject, "AttributeName");
139     _PTR(AttributeName) aName = anAttr;
140     if (aName)
141       aName->SetValue(theName);
142   }
143
144   void SetValue (_PTR(SObject) theSObject, const char* theValue)
145   {
146     _PTR(Study) aStudy = GetActiveStudyDocument();
147     if (aStudy->GetProperties()->IsLocked())
148       return;
149     _PTR(StudyBuilder) aBuilder = aStudy->NewBuilder();
150     _PTR(GenericAttribute) anAttr =
151       aBuilder->FindOrCreateAttribute(theSObject, "AttributeComment");
152     _PTR(AttributeComment) aComment = anAttr;
153     if (aComment)
154       aComment->SetValue(theValue);
155   }
156   
157   void setFileName (_PTR(SObject) theSObject, const char* theValue)
158   {
159     _PTR(Study) aStudy = GetActiveStudyDocument();
160     if (aStudy->GetProperties()->IsLocked())
161       return;
162     _PTR(StudyBuilder) aBuilder = aStudy->NewBuilder();
163     _PTR(GenericAttribute) anAttr =
164       aBuilder->FindOrCreateAttribute(theSObject, "AttributeExternalFileDef");
165     _PTR(AttributeExternalFileDef) aFileName = anAttr;
166     if (aFileName)
167       aFileName->SetValue(theValue);
168   }
169   
170   void setFileType (_PTR(SObject) theSObject, const char* theValue)
171   {
172     _PTR(Study) aStudy = GetActiveStudyDocument();
173     if (aStudy->GetProperties()->IsLocked())
174       return;
175     _PTR(StudyBuilder) aBuilder = aStudy->NewBuilder();
176     _PTR(GenericAttribute) anAttr =
177       aBuilder->FindOrCreateAttribute(theSObject, "AttributeFileType");
178     _PTR(AttributeFileType) aFileType = anAttr;
179     if (aFileType)
180       aFileType->SetValue(theValue);
181   }
182
183   CORBA::Object_var SObjectToObject (_PTR(SObject) theSObject,
184                                      _PTR(Study)   theStudy)
185   {
186     SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>
187       (SUIT_Session::session()->activeApplication());
188     if (theSObject) {
189       _PTR(GenericAttribute) anAttr;
190       if (theSObject->FindAttribute(anAttr, "AttributeIOR")) {
191         _PTR(AttributeIOR) anIOR = anAttr;
192         CORBA::String_var aVal = anIOR->Value().c_str();
193         return app->orb()->string_to_object(aVal);
194       }
195     }
196     return CORBA::Object::_nil();
197   }
198
199   CORBA::Object_var SObjectToObject (_PTR(SObject) theSObject)
200   {
201     _PTR(Study) aStudy = GetActiveStudyDocument();
202     return SObjectToObject(theSObject,aStudy);
203   }
204
205   CORBA::Object_var IObjectToObject (const Handle(SALOME_InteractiveObject)& theIO)
206   {
207     if (!theIO.IsNull()) {
208       if (theIO->hasEntry()) {
209         _PTR(Study) aStudy = GetActiveStudyDocument();
210         _PTR(SObject) anObj = aStudy->FindObjectID(theIO->getEntry());
211         return SObjectToObject(anObj,aStudy);
212       }
213     }
214     return CORBA::Object::_nil();
215   }
216
217   CORBA::Object_var IORToObject (const char* theIOR)
218   {
219     SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>
220       (SUIT_Session::session()->activeApplication());
221     return app->orb()->string_to_object(theIOR);
222   }
223
224   int GetNameOfSelectedIObjects(LightApp_SelectionMgr* theMgr, QString& theName)
225   {
226     if (!theMgr)
227       return 0;
228
229     SALOME_ListIO selected;
230     theMgr->selectedObjects(selected);
231     int aNbSel = selected.Extent();
232     if (aNbSel == 1) {
233       Handle(SALOME_InteractiveObject) anIObject = selected.First();
234       theName = anIObject->getName();
235     } else {
236       theName = QObject::tr("SMESH_OBJECTS_SELECTED").arg(aNbSel);
237     }
238     return aNbSel;
239   }
240
241   _PTR(SObject) GetMeshOrSubmesh (_PTR(SObject) theSObject)
242   {
243     GEOM::GEOM_Object_var aShape = SObjectToInterface<GEOM::GEOM_Object>(theSObject);
244     if (!aShape->_is_nil()){ //It s a shape
245       return theSObject->GetFather();
246     }
247     _PTR(SObject) aSObject;
248     if (theSObject->ReferencedObject(aSObject)) {
249       aSObject = theSObject->GetFather();
250       return aSObject->GetFather();
251     }
252     return theSObject->GetFather();
253   }
254
255   void ModifiedMesh (_PTR(SObject) theSObject, bool theIsRight)
256   {
257     _PTR(Study) aStudy = GetActiveStudyDocument();
258     if (aStudy->GetProperties()->IsLocked())
259       return;
260
261     _PTR(StudyBuilder) aBuilder = aStudy->NewBuilder();
262     _PTR(GenericAttribute) anAttr =
263       aBuilder->FindOrCreateAttribute(theSObject,"AttributePixMap");
264     _PTR(AttributePixMap) aPixmap = anAttr;
265     if (theIsRight) {
266       aPixmap->SetPixMap("ICON_SMESH_TREE_MESH");
267     } else {
268       aPixmap->SetPixMap("ICON_SMESH_TREE_MESH_WARN");
269     }
270
271     _PTR(ChildIterator) anIter = aStudy->NewChildIterator(theSObject);
272     for (int i = 1; anIter->More(); anIter->Next(), i++) {
273       _PTR(SObject) aSObj = anIter->Value();
274       if (i >= 4) {
275         _PTR(ChildIterator) anIter1 = aStudy->NewChildIterator(aSObj);
276         for (; anIter1->More(); anIter1->Next()) {
277           _PTR(SObject) aSObj1 = anIter1->Value();
278           anAttr = aBuilder->FindOrCreateAttribute(aSObj1, "AttributePixMap");
279           aPixmap = anAttr;
280           if (theIsRight) {
281             aPixmap->SetPixMap("ICON_SMESH_TREE_MESH");
282           } else {
283             aPixmap->SetPixMap("ICON_SMESH_TREE_MESH_WARN");
284           }
285         }
286       }
287     }
288   }
289
290 //  void UpdateObjBrowser (bool)
291 //  {
292 //    //SMESHGUI::activeStudy()->updateObjBrowser(true);
293 //    //SalomeApp_Application* anApp = dynamic_cast<SalomeApp_Application*>
294 //    //  (SUIT_Session::session()->activeApplication());
295 //    //if (anApp) anApp->objectBrowser()->updateTree();
296 //    SMESHGUI::GetSMESHGUI()->updateObjBrowser();
297 //  }
298 }