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