Salome HOME
IPAL9193 (Jun 16)
[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   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 SalomeApp_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   CORBA::Object_var SObjectToObject (_PTR(SObject) theSObject,
158                                      _PTR(Study)   theStudy)
159   {
160     SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>
161       (SUIT_Session::session()->activeApplication());
162     if (theSObject) {
163       _PTR(GenericAttribute) anAttr;
164       if (theSObject->FindAttribute(anAttr, "AttributeIOR")) {
165         _PTR(AttributeIOR) anIOR = anAttr;
166         CORBA::String_var aVal = anIOR->Value().c_str();
167         return app->orb()->string_to_object(aVal);
168       }
169     }
170     return CORBA::Object::_nil();
171   }
172
173   CORBA::Object_var SObjectToObject (_PTR(SObject) theSObject)
174   {
175     _PTR(Study) aStudy = GetActiveStudyDocument();
176     return SObjectToObject(theSObject,aStudy);
177   }
178
179   CORBA::Object_var IObjectToObject (const Handle(SALOME_InteractiveObject)& theIO)
180   {
181     if (!theIO.IsNull()) {
182       if (theIO->hasEntry()) {
183         _PTR(Study) aStudy = GetActiveStudyDocument();
184         _PTR(SObject) anObj = aStudy->FindObjectID(theIO->getEntry());
185         return SObjectToObject(anObj,aStudy);
186       }
187     }
188     return CORBA::Object::_nil();
189   }
190
191   CORBA::Object_var IORToObject (const char* theIOR)
192   {
193     SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>
194       (SUIT_Session::session()->activeApplication());
195     return app->orb()->string_to_object(theIOR);
196   }
197
198   int GetNameOfSelectedIObjects(SalomeApp_SelectionMgr* theMgr, QString& theName)
199   {
200     if (!theMgr)
201       return 0;
202
203     SALOME_ListIO selected;
204     theMgr->selectedObjects(selected);
205     int aNbSel = selected.Extent();
206     if (aNbSel == 1) {
207       Handle(SALOME_InteractiveObject) anIObject = selected.First();
208       theName = anIObject->getName();
209     } else {
210       theName = QObject::tr("SMESH_OBJECTS_SELECTED").arg(aNbSel);
211     }
212     return aNbSel;
213   }
214
215   _PTR(SObject) GetMeshOrSubmesh (_PTR(SObject) theSObject)
216   {
217     GEOM::GEOM_Object_var aShape = SObjectToInterface<GEOM::GEOM_Object>(theSObject);
218     if (!aShape->_is_nil()){ //It s a shape
219       return theSObject->GetFather();
220     }
221     _PTR(SObject) aSObject;
222     if (theSObject->ReferencedObject(aSObject)) {
223       aSObject = theSObject->GetFather();
224       return aSObject->GetFather();
225     }
226     return theSObject->GetFather();
227   }
228
229   void ModifiedMesh (_PTR(SObject) theSObject, bool theIsRight)
230   {
231     _PTR(Study) aStudy = GetActiveStudyDocument();
232     if (aStudy->GetProperties()->IsLocked())
233       return;
234
235     _PTR(StudyBuilder) aBuilder = aStudy->NewBuilder();
236     _PTR(GenericAttribute) anAttr =
237       aBuilder->FindOrCreateAttribute(theSObject,"AttributePixMap");
238     _PTR(AttributePixMap) aPixmap = anAttr;
239     if (theIsRight) {
240       aPixmap->SetPixMap("ICON_SMESH_TREE_MESH");
241     } else {
242       aPixmap->SetPixMap("ICON_SMESH_TREE_MESH_WARN");
243     }
244
245     _PTR(ChildIterator) anIter = aStudy->NewChildIterator(theSObject);
246     for (int i = 1; anIter->More(); anIter->Next(), i++) {
247       _PTR(SObject) aSObj = anIter->Value();
248       if (i >= 4) {
249         _PTR(ChildIterator) anIter1 = aStudy->NewChildIterator(aSObj);
250         for (; anIter1->More(); anIter1->Next()) {
251           _PTR(SObject) aSObj1 = anIter1->Value();
252           anAttr = aBuilder->FindOrCreateAttribute(aSObj1, "AttributePixMap");
253           aPixmap = anAttr;
254           if (theIsRight) {
255             aPixmap->SetPixMap("ICON_SMESH_TREE_MESH");
256           } else {
257             aPixmap->SetPixMap("ICON_SMESH_TREE_MESH_WARN");
258           }
259         }
260       }
261     }
262   }
263
264 //  void UpdateObjBrowser (bool)
265 //  {
266 //    //SMESHGUI::activeStudy()->updateObjBrowser(true);
267 //    //SalomeApp_Application* anApp = dynamic_cast<SalomeApp_Application*>
268 //    //  (SUIT_Session::session()->activeApplication());
269 //    //if (anApp) anApp->objectBrowser()->updateTree();
270 //    SMESHGUI::GetSMESHGUI()->updateObjBrowser();
271 //  }
272 }