Salome HOME
Merge remote branch 'origin/gdd/translations'
[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
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         CORBA::String_var aVal = anIOR->Value().c_str();
201         // string_to_object() DOC: If the input string is not valid ...
202         // a CORBA::SystemException is thrown.
203         if ( aVal && strlen( aVal ) > 0 )
204           return app->orb()->string_to_object(aVal);
205       }
206     }
207     return CORBA::Object::_nil();
208   }
209
210   CORBA::Object_var SObjectToObject (_PTR(SObject) theSObject)
211   {
212     _PTR(Study) aStudy = GetActiveStudyDocument();
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 >= 4) {
304         _PTR(ChildIterator) anIter1 = aStudy->NewChildIterator(aSObj);
305         for ( ; anIter1->More(); anIter1->Next())
306         {
307           _PTR(SObject) aSObj1 = anIter1->Value();
308
309           anAttr = aBuilder->FindOrCreateAttribute(aSObj1, "AttributePixMap");
310           aPixmap = anAttr;
311
312           std::string entry = aSObj1->GetID();
313           int objType = SMESHGUI_Selection::type( entry.c_str(), aStudy );
314
315           SMESH::SMESH_IDSource_var idSrc = SObjectToInterface<SMESH::SMESH_IDSource>( aSObj1 );
316           if ( !idSrc->_is_nil() )
317           {
318             SMESH::SMESH_GroupOnFilter_var gof =
319               SObjectToInterface<SMESH::SMESH_GroupOnFilter>( aSObj1 );
320             const bool isGroupOnFilter = !gof->_is_nil();
321
322             bool isEmpty = false;
323             if ( !isGroupOnFilter ) // GetTypes() can be very long on isGroupOnFilter!
324             {
325               SMESH::array_of_ElementType_var elemTypes = idSrc->GetTypes();
326               isEmpty = ( elemTypes->length() == 0 );
327             }
328             if ( isEmpty )
329               aPixmap->SetPixMap("ICON_SMESH_TREE_MESH_WARN");
330             else if ( objType != GROUP )
331               aPixmap->SetPixMap( "ICON_SMESH_TREE_MESH" );
332             else if ( isGroupOnFilter )
333               aPixmap->SetPixMap( "ICON_SMESH_TREE_GROUP_ON_FILTER" );
334             else
335               aPixmap->SetPixMap( "ICON_SMESH_TREE_GROUP" );
336           }
337           else
338           {
339             if ( !theIsNotModif )
340               aPixmap->SetPixMap( pmName );
341             else if ( objType == GROUP )
342               aPixmap->SetPixMap( "ICON_SMESH_TREE_GROUP" );
343             else
344               aPixmap->SetPixMap( "ICON_SMESH_TREE_MESH" );
345           }
346         }
347       }
348     }
349   }
350
351   void ShowHelpFile (const QString& theHelpFileName)
352   {
353     LightApp_Application* app = (LightApp_Application*)(SUIT_Session::session()->activeApplication());
354     if (app) {
355       SMESHGUI* gui = SMESHGUI::GetSMESHGUI();
356       app->onHelpContextModule(gui ? app->moduleName(gui->moduleName()) : QString(""),
357                                theHelpFileName);
358     }
359     else {
360       SUIT_MessageBox::warning(0, QObject::tr("WRN_WARNING"),
361                                QObject::tr("EXTERNAL_BROWSER_CANNOT_SHOW_PAGE").
362                                arg(app->resourceMgr()->stringValue("ExternalBrowser", 
363                                                                    "application")).
364                                arg(theHelpFileName));
365     }
366   }
367
368   //=======================================================================
369   /**
370      Return normale to a given face
371   */
372   //=======================================================================
373   gp_XYZ getNormale( const SMDS_MeshFace* theFace )
374   {
375     gp_XYZ n;
376     int aNbNode = theFace->NbNodes();
377     TColgp_Array1OfXYZ anArrOfXYZ(1,4);
378     SMDS_ElemIteratorPtr aNodeItr = theFace->nodesIterator();
379     int i = 1;
380     for ( ; aNodeItr->more() && i <= 4; i++ ) {
381       SMDS_MeshNode* aNode = (SMDS_MeshNode*)aNodeItr->next();
382       anArrOfXYZ.SetValue(i, gp_XYZ( aNode->X(), aNode->Y(), aNode->Z() ) );
383     }
384     
385     gp_XYZ q1 = anArrOfXYZ.Value(2) - anArrOfXYZ.Value(1);
386     gp_XYZ q2 = anArrOfXYZ.Value(3) - anArrOfXYZ.Value(1);
387     n  = q1 ^ q2;
388     if ( aNbNode > 3 ) {
389       gp_XYZ q3 = anArrOfXYZ.Value(4) - anArrOfXYZ.Value(1);
390       n += q2 ^ q3;
391     }
392     double len = n.Modulus();
393     if ( len > 0 )
394       n /= len;
395     return n;
396   }
397   
398   QString fromUtf8( const char* txt )
399   {
400 #ifdef PAL22528_UNICODE
401     return QString::fromUtf8( txt );
402 #else
403     return QString( txt );
404 #endif
405   }
406
407   QString fromUtf8( const std::string& txt )
408   {
409     return fromUtf8( txt.c_str() );
410   }
411
412   const char* toUtf8( const QString& txt )
413   {
414 #ifdef PAL22528_UNICODE
415     return txt.toUtf8().constData();
416 #else
417     return txt.toLatin1().constData();
418 #endif
419   }
420
421 } // end of namespace SMESH