Salome HOME
Merge branch 'BR_v14_rc' of ssh://git.salome-platform.org/modules/hydro into BR_v14_rc
[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 #include <HYDROData_LandCover.h>
32
33 #include <GeometryGUI.h>
34 #include <GEOMBase.h>
35
36 #include <GraphicsView_Viewer.h>
37
38 #include <SalomeApp_Study.h>
39
40 #include <LightApp_Application.h>
41 #include <LightApp_DataOwner.h>
42 #include <LightApp_SelectionMgr.h>
43
44 #include <OCCViewer_ViewModel.h>
45 #include <OCCViewer_ViewFrame.h>
46
47 #include <QtxWorkstack.h>
48 #include <QtxActionToolMgr.h>
49
50 #include <STD_TabDesktop.h>
51
52 #include <SUIT_Session.h>
53 #include <SUIT_Study.h>
54 #include <SUIT_ViewManager.h>
55 #include <SUIT_ViewWindow.h>
56
57 #include <SALOMEDSClient.hxx>
58
59 #include <SVTK_ViewModel.h>
60
61 #include <QDir>
62 #include <QFileInfo>
63 #include <QDockWidget>
64 #include <QTextCodec>
65
66 // Definition of this id allows to use 'latin1' (Qt alias for 'ISO-8859-1')
67 // encoding instead of default 'System'
68 #define USE_LATIN1_ENCODING
69
70 // #define DEB_GROUPS 1
71
72 QString HYDROGUI_Tool::ToQString( const TCollection_AsciiString& src )
73 {
74 #ifdef USE_LATIN1_ENCODING
75   QTextCodec* codec = QTextCodec::codecForName( "latin1" ); // alias for ISO-8859-1
76 #else
77   QTextCodec* codec = QTextCodec::codecForLocale();
78 #endif
79   QString res;
80   if ( !src.IsEmpty() )
81     res = codec ? codec->toUnicode( (char*)src.ToCString(), src.Length() ) :
82       QString( (char*)src.ToCString() );
83   return res;
84 }
85
86 QString HYDROGUI_Tool::ToQString( const TCollection_ExtendedString& src )
87 {
88   return QString( (QChar*)src.ToExtString(), src.Length() );
89 }
90
91 QString HYDROGUI_Tool::ToQString( const Handle(TCollection_HAsciiString)& src )
92 {
93   if( src.IsNull() )
94     return QString();
95   else
96     return ToQString( src->String() );
97 }
98
99 QString HYDROGUI_Tool::ToQString( const Handle(TCollection_HExtendedString)& src )
100 {
101   if( src.IsNull() )
102     return QString();
103   return ToQString( src->String() );
104 }
105
106 TCollection_AsciiString HYDROGUI_Tool::ToAsciiString( const QString& src )
107 {
108   TCollection_AsciiString res;
109   if( !src.isNull() )
110   {
111 #ifdef USE_LATIN1_ENCODING
112     QTextCodec* codec = QTextCodec::codecForName( "latin1" ); // alias for ISO-8859-1
113 #else
114     QTextCodec* codec = QTextCodec::codecForLocale();
115 #endif
116     if( codec )
117     {
118       QByteArray str = codec->fromUnicode( src );
119       res = TCollection_AsciiString( (Standard_CString)str.constData() );
120     }
121     else
122       res = TCollection_AsciiString( src.toLatin1().data() );
123   }
124   return res;
125 }
126
127 TCollection_ExtendedString HYDROGUI_Tool::ToExtString( const QString& src )
128 {
129   if( src.isEmpty() )
130     return TCollection_ExtendedString();
131
132   Standard_Integer len = src.length();
133   Standard_ExtString extStr = new Standard_ExtCharacter[ ( len + 1 ) * 2 ];
134   memcpy( (void*)extStr, src.unicode(), len * 2 );
135   ((short*)extStr)[ len ] = 0;
136
137   TCollection_ExtendedString trg( extStr );
138   delete [] extStr;
139   return trg;
140 }
141
142 Handle(TCollection_HAsciiString) HYDROGUI_Tool::ToHAsciiString( const QString& src )
143 {
144   return new TCollection_HAsciiString( ToAsciiString( src ) );
145 }
146
147 Handle(TCollection_HExtendedString) HYDROGUI_Tool::ToHExtString( const QString& src )
148 {
149   return new TCollection_HExtendedString( ToExtString( src ) );
150 }
151
152 int HYDROGUI_Tool::GetActiveStudyId()
153 {
154   if( SUIT_Session* aSession = SUIT_Session::session() )
155     if( SUIT_Application* anApp = aSession->activeApplication() )
156       if( SUIT_Study* aStudy = anApp->activeStudy() )
157         return aStudy->id();
158   return 0;
159 }
160
161 QString HYDROGUI_Tool::GetTempDir( const bool theToCreate )
162 {
163   QString aRes;
164
165   char* tmpdir = getenv ( "HYDRO_TMP_DIR" );
166   if ( tmpdir )
167   {
168     // try to create folder if it does not exist
169     QFileInfo fi( tmpdir );
170     if ( !fi.exists() && theToCreate )
171     {
172       if ( QDir().mkdir( tmpdir ) )
173         QFile::setPermissions( tmpdir, (QFile::Permissions)0x4FFFF );
174        QFileInfo fi( tmpdir );
175        if ( !fi.exists() || !fi.isWritable() )
176          tmpdir = 0;
177     }
178   }
179   if ( !tmpdir )
180     tmpdir = getenv ( "TEMP" );
181   if ( !tmpdir )
182     tmpdir = getenv ( "TMP" );
183   if ( !tmpdir )
184   {
185 #ifdef WNT
186     tmpdir = "C:\\";
187 #else
188     tmpdir = strdup( "/tmp" );
189 #endif
190   }
191   aRes = tmpdir;
192   
193   QFileInfo fi( aRes );
194   if ( !fi.exists() || !fi.isWritable() )
195     aRes = QString::null;
196
197   return aRes;
198 }
199
200 void HYDROGUI_Tool::SetActiveViewManager( HYDROGUI_Module* theModule,
201                                           SUIT_ViewManager* theViewManager )
202 {
203   if( theViewManager )
204     if( SUIT_ViewWindow* aViewWindow = theViewManager->getActiveView() )
205       if( STD_TabDesktop* aTabDesktop = dynamic_cast<STD_TabDesktop*>( theModule->getApp()->desktop() ) )
206         if( QtxWorkstack* aWorkstack = aTabDesktop->workstack() )
207           aWorkstack->setActiveWindow( aViewWindow );
208 }
209
210 bool HYDROGUI_Tool::IsObjectHasPresentation( const Handle(HYDROData_Entity)& theObject,
211                                              const QString&                  theViewerType )
212 {
213   if ( theObject.IsNull() )
214     return false;
215
216   ObjectKind anObjectKind = theObject->GetKind();
217   if ( theViewerType.isEmpty() || theViewerType == OCCViewer_Viewer::Type() )
218   {
219     if ( anObjectKind == KIND_IMAGE ||
220          anObjectKind == KIND_POLYLINEXY ||
221          anObjectKind == KIND_POLYLINE ||
222          anObjectKind == KIND_IMMERSIBLE_ZONE ||
223          anObjectKind == KIND_REGION ||
224          anObjectKind == KIND_ZONE ||
225          anObjectKind == KIND_OBSTACLE ||
226          anObjectKind == KIND_PROFILE ||
227          anObjectKind == KIND_STREAM ||
228          anObjectKind == KIND_CHANNEL ||
229          anObjectKind == KIND_DIGUE ||
230          anObjectKind == KIND_DUMMY_3D || 
231          anObjectKind == KIND_BATHYMETRY ||
232          anObjectKind == KIND_LAND_COVER
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 HYDROData_SequenceOfObjects HYDROGUI_Tool::GetLandCovers( HYDROGUI_Module* theModule )
368 {
369   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
370
371   HYDROData_SequenceOfObjects aResSeq;
372
373   HYDROData_Iterator anIter( aDocument );
374   for ( ; anIter.More(); anIter.Next() )
375   {
376     Handle(HYDROData_Entity) anObj = anIter.Current();
377     if ( !anObj.IsNull() && anObj->IsKind( STANDARD_TYPE(HYDROData_LandCover) ))
378       aResSeq.Append( anObj );
379   }
380   
381   return aResSeq;
382 }
383
384 ObjectKind HYDROGUI_Tool::GetSelectedPartition( HYDROGUI_Module* theModule )
385 {
386   HYDROGUI_DataModel* aModel = theModule->getDataModel();
387
388   SUIT_SelectionMgr* aSelectionMgr = theModule->getApp()->selectionMgr();
389   SUIT_DataOwnerPtrList anOwners;
390   aSelectionMgr->selected( anOwners );
391
392   if( anOwners.size() != 1 )
393     return KIND_UNKNOWN;
394
395   if( LightApp_DataOwner* anOwner = dynamic_cast<LightApp_DataOwner*>( anOwners.first().operator->() ) )
396   {
397     QString anEntry = anOwner->entry();
398     QString aPrefix = HYDROGUI_DataObject::entryPrefix();
399     if( anEntry.left( aPrefix.length() ) == aPrefix )
400     {
401       anEntry.remove( aPrefix );
402       for( ObjectKind anObjectKind = KIND_UNKNOWN + 1; anObjectKind <= KIND_LAST; anObjectKind++ )
403         if( HYDROGUI_DataModel::tr( HYDROGUI_DataModel::partitionName( anObjectKind ).toAscii() ) == anEntry )
404           return anObjectKind;
405     }
406   }
407   return KIND_UNKNOWN;
408 }
409
410 QStringList HYDROGUI_Tool::GetSelectedGeomObjects( HYDROGUI_Module* theModule,
411                                                    QList<GEOM::shape_type> theTypes )
412 {
413   QStringList anEntryList;
414
415   // Get active SalomeApp_Study
416   SalomeApp_Study* aStudy = NULL;
417   if ( theModule && theModule->getApp() ) {
418     aStudy = dynamic_cast<SalomeApp_Study*>( theModule->getApp()->activeStudy() );
419   }
420   if ( !aStudy ) {
421     return anEntryList;
422   }
423
424   // Get selection
425   SUIT_SelectionMgr* aSelectionMgr = theModule->getApp()->selectionMgr();
426   SUIT_DataOwnerPtrList anOwners;
427   aSelectionMgr->selected( anOwners );
428
429   // Check if the selected objects belong to GEOM and have a shape
430   foreach( SUIT_DataOwner* aSUITOwner, anOwners )
431   {
432     if( LightApp_DataOwner* anOwner = dynamic_cast<LightApp_DataOwner*>( aSUITOwner ) )
433     {
434       QString anEntry = anOwner->entry();
435       _PTR(SObject) aSObject( aStudy->studyDS()->FindObjectID(qPrintable(anEntry)) );
436       if (aSObject) {
437          _PTR(SComponent) aSComponent = aSObject->GetFatherComponent();
438         if ( aSComponent && aSComponent->ComponentDataType() == "GEOM" ) {
439           GEOM::GEOM_Object_var aGeomObj = 
440             GEOMBase::GetObjectFromIOR( aSObject->GetIOR().c_str() );
441
442           if ( !aGeomObj->_is_nil() && aGeomObj->IsShape() && 
443                theTypes.contains( aGeomObj->GetShapeType() ) ) {
444             anEntryList << anEntry;
445           }
446         }
447       }
448     }
449   }
450
451   return anEntryList;
452 }
453
454 Handle(HYDROData_Entity) HYDROGUI_Tool::FindObjectByName( HYDROGUI_Module* theModule,
455                                                           const QString&   theName,
456                                                           const ObjectKind theObjectKind )
457 {
458   Handle(HYDROData_Entity) aResObj;
459   
460   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
461   if ( !aDocument.IsNull() )
462     aResObj = aDocument->FindObjectByName( theName, theObjectKind );
463   
464   return aResObj;
465 }
466
467 HYDROData_SequenceOfObjects HYDROGUI_Tool::FindObjectsByNames( HYDROGUI_Module*   theModule,
468                                                                const QStringList& theNames,
469                                                                const ObjectKind   theObjectKind )
470 {
471   HYDROData_SequenceOfObjects aResSeq;
472
473   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
474   if ( !aDocument.IsNull() )
475     aResSeq = aDocument->FindObjectsByNames( theNames, theObjectKind );
476
477   return aResSeq;
478 }
479
480 QString HYDROGUI_Tool::GenerateObjectName( HYDROGUI_Module*   theModule,
481                                            const QString&     thePrefix,
482                                            const QStringList& theUsedNames,
483                                            const bool         theIsTryToUsePurePrefix)
484 {
485   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
486   return HYDROData_Tool::GenerateObjectName( aDocument, thePrefix, theUsedNames, theIsTryToUsePurePrefix );
487 }
488
489 size_t HYDROGUI_Tool::GetActiveViewId( HYDROGUI_Module* theModule,
490                                        const QString&   theViewId )
491 {
492   size_t aViewId = 0;
493   SUIT_ViewManager* aViewMgr = theModule->getApp()->activeViewManager();
494   if( !aViewMgr || ( !theViewId.isEmpty() && aViewMgr->getType() != theViewId ) )
495     return aViewId;
496
497   if( SUIT_ViewModel* aViewer = aViewMgr->getViewModel() )
498     aViewId = (size_t)aViewer;
499   return aViewId;
500 }
501
502 size_t HYDROGUI_Tool::GetActiveGraphicsViewId( HYDROGUI_Module* theModule )
503 {
504   return GetActiveViewId( theModule, GraphicsView_Viewer::Type() );
505 }
506
507 size_t HYDROGUI_Tool::GetActiveOCCViewId( HYDROGUI_Module* theModule )
508 {
509   return GetActiveViewId( theModule, OCCViewer_Viewer::Type() );
510 }
511
512 QList<size_t> getViewIdList( HYDROGUI_Module* theModule,
513                              const QString&   theViewId )
514 {
515   QList<size_t> aList;
516   ViewManagerList aViewMgrs;
517   theModule->getApp()->viewManagers( theViewId, aViewMgrs );
518   QListIterator<SUIT_ViewManager*> anIter( aViewMgrs );
519   while( anIter.hasNext() )
520   {
521     if( SUIT_ViewManager* aViewMgr = anIter.next() )
522     {
523       if( SUIT_ViewModel* aViewer = aViewMgr->getViewModel() )
524         aList.append( (size_t)aViewer );
525     }
526   }
527   return aList;
528 }
529
530 QList<size_t> HYDROGUI_Tool::GetGraphicsViewIdList( HYDROGUI_Module* theModule )
531 {
532   return getViewIdList( theModule, GraphicsView_Viewer::Type() );
533 }
534
535 QList<size_t> HYDROGUI_Tool::GetOCCViewIdList( HYDROGUI_Module* theModule )
536 {
537   return getViewIdList( theModule, OCCViewer_Viewer::Type() );
538 }
539
540 void HYDROGUI_Tool::setOCCActionShown( OCCViewer_ViewFrame* theViewFrame,
541                                        const int theActionId,
542                                        const bool isShown )
543 {
544   if ( !theViewFrame )
545     return;
546
547   OCCViewer_ViewWindow* aView = theViewFrame->getView( OCCViewer_ViewFrame::MAIN_VIEW );
548   if ( aView ) {
549     aView->toolMgr()->setShown( theActionId, isShown );
550     if ( theActionId == OCCViewer_ViewWindow::MaximizedId )
551       theViewFrame->onMaximizedView( aView, true );
552   }
553 }
554
555 void HYDROGUI_Tool::setOCCActionShown( HYDROGUI_Module* theModule,
556                                        const int theActionId,
557                                        const bool isShown )
558 {
559   QList<size_t> aList;
560   ViewManagerList aViewMgrs;
561   theModule->getApp()->viewManagers( OCCViewer_Viewer::Type(), aViewMgrs );
562   QListIterator<SUIT_ViewManager*> anIter( aViewMgrs );
563   while( anIter.hasNext() )
564   {
565     if( SUIT_ViewManager* aViewMgr = anIter.next() )
566     {
567       OCCViewer_ViewFrame* aViewFrame = dynamic_cast<OCCViewer_ViewFrame*>
568                                                            ( aViewMgr->getActiveView() );
569       if ( aViewFrame )
570         setOCCActionShown( aViewFrame, theActionId, isShown );
571     }
572   }
573 }
574
575 void HYDROGUI_Tool::GetObjectReferences( const Handle(HYDROData_Entity)& theObj,
576                                          HYDROData_SequenceOfObjects& theRefObjects,
577                                          QStringList& theRefNames )
578 {
579   if( theObj.IsNull() )
580     return;
581
582   HYDROData_SequenceOfObjects anAllRefObjects = theObj->GetAllReferenceObjects();
583   theRefObjects.Append( anAllRefObjects );
584
585   for( int i = 1, n = anAllRefObjects.Length(); i <= n; ++i )
586   {
587     Handle(HYDROData_Entity) aRefObj = theRefObjects.Value( i );
588     if( aRefObj.IsNull() || aRefObj->IsRemoved() )
589       continue;
590
591     QString aRefObjectName = aRefObj->GetName();
592     if( theRefNames.contains( aRefObjectName ) )
593       continue;
594
595     theRefObjects.Append( aRefObj );
596     theRefNames.append( aRefObjectName );
597
598     GetObjectReferences( aRefObj, theRefObjects, theRefNames );
599   }
600 }
601
602 HYDROData_SequenceOfObjects HYDROGUI_Tool::GetObjectBackReferences( 
603   HYDROGUI_Module*                theModule,
604   const Handle(HYDROData_Entity)& theObj )
605 {
606   if( theObj.IsNull() )
607     return HYDROData_SequenceOfObjects();
608
609   QString anObjName = theObj->GetName();
610
611   QMap<QString,HYDROData_SequenceOfObjects> aMapOfBackRefs =
612     GetObjectsBackReferences( theModule, QStringList() << anObjName );
613
614   return aMapOfBackRefs[ anObjName ];
615 }
616
617 QMap<QString,HYDROData_SequenceOfObjects> HYDROGUI_Tool::GetObjectsBackReferences(
618   HYDROGUI_Module*   theModule, const QStringList& theObjectNames )
619 {
620   QMap<QString,HYDROData_SequenceOfObjects> aResMap;
621
622   if( theObjectNames.isEmpty() )
623     return aResMap;
624
625   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
626   if( aDocument.IsNull() )
627     return aResMap;
628
629   HYDROData_Iterator anIterator( aDocument );
630   for( ; anIterator.More(); anIterator.Next() )
631   {
632     Handle(HYDROData_Entity) anObject = anIterator.Current();
633     if( anObject.IsNull() || anObject->IsRemoved() )
634       continue;
635
636     QString anObjectName = anObject->GetName();
637     if ( theObjectNames.contains( anObjectName ) )
638       continue;
639
640     HYDROData_SequenceOfObjects aRefObjects = anObject->GetAllReferenceObjects();
641     for ( int i = 1, n = aRefObjects.Length(); i <= n; ++i )
642     {
643       Handle(HYDROData_Entity) aRefObject = aRefObjects.Value( i );
644       if( aRefObject.IsNull() || aRefObject->IsRemoved() )
645         continue;
646
647       QString aRefObjectName = aRefObject->GetName();
648       if ( !theObjectNames.contains( aRefObjectName ) )
649         continue;
650
651       aResMap[ aRefObjectName ].Append( anObject );
652     }
653   }
654
655   return aResMap;
656 }
657
658 QDockWidget* HYDROGUI_Tool::WindowDock( QWidget* wid )
659 {
660   if ( !wid )
661     return 0;
662
663   QDockWidget* dock = 0;
664   QWidget* w = wid->parentWidget();
665   while ( w && !dock )
666   {
667     dock = ::qobject_cast<QDockWidget*>( w );
668     w = w->parentWidget();
669   }
670   return dock;
671 }
672
673 QColor HYDROGUI_Tool::GenerateFillingColor( HYDROGUI_Module*   theModule,
674                                             const QStringList& theZoneNames )
675 {
676   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
677   return GenerateFillingColor( aDocument, theZoneNames );
678 }
679
680 QColor HYDROGUI_Tool::GenerateFillingColor( const Handle(HYDROData_Document)& theDoc,
681                                             const QStringList&                theZoneNames )
682 {
683   QColor aFillingColor( HYDROData_ImmersibleZone::DefaultFillingColor() );
684
685   int aCounter = 0;
686   int aR = 0, aG = 0, aB = 0;
687   QStringListIterator aZoneNameIter( theZoneNames );
688   while( aZoneNameIter.hasNext() )
689   {
690     const QString& aZoneName = aZoneNameIter.next();
691     Handle(HYDROData_ImmersibleZone) aRefZone = 
692       Handle(HYDROData_ImmersibleZone)::DownCast( theDoc->FindObjectByName( aZoneName, KIND_IMMERSIBLE_ZONE ) );
693     if( !aRefZone.IsNull() )
694     {
695       QColor aRefColor = aRefZone->GetFillingColor();
696       aR += aRefColor.red();
697       aG += aRefColor.green();
698       aB += aRefColor.blue();
699       aCounter++;
700     }
701   }
702   
703   if( aCounter > 0 )
704   {
705     aFillingColor = QColor( aR / aCounter, aG / aCounter, aB / aCounter );
706   }
707
708   return aFillingColor;
709 }
710
711 QStringList HYDROGUI_Tool::FindExistingObjectsNames( const Handle(HYDROData_Document)& theDoc, 
712                                                      const ObjectKind theObjectKind,
713                                                      bool isCheckValidProfile )
714 {
715   QStringList aNames;
716
717   HYDROData_Iterator anIter( theDoc, theObjectKind );
718   for ( ; anIter.More(); anIter.Next() ) {
719     Handle(HYDROData_Entity) anObject = anIter.Current();
720
721     bool isOK = !anObject.IsNull();
722     
723     if( isOK && isCheckValidProfile )
724     {
725       Handle_HYDROData_Profile aProfile = Handle_HYDROData_Profile::DownCast( anObject );
726       if( !aProfile.IsNull() && !aProfile->IsValid() )
727         isOK = false;
728     }
729       
730     if( isOK )
731       aNames.append( anObject->GetName() );
732   }
733
734   return aNames;
735 }
736
737 QString HYDROGUI_Tool::GetCoordinateString( const double theNumber, bool isInLocale )
738 {
739   if( isInLocale )
740   {
741     static QLocale aLocale( QLocale::English, QLocale::France );
742     return aLocale.toString( theNumber, 'f', 2 );
743   }
744   else
745     return QString::number( theNumber, 'f', 2 );
746 }
747
748 Handle(Image_PixMap) HYDROGUI_Tool::Pixmap( const QImage& theImage )
749 {
750     Handle(Image_PixMap) pix;
751     if ( theImage.isNull() || theImage.format() == QImage::Format_Invalid )
752         return pix;
753
754     Handle(Image_PixMap) tmpPix = new Image_PixMap();
755     tmpPix->SetTopDown( false );
756     QImage anImage = theImage.mirrored();
757     if ( !anImage.hasAlphaChannel() && anImage.allGray() )
758     {
759         tmpPix->InitTrash( Image_PixMap::ImgGray, anImage.width(), anImage.height(), anImage.width() );
760         for ( int r = 0; r < anImage.height(); r++ )
761         {
762             Standard_Byte* aRowData = tmpPix->ChangeRow( anImage.height() - r - 1 );
763             for  ( int p = 0; p < anImage.width(); p++ )
764                 aRowData[p] = qRed( anImage.pixel( p, r ) );
765         }
766     }
767     else
768     {
769         Image_PixMap::ImgFormat aFormat;
770         if ( anImage.hasAlphaChannel() )
771         {
772             if ( anImage.format() != QImage::Format_ARGB32 )
773                 anImage = anImage.convertToFormat( QImage::Format_ARGB32 );
774             aFormat = Image_PixMap::ImgRGBA;
775         }
776         else
777         {
778             if ( anImage.format() != QImage::Format_RGB888 )
779                 anImage = anImage.convertToFormat( QImage::Format_RGB888 );
780             aFormat = Image_PixMap::ImgRGB;
781         }
782
783         tmpPix->InitWrapper( aFormat, (Standard_Byte*)anImage.bits(), anImage.width(), anImage.height(), anImage.bytesPerLine() );
784     }
785
786     if ( !tmpPix.IsNull() )
787     {
788         pix = new Image_PixMap();
789         pix->InitCopy( *tmpPix.operator->() );
790         pix->SetTopDown( tmpPix->IsTopDown() );
791     }
792
793     return pix;
794 }
795
796 void HYDROGUI_Tool::DeleteGeomObjects( HYDROGUI_Module* theModule, const QStringList& theEntries )
797 {
798   QStringList anEntryList;
799
800   // Get active SalomeApp_Study
801   SalomeApp_Study* aStudy = NULL;
802   if ( theModule && theModule->getApp() ) {
803     aStudy = dynamic_cast<SalomeApp_Study*>( theModule->getApp()->activeStudy() );
804   }
805   if ( !aStudy ) {
806     return;
807   }
808  
809   // Get GEOM engine
810   GEOM::GEOM_Gen_var aGeomEngine = GeometryGUI::GetGeomGen();
811   if ( aGeomEngine->_is_nil() ) {
812     return;
813   }
814   
815   // Delete GEOM objects
816   _PTR(StudyBuilder) aStudyBuilder( aStudy->studyDS()->NewBuilder() );
817   foreach ( const QString anEntry, theEntries ) {
818     _PTR(SObject) aSObject( aStudy->studyDS()->FindObjectID( qPrintable(anEntry) ) );
819     if ( aSObject ) {
820       GEOM::GEOM_Object_var aGeomObj = 
821         GEOMBase::GetObjectFromIOR( aSObject->GetIOR().c_str() );
822
823       if ( !aGeomObj->_is_nil() ) {
824         aGeomEngine->RemoveObject( aGeomObj );
825       }
826
827       aStudyBuilder->RemoveObject( aSObject );
828     }
829   }
830 }