Salome HOME
Restore change of mesh icon depending on its status
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_Utils.cxx
1 // Copyright (C) 2007-2016  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, or (at your option) any later version.
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
23 // SMESH SMESHGUI : GUI for SMESH component
24 // File   : SMESHGUI_Utils.cxx
25 // Author : Open CASCADE S.A.S.
26 // SMESH includes
27 //
28 #include "SMESHGUI_Utils.h"
29 #include "SMESHGUI.h"
30 #include "SMESHGUI_Selection.h"
31 #include "SMESH_Type.h"
32 #include "SMESH_MeshAlgos.hxx"
33 #include <SMDS_MeshNode.hxx>
34 #include <SMDS_MeshFace.hxx>
35
36 // SALOME GUI includes
37 #include <SUIT_Desktop.h>
38 #include <SUIT_Session.h>
39 #include <SUIT_MessageBox.h>
40 #include <SUIT_ResourceMgr.h>
41
42 #include <LightApp_SelectionMgr.h>
43 #include <SalomeApp_Application.h>
44 #include <SalomeApp_Module.h>
45 #include <SalomeApp_Study.h>
46
47 #include <SALOME_ListIO.hxx>
48
49 // OCC includes
50 #include <gp_XYZ.hxx>
51 #include <TColgp_Array1OfXYZ.hxx>
52
53 #include CORBA_SERVER_HEADER(SMESH_Group)
54
55 //VSR: uncomment below macro to support unicode text properly in SALOME
56 //     current commented out due to regressions
57 //#define PAL22528_UNICODE
58
59 namespace SMESH
60 {
61   SUIT_Desktop*
62   GetDesktop(const CAM_Module* theModule)
63   {
64     return theModule->application()->desktop();
65   }
66
67   LightApp_SelectionMgr*
68   GetSelectionMgr(const SalomeApp_Module* theModule)
69   {
70     return theModule->getApp()->selectionMgr();
71   }
72
73   SUIT_ResourceMgr*
74   GetResourceMgr( const SalomeApp_Module* )
75   {
76     return SUIT_Session::session()->resourceMgr();
77   }
78
79   CORBA::Object_var 
80   DataOwnerToObject(const LightApp_DataOwnerPtr& theOwner)
81   {
82     CORBA::Object_var anObj;
83     if(theOwner){
84       const Handle(SALOME_InteractiveObject)& anIO = theOwner->IO();
85       if(!anIO.IsNull()){
86         if(anIO->hasEntry()){
87           _PTR(SObject) aSObj = getStudy()->FindObjectID(anIO->getEntry());
88           anObj = SObjectToObject(aSObj);
89         }
90       }
91     }
92     return anObj;
93   }
94
95   SUIT_Study* GetActiveStudy()
96   {
97     SUIT_Application* app = SUIT_Session::session()->activeApplication();
98     if (app)
99       return app->activeStudy();
100     else
101       return NULL;
102   }
103
104   SUIT_ViewWindow* GetActiveWindow()
105   {
106     SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>
107       (SUIT_Session::session()->activeApplication());
108     if (app && app->desktop() )
109       return app->desktop()->activeWindow();
110     else
111       return NULL;
112   }
113
114   _PTR(Study) getStudy()
115   {
116     static _PTR(Study) _study;
117     if(!_study)
118       _study = SalomeApp_Application::getStudy();
119     return _study;
120   }
121
122   _PTR(SObject) FindSObject (CORBA::Object_ptr theObject)
123   {
124     SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>
125       (SUIT_Session::session()->activeApplication());
126     if (app && !CORBA::is_nil(theObject)) {
127       CORBA::String_var anIOR = app->orb()->object_to_string(theObject);
128       if (strcmp(anIOR.in(), "") != 0)
129         return getStudy()->FindObjectIOR(anIOR.in());
130     }
131     return _PTR(SObject)();
132   }
133
134   void SetName (_PTR(SObject) theSObject, const QString& theName)
135   {
136     if (getStudy()->GetProperties()->IsLocked())
137       return;
138     SMESHGUI::GetSMESHGen()->SetName(theSObject->GetIOR().c_str(), theName.toLatin1().data());
139   }
140
141   void SetValue (_PTR(SObject) theSObject, const QString& theValue)
142   {
143     _PTR(Study) aStudy = getStudy();
144     if (aStudy->GetProperties()->IsLocked())
145       return;
146     _PTR(StudyBuilder) aBuilder = aStudy->NewBuilder();
147     _PTR(GenericAttribute) anAttr =
148       aBuilder->FindOrCreateAttribute(theSObject, "AttributeComment");
149     _PTR(AttributeComment) aComment = anAttr;
150     if (aComment)
151       aComment->SetValue(theValue.toLatin1().data());
152   }
153   
154   void setFileName (_PTR(SObject) theSObject, const QString& theValue)
155   {
156     _PTR(Study) aStudy = getStudy();
157     if (aStudy->GetProperties()->IsLocked())
158       return;
159     _PTR(StudyBuilder) aBuilder = aStudy->NewBuilder();
160     _PTR(GenericAttribute) anAttr =
161       aBuilder->FindOrCreateAttribute(theSObject, "AttributeExternalFileDef");
162     _PTR(AttributeExternalFileDef) aFileName = anAttr;
163     if (aFileName)
164       aFileName->SetValue(theValue.toLatin1().data());
165   }
166   
167   void setFileType (_PTR(SObject) theSObject, const QString& theValue)
168   {
169     _PTR(Study) aStudy = getStudy();
170     if (aStudy->GetProperties()->IsLocked())
171       return;
172     _PTR(StudyBuilder) aBuilder = aStudy->NewBuilder();
173     _PTR(GenericAttribute) anAttr =
174       aBuilder->FindOrCreateAttribute(theSObject, "AttributeFileType");
175     _PTR(AttributeFileType) aFileType = anAttr;
176     if (aFileType)
177       aFileType->SetValue(theValue.toLatin1().data());
178   }
179
180   CORBA::Object_var SObjectToObject (_PTR(SObject) theSObject )
181   {
182     SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>
183       (SUIT_Session::session()->activeApplication());
184     if (theSObject) {
185       _PTR(GenericAttribute) anAttr;
186       if (theSObject->FindAttribute(anAttr, "AttributeIOR")) {
187         _PTR(AttributeIOR) anIOR = anAttr;
188         std::string aVal = anIOR->Value();
189         // string_to_object() DOC: If the input string is not valid ...
190         // a CORBA::SystemException is thrown.
191         if ( aVal.size() > 0 )
192           return app->orb()->string_to_object( aVal.c_str() );
193       }
194     }
195     return CORBA::Object::_nil();
196   }
197
198   _PTR(SObject) ObjectToSObject( CORBA::Object_ptr theObject )
199   {
200     _PTR(SObject) res;
201     SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>
202       (SUIT_Session::session()->activeApplication());
203     if ( app ) {
204       CORBA::String_var ior = app->orb()->object_to_string( theObject );
205       if ( strlen( ior ) > 0 )
206         res = getStudy()->FindObjectIOR( ior.in() );
207     }
208     return res;
209   }
210
211   CORBA::Object_var IObjectToObject (const Handle(SALOME_InteractiveObject)& theIO)
212   {
213     if (!theIO.IsNull()) {
214       if (theIO->hasEntry()) {
215         _PTR(SObject) anObj = getStudy()->FindObjectID(theIO->getEntry());
216         return SObjectToObject(anObj);
217       }
218     }
219     return CORBA::Object::_nil();
220   }
221
222   CORBA::Object_var IORToObject (const QString& theIOR)
223   {
224     SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>
225       (SUIT_Session::session()->activeApplication());
226     return app->orb()->string_to_object(theIOR.toLatin1().data());
227   }
228
229   int GetNameOfSelectedIObjects(LightApp_SelectionMgr* theMgr, QString& theName)
230   {
231     if (!theMgr)
232       return 0;
233
234     SALOME_ListIO selected;
235     theMgr->selectedObjects(selected);
236     int aNbSel = selected.Extent();
237     if (aNbSel == 1) {
238       Handle(SALOME_InteractiveObject) anIObject = selected.First();
239       theName = QString( anIObject->getName() ).trimmed();
240     } else {
241       theName = QObject::tr("SMESH_OBJECTS_SELECTED").arg(aNbSel);
242     }
243     return aNbSel;
244   }
245
246   _PTR(SObject) GetMeshOrSubmesh (_PTR(SObject) theSObject)
247   {
248     GEOM::GEOM_Object_var aShape = SObjectToInterface<GEOM::GEOM_Object>(theSObject);
249     if (!aShape->_is_nil()){ //It s a shape
250       return theSObject->GetFather();
251     }
252     _PTR(SObject) aSObject;
253     if (theSObject->ReferencedObject(aSObject)) {
254       aSObject = theSObject->GetFather();
255       return aSObject->GetFather();
256     }
257     return theSObject->GetFather();
258   }
259
260   void ShowHelpFile (const QString& theHelpFileName)
261   {
262     LightApp_Application* app = (LightApp_Application*)(SUIT_Session::session()->activeApplication());
263     if (app) {
264       SMESHGUI* gui = SMESHGUI::GetSMESHGUI();
265       app->onHelpContextModule(gui ? app->moduleName(gui->moduleName()) : QString(""),
266                                theHelpFileName);
267     }
268     else {
269       SUIT_MessageBox::warning(0, QObject::tr("WRN_WARNING"),
270                                QObject::tr("EXTERNAL_BROWSER_CANNOT_SHOW_PAGE").
271                                arg(app->resourceMgr()->stringValue("ExternalBrowser", 
272                                                                    "application")).
273                                arg(theHelpFileName));
274     }
275   }
276
277   //=======================================================================
278   /**
279      Return normale to a given face
280   */
281   //=======================================================================
282   gp_XYZ getNormale( const SMDS_MeshFace* theFace )
283   {
284     gp_XYZ n;
285     SMESH_MeshAlgos::FaceNormal( theFace, n, /*normalized=*/true );
286     return n;
287   }
288   
289   QString fromUtf8( const char* txt )
290   {
291 #ifdef PAL22528_UNICODE
292     return QString::fromUtf8( txt );
293 #else
294     return QString( txt );
295 #endif
296   }
297
298   QString fromUtf8( const std::string& txt )
299   {
300     return fromUtf8( txt.c_str() );
301   }
302
303   toUtf8::toUtf8( const QString& txt )
304   {
305 #ifdef PAL22528_UNICODE
306     assign( txt.toUtf8().constData() );
307 #else
308     assign( txt.toLatin1().constData() );
309 #endif
310   }
311
312 } // end of namespace SMESH