Salome HOME
Copyright update 2022
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_Utils.cxx
1 // Copyright (C) 2007-2022  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.toUtf8().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.toUtf8().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.toUtf8().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.toUtf8().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   CORBA::Object_var EntryToObject( const QString& theEntry )
230   {
231     _PTR(SObject) anObj = getStudy()->FindObjectID( theEntry.toLatin1().data() );
232     return SObjectToObject(anObj);
233   }
234
235   int GetNameOfSelectedIObjects(LightApp_SelectionMgr* theMgr, QString& theName)
236   {
237     if (!theMgr)
238       return 0;
239
240     SALOME_ListIO selected;
241     theMgr->selectedObjects(selected);
242     int aNbSel = selected.Extent();
243     if (aNbSel == 1) {
244       Handle(SALOME_InteractiveObject) anIObject = selected.First();
245       theName = GetName( anIObject );
246     } else {
247       theName = QObject::tr("SMESH_OBJECTS_SELECTED").arg(aNbSel);
248     }
249     return aNbSel;
250   }
251
252   QString GetName( const Handle(SALOME_InteractiveObject)& theIO )
253   {
254     QString name;
255     if ( !theIO.IsNull() )
256     {
257       name = QString( theIO->getName() ).trimmed();
258
259       if ( name.isEmpty() && theIO->hasEntry() )
260       {
261         _PTR(SObject) sObj = getStudy()->FindObjectID( theIO->getEntry() );
262         if ( sObj )
263           name = sObj->GetName().c_str();
264       }
265     }
266     return name.trimmed();
267   }
268
269   _PTR(SObject) GetMeshOrSubmesh (_PTR(SObject) theSObject)
270   {
271     GEOM::GEOM_Object_var aShape = SObjectToInterface<GEOM::GEOM_Object>(theSObject);
272     if (!aShape->_is_nil()){ //It s a shape
273       return theSObject->GetFather();
274     }
275     _PTR(SObject) aSObject;
276     if (theSObject->ReferencedObject(aSObject)) {
277       aSObject = theSObject->GetFather();
278       return aSObject->GetFather();
279     }
280     return theSObject->GetFather();
281   }
282
283   void ShowHelpFile (const QString& theHelpFileName)
284   {
285     LightApp_Application* app = (LightApp_Application*)(SUIT_Session::session()->activeApplication());
286     if (app) {
287       SMESHGUI* gui = SMESHGUI::GetSMESHGUI();
288       app->onHelpContextModule(gui ? app->moduleName(gui->moduleName()) : QString(""),
289                                theHelpFileName);
290     }
291     else {
292       SUIT_MessageBox::warning(0, QObject::tr("WRN_WARNING"),
293                                QObject::tr("EXTERNAL_BROWSER_CANNOT_SHOW_PAGE").
294                                arg(app->resourceMgr()->stringValue("ExternalBrowser", 
295                                                                    "application")).
296                                arg(theHelpFileName));
297     }
298   }
299
300   //=======================================================================
301   /**
302      Return normale to a given face
303   */
304   //=======================================================================
305   gp_XYZ getNormale( const SMDS_MeshFace* theFace )
306   {
307     gp_XYZ n;
308     SMESH_MeshAlgos::FaceNormal( theFace, n, /*normalized=*/true );
309     return n;
310   }
311   
312   QString fromUtf8( const char* txt )
313   {
314 #ifdef PAL22528_UNICODE
315     return QString::fromUtf8( txt );
316 #else
317     return QString( txt );
318 #endif
319   }
320
321   QString fromUtf8( const std::string& txt )
322   {
323     return fromUtf8( txt.c_str() );
324   }
325
326   toUtf8::toUtf8( const QString& txt )
327   {
328 #ifdef PAL22528_UNICODE
329     assign( txt.toUtf8().constData() );
330 #else
331     assign( txt.toLatin1().constData() );
332 #endif
333   }
334
335 } // end of namespace SMESH