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