Salome HOME
5e026dd4b61866821e7c5633b616bdf4789092c4
[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   SUIT_SelectionMgr* aSelectionMgr = theModule->getApp()->selectionMgr();
360   SUIT_DataOwnerPtrList anOwners;
361   aSelectionMgr->selected( anOwners );
362
363   // Check if the selected objects belong to GEOM and have a shape
364   foreach( SUIT_DataOwner* aSUITOwner, anOwners )
365   {
366     if( LightApp_DataOwner* anOwner = dynamic_cast<LightApp_DataOwner*>( aSUITOwner ) )
367     {
368       QString anEntry = anOwner->entry();
369       _PTR(SObject) aSObject( aStudy->studyDS()->FindObjectID(qPrintable(anEntry)) );
370       if (aSObject) {
371          _PTR(SComponent) aSComponent = aSObject->GetFatherComponent();
372         if ( aSComponent && aSComponent->ComponentDataType() == "GEOM" ) {
373           GEOM::GEOM_Object_var aGeomObj = 
374             GEOMBase::GetObjectFromIOR( aSObject->GetIOR().c_str() );
375
376           if ( !aGeomObj->_is_nil() && aGeomObj->IsShape() ) {
377             anEntryList << anEntry;
378           }
379         }
380       }
381     }
382   }
383
384   return anEntryList;
385 }
386
387 Handle(HYDROData_Entity) HYDROGUI_Tool::FindObjectByName( HYDROGUI_Module* theModule,
388                                                           const QString&   theName,
389                                                           const ObjectKind theObjectKind )
390 {
391   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
392   return HYDROData_Tool::FindObjectByName( aDocument, theName, theObjectKind );
393 }
394
395 HYDROData_SequenceOfObjects HYDROGUI_Tool::FindObjectsByNames( HYDROGUI_Module*   theModule,
396                                                                const QStringList& theNames,
397                                                                const ObjectKind   theObjectKind )
398 {
399   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
400   return HYDROData_Tool::FindObjectsByNames( aDocument, theNames, theObjectKind );
401 }
402
403 QString HYDROGUI_Tool::GenerateObjectName( HYDROGUI_Module*   theModule,
404                                            const QString&     thePrefix,
405                                            const QStringList& theUsedNames,
406                                            const bool         theIsTryToUsePurePrefix)
407 {
408   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
409   return HYDROData_Tool::GenerateObjectName( aDocument, thePrefix, theUsedNames, theIsTryToUsePurePrefix );
410 }
411
412 size_t HYDROGUI_Tool::GetActiveViewId( HYDROGUI_Module* theModule,
413                                        const QString&   theViewId )
414 {
415   size_t aViewId = 0;
416   SUIT_ViewManager* aViewMgr = theModule->getApp()->activeViewManager();
417   if( !aViewMgr || ( !theViewId.isEmpty() && aViewMgr->getType() != theViewId ) )
418     return aViewId;
419
420   if( SUIT_ViewModel* aViewer = aViewMgr->getViewModel() )
421     aViewId = (size_t)aViewer;
422   return aViewId;
423 }
424
425 size_t HYDROGUI_Tool::GetActiveGraphicsViewId( HYDROGUI_Module* theModule )
426 {
427   return GetActiveViewId( theModule, GraphicsView_Viewer::Type() );
428 }
429
430 size_t HYDROGUI_Tool::GetActiveOCCViewId( HYDROGUI_Module* theModule )
431 {
432   return GetActiveViewId( theModule, OCCViewer_Viewer::Type() );
433 }
434
435 QList<size_t> getViewIdList( HYDROGUI_Module* theModule,
436                              const QString&   theViewId )
437 {
438   QList<size_t> aList;
439   ViewManagerList aViewMgrs;
440   theModule->getApp()->viewManagers( theViewId, aViewMgrs );
441   QListIterator<SUIT_ViewManager*> anIter( aViewMgrs );
442   while( anIter.hasNext() )
443   {
444     if( SUIT_ViewManager* aViewMgr = anIter.next() )
445     {
446       if( SUIT_ViewModel* aViewer = aViewMgr->getViewModel() )
447         aList.append( (size_t)aViewer );
448     }
449   }
450   return aList;
451 }
452
453 QList<size_t> HYDROGUI_Tool::GetGraphicsViewIdList( HYDROGUI_Module* theModule )
454 {
455   return getViewIdList( theModule, GraphicsView_Viewer::Type() );
456 }
457
458 QList<size_t> HYDROGUI_Tool::GetOCCViewIdList( HYDROGUI_Module* theModule )
459 {
460   return getViewIdList( theModule, OCCViewer_Viewer::Type() );
461 }
462
463 void HYDROGUI_Tool::GetObjectReferences( const Handle(HYDROData_Image)& theImage,
464                                          HYDROData_SequenceOfObjects& theRefObjects,
465                                          QStringList& theRefNames )
466 {
467   if( theImage.IsNull() )
468     return;
469
470   for( int anIndex = 0, aNbRef = theImage->NbReferences(); anIndex < aNbRef; anIndex++ )
471   {
472     Handle(HYDROData_Entity) aRefObj = theImage->Reference( anIndex );
473     if( !aRefObj.IsNull() && !aRefObj->IsRemoved() )
474     {
475       QString aName = aRefObj->GetName();
476       if( !theRefNames.contains( aName ) )
477       {
478         theRefObjects.Append( aRefObj );
479         theRefNames.append( aRefObj->GetName() );
480         if( aRefObj->GetKind() == KIND_IMAGE )
481         {
482           Handle(HYDROData_Image) aRefImage = Handle(HYDROData_Image)::DownCast( aRefObj );
483           if( !aRefImage.IsNull() )
484             GetObjectReferences( aRefImage, theRefObjects, theRefNames );
485         }
486       }
487     }
488   }
489 }
490
491 void HYDROGUI_Tool::GetObjectBackReferences( HYDROGUI_Module* theModule,
492                                              const Handle(HYDROData_Entity)& theObj,
493                                              HYDROData_SequenceOfObjects& theBackRefObjects,
494                                              QStringList& theBackRefNames )
495 {
496   if( theObj.IsNull() )
497     return;
498
499   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
500   if( aDocument.IsNull() )
501     return;
502
503   QString aName = theObj->GetName();
504
505   HYDROData_Iterator anIterator( aDocument, KIND_IMAGE );
506   for( ; anIterator.More(); anIterator.Next() )
507   {
508     Handle(HYDROData_Image) anImage = Handle(HYDROData_Image)::DownCast( anIterator.Current() );
509     if( !anImage.IsNull() )
510     {
511       HYDROData_SequenceOfObjects aRefObjects;
512       QStringList aRefNames;
513       GetObjectReferences( anImage, aRefObjects, aRefNames );
514       if( aRefNames.contains( aName ) )
515       {
516         theBackRefObjects.Append( anImage );
517         theBackRefNames.append( anImage->GetName() );
518       }
519     }
520   }
521 }
522
523
524 QDockWidget* HYDROGUI_Tool::WindowDock( QWidget* wid )
525 {
526   if ( !wid )
527     return 0;
528
529   QDockWidget* dock = 0;
530   QWidget* w = wid->parentWidget();
531   while ( w && !dock )
532   {
533     dock = ::qobject_cast<QDockWidget*>( w );
534     w = w->parentWidget();
535   }
536   return dock;
537 }
538
539 QColor HYDROGUI_Tool::GenerateFillingColor( HYDROGUI_Module*   theModule,
540                                             const QStringList& theZoneNames )
541 {
542   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
543   return GenerateFillingColor( aDocument, theZoneNames );
544 }
545
546 QColor HYDROGUI_Tool::GenerateFillingColor( const Handle(HYDROData_Document)& theDoc,
547                                             const QStringList&                theZoneNames )
548 {
549   QColor aFillingColor( HYDROData_ImmersibleZone::DefaultFillingColor() );
550
551   int aCounter = 0;
552   int aR = 0, aG = 0, aB = 0;
553   QStringListIterator aZoneNameIter( theZoneNames );
554   while( aZoneNameIter.hasNext() )
555   {
556     const QString& aZoneName = aZoneNameIter.next();
557     Handle(HYDROData_ImmersibleZone) aRefZone = 
558       Handle(HYDROData_ImmersibleZone)::DownCast(
559         HYDROData_Tool::FindObjectByName( theDoc, aZoneName, KIND_IMMERSIBLE_ZONE ) );
560     if( !aRefZone.IsNull() )
561     {
562       QColor aRefColor = aRefZone->GetFillingColor();
563       aR += aRefColor.red();
564       aG += aRefColor.green();
565       aB += aRefColor.blue();
566       aCounter++;
567     }
568   }
569   
570   if( aCounter > 0 )
571   {
572     aFillingColor = QColor( aR / aCounter, aG / aCounter, aB / aCounter );
573   }
574
575   return aFillingColor;
576 }
577
578 QStringList HYDROGUI_Tool::FindExistingObjectsNames( const Handle(HYDROData_Document)& theDoc, 
579                                                      const ObjectKind theObjectKind )
580 {
581   QStringList aNames;
582
583   HYDROData_Iterator anIter( theDoc, theObjectKind );
584   for ( ; anIter.More(); anIter.Next() ) {
585     Handle(HYDROData_Entity) anObject = anIter.Current();
586     if( !anObject.IsNull() ) {
587       aNames.append( anObject->GetName() );
588     }
589   }
590
591   return aNames;
592 }