Salome HOME
Fictive 3D object for objects with 2 types of presentation (Bug #216).
[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 {
388   QStringList anEntryList;
389
390   // Get active SalomeApp_Study
391   SalomeApp_Study* aStudy = NULL;
392   if ( theModule && theModule->getApp() ) {
393     aStudy = dynamic_cast<SalomeApp_Study*>( theModule->getApp()->activeStudy() );
394   }
395   if ( !aStudy ) {
396     return anEntryList;
397   }
398
399   // Get selection
400   SUIT_SelectionMgr* aSelectionMgr = theModule->getApp()->selectionMgr();
401   SUIT_DataOwnerPtrList anOwners;
402   aSelectionMgr->selected( anOwners );
403
404   // Check if the selected objects belong to GEOM and have a shape
405   foreach( SUIT_DataOwner* aSUITOwner, anOwners )
406   {
407     if( LightApp_DataOwner* anOwner = dynamic_cast<LightApp_DataOwner*>( aSUITOwner ) )
408     {
409       QString anEntry = anOwner->entry();
410       _PTR(SObject) aSObject( aStudy->studyDS()->FindObjectID(qPrintable(anEntry)) );
411       if (aSObject) {
412          _PTR(SComponent) aSComponent = aSObject->GetFatherComponent();
413         if ( aSComponent && aSComponent->ComponentDataType() == "GEOM" ) {
414           GEOM::GEOM_Object_var aGeomObj = 
415             GEOMBase::GetObjectFromIOR( aSObject->GetIOR().c_str() );
416
417           if ( !aGeomObj->_is_nil() && aGeomObj->IsShape() ) {
418             anEntryList << anEntry;
419           }
420         }
421       }
422     }
423   }
424
425   return anEntryList;
426 }
427
428 Handle(HYDROData_Entity) HYDROGUI_Tool::FindObjectByName( HYDROGUI_Module* theModule,
429                                                           const QString&   theName,
430                                                           const ObjectKind theObjectKind )
431 {
432   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
433   return HYDROData_Tool::FindObjectByName( aDocument, theName, theObjectKind );
434 }
435
436 HYDROData_SequenceOfObjects HYDROGUI_Tool::FindObjectsByNames( HYDROGUI_Module*   theModule,
437                                                                const QStringList& theNames,
438                                                                const ObjectKind   theObjectKind )
439 {
440   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
441   return HYDROData_Tool::FindObjectsByNames( aDocument, theNames, theObjectKind );
442 }
443
444 QString HYDROGUI_Tool::GenerateObjectName( HYDROGUI_Module*   theModule,
445                                            const QString&     thePrefix,
446                                            const QStringList& theUsedNames,
447                                            const bool         theIsTryToUsePurePrefix)
448 {
449   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
450   return HYDROData_Tool::GenerateObjectName( aDocument, thePrefix, theUsedNames, theIsTryToUsePurePrefix );
451 }
452
453 size_t HYDROGUI_Tool::GetActiveViewId( HYDROGUI_Module* theModule,
454                                        const QString&   theViewId )
455 {
456   size_t aViewId = 0;
457   SUIT_ViewManager* aViewMgr = theModule->getApp()->activeViewManager();
458   if( !aViewMgr || ( !theViewId.isEmpty() && aViewMgr->getType() != theViewId ) )
459     return aViewId;
460
461   if( SUIT_ViewModel* aViewer = aViewMgr->getViewModel() )
462     aViewId = (size_t)aViewer;
463   return aViewId;
464 }
465
466 size_t HYDROGUI_Tool::GetActiveGraphicsViewId( HYDROGUI_Module* theModule )
467 {
468   return GetActiveViewId( theModule, GraphicsView_Viewer::Type() );
469 }
470
471 size_t HYDROGUI_Tool::GetActiveOCCViewId( HYDROGUI_Module* theModule )
472 {
473   return GetActiveViewId( theModule, OCCViewer_Viewer::Type() );
474 }
475
476 QList<size_t> getViewIdList( HYDROGUI_Module* theModule,
477                              const QString&   theViewId )
478 {
479   QList<size_t> aList;
480   ViewManagerList aViewMgrs;
481   theModule->getApp()->viewManagers( theViewId, aViewMgrs );
482   QListIterator<SUIT_ViewManager*> anIter( aViewMgrs );
483   while( anIter.hasNext() )
484   {
485     if( SUIT_ViewManager* aViewMgr = anIter.next() )
486     {
487       if( SUIT_ViewModel* aViewer = aViewMgr->getViewModel() )
488         aList.append( (size_t)aViewer );
489     }
490   }
491   return aList;
492 }
493
494 QList<size_t> HYDROGUI_Tool::GetGraphicsViewIdList( HYDROGUI_Module* theModule )
495 {
496   return getViewIdList( theModule, GraphicsView_Viewer::Type() );
497 }
498
499 QList<size_t> HYDROGUI_Tool::GetOCCViewIdList( HYDROGUI_Module* theModule )
500 {
501   return getViewIdList( theModule, OCCViewer_Viewer::Type() );
502 }
503
504 void HYDROGUI_Tool::setOCCActionShown( OCCViewer_ViewFrame* theViewFrame,
505                                        const int theActionId,
506                                        const bool isShown )
507 {
508   if ( !theViewFrame )
509     return;
510
511   OCCViewer_ViewWindow* aView = theViewFrame->getView( OCCViewer_ViewFrame::MAIN_VIEW );
512   if ( aView ) {
513     aView->toolMgr()->setShown( theActionId, isShown );
514     if ( theActionId == OCCViewer_ViewWindow::MaximizedId )
515       theViewFrame->onMaximizedView( aView, true );
516   }
517 }
518
519 void HYDROGUI_Tool::setOCCActionShown( HYDROGUI_Module* theModule,
520                                        const int theActionId,
521                                        const bool isShown )
522 {
523   QList<size_t> aList;
524   ViewManagerList aViewMgrs;
525   theModule->getApp()->viewManagers( OCCViewer_Viewer::Type(), aViewMgrs );
526   QListIterator<SUIT_ViewManager*> anIter( aViewMgrs );
527   while( anIter.hasNext() )
528   {
529     if( SUIT_ViewManager* aViewMgr = anIter.next() )
530     {
531       OCCViewer_ViewFrame* aViewFrame = dynamic_cast<OCCViewer_ViewFrame*>
532                                                            ( aViewMgr->getActiveView() );
533       if ( aViewFrame )
534         setOCCActionShown( aViewFrame, theActionId, isShown );
535     }
536   }
537 }
538
539 void HYDROGUI_Tool::GetObjectReferences( const Handle(HYDROData_Entity)& theObj,
540                                          HYDROData_SequenceOfObjects& theRefObjects,
541                                          QStringList& theRefNames )
542 {
543   if( theObj.IsNull() )
544     return;
545
546   HYDROData_SequenceOfObjects anAllRefObjects = theObj->GetAllReferenceObjects();
547   theRefObjects.Append( anAllRefObjects );
548
549   for( int i = 1, n = anAllRefObjects.Length(); i <= n; ++i )
550   {
551     Handle(HYDROData_Entity) aRefObj = theRefObjects.Value( i );
552     if( aRefObj.IsNull() || aRefObj->IsRemoved() )
553       continue;
554
555     QString aRefObjectName = aRefObj->GetName();
556     if( theRefNames.contains( aRefObjectName ) )
557       continue;
558
559     theRefObjects.Append( aRefObj );
560     theRefNames.append( aRefObjectName );
561
562     GetObjectReferences( aRefObj, theRefObjects, theRefNames );
563   }
564 }
565
566 HYDROData_SequenceOfObjects HYDROGUI_Tool::GetObjectBackReferences( 
567   HYDROGUI_Module*                theModule,
568   const Handle(HYDROData_Entity)& theObj )
569 {
570   if( theObj.IsNull() )
571     return HYDROData_SequenceOfObjects();
572
573   QString anObjName = theObj->GetName();
574
575   QMap<QString,HYDROData_SequenceOfObjects> aMapOfBackRefs =
576     GetObjectsBackReferences( theModule, QStringList() << anObjName );
577
578   return aMapOfBackRefs[ anObjName ];
579 }
580
581 QMap<QString,HYDROData_SequenceOfObjects> HYDROGUI_Tool::GetObjectsBackReferences(
582   HYDROGUI_Module*   theModule, const QStringList& theObjectNames )
583 {
584   QMap<QString,HYDROData_SequenceOfObjects> aResMap;
585
586   if( theObjectNames.isEmpty() )
587     return aResMap;
588
589   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
590   if( aDocument.IsNull() )
591     return aResMap;
592
593   HYDROData_Iterator anIterator( aDocument );
594   for( ; anIterator.More(); anIterator.Next() )
595   {
596     Handle(HYDROData_Entity) anObject = anIterator.Current();
597     if( anObject.IsNull() || anObject->IsRemoved() )
598       continue;
599
600     QString anObjectName = anObject->GetName();
601     if ( theObjectNames.contains( anObjectName ) )
602       continue;
603
604     HYDROData_SequenceOfObjects aRefObjects = anObject->GetAllReferenceObjects();
605     for ( int i = 1, n = aRefObjects.Length(); i <= n; ++i )
606     {
607       Handle(HYDROData_Entity) aRefObject = aRefObjects.Value( i );
608       if( aRefObject.IsNull() || aRefObject->IsRemoved() )
609         continue;
610
611       QString aRefObjectName = aRefObject->GetName();
612       if ( !theObjectNames.contains( aRefObjectName ) )
613         continue;
614
615       aResMap[ aRefObjectName ].Append( anObject );
616     }
617   }
618
619   return aResMap;
620 }
621
622 QDockWidget* HYDROGUI_Tool::WindowDock( QWidget* wid )
623 {
624   if ( !wid )
625     return 0;
626
627   QDockWidget* dock = 0;
628   QWidget* w = wid->parentWidget();
629   while ( w && !dock )
630   {
631     dock = ::qobject_cast<QDockWidget*>( w );
632     w = w->parentWidget();
633   }
634   return dock;
635 }
636
637 QColor HYDROGUI_Tool::GenerateFillingColor( HYDROGUI_Module*   theModule,
638                                             const QStringList& theZoneNames )
639 {
640   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
641   return GenerateFillingColor( aDocument, theZoneNames );
642 }
643
644 QColor HYDROGUI_Tool::GenerateFillingColor( const Handle(HYDROData_Document)& theDoc,
645                                             const QStringList&                theZoneNames )
646 {
647   QColor aFillingColor( HYDROData_ImmersibleZone::DefaultFillingColor() );
648
649   int aCounter = 0;
650   int aR = 0, aG = 0, aB = 0;
651   QStringListIterator aZoneNameIter( theZoneNames );
652   while( aZoneNameIter.hasNext() )
653   {
654     const QString& aZoneName = aZoneNameIter.next();
655     Handle(HYDROData_ImmersibleZone) aRefZone = 
656       Handle(HYDROData_ImmersibleZone)::DownCast(
657         HYDROData_Tool::FindObjectByName( theDoc, aZoneName, KIND_IMMERSIBLE_ZONE ) );
658     if( !aRefZone.IsNull() )
659     {
660       QColor aRefColor = aRefZone->GetFillingColor();
661       aR += aRefColor.red();
662       aG += aRefColor.green();
663       aB += aRefColor.blue();
664       aCounter++;
665     }
666   }
667   
668   if( aCounter > 0 )
669   {
670     aFillingColor = QColor( aR / aCounter, aG / aCounter, aB / aCounter );
671   }
672
673   return aFillingColor;
674 }
675
676 QStringList HYDROGUI_Tool::FindExistingObjectsNames( const Handle(HYDROData_Document)& theDoc, 
677                                                      const ObjectKind theObjectKind )
678 {
679   QStringList aNames;
680
681   HYDROData_Iterator anIter( theDoc, theObjectKind );
682   for ( ; anIter.More(); anIter.Next() ) {
683     Handle(HYDROData_Entity) anObject = anIter.Current();
684     if( !anObject.IsNull() ) {
685       aNames.append( anObject->GetName() );
686     }
687   }
688
689   return aNames;
690 }
691
692 QString HYDROGUI_Tool::GetCoordinateString( const double theNumber )
693 {
694   return QString::number( theNumber, 'f', 2 );
695 }