Salome HOME
e6fdb1c157d7aa469158bd37b1e59ea8dd56259c
[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 ModifiedMesh (_PTR(SObject) theSObject, bool theIsNotModif, bool isEmptyMesh)
261   {
262     _PTR(Study) aStudy = getStudy();
263     if ( !aStudy || aStudy->GetProperties()->IsLocked())
264       return;
265
266     _PTR(StudyBuilder) aBuilder = aStudy->NewBuilder();
267     _PTR(GenericAttribute) anAttr =
268       aBuilder->FindOrCreateAttribute(theSObject,"AttributePixMap");
269     _PTR(AttributePixMap) aPixmap = anAttr;
270
271     std::string pmName;
272     if (theIsNotModif)
273       pmName = "ICON_SMESH_TREE_MESH";
274     else if ( isEmptyMesh )
275       pmName = "ICON_SMESH_TREE_MESH_WARN";
276     else
277       pmName = "ICON_SMESH_TREE_MESH_PARTIAL";
278     aPixmap->SetPixMap( pmName );
279
280     _PTR(ChildIterator) anIter = aStudy->NewChildIterator(theSObject);
281     for ( ; anIter->More(); anIter->Next() ) {
282       _PTR(SObject) aSObj = anIter->Value();
283       if ( aSObj->Tag() >= SMESH::Tag_FirstSubMesh )
284       {
285         _PTR(ChildIterator) anIter1 = aStudy->NewChildIterator(aSObj);
286         for ( ; anIter1->More(); anIter1->Next())
287         {
288           _PTR(SObject) aSObj1 = anIter1->Value();
289           _PTR(SObject) aSObjectRef;
290           if (aSObj1->ReferencedObject(aSObjectRef))
291             continue; // reference to an object
292
293           anAttr = aBuilder->FindOrCreateAttribute(aSObj1, "AttributePixMap");
294           aPixmap = anAttr;
295
296           std::string entry = aSObj1->GetID();
297           int objType = SMESHGUI_Selection::type( entry.c_str() );
298           if ( objType == SMESH::HYPOTHESIS || objType == SMESH::ALGORITHM )
299             continue;
300
301           SMESH::SMESH_IDSource_var idSrc = SObjectToInterface<SMESH::SMESH_IDSource>( aSObj1 );
302           if ( !idSrc->_is_nil() )
303           {
304             SMESH::SMESH_GroupOnFilter_var gof =
305               SObjectToInterface<SMESH::SMESH_GroupOnFilter>( aSObj1 );
306             const bool isGroupOnFilter = !gof->_is_nil();
307
308             bool isEmpty = false;
309             if ( !isGroupOnFilter ) // GetTypes() can be very long on GroupOnFilter!
310             {
311               SMESH::array_of_ElementType_var elemTypes = idSrc->GetTypes();
312               isEmpty = ( elemTypes->length() == 0 );
313             }
314             if ( isEmpty )
315               aPixmap->SetPixMap("ICON_SMESH_TREE_MESH_WARN");
316             else if ( objType != GROUP )
317               aPixmap->SetPixMap( "ICON_SMESH_TREE_MESH" );
318             else if ( isGroupOnFilter )
319               aPixmap->SetPixMap( "ICON_SMESH_TREE_GROUP_ON_FILTER" );
320             else
321               aPixmap->SetPixMap( "ICON_SMESH_TREE_GROUP" );
322           }
323           else // is it necessary?
324           {
325             if ( !theIsNotModif )
326               aPixmap->SetPixMap( pmName );
327             else if ( objType == GROUP )
328               aPixmap->SetPixMap( "ICON_SMESH_TREE_GROUP" );
329             else
330               aPixmap->SetPixMap( "ICON_SMESH_TREE_MESH" );
331           }
332         }
333       }
334     }
335   }
336
337   void ShowHelpFile (const QString& theHelpFileName)
338   {
339     LightApp_Application* app = (LightApp_Application*)(SUIT_Session::session()->activeApplication());
340     if (app) {
341       SMESHGUI* gui = SMESHGUI::GetSMESHGUI();
342       app->onHelpContextModule(gui ? app->moduleName(gui->moduleName()) : QString(""),
343                                theHelpFileName);
344     }
345     else {
346       SUIT_MessageBox::warning(0, QObject::tr("WRN_WARNING"),
347                                QObject::tr("EXTERNAL_BROWSER_CANNOT_SHOW_PAGE").
348                                arg(app->resourceMgr()->stringValue("ExternalBrowser", 
349                                                                    "application")).
350                                arg(theHelpFileName));
351     }
352   }
353
354   //=======================================================================
355   /**
356      Return normale to a given face
357   */
358   //=======================================================================
359   gp_XYZ getNormale( const SMDS_MeshFace* theFace )
360   {
361     gp_XYZ n;
362     SMESH_MeshAlgos::FaceNormal( theFace, n, /*normalized=*/true );
363     return n;
364   }
365   
366   QString fromUtf8( const char* txt )
367   {
368 #ifdef PAL22528_UNICODE
369     return QString::fromUtf8( txt );
370 #else
371     return QString( txt );
372 #endif
373   }
374
375   QString fromUtf8( const std::string& txt )
376   {
377     return fromUtf8( txt.c_str() );
378   }
379
380   toUtf8::toUtf8( const QString& txt )
381   {
382 #ifdef PAL22528_UNICODE
383     assign( txt.toUtf8().constData() );
384 #else
385     assign( txt.toLatin1().constData() );
386 #endif
387   }
388
389 } // end of namespace SMESH