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