Salome HOME
Join modifications from BR_Dev_For_4_0 tag V4_1_1.
[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 #include "SMESHGUI.h"
25
26 #include "OB_Browser.h"
27
28 #include "SUIT_Desktop.h"
29 #include "SUIT_Application.h"
30 #include "SUIT_Session.h"
31 #include "SUIT_MessageBox.h"
32
33 #include "LightApp_SelectionMgr.h"
34 #include "SalomeApp_Application.h"
35 #include "SalomeApp_Module.h"
36 #include "SalomeApp_Study.h"
37
38 #include "SALOME_ListIO.hxx"
39
40 #include "SALOMEconfig.h"
41 #include CORBA_CLIENT_HEADER(GEOM_Gen)
42
43 #include "utilities.h"
44
45 //using namespace std;
46
47 namespace SMESH{
48
49   SUIT_Desktop*
50   GetDesktop(const CAM_Module* theModule)
51   {
52     return theModule->application()->desktop();
53   }
54
55   LightApp_SelectionMgr*
56   GetSelectionMgr(const SalomeApp_Module* theModule)
57   {
58     return theModule->getApp()->selectionMgr();
59   }
60
61   SUIT_ResourceMgr*
62   GetResourceMgr( const SalomeApp_Module* )
63   {
64     return SUIT_Session::session()->resourceMgr();
65   }
66
67   _PTR(Study)
68   GetCStudy(const SalomeApp_Study* theStudy)
69   {
70     return theStudy->studyDS();
71   }
72
73   CORBA::Object_var 
74   DataOwnerToObject(const LightApp_DataOwnerPtr& theOwner)
75   {
76     CORBA::Object_var anObj;
77     if(theOwner){
78       const Handle(SALOME_InteractiveObject)& anIO = theOwner->IO();
79       if(!anIO.IsNull()){
80         if(anIO->hasEntry()){
81           _PTR(Study) aStudy = GetActiveStudyDocument();
82           _PTR(SObject) aSObj = aStudy->FindObjectID(anIO->getEntry());
83           anObj = SObjectToObject(aSObj,aStudy);
84         }
85       }
86     }
87     return anObj;
88   }
89
90
91   SUIT_Study* GetActiveStudy()
92   {
93     SUIT_Application* app = SUIT_Session::session()->activeApplication();
94     if (app)
95       return app->activeStudy();
96     else
97       return NULL;
98   }
99
100   SUIT_ViewWindow* GetActiveWindow()
101   {
102     SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>
103       (SUIT_Session::session()->activeApplication());
104     if (app && app->desktop() )
105       return app->desktop()->activeWindow();
106     else
107       return NULL;
108   }
109
110   _PTR(Study) GetActiveStudyDocument()
111   {
112     SalomeApp_Study* aStudy = dynamic_cast<SalomeApp_Study*>(GetActiveStudy());
113     if (aStudy)
114       return aStudy->studyDS();
115     else
116       return _PTR(Study)();
117   }
118
119   _PTR(SObject) FindSObject (CORBA::Object_ptr theObject)
120   {
121     SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>
122       (SUIT_Session::session()->activeApplication());
123     if (app && !CORBA::is_nil(theObject)) {
124       if(_PTR(Study) aStudy = GetActiveStudyDocument()){
125         CORBA::String_var anIOR = app->orb()->object_to_string(theObject);
126         if (strcmp(anIOR.in(), "") != 0)
127           return aStudy->FindObjectIOR(anIOR.in());
128       }
129     }
130     return _PTR(SObject)();
131   }
132
133   void SetName (_PTR(SObject) theSObject, const char* theName)
134   {
135     _PTR(Study) aStudy = GetActiveStudyDocument();
136     if (aStudy->GetProperties()->IsLocked())
137       return;
138     _PTR(StudyBuilder) aBuilder = aStudy->NewBuilder();
139     _PTR(GenericAttribute) anAttr =
140       aBuilder->FindOrCreateAttribute(theSObject, "AttributeName");
141     _PTR(AttributeName) aName = anAttr;
142     if (aName)
143       aName->SetValue(theName);
144   }
145
146   void SetValue (_PTR(SObject) theSObject, const char* theValue)
147   {
148     _PTR(Study) aStudy = GetActiveStudyDocument();
149     if (aStudy->GetProperties()->IsLocked())
150       return;
151     _PTR(StudyBuilder) aBuilder = aStudy->NewBuilder();
152     _PTR(GenericAttribute) anAttr =
153       aBuilder->FindOrCreateAttribute(theSObject, "AttributeComment");
154     _PTR(AttributeComment) aComment = anAttr;
155     if (aComment)
156       aComment->SetValue(theValue);
157   }
158   
159   void setFileName (_PTR(SObject) theSObject, const char* theValue)
160   {
161     _PTR(Study) aStudy = GetActiveStudyDocument();
162     if (aStudy->GetProperties()->IsLocked())
163       return;
164     _PTR(StudyBuilder) aBuilder = aStudy->NewBuilder();
165     _PTR(GenericAttribute) anAttr =
166       aBuilder->FindOrCreateAttribute(theSObject, "AttributeExternalFileDef");
167     _PTR(AttributeExternalFileDef) aFileName = anAttr;
168     if (aFileName)
169       aFileName->SetValue(theValue);
170   }
171   
172   void setFileType (_PTR(SObject) theSObject, const char* theValue)
173   {
174     _PTR(Study) aStudy = GetActiveStudyDocument();
175     if (aStudy->GetProperties()->IsLocked())
176       return;
177     _PTR(StudyBuilder) aBuilder = aStudy->NewBuilder();
178     _PTR(GenericAttribute) anAttr =
179       aBuilder->FindOrCreateAttribute(theSObject, "AttributeFileType");
180     _PTR(AttributeFileType) aFileType = anAttr;
181     if (aFileType)
182       aFileType->SetValue(theValue);
183   }
184
185   CORBA::Object_var SObjectToObject (_PTR(SObject) theSObject,
186                                      _PTR(Study)   theStudy)
187   {
188     SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>
189       (SUIT_Session::session()->activeApplication());
190     if (theSObject) {
191       _PTR(GenericAttribute) anAttr;
192       if (theSObject->FindAttribute(anAttr, "AttributeIOR")) {
193         _PTR(AttributeIOR) anIOR = anAttr;
194         CORBA::String_var aVal = anIOR->Value().c_str();
195         return app->orb()->string_to_object(aVal);
196       }
197     }
198     return CORBA::Object::_nil();
199   }
200
201   CORBA::Object_var SObjectToObject (_PTR(SObject) theSObject)
202   {
203     _PTR(Study) aStudy = GetActiveStudyDocument();
204     return SObjectToObject(theSObject,aStudy);
205   }
206
207   CORBA::Object_var IObjectToObject (const Handle(SALOME_InteractiveObject)& theIO)
208   {
209     if (!theIO.IsNull()) {
210       if (theIO->hasEntry()) {
211         _PTR(Study) aStudy = GetActiveStudyDocument();
212         _PTR(SObject) anObj = aStudy->FindObjectID(theIO->getEntry());
213         return SObjectToObject(anObj,aStudy);
214       }
215     }
216     return CORBA::Object::_nil();
217   }
218
219   CORBA::Object_var IORToObject (const char* theIOR)
220   {
221     SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>
222       (SUIT_Session::session()->activeApplication());
223     return app->orb()->string_to_object(theIOR);
224   }
225
226   int GetNameOfSelectedIObjects(LightApp_SelectionMgr* theMgr, QString& theName)
227   {
228     if (!theMgr)
229       return 0;
230
231     SALOME_ListIO selected;
232     theMgr->selectedObjects(selected);
233     int aNbSel = selected.Extent();
234     if (aNbSel == 1) {
235       Handle(SALOME_InteractiveObject) anIObject = selected.First();
236       theName = anIObject->getName();
237     } else {
238       theName = QObject::tr("SMESH_OBJECTS_SELECTED").arg(aNbSel);
239     }
240     return aNbSel;
241   }
242
243   _PTR(SObject) GetMeshOrSubmesh (_PTR(SObject) theSObject)
244   {
245     GEOM::GEOM_Object_var aShape = SObjectToInterface<GEOM::GEOM_Object>(theSObject);
246     if (!aShape->_is_nil()){ //It s a shape
247       return theSObject->GetFather();
248     }
249     _PTR(SObject) aSObject;
250     if (theSObject->ReferencedObject(aSObject)) {
251       aSObject = theSObject->GetFather();
252       return aSObject->GetFather();
253     }
254     return theSObject->GetFather();
255   }
256
257   void ModifiedMesh (_PTR(SObject) theSObject, bool theIsNotModif, bool isEmptyMesh)
258   {
259     _PTR(Study) aStudy = GetActiveStudyDocument();
260     if (aStudy->GetProperties()->IsLocked())
261       return;
262
263     _PTR(StudyBuilder) aBuilder = aStudy->NewBuilder();
264     _PTR(GenericAttribute) anAttr =
265       aBuilder->FindOrCreateAttribute(theSObject,"AttributePixMap");
266     _PTR(AttributePixMap) aPixmap = anAttr;
267     if (theIsNotModif) {
268       aPixmap->SetPixMap("ICON_SMESH_TREE_MESH");
269     } else if ( isEmptyMesh ) {
270       aPixmap->SetPixMap("ICON_SMESH_TREE_MESH_WARN");
271     } else {
272       aPixmap->SetPixMap("ICON_SMESH_TREE_MESH_PARTIAL");
273     }
274
275     _PTR(ChildIterator) anIter = aStudy->NewChildIterator(theSObject);
276     for (int i = 1; anIter->More(); anIter->Next(), i++) {
277       _PTR(SObject) aSObj = anIter->Value();
278       if (i >= 4) {
279         _PTR(ChildIterator) anIter1 = aStudy->NewChildIterator(aSObj);
280         for (; anIter1->More(); anIter1->Next()) {
281           _PTR(SObject) aSObj1 = anIter1->Value();
282           anAttr = aBuilder->FindOrCreateAttribute(aSObj1, "AttributePixMap");
283           aPixmap = anAttr;
284           if (theIsNotModif) {
285             aPixmap->SetPixMap("ICON_SMESH_TREE_MESH");
286           } else if ( isEmptyMesh ) {
287             aPixmap->SetPixMap("ICON_SMESH_TREE_MESH_WARN");
288           } else {
289             aPixmap->SetPixMap("ICON_SMESH_TREE_MESH_PARTIAL");
290           }
291         }
292       }
293     }
294   }
295
296   void ShowHelpFile (QString theHelpFileName)
297   {
298     LightApp_Application* app = (LightApp_Application*)(SUIT_Session::session()->activeApplication());
299     if (app) {
300       SMESHGUI* gui = SMESHGUI::GetSMESHGUI();
301       app->onHelpContextModule(gui ? app->moduleName(gui->moduleName()) : QString(""),
302                                theHelpFileName);
303     }
304     else {
305       SUIT_MessageBox::warn1(0, QObject::tr("WRN_WARNING"),
306                              QObject::tr("EXTERNAL_BROWSER_CANNOT_SHOW_PAGE").
307                              arg(app->resourceMgr()->stringValue("ExternalBrowser", "application")).arg(theHelpFileName),
308                              QObject::tr("BUT_OK"));
309       }
310   }
311
312 //  void UpdateObjBrowser (bool)
313 //  {
314 //    //SMESHGUI::activeStudy()->updateObjBrowser(true);
315 //    //SalomeApp_Application* anApp = dynamic_cast<SalomeApp_Application*>
316 //    //  (SUIT_Session::session()->activeApplication());
317 //    //if (anApp) anApp->objectBrowser()->updateTree();
318 //    SMESHGUI::GetSMESHGUI()->updateObjBrowser();
319 //  }
320 }