Salome HOME
SMH: Preparation version 3.0.0 - merge (HEAD+POLYWORK)
[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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
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 "SalomeApp_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   SalomeApp_SelectionMgr*
54   GetSelectionMgr(const SalomeApp_Module* theModule)
55   {
56     return theModule->getApp()->selectionMgr();
57   }
58
59
60   _PTR(Study)
61   GetCStudy(const SalomeApp_Study* theStudy)
62   {
63     return theStudy->studyDS();
64   }
65
66   CORBA::Object_var 
67   DataOwnerToObject(const SalomeApp_DataOwnerPtr& theOwner)
68   {
69     CORBA::Object_var anObj;
70     if(theOwner){
71       const Handle(SALOME_InteractiveObject)& anIO = theOwner->IO();
72       if(!anIO.IsNull()){
73         if(anIO->hasEntry()){
74           _PTR(Study) aStudy = GetActiveStudyDocument();
75           _PTR(SObject) aSObj = aStudy->FindObjectID(anIO->getEntry());
76           anObj = SObjectToObject(aSObj,aStudy);
77         }
78       }
79     }
80     return anObj;
81   }
82
83
84   SUIT_Study* GetActiveStudy()
85   {
86     SUIT_Application* app = SUIT_Session::session()->activeApplication();
87     if (app)
88       return app->activeStudy();
89     else
90       return NULL;
91   }
92
93   SUIT_ViewWindow* GetActiveWindow()
94   {
95     SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>
96       (SUIT_Session::session()->activeApplication());
97     if (app && app->activeViewManager())
98       return app->activeViewManager()->getActiveView();
99     else
100       return NULL;
101   }
102
103   _PTR(Study) GetActiveStudyDocument()
104   {
105     SalomeApp_Study* aStudy = dynamic_cast<SalomeApp_Study*>(GetActiveStudy());
106     if (aStudy)
107       return aStudy->studyDS();
108     else
109       return _PTR(Study)();
110   }
111
112   _PTR(SObject) FindSObject (CORBA::Object_ptr theObject)
113   {
114     SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>
115       (SUIT_Session::session()->activeApplication());
116     if (app && !CORBA::is_nil(theObject)) {
117       if(_PTR(Study) aStudy = GetActiveStudyDocument()){
118         CORBA::String_var anIOR = app->orb()->object_to_string(theObject);
119         if (strcmp(anIOR.in(), "") != 0)
120           return aStudy->FindObjectIOR(anIOR.in());
121       }
122     }
123     return _PTR(SObject)();
124   }
125
126   void SetName (_PTR(SObject) theSObject, const char* theName)
127   {
128     _PTR(Study) aStudy = GetActiveStudyDocument();
129     if (aStudy->GetProperties()->IsLocked())
130       return;
131     _PTR(StudyBuilder) aBuilder = aStudy->NewBuilder();
132     _PTR(GenericAttribute) anAttr =
133       aBuilder->FindOrCreateAttribute(theSObject, "AttributeName");
134     _PTR(AttributeName) aName = anAttr;
135     if (aName)
136       aName->SetValue(theName);
137   }
138
139   void SetValue (_PTR(SObject) theSObject, const char* theValue)
140   {
141     _PTR(Study) aStudy = GetActiveStudyDocument();
142     if (aStudy->GetProperties()->IsLocked())
143       return;
144     _PTR(StudyBuilder) aBuilder = aStudy->NewBuilder();
145     _PTR(GenericAttribute) anAttr =
146       aBuilder->FindOrCreateAttribute(theSObject, "AttributeComment");
147     _PTR(AttributeComment) aComment = anAttr;
148     if (aComment)
149       aComment->SetValue(theValue);
150   }
151
152   CORBA::Object_var SObjectToObject (_PTR(SObject) theSObject,
153                                      _PTR(Study)   theStudy)
154   {
155     SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>
156       (SUIT_Session::session()->activeApplication());
157     if (theSObject) {
158       _PTR(GenericAttribute) anAttr;
159       if (theSObject->FindAttribute(anAttr, "AttributeIOR")) {
160         _PTR(AttributeIOR) anIOR = anAttr;
161         CORBA::String_var aVal = anIOR->Value().c_str();
162         return app->orb()->string_to_object(aVal);
163       }
164     }
165     return CORBA::Object::_nil();
166   }
167
168   CORBA::Object_var SObjectToObject (_PTR(SObject) theSObject)
169   {
170     _PTR(Study) aStudy = GetActiveStudyDocument();
171     return SObjectToObject(theSObject,aStudy);
172   }
173
174   CORBA::Object_var IObjectToObject (const Handle(SALOME_InteractiveObject)& theIO)
175   {
176     if (!theIO.IsNull()) {
177       if (theIO->hasEntry()) {
178         _PTR(Study) aStudy = GetActiveStudyDocument();
179         _PTR(SObject) anObj = aStudy->FindObjectID(theIO->getEntry());
180         return SObjectToObject(anObj,aStudy);
181       }
182     }
183     return CORBA::Object::_nil();
184   }
185
186   CORBA::Object_var IORToObject (const char* theIOR)
187   {
188     SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>
189       (SUIT_Session::session()->activeApplication());
190     return app->orb()->string_to_object(theIOR);
191   }
192
193   int GetNameOfSelectedIObjects(SalomeApp_SelectionMgr* theMgr, QString& theName)
194   {
195     if (!theMgr)
196       return 0;
197
198     SALOME_ListIO selected;
199     theMgr->selectedObjects(selected);
200     int aNbSel = selected.Extent();
201     if (aNbSel == 1) {
202       Handle(SALOME_InteractiveObject) anIObject = selected.First();
203       theName = anIObject->getName();
204     } else {
205       theName = QObject::tr("SMESH_OBJECTS_SELECTED").arg(aNbSel);
206     }
207     return aNbSel;
208   }
209
210   _PTR(SObject) GetMeshOrSubmesh (_PTR(SObject) theSObject)
211   {
212     GEOM::GEOM_Object_var aShape = SObjectToInterface<GEOM::GEOM_Object>(theSObject);
213     if (!aShape->_is_nil()){ //It s a shape
214       return theSObject->GetFather();
215     }
216     _PTR(SObject) aSObject;
217     if (theSObject->ReferencedObject(aSObject)) {
218       aSObject = theSObject->GetFather();
219       return aSObject->GetFather();
220     }
221     return theSObject->GetFather();
222   }
223
224   void ModifiedMesh (_PTR(SObject) theSObject, bool theIsRight)
225   {
226     _PTR(Study) aStudy = GetActiveStudyDocument();
227     if (aStudy->GetProperties()->IsLocked())
228       return;
229
230     _PTR(StudyBuilder) aBuilder = aStudy->NewBuilder();
231     _PTR(GenericAttribute) anAttr =
232       aBuilder->FindOrCreateAttribute(theSObject,"AttributePixMap");
233     _PTR(AttributePixMap) aPixmap = anAttr;
234     if (theIsRight) {
235       aPixmap->SetPixMap("ICON_SMESH_TREE_MESH");
236     } else {
237       aPixmap->SetPixMap("ICON_SMESH_TREE_MESH_WARN");
238     }
239
240     _PTR(ChildIterator) anIter = aStudy->NewChildIterator(theSObject);
241     for (int i = 1; anIter->More(); anIter->Next(), i++) {
242       _PTR(SObject) aSObj = anIter->Value();
243       if (i >= 4) {
244         _PTR(ChildIterator) anIter1 = aStudy->NewChildIterator(aSObj);
245         for (; anIter1->More(); anIter1->Next()) {
246           _PTR(SObject) aSObj1 = anIter1->Value();
247           anAttr = aBuilder->FindOrCreateAttribute(aSObj1, "AttributePixMap");
248           aPixmap = anAttr;
249           if (theIsRight) {
250             aPixmap->SetPixMap("ICON_SMESH_TREE_MESH");
251           } else {
252             aPixmap->SetPixMap("ICON_SMESH_TREE_MESH_WARN");
253           }
254         }
255       }
256     }
257   }
258
259 //  void UpdateObjBrowser (bool)
260 //  {
261 //    //SMESHGUI::activeStudy()->updateObjBrowser(true);
262 //    //SalomeApp_Application* anApp = dynamic_cast<SalomeApp_Application*>
263 //    //  (SUIT_Session::session()->activeApplication());
264 //    //if (anApp) anApp->objectBrowser()->updateTree();
265 //    SMESHGUI::GetSMESHGUI()->updateObjBrowser();
266 //  }
267 }