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