]> SALOME platform Git repositories - modules/hydro.git/blob - src/HYDROGUI/HYDROGUI_Tool.cxx
Salome HOME
refs #432: refactoring of the shape class
[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          anObjectKind == KIND_BATHYMETRY
234 #ifdef DEB_GROUPS
235          || anObjectKind == KIND_SHAPES_GROUP ||
236          anObjectKind == KIND_SPLITTED_GROUP
237 #endif
238          )
239     {
240       return true;
241     }
242   }
243   
244   if ( theViewerType.isEmpty() || theViewerType == SVTK_Viewer::Type() )
245   {
246     if ( anObjectKind == KIND_BATHYMETRY )
247       return true;
248   }
249
250   if ( theViewerType.isEmpty() || theViewerType == GraphicsView_Viewer::Type() )
251   {
252     if ( anObjectKind == KIND_IMAGE ||
253          anObjectKind == KIND_POLYLINEXY )
254       return true;
255   }
256
257   return false;
258 }
259
260 void HYDROGUI_Tool::GetPrsSubObjects( HYDROGUI_Module* theModule,
261                                       HYDROData_SequenceOfObjects& theSeq )
262 {
263   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
264   if( !aDocument.IsNull() )
265   {
266     HYDROData_Iterator anIterator( aDocument );
267     for( ; anIterator.More(); anIterator.Next() )
268     {
269       Handle(HYDROData_Entity) anObject = anIterator.Current();
270       if ( !IsObjectHasPresentation( anObject ) )
271         continue;
272
273       theSeq.Append( anObject );
274     }
275   }
276 }
277
278 HYDROGUI_Prs* HYDROGUI_Tool::GetPresentation( const Handle(HYDROData_Entity)& theObj,
279                                               const GraphicsView_ObjectList& theObjects )
280 {
281   if( !theObj.IsNull() )
282   {
283     GraphicsView_ObjectListIterator anIter( theObjects );
284     while( anIter.hasNext() )
285     {
286       if( HYDROGUI_Prs* aPrs = dynamic_cast<HYDROGUI_Prs*>( anIter.next() ) )
287       {
288         Handle(HYDROData_Entity) anObj = aPrs->getObject();
289         if( IsEqual( anObj, theObj ) )
290           return aPrs;
291       }
292     }
293   }
294   return NULL;
295 }
296
297 GraphicsView_ObjectList HYDROGUI_Tool::GetPrsList( GraphicsView_ViewPort* theViewPort )
298 {
299   GraphicsView_ObjectList aList;
300   if( theViewPort )
301   {
302     GraphicsView_ObjectListIterator anIter( theViewPort->getObjects() );
303     while( anIter.hasNext() )
304       if( HYDROGUI_Prs* aPrs = dynamic_cast<HYDROGUI_Prs*>( anIter.next() ) )
305         aList.append( aPrs );
306   }
307   return aList;
308 }
309
310 HYDROData_SequenceOfObjects HYDROGUI_Tool::GetSelectedObjects( HYDROGUI_Module* theModule )
311 {
312   HYDROData_SequenceOfObjects aSeq;
313
314   HYDROGUI_DataModel* aModel = theModule->getDataModel();
315
316   SUIT_SelectionMgr* aSelectionMgr = theModule->getApp()->selectionMgr();
317   SUIT_DataOwnerPtrList anOwners;
318   aSelectionMgr->selected( anOwners );
319
320   QStringList aCollectedNameList; // to avoid duplication
321   foreach( SUIT_DataOwner* aSUITOwner, anOwners )
322   {
323     if( LightApp_DataOwner* anOwner = dynamic_cast<LightApp_DataOwner*>( aSUITOwner ) )
324     {
325       Handle(HYDROData_Entity) anObject = aModel->objectByEntry( anOwner->entry() );
326       if( !anObject.IsNull() )
327       {
328         QString aName = anObject->GetName();
329         if( !aCollectedNameList.contains( aName ) )
330         {
331           aSeq.Append( anObject );
332           aCollectedNameList.append( aName );
333         }
334       }
335     }
336   }
337   return aSeq;
338 }
339
340 Handle(HYDROData_Entity) HYDROGUI_Tool::GetSelectedObject( HYDROGUI_Module* theModule )
341 {
342   HYDROData_SequenceOfObjects aSeq = GetSelectedObjects( theModule );
343   if( !aSeq.IsEmpty() )
344     return aSeq.First();
345   return NULL;
346 }
347
348 HYDROData_SequenceOfObjects HYDROGUI_Tool::GetGeometryObjects( 
349   HYDROGUI_Module* theModule )
350 {
351   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
352
353   HYDROData_SequenceOfObjects aResSeq;
354
355   HYDROData_Iterator anIter( aDocument );
356   for ( ; anIter.More(); anIter.Next() )
357   {
358     Handle(HYDROData_Entity) anObj = anIter.Current();
359     if ( !HYDROData_Tool::IsGeometryObject( anObj ) )
360       continue;
361
362     aResSeq.Append( anObj );
363   }
364   
365   return aResSeq;
366 }
367
368 ObjectKind HYDROGUI_Tool::GetSelectedPartition( HYDROGUI_Module* theModule )
369 {
370   HYDROGUI_DataModel* aModel = theModule->getDataModel();
371
372   SUIT_SelectionMgr* aSelectionMgr = theModule->getApp()->selectionMgr();
373   SUIT_DataOwnerPtrList anOwners;
374   aSelectionMgr->selected( anOwners );
375
376   if( anOwners.size() != 1 )
377     return KIND_UNKNOWN;
378
379   if( LightApp_DataOwner* anOwner = dynamic_cast<LightApp_DataOwner*>( anOwners.first().operator->() ) )
380   {
381     QString anEntry = anOwner->entry();
382     QString aPrefix = HYDROGUI_DataObject::entryPrefix();
383     if( anEntry.left( aPrefix.length() ) == aPrefix )
384     {
385       anEntry.remove( aPrefix );
386       for( ObjectKind anObjectKind = KIND_UNKNOWN + 1; anObjectKind <= KIND_LAST; anObjectKind++ )
387         if( HYDROGUI_DataModel::tr( HYDROGUI_DataModel::partitionName( anObjectKind ).toAscii() ) == anEntry )
388           return anObjectKind;
389     }
390   }
391   return KIND_UNKNOWN;
392 }
393
394 QStringList HYDROGUI_Tool::GetSelectedGeomObjects( HYDROGUI_Module* theModule,
395                                                    QList<GEOM::shape_type> theTypes )
396 {
397   QStringList anEntryList;
398
399   // Get active SalomeApp_Study
400   SalomeApp_Study* aStudy = NULL;
401   if ( theModule && theModule->getApp() ) {
402     aStudy = dynamic_cast<SalomeApp_Study*>( theModule->getApp()->activeStudy() );
403   }
404   if ( !aStudy ) {
405     return anEntryList;
406   }
407
408   // Get selection
409   SUIT_SelectionMgr* aSelectionMgr = theModule->getApp()->selectionMgr();
410   SUIT_DataOwnerPtrList anOwners;
411   aSelectionMgr->selected( anOwners );
412
413   // Check if the selected objects belong to GEOM and have a shape
414   foreach( SUIT_DataOwner* aSUITOwner, anOwners )
415   {
416     if( LightApp_DataOwner* anOwner = dynamic_cast<LightApp_DataOwner*>( aSUITOwner ) )
417     {
418       QString anEntry = anOwner->entry();
419       _PTR(SObject) aSObject( aStudy->studyDS()->FindObjectID(qPrintable(anEntry)) );
420       if (aSObject) {
421          _PTR(SComponent) aSComponent = aSObject->GetFatherComponent();
422         if ( aSComponent && aSComponent->ComponentDataType() == "GEOM" ) {
423           GEOM::GEOM_Object_var aGeomObj = 
424             GEOMBase::GetObjectFromIOR( aSObject->GetIOR().c_str() );
425
426           if ( !aGeomObj->_is_nil() && aGeomObj->IsShape() && 
427                theTypes.contains( aGeomObj->GetShapeType() ) ) {
428             anEntryList << anEntry;
429           }
430         }
431       }
432     }
433   }
434
435   return anEntryList;
436 }
437
438 Handle(HYDROData_Entity) HYDROGUI_Tool::FindObjectByName( HYDROGUI_Module* theModule,
439                                                           const QString&   theName,
440                                                           const ObjectKind theObjectKind )
441 {
442   Handle(HYDROData_Entity) aResObj;
443   
444   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
445   if ( !aDocument.IsNull() )
446     aResObj = aDocument->FindObjectByName( theName, theObjectKind );
447   
448   return aResObj;
449 }
450
451 HYDROData_SequenceOfObjects HYDROGUI_Tool::FindObjectsByNames( HYDROGUI_Module*   theModule,
452                                                                const QStringList& theNames,
453                                                                const ObjectKind   theObjectKind )
454 {
455   HYDROData_SequenceOfObjects aResSeq;
456
457   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
458   if ( !aDocument.IsNull() )
459     aResSeq = aDocument->FindObjectsByNames( theNames, theObjectKind );
460
461   return aResSeq;
462 }
463
464 QString HYDROGUI_Tool::GenerateObjectName( HYDROGUI_Module*   theModule,
465                                            const QString&     thePrefix,
466                                            const QStringList& theUsedNames,
467                                            const bool         theIsTryToUsePurePrefix)
468 {
469   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
470   return HYDROData_Tool::GenerateObjectName( aDocument, thePrefix, theUsedNames, theIsTryToUsePurePrefix );
471 }
472
473 size_t HYDROGUI_Tool::GetActiveViewId( HYDROGUI_Module* theModule,
474                                        const QString&   theViewId )
475 {
476   size_t aViewId = 0;
477   SUIT_ViewManager* aViewMgr = theModule->getApp()->activeViewManager();
478   if( !aViewMgr || ( !theViewId.isEmpty() && aViewMgr->getType() != theViewId ) )
479     return aViewId;
480
481   if( SUIT_ViewModel* aViewer = aViewMgr->getViewModel() )
482     aViewId = (size_t)aViewer;
483   return aViewId;
484 }
485
486 size_t HYDROGUI_Tool::GetActiveGraphicsViewId( HYDROGUI_Module* theModule )
487 {
488   return GetActiveViewId( theModule, GraphicsView_Viewer::Type() );
489 }
490
491 size_t HYDROGUI_Tool::GetActiveOCCViewId( HYDROGUI_Module* theModule )
492 {
493   return GetActiveViewId( theModule, OCCViewer_Viewer::Type() );
494 }
495
496 QList<size_t> getViewIdList( HYDROGUI_Module* theModule,
497                              const QString&   theViewId )
498 {
499   QList<size_t> aList;
500   ViewManagerList aViewMgrs;
501   theModule->getApp()->viewManagers( theViewId, aViewMgrs );
502   QListIterator<SUIT_ViewManager*> anIter( aViewMgrs );
503   while( anIter.hasNext() )
504   {
505     if( SUIT_ViewManager* aViewMgr = anIter.next() )
506     {
507       if( SUIT_ViewModel* aViewer = aViewMgr->getViewModel() )
508         aList.append( (size_t)aViewer );
509     }
510   }
511   return aList;
512 }
513
514 QList<size_t> HYDROGUI_Tool::GetGraphicsViewIdList( HYDROGUI_Module* theModule )
515 {
516   return getViewIdList( theModule, GraphicsView_Viewer::Type() );
517 }
518
519 QList<size_t> HYDROGUI_Tool::GetOCCViewIdList( HYDROGUI_Module* theModule )
520 {
521   return getViewIdList( theModule, OCCViewer_Viewer::Type() );
522 }
523
524 void HYDROGUI_Tool::setOCCActionShown( OCCViewer_ViewFrame* theViewFrame,
525                                        const int theActionId,
526                                        const bool isShown )
527 {
528   if ( !theViewFrame )
529     return;
530
531   OCCViewer_ViewWindow* aView = theViewFrame->getView( OCCViewer_ViewFrame::MAIN_VIEW );
532   if ( aView ) {
533     aView->toolMgr()->setShown( theActionId, isShown );
534     if ( theActionId == OCCViewer_ViewWindow::MaximizedId )
535       theViewFrame->onMaximizedView( aView, true );
536   }
537 }
538
539 void HYDROGUI_Tool::setOCCActionShown( HYDROGUI_Module* theModule,
540                                        const int theActionId,
541                                        const bool isShown )
542 {
543   QList<size_t> aList;
544   ViewManagerList aViewMgrs;
545   theModule->getApp()->viewManagers( OCCViewer_Viewer::Type(), aViewMgrs );
546   QListIterator<SUIT_ViewManager*> anIter( aViewMgrs );
547   while( anIter.hasNext() )
548   {
549     if( SUIT_ViewManager* aViewMgr = anIter.next() )
550     {
551       OCCViewer_ViewFrame* aViewFrame = dynamic_cast<OCCViewer_ViewFrame*>
552                                                            ( aViewMgr->getActiveView() );
553       if ( aViewFrame )
554         setOCCActionShown( aViewFrame, theActionId, isShown );
555     }
556   }
557 }
558
559 void HYDROGUI_Tool::GetObjectReferences( const Handle(HYDROData_Entity)& theObj,
560                                          HYDROData_SequenceOfObjects& theRefObjects,
561                                          QStringList& theRefNames )
562 {
563   if( theObj.IsNull() )
564     return;
565
566   HYDROData_SequenceOfObjects anAllRefObjects = theObj->GetAllReferenceObjects();
567   theRefObjects.Append( anAllRefObjects );
568
569   for( int i = 1, n = anAllRefObjects.Length(); i <= n; ++i )
570   {
571     Handle(HYDROData_Entity) aRefObj = theRefObjects.Value( i );
572     if( aRefObj.IsNull() || aRefObj->IsRemoved() )
573       continue;
574
575     QString aRefObjectName = aRefObj->GetName();
576     if( theRefNames.contains( aRefObjectName ) )
577       continue;
578
579     theRefObjects.Append( aRefObj );
580     theRefNames.append( aRefObjectName );
581
582     GetObjectReferences( aRefObj, theRefObjects, theRefNames );
583   }
584 }
585
586 HYDROData_SequenceOfObjects HYDROGUI_Tool::GetObjectBackReferences( 
587   HYDROGUI_Module*                theModule,
588   const Handle(HYDROData_Entity)& theObj )
589 {
590   if( theObj.IsNull() )
591     return HYDROData_SequenceOfObjects();
592
593   QString anObjName = theObj->GetName();
594
595   QMap<QString,HYDROData_SequenceOfObjects> aMapOfBackRefs =
596     GetObjectsBackReferences( theModule, QStringList() << anObjName );
597
598   return aMapOfBackRefs[ anObjName ];
599 }
600
601 QMap<QString,HYDROData_SequenceOfObjects> HYDROGUI_Tool::GetObjectsBackReferences(
602   HYDROGUI_Module*   theModule, const QStringList& theObjectNames )
603 {
604   QMap<QString,HYDROData_SequenceOfObjects> aResMap;
605
606   if( theObjectNames.isEmpty() )
607     return aResMap;
608
609   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
610   if( aDocument.IsNull() )
611     return aResMap;
612
613   HYDROData_Iterator anIterator( aDocument );
614   for( ; anIterator.More(); anIterator.Next() )
615   {
616     Handle(HYDROData_Entity) anObject = anIterator.Current();
617     if( anObject.IsNull() || anObject->IsRemoved() )
618       continue;
619
620     QString anObjectName = anObject->GetName();
621     if ( theObjectNames.contains( anObjectName ) )
622       continue;
623
624     HYDROData_SequenceOfObjects aRefObjects = anObject->GetAllReferenceObjects();
625     for ( int i = 1, n = aRefObjects.Length(); i <= n; ++i )
626     {
627       Handle(HYDROData_Entity) aRefObject = aRefObjects.Value( i );
628       if( aRefObject.IsNull() || aRefObject->IsRemoved() )
629         continue;
630
631       QString aRefObjectName = aRefObject->GetName();
632       if ( !theObjectNames.contains( aRefObjectName ) )
633         continue;
634
635       aResMap[ aRefObjectName ].Append( anObject );
636     }
637   }
638
639   return aResMap;
640 }
641
642 QDockWidget* HYDROGUI_Tool::WindowDock( QWidget* wid )
643 {
644   if ( !wid )
645     return 0;
646
647   QDockWidget* dock = 0;
648   QWidget* w = wid->parentWidget();
649   while ( w && !dock )
650   {
651     dock = ::qobject_cast<QDockWidget*>( w );
652     w = w->parentWidget();
653   }
654   return dock;
655 }
656
657 QColor HYDROGUI_Tool::GenerateFillingColor( HYDROGUI_Module*   theModule,
658                                             const QStringList& theZoneNames )
659 {
660   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
661   return GenerateFillingColor( aDocument, theZoneNames );
662 }
663
664 QColor HYDROGUI_Tool::GenerateFillingColor( const Handle(HYDROData_Document)& theDoc,
665                                             const QStringList&                theZoneNames )
666 {
667   QColor aFillingColor( HYDROData_ImmersibleZone::DefaultFillingColor() );
668
669   int aCounter = 0;
670   int aR = 0, aG = 0, aB = 0;
671   QStringListIterator aZoneNameIter( theZoneNames );
672   while( aZoneNameIter.hasNext() )
673   {
674     const QString& aZoneName = aZoneNameIter.next();
675     Handle(HYDROData_ImmersibleZone) aRefZone = 
676       Handle(HYDROData_ImmersibleZone)::DownCast( theDoc->FindObjectByName( aZoneName, KIND_IMMERSIBLE_ZONE ) );
677     if( !aRefZone.IsNull() )
678     {
679       QColor aRefColor = aRefZone->GetFillingColor();
680       aR += aRefColor.red();
681       aG += aRefColor.green();
682       aB += aRefColor.blue();
683       aCounter++;
684     }
685   }
686   
687   if( aCounter > 0 )
688   {
689     aFillingColor = QColor( aR / aCounter, aG / aCounter, aB / aCounter );
690   }
691
692   return aFillingColor;
693 }
694
695 QStringList HYDROGUI_Tool::FindExistingObjectsNames( const Handle(HYDROData_Document)& theDoc, 
696                                                      const ObjectKind theObjectKind )
697 {
698   QStringList aNames;
699
700   HYDROData_Iterator anIter( theDoc, theObjectKind );
701   for ( ; anIter.More(); anIter.Next() ) {
702     Handle(HYDROData_Entity) anObject = anIter.Current();
703     if( !anObject.IsNull() ) {
704       aNames.append( anObject->GetName() );
705     }
706   }
707
708   return aNames;
709 }
710
711 QString HYDROGUI_Tool::GetCoordinateString( const double theNumber, bool isInLocale )
712 {
713   if( isInLocale )
714   {
715     static QLocale aLocale( QLocale::English, QLocale::France );
716     return aLocale.toString( theNumber, 'f', 2 );
717   }
718   else
719     return QString::number( theNumber, 'f', 2 );
720 }
721