Salome HOME
19d4e9c93b53c47aa71d1d76f5b54ae486fc1902
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_Tool.cxx
1 // Copyright (C) 2007-2013  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.
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 #include "HYDROGUI_Tool.h"
24
25 #include "HYDROGUI_DataModel.h"
26 #include "HYDROGUI_DataObject.h"
27 #include "HYDROGUI_Module.h"
28 #include "HYDROGUI_Prs.h"
29
30 #include <HYDROData_Document.h>
31 #include <HYDROData_Iterator.h>
32 #include <HYDROData_ImmersibleZone.h>
33 #include <HYDROData_Tool.h>
34
35 #include <GEOMBase.h>
36
37 #include <GraphicsView_Viewer.h>
38
39 #include <SalomeApp_Study.h>
40
41 #include <LightApp_Application.h>
42 #include <LightApp_DataOwner.h>
43 #include <LightApp_SelectionMgr.h>
44
45 #include <OCCViewer_ViewModel.h>
46
47 #include <QtxWorkstack.h>
48
49 #include <STD_TabDesktop.h>
50
51 #include <SUIT_Session.h>
52 #include <SUIT_Study.h>
53 #include <SUIT_ViewManager.h>
54 #include <SUIT_ViewWindow.h>
55
56 #include <SALOMEDSClient.hxx>
57
58 #include <QDir>
59 #include <QFileInfo>
60 #include <QDockWidget>
61 #include <QTextCodec>
62
63 // Definition of this id allows to use 'latin1' (Qt alias for 'ISO-8859-1')
64 // encoding instead of default 'System'
65 #define USE_LATIN1_ENCODING
66
67 QString HYDROGUI_Tool::ToQString( const TCollection_AsciiString& src )
68 {
69 #ifdef USE_LATIN1_ENCODING
70   QTextCodec* codec = QTextCodec::codecForName( "latin1" ); // alias for ISO-8859-1
71 #else
72   QTextCodec* codec = QTextCodec::codecForLocale();
73 #endif
74   QString res;
75   if ( !src.IsEmpty() )
76     res = codec ? codec->toUnicode( (char*)src.ToCString(), src.Length() ) :
77       QString( (char*)src.ToCString() );
78   return res;
79 }
80
81 QString HYDROGUI_Tool::ToQString( const TCollection_ExtendedString& src )
82 {
83   return QString( (QChar*)src.ToExtString(), src.Length() );
84 }
85
86 QString HYDROGUI_Tool::ToQString( const Handle(TCollection_HAsciiString)& src )
87 {
88   if( src.IsNull() )
89     return QString();
90   else
91     return ToQString( src->String() );
92 }
93
94 QString HYDROGUI_Tool::ToQString( const Handle(TCollection_HExtendedString)& src )
95 {
96   if( src.IsNull() )
97     return QString();
98   return ToQString( src->String() );
99 }
100
101 TCollection_AsciiString HYDROGUI_Tool::ToAsciiString( const QString& src )
102 {
103   TCollection_AsciiString res;
104   if( !src.isNull() )
105   {
106 #ifdef USE_LATIN1_ENCODING
107     QTextCodec* codec = QTextCodec::codecForName( "latin1" ); // alias for ISO-8859-1
108 #else
109     QTextCodec* codec = QTextCodec::codecForLocale();
110 #endif
111     if( codec )
112     {
113       QByteArray str = codec->fromUnicode( src );
114       res = TCollection_AsciiString( (Standard_CString)str.constData() );
115     }
116     else
117       res = TCollection_AsciiString( src.toLatin1().data() );
118   }
119   return res;
120 }
121
122 TCollection_ExtendedString HYDROGUI_Tool::ToExtString( const QString& src )
123 {
124   if( src.isEmpty() )
125     return TCollection_ExtendedString();
126
127   Standard_Integer len = src.length();
128   Standard_ExtString extStr = new Standard_ExtCharacter[ ( len + 1 ) * 2 ];
129   memcpy( (void*)extStr, src.unicode(), len * 2 );
130   ((short*)extStr)[ len ] = 0;
131
132   TCollection_ExtendedString trg( extStr );
133   delete [] extStr;
134   return trg;
135 }
136
137 Handle(TCollection_HAsciiString) HYDROGUI_Tool::ToHAsciiString( const QString& src )
138 {
139   return new TCollection_HAsciiString( ToAsciiString( src ) );
140 }
141
142 Handle(TCollection_HExtendedString) HYDROGUI_Tool::ToHExtString( const QString& src )
143 {
144   return new TCollection_HExtendedString( ToExtString( src ) );
145 }
146
147 int HYDROGUI_Tool::GetActiveStudyId()
148 {
149   if( SUIT_Session* aSession = SUIT_Session::session() )
150     if( SUIT_Application* anApp = aSession->activeApplication() )
151       if( SUIT_Study* aStudy = anApp->activeStudy() )
152         return aStudy->id();
153   return 0;
154 }
155
156 QString HYDROGUI_Tool::GetTempDir( const bool theToCreate )
157 {
158   QString aRes;
159
160   char* tmpdir = getenv ( "HYDRO_TMP_DIR" );
161   if ( tmpdir )
162   {
163     // try to create folder if it does not exist
164     QFileInfo fi( tmpdir );
165     if ( !fi.exists() && theToCreate )
166     {
167       if ( QDir().mkdir( tmpdir ) )
168         QFile::setPermissions( tmpdir, (QFile::Permissions)0x4FFFF );
169        QFileInfo fi( tmpdir );
170        if ( !fi.exists() || !fi.isWritable() )
171          tmpdir = 0;
172     }
173   }
174   if ( !tmpdir )
175     tmpdir = getenv ( "TEMP" );
176   if ( !tmpdir )
177     tmpdir = getenv ( "TMP" );
178   if ( !tmpdir )
179   {
180 #ifdef WNT
181     tmpdir = "C:\\";
182 #else
183     tmpdir = "/tmp";
184 #endif
185   }
186   aRes = tmpdir;
187   
188   QFileInfo fi( aRes );
189   if ( !fi.exists() || !fi.isWritable() )
190     aRes = QString::null;
191
192   return aRes;
193 }
194
195 void HYDROGUI_Tool::SetActiveViewManager( HYDROGUI_Module* theModule,
196                                           SUIT_ViewManager* theViewManager )
197 {
198   if( theViewManager )
199     if( SUIT_ViewWindow* aViewWindow = theViewManager->getActiveView() )
200       if( STD_TabDesktop* aTabDesktop = dynamic_cast<STD_TabDesktop*>( theModule->getApp()->desktop() ) )
201         if( QtxWorkstack* aWorkstack = aTabDesktop->workstack() )
202           aWorkstack->setActiveWindow( aViewWindow );
203 }
204
205 void HYDROGUI_Tool::GetPrsSubObjects( HYDROGUI_Module* theModule,
206                                       HYDROData_SequenceOfObjects& theSeq )
207 {
208   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
209   if( !aDocument.IsNull() )
210   {
211     HYDROData_Iterator anIterator( aDocument );
212     for( ; anIterator.More(); anIterator.Next() )
213     {
214       Handle(HYDROData_Entity) anObject = anIterator.Current();
215       if( !anObject.IsNull() && ( 
216         ( anObject->GetKind() == KIND_IMAGE ) ||
217         ( anObject->GetKind() == KIND_POLYLINE ) ||
218         ( anObject->GetKind() == KIND_IMMERSIBLE_ZONE ) ||
219         ( anObject->GetKind() == KIND_REGION ) ||
220         ( anObject->GetKind() == KIND_ZONE ) ||
221         ( anObject->GetKind() == KIND_OBSTACLE ) ) )
222       {
223         theSeq.Append( anObject );
224       }
225     }
226   }
227 }
228
229 HYDROGUI_Prs* HYDROGUI_Tool::GetPresentation( const Handle(HYDROData_Entity)& theObj,
230                                               const GraphicsView_ObjectList& theObjects )
231 {
232   if( !theObj.IsNull() )
233   {
234     GraphicsView_ObjectListIterator anIter( theObjects );
235     while( anIter.hasNext() )
236     {
237       if( HYDROGUI_Prs* aPrs = dynamic_cast<HYDROGUI_Prs*>( anIter.next() ) )
238       {
239         Handle(HYDROData_Entity) anObj = aPrs->getObject();
240         if( IsEqual( anObj, theObj ) )
241           return aPrs;
242       }
243     }
244   }
245   return NULL;
246 }
247
248 GraphicsView_ObjectList HYDROGUI_Tool::GetPrsList( GraphicsView_ViewPort* theViewPort )
249 {
250   GraphicsView_ObjectList aList;
251   if( theViewPort )
252   {
253     GraphicsView_ObjectListIterator anIter( theViewPort->getObjects() );
254     while( anIter.hasNext() )
255       if( HYDROGUI_Prs* aPrs = dynamic_cast<HYDROGUI_Prs*>( anIter.next() ) )
256         aList.append( aPrs );
257   }
258   return aList;
259 }
260
261 HYDROData_SequenceOfObjects HYDROGUI_Tool::GetSelectedObjects( HYDROGUI_Module* theModule )
262 {
263   HYDROData_SequenceOfObjects aSeq;
264
265   HYDROGUI_DataModel* aModel = theModule->getDataModel();
266
267   SUIT_SelectionMgr* aSelectionMgr = theModule->getApp()->selectionMgr();
268   SUIT_DataOwnerPtrList anOwners;
269   aSelectionMgr->selected( anOwners );
270
271   QStringList aCollectedNameList; // to avoid duplication
272   foreach( SUIT_DataOwner* aSUITOwner, anOwners )
273   {
274     if( LightApp_DataOwner* anOwner = dynamic_cast<LightApp_DataOwner*>( aSUITOwner ) )
275     {
276       Handle(HYDROData_Entity) anObject = aModel->objectByEntry( anOwner->entry() );
277       if( !anObject.IsNull() )
278       {
279         QString aName = anObject->GetName();
280         if( !aCollectedNameList.contains( aName ) )
281         {
282           aSeq.Append( anObject );
283           aCollectedNameList.append( aName );
284         }
285       }
286     }
287   }
288   return aSeq;
289 }
290
291 Handle(HYDROData_Entity) HYDROGUI_Tool::GetSelectedObject( HYDROGUI_Module* theModule )
292 {
293   HYDROData_SequenceOfObjects aSeq = GetSelectedObjects( theModule );
294   if( !aSeq.IsEmpty() )
295     return aSeq.First();
296   return NULL;
297 }
298
299 HYDROData_SequenceOfObjects HYDROGUI_Tool::GetGeometryObjects( 
300   HYDROGUI_Module* theModule )
301 {
302   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
303
304   HYDROData_SequenceOfObjects aResSeq;
305
306   HYDROData_Iterator anIter( aDocument );
307   for ( ; anIter.More(); anIter.Next() )
308   {
309     Handle(HYDROData_Entity) anObj = anIter.Current();
310     if ( !HYDROData_Tool::IsGeometryObject( anObj ) )
311       continue;
312
313     aResSeq.Append( anObj );
314   }
315   
316   return aResSeq;
317 }
318
319 ObjectKind HYDROGUI_Tool::GetSelectedPartition( HYDROGUI_Module* theModule )
320 {
321   HYDROGUI_DataModel* aModel = theModule->getDataModel();
322
323   SUIT_SelectionMgr* aSelectionMgr = theModule->getApp()->selectionMgr();
324   SUIT_DataOwnerPtrList anOwners;
325   aSelectionMgr->selected( anOwners );
326
327   if( anOwners.size() != 1 )
328     return KIND_UNKNOWN;
329
330   if( LightApp_DataOwner* anOwner = dynamic_cast<LightApp_DataOwner*>( anOwners.first().operator->() ) )
331   {
332     QString anEntry = anOwner->entry();
333     QString aPrefix = HYDROGUI_DataObject::entryPrefix();
334     if( anEntry.left( aPrefix.length() ) == aPrefix )
335     {
336       anEntry.remove( aPrefix );
337       for( ObjectKind anObjectKind = KIND_UNKNOWN + 1; anObjectKind <= KIND_LAST; anObjectKind++ )
338         if( HYDROGUI_DataModel::tr( HYDROGUI_DataModel::partitionName( anObjectKind ).toAscii() ) == anEntry )
339           return anObjectKind;
340     }
341   }
342   return KIND_UNKNOWN;
343 }
344
345 QStringList HYDROGUI_Tool::GetSelectedGeomObjects( HYDROGUI_Module* theModule )
346 {
347   QStringList anEntryList;
348
349   // Get active SalomeApp_Study
350   SalomeApp_Study* aStudy = NULL;
351   if ( theModule && theModule->getApp() ) {
352     aStudy = dynamic_cast<SalomeApp_Study*>( theModule->getApp()->activeStudy() );
353   }
354   if ( !aStudy ) {
355     return anEntryList;
356   }
357
358   // Get selection
359   HYDROGUI_DataModel* aModel = theModule->getDataModel();
360
361   SUIT_SelectionMgr* aSelectionMgr = theModule->getApp()->selectionMgr();
362   SUIT_DataOwnerPtrList anOwners;
363   aSelectionMgr->selected( anOwners );
364
365   // Check if the selected objects belong to GEOM and have a shape
366   foreach( SUIT_DataOwner* aSUITOwner, anOwners )
367   {
368     if( LightApp_DataOwner* anOwner = dynamic_cast<LightApp_DataOwner*>( aSUITOwner ) )
369     {
370       QString anEntry = anOwner->entry();
371       _PTR(SObject) aSObject( aStudy->studyDS()->FindObjectID(qPrintable(anEntry)) );
372       if (aSObject) {
373          _PTR(SComponent) aSComponent = aSObject->GetFatherComponent();
374         if ( aSComponent && aSComponent->ComponentDataType() == "GEOM" ) {
375           GEOM::GEOM_Object_var aGeomObj = 
376             GEOMBase::GetObjectFromIOR( aSObject->GetIOR().c_str() );
377
378           if ( !aGeomObj->_is_nil() && aGeomObj->IsShape() ) {
379             anEntryList << anEntry;
380           }
381         }
382       }
383     }
384   }
385
386   return anEntryList;
387 }
388
389 Handle(HYDROData_Entity) HYDROGUI_Tool::FindObjectByName( HYDROGUI_Module* theModule,
390                                                           const QString&   theName,
391                                                           const ObjectKind theObjectKind )
392 {
393   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
394   return HYDROData_Tool::FindObjectByName( aDocument, theName, theObjectKind );
395 }
396
397 HYDROData_SequenceOfObjects HYDROGUI_Tool::FindObjectsByNames( HYDROGUI_Module*   theModule,
398                                                                const QStringList& theNames,
399                                                                const ObjectKind   theObjectKind )
400 {
401   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
402   return HYDROData_Tool::FindObjectsByNames( aDocument, theNames, theObjectKind );
403 }
404
405 QString HYDROGUI_Tool::GenerateObjectName( HYDROGUI_Module*   theModule,
406                                            const QString&     thePrefix,
407                                            const QStringList& theUsedNames )
408 {
409   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
410   return HYDROData_Tool::GenerateObjectName( aDocument, thePrefix, theUsedNames );
411 }
412
413 size_t HYDROGUI_Tool::GetActiveViewId( HYDROGUI_Module* theModule,
414                                        const QString&   theViewId )
415 {
416   size_t aViewId = 0;
417   SUIT_ViewManager* aViewMgr = theModule->getApp()->activeViewManager();
418   if( !aViewMgr || ( !theViewId.isEmpty() && aViewMgr->getType() != theViewId ) )
419     return aViewId;
420
421   if( SUIT_ViewModel* aViewer = aViewMgr->getViewModel() )
422     aViewId = (size_t)aViewer;
423   return aViewId;
424 }
425
426 size_t HYDROGUI_Tool::GetActiveGraphicsViewId( HYDROGUI_Module* theModule )
427 {
428   return GetActiveViewId( theModule, GraphicsView_Viewer::Type() );
429 }
430
431 size_t HYDROGUI_Tool::GetActiveOCCViewId( HYDROGUI_Module* theModule )
432 {
433   return GetActiveViewId( theModule, OCCViewer_Viewer::Type() );
434 }
435
436 QList<size_t> getViewIdList( HYDROGUI_Module* theModule,
437                              const QString&   theViewId )
438 {
439   QList<size_t> aList;
440   ViewManagerList aViewMgrs;
441   theModule->getApp()->viewManagers( theViewId, aViewMgrs );
442   QListIterator<SUIT_ViewManager*> anIter( aViewMgrs );
443   while( anIter.hasNext() )
444   {
445     if( SUIT_ViewManager* aViewMgr = anIter.next() )
446     {
447       if( SUIT_ViewModel* aViewer = aViewMgr->getViewModel() )
448         aList.append( (size_t)aViewer );
449     }
450   }
451   return aList;
452 }
453
454 QList<size_t> HYDROGUI_Tool::GetGraphicsViewIdList( HYDROGUI_Module* theModule )
455 {
456   return getViewIdList( theModule, GraphicsView_Viewer::Type() );
457 }
458
459 QList<size_t> HYDROGUI_Tool::GetOCCViewIdList( HYDROGUI_Module* theModule )
460 {
461   return getViewIdList( theModule, OCCViewer_Viewer::Type() );
462 }
463
464 void HYDROGUI_Tool::GetObjectReferences( const Handle(HYDROData_Image)& theImage,
465                                          HYDROData_SequenceOfObjects& theRefObjects,
466                                          QStringList& theRefNames )
467 {
468   if( theImage.IsNull() )
469     return;
470
471   for( int anIndex = 0, aNbRef = theImage->NbReferences(); anIndex < aNbRef; anIndex++ )
472   {
473     Handle(HYDROData_Entity) aRefObj = theImage->Reference( anIndex );
474     if( !aRefObj.IsNull() && !aRefObj->IsRemoved() )
475     {
476       QString aName = aRefObj->GetName();
477       if( !theRefNames.contains( aName ) )
478       {
479         theRefObjects.Append( aRefObj );
480         theRefNames.append( aRefObj->GetName() );
481         if( aRefObj->GetKind() == KIND_IMAGE )
482         {
483           Handle(HYDROData_Image) aRefImage = Handle(HYDROData_Image)::DownCast( aRefObj );
484           if( !aRefImage.IsNull() )
485             GetObjectReferences( aRefImage, theRefObjects, theRefNames );
486         }
487       }
488     }
489   }
490 }
491
492 void HYDROGUI_Tool::GetObjectBackReferences( HYDROGUI_Module* theModule,
493                                              const Handle(HYDROData_Entity)& theObj,
494                                              HYDROData_SequenceOfObjects& theBackRefObjects,
495                                              QStringList& theBackRefNames )
496 {
497   if( theObj.IsNull() )
498     return;
499
500   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
501   if( aDocument.IsNull() )
502     return;
503
504   QString aName = theObj->GetName();
505
506   HYDROData_Iterator anIterator( aDocument, KIND_IMAGE );
507   for( ; anIterator.More(); anIterator.Next() )
508   {
509     Handle(HYDROData_Image) anImage = Handle(HYDROData_Image)::DownCast( anIterator.Current() );
510     if( !anImage.IsNull() )
511     {
512       HYDROData_SequenceOfObjects aRefObjects;
513       QStringList aRefNames;
514       GetObjectReferences( anImage, aRefObjects, aRefNames );
515       if( aRefNames.contains( aName ) )
516       {
517         theBackRefObjects.Append( anImage );
518         theBackRefNames.append( anImage->GetName() );
519       }
520     }
521   }
522 }
523
524
525 QDockWidget* HYDROGUI_Tool::WindowDock( QWidget* wid )
526 {
527   if ( !wid )
528     return 0;
529
530   QDockWidget* dock = 0;
531   QWidget* w = wid->parentWidget();
532   while ( w && !dock )
533   {
534     dock = ::qobject_cast<QDockWidget*>( w );
535     w = w->parentWidget();
536   }
537   return dock;
538 }
539
540 QColor HYDROGUI_Tool::GenerateFillingColor( HYDROGUI_Module*   theModule,
541                                             const QStringList& theZoneNames )
542 {
543   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
544   return GenerateFillingColor( aDocument, theZoneNames );
545 }
546
547 QColor HYDROGUI_Tool::GenerateFillingColor( const Handle(HYDROData_Document)& theDoc,
548                                             const QStringList&                theZoneNames )
549 {
550   QColor aFillingColor( HYDROData_ImmersibleZone::DefaultFillingColor() );
551
552   int aCounter = 0;
553   int aR = 0, aG = 0, aB = 0;
554   QStringListIterator aZoneNameIter( theZoneNames );
555   while( aZoneNameIter.hasNext() )
556   {
557     const QString& aZoneName = aZoneNameIter.next();
558     Handle(HYDROData_ImmersibleZone) aRefZone = 
559       Handle(HYDROData_ImmersibleZone)::DownCast(
560         HYDROData_Tool::FindObjectByName( theDoc, aZoneName, KIND_IMMERSIBLE_ZONE ) );
561     if( !aRefZone.IsNull() )
562     {
563       QColor aRefColor = aRefZone->GetFillingColor();
564       aR += aRefColor.red();
565       aG += aRefColor.green();
566       aB += aRefColor.blue();
567       aCounter++;
568     }
569   }
570   
571   if( aCounter > 0 )
572   {
573     aFillingColor = QColor( aR / aCounter, aG / aCounter, aB / aCounter );
574   }
575
576   return aFillingColor;
577 }
578
579 QStringList HYDROGUI_Tool::FindExistingObjectsNames( const Handle(HYDROData_Document)& theDoc, 
580                                                      const ObjectKind theObjectKind )
581 {
582   QStringList aNames;
583
584   HYDROData_Iterator anIter( theDoc, theObjectKind );
585   for ( ; anIter.More(); anIter.Next() ) {
586     Handle(HYDROData_Entity) anObject = anIter.Current();
587     if( !anObject.IsNull() ) {
588       aNames.append( anObject->GetName() );
589     }
590   }
591
592   return aNames;
593 }