Salome HOME
Fix 17 regressions
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_Utils.cxx
1 // Copyright (C) 2007-2015  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   _PTR(Study)
80   GetCStudy(const SalomeApp_Study* theStudy)
81   {
82     return theStudy->studyDS();
83   }
84
85   CORBA::Object_var 
86   DataOwnerToObject(const LightApp_DataOwnerPtr& theOwner)
87   {
88     CORBA::Object_var anObj;
89     if(theOwner){
90       const Handle(SALOME_InteractiveObject)& anIO = theOwner->IO();
91       if(!anIO.IsNull()){
92         if(anIO->hasEntry()){
93           _PTR(Study) aStudy = GetActiveStudyDocument();
94           _PTR(SObject) aSObj = aStudy->FindObjectID(anIO->getEntry());
95           anObj = SObjectToObject(aSObj,aStudy);
96         }
97       }
98     }
99     return anObj;
100   }
101
102   SUIT_Study* GetActiveStudy()
103   {
104     SUIT_Application* app = SUIT_Session::session()->activeApplication();
105     if (app)
106       return app->activeStudy();
107     else
108       return NULL;
109   }
110
111   SUIT_ViewWindow* GetActiveWindow()
112   {
113     SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>
114       (SUIT_Session::session()->activeApplication());
115     if (app && app->desktop() )
116       return app->desktop()->activeWindow();
117     else
118       return NULL;
119   }
120
121   _PTR(Study) GetActiveStudyDocument()
122   {
123     SalomeApp_Study* aStudy = dynamic_cast<SalomeApp_Study*>(GetActiveStudy());
124     if (aStudy)
125       return aStudy->studyDS();
126     else
127       return _PTR(Study)();
128   }
129
130   _PTR(SObject) FindSObject (CORBA::Object_ptr theObject)
131   {
132     SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>
133       (SUIT_Session::session()->activeApplication());
134     if (app && !CORBA::is_nil(theObject)) {
135       if(_PTR(Study) aStudy = GetActiveStudyDocument()){
136         CORBA::String_var anIOR = app->orb()->object_to_string(theObject);
137         if (strcmp(anIOR.in(), "") != 0)
138           return aStudy->FindObjectIOR(anIOR.in());
139       }
140     }
141     return _PTR(SObject)();
142   }
143
144   void SetName (_PTR(SObject) theSObject, const QString& theName)
145   {
146     _PTR(Study) aStudy = GetActiveStudyDocument();
147     if (aStudy->GetProperties()->IsLocked())
148       return;
149     SMESHGUI::GetSMESHGen()->SetName(theSObject->GetIOR().c_str(), theName.toLatin1().data());
150   }
151
152   void SetValue (_PTR(SObject) theSObject, const QString& theValue)
153   {
154     _PTR(Study) aStudy = GetActiveStudyDocument();
155     if (aStudy->GetProperties()->IsLocked())
156       return;
157     _PTR(StudyBuilder) aBuilder = aStudy->NewBuilder();
158     _PTR(GenericAttribute) anAttr =
159       aBuilder->FindOrCreateAttribute(theSObject, "AttributeComment");
160     _PTR(AttributeComment) aComment = anAttr;
161     if (aComment)
162       aComment->SetValue(theValue.toLatin1().data());
163   }
164   
165   void setFileName (_PTR(SObject) theSObject, const QString& theValue)
166   {
167     _PTR(Study) aStudy = GetActiveStudyDocument();
168     if (aStudy->GetProperties()->IsLocked())
169       return;
170     _PTR(StudyBuilder) aBuilder = aStudy->NewBuilder();
171     _PTR(GenericAttribute) anAttr =
172       aBuilder->FindOrCreateAttribute(theSObject, "AttributeExternalFileDef");
173     _PTR(AttributeExternalFileDef) aFileName = anAttr;
174     if (aFileName)
175       aFileName->SetValue(theValue.toLatin1().data());
176   }
177   
178   void setFileType (_PTR(SObject) theSObject, const QString& theValue)
179   {
180     _PTR(Study) aStudy = GetActiveStudyDocument();
181     if (aStudy->GetProperties()->IsLocked())
182       return;
183     _PTR(StudyBuilder) aBuilder = aStudy->NewBuilder();
184     _PTR(GenericAttribute) anAttr =
185       aBuilder->FindOrCreateAttribute(theSObject, "AttributeFileType");
186     _PTR(AttributeFileType) aFileType = anAttr;
187     if (aFileType)
188       aFileType->SetValue(theValue.toLatin1().data());
189   }
190
191   CORBA::Object_var SObjectToObject (_PTR(SObject) theSObject,
192                                      _PTR(Study)   /*theStudy*/)
193   {
194     SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>
195       (SUIT_Session::session()->activeApplication());
196     if (theSObject) {
197       _PTR(GenericAttribute) anAttr;
198       if (theSObject->FindAttribute(anAttr, "AttributeIOR")) {
199         _PTR(AttributeIOR) anIOR = anAttr;
200         std::string aVal = anIOR->Value();
201         // string_to_object() DOC: If the input string is not valid ...
202         // a CORBA::SystemException is thrown.
203         if ( aVal.size() > 0 )
204           return app->orb()->string_to_object( aVal.c_str() );
205       }
206     }
207     return CORBA::Object::_nil();
208   }
209
210   CORBA::Object_var SObjectToObject (_PTR(SObject) theSObject)
211   {
212     _PTR(Study) aStudy;// = GetActiveStudyDocument(); -- aStudy is not used
213     return SObjectToObject(theSObject,aStudy);
214   }
215
216   _PTR(SObject) ObjectToSObject( CORBA::Object_ptr theObject )
217   {
218     _PTR(SObject) res;
219     SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>
220       (SUIT_Session::session()->activeApplication());
221     if ( app ) {
222       CORBA::String_var ior = app->orb()->object_to_string( theObject );
223       SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
224       if ( study && strlen( ior ) > 0 )
225         res = study->studyDS()->FindObjectIOR( ior.in() );
226     }
227     return res;
228   }
229
230   CORBA::Object_var IObjectToObject (const Handle(SALOME_InteractiveObject)& theIO)
231   {
232     if (!theIO.IsNull()) {
233       if (theIO->hasEntry()) {
234         _PTR(Study) aStudy = GetActiveStudyDocument();
235         _PTR(SObject) anObj = aStudy->FindObjectID(theIO->getEntry());
236         return SObjectToObject(anObj,aStudy);
237       }
238     }
239     return CORBA::Object::_nil();
240   }
241
242   CORBA::Object_var IORToObject (const QString& theIOR)
243   {
244     SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>
245       (SUIT_Session::session()->activeApplication());
246     return app->orb()->string_to_object(theIOR.toLatin1().data());
247   }
248
249   int GetNameOfSelectedIObjects(LightApp_SelectionMgr* theMgr, QString& theName)
250   {
251     if (!theMgr)
252       return 0;
253
254     SALOME_ListIO selected;
255     theMgr->selectedObjects(selected);
256     int aNbSel = selected.Extent();
257     if (aNbSel == 1) {
258       Handle(SALOME_InteractiveObject) anIObject = selected.First();
259       theName = QString( anIObject->getName() ).trimmed();
260     } else {
261       theName = QObject::tr("SMESH_OBJECTS_SELECTED").arg(aNbSel);
262     }
263     return aNbSel;
264   }
265
266   _PTR(SObject) GetMeshOrSubmesh (_PTR(SObject) theSObject)
267   {
268     GEOM::GEOM_Object_var aShape = SObjectToInterface<GEOM::GEOM_Object>(theSObject);
269     if (!aShape->_is_nil()){ //It s a shape
270       return theSObject->GetFather();
271     }
272     _PTR(SObject) aSObject;
273     if (theSObject->ReferencedObject(aSObject)) {
274       aSObject = theSObject->GetFather();
275       return aSObject->GetFather();
276     }
277     return theSObject->GetFather();
278   }
279
280   void ModifiedMesh (_PTR(SObject) theSObject, bool theIsNotModif, bool isEmptyMesh)
281   {
282     _PTR(Study) aStudy = GetActiveStudyDocument();
283     if (aStudy->GetProperties()->IsLocked())
284       return;
285
286     _PTR(StudyBuilder) aBuilder = aStudy->NewBuilder();
287     _PTR(GenericAttribute) anAttr =
288       aBuilder->FindOrCreateAttribute(theSObject,"AttributePixMap");
289     _PTR(AttributePixMap) aPixmap = anAttr;
290
291     std::string pmName;
292     if (theIsNotModif)
293       pmName = "ICON_SMESH_TREE_MESH";
294     else if ( isEmptyMesh )
295       pmName = "ICON_SMESH_TREE_MESH_WARN";
296     else
297       pmName = "ICON_SMESH_TREE_MESH_PARTIAL";
298     aPixmap->SetPixMap( pmName );
299
300     _PTR(ChildIterator) anIter = aStudy->NewChildIterator(theSObject);
301     for (int i = 1; anIter->More(); anIter->Next(), i++) {
302       _PTR(SObject) aSObj = anIter->Value();
303       /*if (i >= 1)*/ {
304         _PTR(ChildIterator) anIter1 = aStudy->NewChildIterator(aSObj);
305         for ( ; anIter1->More(); anIter1->Next())
306         {
307           _PTR(SObject) aSObj1 = anIter1->Value();
308           _PTR(SObject) aSObjectRef;
309           if (aSObj1->ReferencedObject(aSObjectRef))
310             continue; // reference to an object
311
312           anAttr = aBuilder->FindOrCreateAttribute(aSObj1, "AttributePixMap");
313           aPixmap = anAttr;
314
315           std::string entry = aSObj1->GetID();
316           int objType = SMESHGUI_Selection::type( entry.c_str(), aStudy );
317           if ( objType == SMESH::HYPOTHESIS || objType == SMESH::ALGORITHM )
318             continue;
319
320           SMESH::SMESH_IDSource_var idSrc = SObjectToInterface<SMESH::SMESH_IDSource>( aSObj1 );
321           if ( !idSrc->_is_nil() )
322           {
323             SMESH::SMESH_GroupOnFilter_var gof =
324               SObjectToInterface<SMESH::SMESH_GroupOnFilter>( aSObj1 );
325             const bool isGroupOnFilter = !gof->_is_nil();
326
327             bool isEmpty = false;
328             if ( !isGroupOnFilter ) // GetTypes() can be very long on GroupOnFilter!
329             {
330               SMESH::array_of_ElementType_var elemTypes = idSrc->GetTypes();
331               isEmpty = ( elemTypes->length() == 0 );
332             }
333             if ( isEmpty )
334               aPixmap->SetPixMap("ICON_SMESH_TREE_MESH_WARN");
335             else if ( objType != GROUP )
336               aPixmap->SetPixMap( "ICON_SMESH_TREE_MESH" );
337             else if ( isGroupOnFilter )
338               aPixmap->SetPixMap( "ICON_SMESH_TREE_GROUP_ON_FILTER" );
339             else
340               aPixmap->SetPixMap( "ICON_SMESH_TREE_GROUP" );
341           }
342           else
343           {
344             if ( !theIsNotModif )
345               aPixmap->SetPixMap( pmName );
346             else if ( objType == GROUP )
347               aPixmap->SetPixMap( "ICON_SMESH_TREE_GROUP" );
348             else
349               aPixmap->SetPixMap( "ICON_SMESH_TREE_MESH" );
350           }
351         }
352       }
353     }
354   }
355
356   void ShowHelpFile (const QString& theHelpFileName)
357   {
358     LightApp_Application* app = (LightApp_Application*)(SUIT_Session::session()->activeApplication());
359     if (app) {
360       SMESHGUI* gui = SMESHGUI::GetSMESHGUI();
361       app->onHelpContextModule(gui ? app->moduleName(gui->moduleName()) : QString(""),
362                                theHelpFileName);
363     }
364     else {
365       SUIT_MessageBox::warning(0, QObject::tr("WRN_WARNING"),
366                                QObject::tr("EXTERNAL_BROWSER_CANNOT_SHOW_PAGE").
367                                arg(app->resourceMgr()->stringValue("ExternalBrowser", 
368                                                                    "application")).
369                                arg(theHelpFileName));
370     }
371   }
372
373   //=======================================================================
374   /**
375      Return normale to a given face
376   */
377   //=======================================================================
378   gp_XYZ getNormale( const SMDS_MeshFace* theFace )
379   {
380     gp_XYZ n;
381     SMESH_MeshAlgos::FaceNormal( theFace, n, /*normalized=*/true );
382     return n;
383   }
384   
385   QString fromUtf8( const char* txt )
386   {
387 #ifdef PAL22528_UNICODE
388     return QString::fromUtf8( txt );
389 #else
390     return QString( txt );
391 #endif
392   }
393
394   QString fromUtf8( const std::string& txt )
395   {
396     return fromUtf8( txt.c_str() );
397   }
398
399   toUtf8::toUtf8( const QString& txt )
400   {
401 #ifdef PAL22528_UNICODE
402     assign( txt.toUtf8().constData() );
403 #else
404     assign( txt.toLatin1().constData() );
405 #endif
406   }
407
408 } // end of namespace SMESH