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