Salome HOME
Find object by name were moved to the methods of document.
[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_Entity) aResObj;
442   
443   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
444   if ( !aDocument.IsNull() )
445     aResObj = aDocument->FindObjectByName( theName, theObjectKind );
446   
447   return aResObj;
448 }
449
450 HYDROData_SequenceOfObjects HYDROGUI_Tool::FindObjectsByNames( HYDROGUI_Module*   theModule,
451                                                                const QStringList& theNames,
452                                                                const ObjectKind   theObjectKind )
453 {
454   HYDROData_SequenceOfObjects aResSeq;
455
456   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
457   if ( !aDocument.IsNull() )
458     aResSeq = aDocument->FindObjectsByNames( theNames, theObjectKind );
459
460   return aResSeq;
461 }
462
463 QString HYDROGUI_Tool::GenerateObjectName( HYDROGUI_Module*   theModule,
464                                            const QString&     thePrefix,
465                                            const QStringList& theUsedNames,
466                                            const bool         theIsTryToUsePurePrefix)
467 {
468   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
469   return HYDROData_Tool::GenerateObjectName( aDocument, thePrefix, theUsedNames, theIsTryToUsePurePrefix );
470 }
471
472 size_t HYDROGUI_Tool::GetActiveViewId( HYDROGUI_Module* theModule,
473                                        const QString&   theViewId )
474 {
475   size_t aViewId = 0;
476   SUIT_ViewManager* aViewMgr = theModule->getApp()->activeViewManager();
477   if( !aViewMgr || ( !theViewId.isEmpty() && aViewMgr->getType() != theViewId ) )
478     return aViewId;
479
480   if( SUIT_ViewModel* aViewer = aViewMgr->getViewModel() )
481     aViewId = (size_t)aViewer;
482   return aViewId;
483 }
484
485 size_t HYDROGUI_Tool::GetActiveGraphicsViewId( HYDROGUI_Module* theModule )
486 {
487   return GetActiveViewId( theModule, GraphicsView_Viewer::Type() );
488 }
489
490 size_t HYDROGUI_Tool::GetActiveOCCViewId( HYDROGUI_Module* theModule )
491 {
492   return GetActiveViewId( theModule, OCCViewer_Viewer::Type() );
493 }
494
495 QList<size_t> getViewIdList( HYDROGUI_Module* theModule,
496                              const QString&   theViewId )
497 {
498   QList<size_t> aList;
499   ViewManagerList aViewMgrs;
500   theModule->getApp()->viewManagers( theViewId, aViewMgrs );
501   QListIterator<SUIT_ViewManager*> anIter( aViewMgrs );
502   while( anIter.hasNext() )
503   {
504     if( SUIT_ViewManager* aViewMgr = anIter.next() )
505     {
506       if( SUIT_ViewModel* aViewer = aViewMgr->getViewModel() )
507         aList.append( (size_t)aViewer );
508     }
509   }
510   return aList;
511 }
512
513 QList<size_t> HYDROGUI_Tool::GetGraphicsViewIdList( HYDROGUI_Module* theModule )
514 {
515   return getViewIdList( theModule, GraphicsView_Viewer::Type() );
516 }
517
518 QList<size_t> HYDROGUI_Tool::GetOCCViewIdList( HYDROGUI_Module* theModule )
519 {
520   return getViewIdList( theModule, OCCViewer_Viewer::Type() );
521 }
522
523 void HYDROGUI_Tool::setOCCActionShown( OCCViewer_ViewFrame* theViewFrame,
524                                        const int theActionId,
525                                        const bool isShown )
526 {
527   if ( !theViewFrame )
528     return;
529
530   OCCViewer_ViewWindow* aView = theViewFrame->getView( OCCViewer_ViewFrame::MAIN_VIEW );
531   if ( aView ) {
532     aView->toolMgr()->setShown( theActionId, isShown );
533     if ( theActionId == OCCViewer_ViewWindow::MaximizedId )
534       theViewFrame->onMaximizedView( aView, true );
535   }
536 }
537
538 void HYDROGUI_Tool::setOCCActionShown( HYDROGUI_Module* theModule,
539                                        const int theActionId,
540                                        const bool isShown )
541 {
542   QList<size_t> aList;
543   ViewManagerList aViewMgrs;
544   theModule->getApp()->viewManagers( OCCViewer_Viewer::Type(), aViewMgrs );
545   QListIterator<SUIT_ViewManager*> anIter( aViewMgrs );
546   while( anIter.hasNext() )
547   {
548     if( SUIT_ViewManager* aViewMgr = anIter.next() )
549     {
550       OCCViewer_ViewFrame* aViewFrame = dynamic_cast<OCCViewer_ViewFrame*>
551                                                            ( aViewMgr->getActiveView() );
552       if ( aViewFrame )
553         setOCCActionShown( aViewFrame, theActionId, isShown );
554     }
555   }
556 }
557
558 void HYDROGUI_Tool::GetObjectReferences( const Handle(HYDROData_Entity)& theObj,
559                                          HYDROData_SequenceOfObjects& theRefObjects,
560                                          QStringList& theRefNames )
561 {
562   if( theObj.IsNull() )
563     return;
564
565   HYDROData_SequenceOfObjects anAllRefObjects = theObj->GetAllReferenceObjects();
566   theRefObjects.Append( anAllRefObjects );
567
568   for( int i = 1, n = anAllRefObjects.Length(); i <= n; ++i )
569   {
570     Handle(HYDROData_Entity) aRefObj = theRefObjects.Value( i );
571     if( aRefObj.IsNull() || aRefObj->IsRemoved() )
572       continue;
573
574     QString aRefObjectName = aRefObj->GetName();
575     if( theRefNames.contains( aRefObjectName ) )
576       continue;
577
578     theRefObjects.Append( aRefObj );
579     theRefNames.append( aRefObjectName );
580
581     GetObjectReferences( aRefObj, theRefObjects, theRefNames );
582   }
583 }
584
585 HYDROData_SequenceOfObjects HYDROGUI_Tool::GetObjectBackReferences( 
586   HYDROGUI_Module*                theModule,
587   const Handle(HYDROData_Entity)& theObj )
588 {
589   if( theObj.IsNull() )
590     return HYDROData_SequenceOfObjects();
591
592   QString anObjName = theObj->GetName();
593
594   QMap<QString,HYDROData_SequenceOfObjects> aMapOfBackRefs =
595     GetObjectsBackReferences( theModule, QStringList() << anObjName );
596
597   return aMapOfBackRefs[ anObjName ];
598 }
599
600 QMap<QString,HYDROData_SequenceOfObjects> HYDROGUI_Tool::GetObjectsBackReferences(
601   HYDROGUI_Module*   theModule, const QStringList& theObjectNames )
602 {
603   QMap<QString,HYDROData_SequenceOfObjects> aResMap;
604
605   if( theObjectNames.isEmpty() )
606     return aResMap;
607
608   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
609   if( aDocument.IsNull() )
610     return aResMap;
611
612   HYDROData_Iterator anIterator( aDocument );
613   for( ; anIterator.More(); anIterator.Next() )
614   {
615     Handle(HYDROData_Entity) anObject = anIterator.Current();
616     if( anObject.IsNull() || anObject->IsRemoved() )
617       continue;
618
619     QString anObjectName = anObject->GetName();
620     if ( theObjectNames.contains( anObjectName ) )
621       continue;
622
623     HYDROData_SequenceOfObjects aRefObjects = anObject->GetAllReferenceObjects();
624     for ( int i = 1, n = aRefObjects.Length(); i <= n; ++i )
625     {
626       Handle(HYDROData_Entity) aRefObject = aRefObjects.Value( i );
627       if( aRefObject.IsNull() || aRefObject->IsRemoved() )
628         continue;
629
630       QString aRefObjectName = aRefObject->GetName();
631       if ( !theObjectNames.contains( aRefObjectName ) )
632         continue;
633
634       aResMap[ aRefObjectName ].Append( anObject );
635     }
636   }
637
638   return aResMap;
639 }
640
641 QDockWidget* HYDROGUI_Tool::WindowDock( QWidget* wid )
642 {
643   if ( !wid )
644     return 0;
645
646   QDockWidget* dock = 0;
647   QWidget* w = wid->parentWidget();
648   while ( w && !dock )
649   {
650     dock = ::qobject_cast<QDockWidget*>( w );
651     w = w->parentWidget();
652   }
653   return dock;
654 }
655
656 QColor HYDROGUI_Tool::GenerateFillingColor( HYDROGUI_Module*   theModule,
657                                             const QStringList& theZoneNames )
658 {
659   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
660   return GenerateFillingColor( aDocument, theZoneNames );
661 }
662
663 QColor HYDROGUI_Tool::GenerateFillingColor( const Handle(HYDROData_Document)& theDoc,
664                                             const QStringList&                theZoneNames )
665 {
666   QColor aFillingColor( HYDROData_ImmersibleZone::DefaultFillingColor() );
667
668   int aCounter = 0;
669   int aR = 0, aG = 0, aB = 0;
670   QStringListIterator aZoneNameIter( theZoneNames );
671   while( aZoneNameIter.hasNext() )
672   {
673     const QString& aZoneName = aZoneNameIter.next();
674     Handle(HYDROData_ImmersibleZone) aRefZone = 
675       Handle(HYDROData_ImmersibleZone)::DownCast( theDoc->FindObjectByName( aZoneName, KIND_IMMERSIBLE_ZONE ) );
676     if( !aRefZone.IsNull() )
677     {
678       QColor aRefColor = aRefZone->GetFillingColor();
679       aR += aRefColor.red();
680       aG += aRefColor.green();
681       aB += aRefColor.blue();
682       aCounter++;
683     }
684   }
685   
686   if( aCounter > 0 )
687   {
688     aFillingColor = QColor( aR / aCounter, aG / aCounter, aB / aCounter );
689   }
690
691   return aFillingColor;
692 }
693
694 QStringList HYDROGUI_Tool::FindExistingObjectsNames( const Handle(HYDROData_Document)& theDoc, 
695                                                      const ObjectKind theObjectKind )
696 {
697   QStringList aNames;
698
699   HYDROData_Iterator anIter( theDoc, theObjectKind );
700   for ( ; anIter.More(); anIter.Next() ) {
701     Handle(HYDROData_Entity) anObject = anIter.Current();
702     if( !anObject.IsNull() ) {
703       aNames.append( anObject->GetName() );
704     }
705   }
706
707   return aNames;
708 }
709
710 QString HYDROGUI_Tool::GetCoordinateString( const double theNumber )
711 {
712   return QString::number( theNumber, 'f', 2 );
713 }