Salome HOME
Feature #86: The hierarchy in the Object Browser (T 19).
[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_BATHYMETRY ) ||
221         ( anObject->GetKind() == KIND_ZONE ) ||
222         ( anObject->GetKind() == KIND_OBSTACLE ) ) )
223       {
224         theSeq.Append( anObject );
225       }
226     }
227   }
228 }
229
230 HYDROGUI_Prs* HYDROGUI_Tool::GetPresentation( const Handle(HYDROData_Entity)& theObj,
231                                               const GraphicsView_ObjectList& theObjects )
232 {
233   if( !theObj.IsNull() )
234   {
235     GraphicsView_ObjectListIterator anIter( theObjects );
236     while( anIter.hasNext() )
237     {
238       if( HYDROGUI_Prs* aPrs = dynamic_cast<HYDROGUI_Prs*>( anIter.next() ) )
239       {
240         Handle(HYDROData_Entity) anObj = aPrs->getObject();
241         if( IsEqual( anObj, theObj ) )
242           return aPrs;
243       }
244     }
245   }
246   return NULL;
247 }
248
249 GraphicsView_ObjectList HYDROGUI_Tool::GetPrsList( GraphicsView_ViewPort* theViewPort )
250 {
251   GraphicsView_ObjectList aList;
252   if( theViewPort )
253   {
254     GraphicsView_ObjectListIterator anIter( theViewPort->getObjects() );
255     while( anIter.hasNext() )
256       if( HYDROGUI_Prs* aPrs = dynamic_cast<HYDROGUI_Prs*>( anIter.next() ) )
257         aList.append( aPrs );
258   }
259   return aList;
260 }
261
262 HYDROData_SequenceOfObjects HYDROGUI_Tool::GetSelectedObjects( HYDROGUI_Module* theModule )
263 {
264   HYDROData_SequenceOfObjects aSeq;
265
266   HYDROGUI_DataModel* aModel = theModule->getDataModel();
267
268   SUIT_SelectionMgr* aSelectionMgr = theModule->getApp()->selectionMgr();
269   SUIT_DataOwnerPtrList anOwners;
270   aSelectionMgr->selected( anOwners );
271
272   QStringList aCollectedNameList; // to avoid duplication
273   foreach( SUIT_DataOwner* aSUITOwner, anOwners )
274   {
275     if( LightApp_DataOwner* anOwner = dynamic_cast<LightApp_DataOwner*>( aSUITOwner ) )
276     {
277       Handle(HYDROData_Entity) anObject = aModel->objectByEntry( anOwner->entry() );
278       if( !anObject.IsNull() )
279       {
280         QString aName = anObject->GetName();
281         if( !aCollectedNameList.contains( aName ) )
282         {
283           aSeq.Append( anObject );
284           aCollectedNameList.append( aName );
285         }
286       }
287     }
288   }
289   return aSeq;
290 }
291
292 Handle(HYDROData_Entity) HYDROGUI_Tool::GetSelectedObject( HYDROGUI_Module* theModule )
293 {
294   HYDROData_SequenceOfObjects aSeq = GetSelectedObjects( theModule );
295   if( !aSeq.IsEmpty() )
296     return aSeq.First();
297   return NULL;
298 }
299
300 HYDROData_SequenceOfObjects HYDROGUI_Tool::GetGeometryObjects( 
301   HYDROGUI_Module* theModule )
302 {
303   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
304
305   HYDROData_SequenceOfObjects aResSeq;
306
307   HYDROData_Iterator anIter( aDocument );
308   for ( ; anIter.More(); anIter.Next() )
309   {
310     Handle(HYDROData_Entity) anObj = anIter.Current();
311     if ( !HYDROData_Tool::IsGeometryObject( anObj ) )
312       continue;
313
314     aResSeq.Append( anObj );
315   }
316   
317   return aResSeq;
318 }
319
320 ObjectKind HYDROGUI_Tool::GetSelectedPartition( HYDROGUI_Module* theModule )
321 {
322   HYDROGUI_DataModel* aModel = theModule->getDataModel();
323
324   SUIT_SelectionMgr* aSelectionMgr = theModule->getApp()->selectionMgr();
325   SUIT_DataOwnerPtrList anOwners;
326   aSelectionMgr->selected( anOwners );
327
328   if( anOwners.size() != 1 )
329     return KIND_UNKNOWN;
330
331   if( LightApp_DataOwner* anOwner = dynamic_cast<LightApp_DataOwner*>( anOwners.first().operator->() ) )
332   {
333     QString anEntry = anOwner->entry();
334     QString aPrefix = HYDROGUI_DataObject::entryPrefix();
335     if( anEntry.left( aPrefix.length() ) == aPrefix )
336     {
337       anEntry.remove( aPrefix );
338       for( ObjectKind anObjectKind = KIND_UNKNOWN + 1; anObjectKind <= KIND_LAST; anObjectKind++ )
339         if( HYDROGUI_DataModel::tr( HYDROGUI_DataModel::partitionName( anObjectKind ).toAscii() ) == anEntry )
340           return anObjectKind;
341     }
342   }
343   return KIND_UNKNOWN;
344 }
345
346 QStringList HYDROGUI_Tool::GetSelectedGeomObjects( HYDROGUI_Module* theModule )
347 {
348   QStringList anEntryList;
349
350   // Get active SalomeApp_Study
351   SalomeApp_Study* aStudy = NULL;
352   if ( theModule && theModule->getApp() ) {
353     aStudy = dynamic_cast<SalomeApp_Study*>( theModule->getApp()->activeStudy() );
354   }
355   if ( !aStudy ) {
356     return anEntryList;
357   }
358
359   // Get selection
360   SUIT_SelectionMgr* aSelectionMgr = theModule->getApp()->selectionMgr();
361   SUIT_DataOwnerPtrList anOwners;
362   aSelectionMgr->selected( anOwners );
363
364   // Check if the selected objects belong to GEOM and have a shape
365   foreach( SUIT_DataOwner* aSUITOwner, anOwners )
366   {
367     if( LightApp_DataOwner* anOwner = dynamic_cast<LightApp_DataOwner*>( aSUITOwner ) )
368     {
369       QString anEntry = anOwner->entry();
370       _PTR(SObject) aSObject( aStudy->studyDS()->FindObjectID(qPrintable(anEntry)) );
371       if (aSObject) {
372          _PTR(SComponent) aSComponent = aSObject->GetFatherComponent();
373         if ( aSComponent && aSComponent->ComponentDataType() == "GEOM" ) {
374           GEOM::GEOM_Object_var aGeomObj = 
375             GEOMBase::GetObjectFromIOR( aSObject->GetIOR().c_str() );
376
377           if ( !aGeomObj->_is_nil() && aGeomObj->IsShape() ) {
378             anEntryList << anEntry;
379           }
380         }
381       }
382     }
383   }
384
385   return anEntryList;
386 }
387
388 Handle(HYDROData_Entity) HYDROGUI_Tool::FindObjectByName( HYDROGUI_Module* theModule,
389                                                           const QString&   theName,
390                                                           const ObjectKind theObjectKind )
391 {
392   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
393   return HYDROData_Tool::FindObjectByName( aDocument, theName, theObjectKind );
394 }
395
396 HYDROData_SequenceOfObjects HYDROGUI_Tool::FindObjectsByNames( HYDROGUI_Module*   theModule,
397                                                                const QStringList& theNames,
398                                                                const ObjectKind   theObjectKind )
399 {
400   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
401   return HYDROData_Tool::FindObjectsByNames( aDocument, theNames, theObjectKind );
402 }
403
404 QString HYDROGUI_Tool::GenerateObjectName( HYDROGUI_Module*   theModule,
405                                            const QString&     thePrefix,
406                                            const QStringList& theUsedNames,
407                                            const bool         theIsTryToUsePurePrefix)
408 {
409   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
410   return HYDROData_Tool::GenerateObjectName( aDocument, thePrefix, theUsedNames, theIsTryToUsePurePrefix );
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 }