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