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