Salome HOME
Merge branch 'BR_LAND_COVER' of ssh://git.salome-platform.org/modules/hydro into...
[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 <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 = "/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
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 HYDROData_SequenceOfObjects HYDROGUI_Tool::GetLandCovers( HYDROGUI_Module* theModule )
367 {
368   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
369
370   HYDROData_SequenceOfObjects aResSeq;
371
372   HYDROData_Iterator anIter( aDocument );
373   for ( ; anIter.More(); anIter.Next() )
374   {
375     Handle(HYDROData_Entity) anObj = anIter.Current();
376     if ( !anObj.IsNull() && anObj->IsKind( STANDARD_TYPE(HYDROData_LandCover) ))
377       aResSeq.Append( anObj );
378   }
379   
380   return aResSeq;
381 }
382
383 ObjectKind HYDROGUI_Tool::GetSelectedPartition( HYDROGUI_Module* theModule )
384 {
385   HYDROGUI_DataModel* aModel = theModule->getDataModel();
386
387   SUIT_SelectionMgr* aSelectionMgr = theModule->getApp()->selectionMgr();
388   SUIT_DataOwnerPtrList anOwners;
389   aSelectionMgr->selected( anOwners );
390
391   if( anOwners.size() != 1 )
392     return KIND_UNKNOWN;
393
394   if( LightApp_DataOwner* anOwner = dynamic_cast<LightApp_DataOwner*>( anOwners.first().operator->() ) )
395   {
396     QString anEntry = anOwner->entry();
397     QString aPrefix = HYDROGUI_DataObject::entryPrefix();
398     if( anEntry.left( aPrefix.length() ) == aPrefix )
399     {
400       anEntry.remove( aPrefix );
401       for( ObjectKind anObjectKind = KIND_UNKNOWN + 1; anObjectKind <= KIND_LAST; anObjectKind++ )
402         if( HYDROGUI_DataModel::tr( HYDROGUI_DataModel::partitionName( anObjectKind ).toAscii() ) == anEntry )
403           return anObjectKind;
404     }
405   }
406   return KIND_UNKNOWN;
407 }
408
409 QStringList HYDROGUI_Tool::GetSelectedGeomObjects( HYDROGUI_Module* theModule,
410                                                    QList<GEOM::shape_type> theTypes )
411 {
412   QStringList anEntryList;
413
414   // Get active SalomeApp_Study
415   SalomeApp_Study* aStudy = NULL;
416   if ( theModule && theModule->getApp() ) {
417     aStudy = dynamic_cast<SalomeApp_Study*>( theModule->getApp()->activeStudy() );
418   }
419   if ( !aStudy ) {
420     return anEntryList;
421   }
422
423   // Get selection
424   SUIT_SelectionMgr* aSelectionMgr = theModule->getApp()->selectionMgr();
425   SUIT_DataOwnerPtrList anOwners;
426   aSelectionMgr->selected( anOwners );
427
428   // Check if the selected objects belong to GEOM and have a shape
429   foreach( SUIT_DataOwner* aSUITOwner, anOwners )
430   {
431     if( LightApp_DataOwner* anOwner = dynamic_cast<LightApp_DataOwner*>( aSUITOwner ) )
432     {
433       QString anEntry = anOwner->entry();
434       _PTR(SObject) aSObject( aStudy->studyDS()->FindObjectID(qPrintable(anEntry)) );
435       if (aSObject) {
436          _PTR(SComponent) aSComponent = aSObject->GetFatherComponent();
437         if ( aSComponent && aSComponent->ComponentDataType() == "GEOM" ) {
438           GEOM::GEOM_Object_var aGeomObj = 
439             GEOMBase::GetObjectFromIOR( aSObject->GetIOR().c_str() );
440
441           if ( !aGeomObj->_is_nil() && aGeomObj->IsShape() && 
442                theTypes.contains( aGeomObj->GetShapeType() ) ) {
443             anEntryList << anEntry;
444           }
445         }
446       }
447     }
448   }
449
450   return anEntryList;
451 }
452
453 Handle(HYDROData_Entity) HYDROGUI_Tool::FindObjectByName( HYDROGUI_Module* theModule,
454                                                           const QString&   theName,
455                                                           const ObjectKind theObjectKind )
456 {
457   Handle(HYDROData_Entity) aResObj;
458   
459   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
460   if ( !aDocument.IsNull() )
461     aResObj = aDocument->FindObjectByName( theName, theObjectKind );
462   
463   return aResObj;
464 }
465
466 HYDROData_SequenceOfObjects HYDROGUI_Tool::FindObjectsByNames( HYDROGUI_Module*   theModule,
467                                                                const QStringList& theNames,
468                                                                const ObjectKind   theObjectKind )
469 {
470   HYDROData_SequenceOfObjects aResSeq;
471
472   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
473   if ( !aDocument.IsNull() )
474     aResSeq = aDocument->FindObjectsByNames( theNames, theObjectKind );
475
476   return aResSeq;
477 }
478
479 QString HYDROGUI_Tool::GenerateObjectName( HYDROGUI_Module*   theModule,
480                                            const QString&     thePrefix,
481                                            const QStringList& theUsedNames,
482                                            const bool         theIsTryToUsePurePrefix)
483 {
484   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
485   return HYDROData_Tool::GenerateObjectName( aDocument, thePrefix, theUsedNames, theIsTryToUsePurePrefix );
486 }
487
488 size_t HYDROGUI_Tool::GetActiveViewId( HYDROGUI_Module* theModule,
489                                        const QString&   theViewId )
490 {
491   size_t aViewId = 0;
492   SUIT_ViewManager* aViewMgr = theModule->getApp()->activeViewManager();
493   if( !aViewMgr || ( !theViewId.isEmpty() && aViewMgr->getType() != theViewId ) )
494     return aViewId;
495
496   if( SUIT_ViewModel* aViewer = aViewMgr->getViewModel() )
497     aViewId = (size_t)aViewer;
498   return aViewId;
499 }
500
501 size_t HYDROGUI_Tool::GetActiveGraphicsViewId( HYDROGUI_Module* theModule )
502 {
503   return GetActiveViewId( theModule, GraphicsView_Viewer::Type() );
504 }
505
506 size_t HYDROGUI_Tool::GetActiveOCCViewId( HYDROGUI_Module* theModule )
507 {
508   return GetActiveViewId( theModule, OCCViewer_Viewer::Type() );
509 }
510
511 QList<size_t> getViewIdList( HYDROGUI_Module* theModule,
512                              const QString&   theViewId )
513 {
514   QList<size_t> aList;
515   ViewManagerList aViewMgrs;
516   theModule->getApp()->viewManagers( theViewId, aViewMgrs );
517   QListIterator<SUIT_ViewManager*> anIter( aViewMgrs );
518   while( anIter.hasNext() )
519   {
520     if( SUIT_ViewManager* aViewMgr = anIter.next() )
521     {
522       if( SUIT_ViewModel* aViewer = aViewMgr->getViewModel() )
523         aList.append( (size_t)aViewer );
524     }
525   }
526   return aList;
527 }
528
529 QList<size_t> HYDROGUI_Tool::GetGraphicsViewIdList( HYDROGUI_Module* theModule )
530 {
531   return getViewIdList( theModule, GraphicsView_Viewer::Type() );
532 }
533
534 QList<size_t> HYDROGUI_Tool::GetOCCViewIdList( HYDROGUI_Module* theModule )
535 {
536   return getViewIdList( theModule, OCCViewer_Viewer::Type() );
537 }
538
539 void HYDROGUI_Tool::setOCCActionShown( OCCViewer_ViewFrame* theViewFrame,
540                                        const int theActionId,
541                                        const bool isShown )
542 {
543   if ( !theViewFrame )
544     return;
545
546   OCCViewer_ViewWindow* aView = theViewFrame->getView( OCCViewer_ViewFrame::MAIN_VIEW );
547   if ( aView ) {
548     aView->toolMgr()->setShown( theActionId, isShown );
549     if ( theActionId == OCCViewer_ViewWindow::MaximizedId )
550       theViewFrame->onMaximizedView( aView, true );
551   }
552 }
553
554 void HYDROGUI_Tool::setOCCActionShown( HYDROGUI_Module* theModule,
555                                        const int theActionId,
556                                        const bool isShown )
557 {
558   QList<size_t> aList;
559   ViewManagerList aViewMgrs;
560   theModule->getApp()->viewManagers( OCCViewer_Viewer::Type(), aViewMgrs );
561   QListIterator<SUIT_ViewManager*> anIter( aViewMgrs );
562   while( anIter.hasNext() )
563   {
564     if( SUIT_ViewManager* aViewMgr = anIter.next() )
565     {
566       OCCViewer_ViewFrame* aViewFrame = dynamic_cast<OCCViewer_ViewFrame*>
567                                                            ( aViewMgr->getActiveView() );
568       if ( aViewFrame )
569         setOCCActionShown( aViewFrame, theActionId, isShown );
570     }
571   }
572 }
573
574 void HYDROGUI_Tool::GetObjectReferences( const Handle(HYDROData_Entity)& theObj,
575                                          HYDROData_SequenceOfObjects& theRefObjects,
576                                          QStringList& theRefNames )
577 {
578   if( theObj.IsNull() )
579     return;
580
581   HYDROData_SequenceOfObjects anAllRefObjects = theObj->GetAllReferenceObjects();
582   theRefObjects.Append( anAllRefObjects );
583
584   for( int i = 1, n = anAllRefObjects.Length(); i <= n; ++i )
585   {
586     Handle(HYDROData_Entity) aRefObj = theRefObjects.Value( i );
587     if( aRefObj.IsNull() || aRefObj->IsRemoved() )
588       continue;
589
590     QString aRefObjectName = aRefObj->GetName();
591     if( theRefNames.contains( aRefObjectName ) )
592       continue;
593
594     theRefObjects.Append( aRefObj );
595     theRefNames.append( aRefObjectName );
596
597     GetObjectReferences( aRefObj, theRefObjects, theRefNames );
598   }
599 }
600
601 HYDROData_SequenceOfObjects HYDROGUI_Tool::GetObjectBackReferences( 
602   HYDROGUI_Module*                theModule,
603   const Handle(HYDROData_Entity)& theObj )
604 {
605   if( theObj.IsNull() )
606     return HYDROData_SequenceOfObjects();
607
608   QString anObjName = theObj->GetName();
609
610   QMap<QString,HYDROData_SequenceOfObjects> aMapOfBackRefs =
611     GetObjectsBackReferences( theModule, QStringList() << anObjName );
612
613   return aMapOfBackRefs[ anObjName ];
614 }
615
616 QMap<QString,HYDROData_SequenceOfObjects> HYDROGUI_Tool::GetObjectsBackReferences(
617   HYDROGUI_Module*   theModule, const QStringList& theObjectNames )
618 {
619   QMap<QString,HYDROData_SequenceOfObjects> aResMap;
620
621   if( theObjectNames.isEmpty() )
622     return aResMap;
623
624   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
625   if( aDocument.IsNull() )
626     return aResMap;
627
628   HYDROData_Iterator anIterator( aDocument );
629   for( ; anIterator.More(); anIterator.Next() )
630   {
631     Handle(HYDROData_Entity) anObject = anIterator.Current();
632     if( anObject.IsNull() || anObject->IsRemoved() )
633       continue;
634
635     QString anObjectName = anObject->GetName();
636     if ( theObjectNames.contains( anObjectName ) )
637       continue;
638
639     HYDROData_SequenceOfObjects aRefObjects = anObject->GetAllReferenceObjects();
640     for ( int i = 1, n = aRefObjects.Length(); i <= n; ++i )
641     {
642       Handle(HYDROData_Entity) aRefObject = aRefObjects.Value( i );
643       if( aRefObject.IsNull() || aRefObject->IsRemoved() )
644         continue;
645
646       QString aRefObjectName = aRefObject->GetName();
647       if ( !theObjectNames.contains( aRefObjectName ) )
648         continue;
649
650       aResMap[ aRefObjectName ].Append( anObject );
651     }
652   }
653
654   return aResMap;
655 }
656
657 QDockWidget* HYDROGUI_Tool::WindowDock( QWidget* wid )
658 {
659   if ( !wid )
660     return 0;
661
662   QDockWidget* dock = 0;
663   QWidget* w = wid->parentWidget();
664   while ( w && !dock )
665   {
666     dock = ::qobject_cast<QDockWidget*>( w );
667     w = w->parentWidget();
668   }
669   return dock;
670 }
671
672 QColor HYDROGUI_Tool::GenerateFillingColor( HYDROGUI_Module*   theModule,
673                                             const QStringList& theZoneNames )
674 {
675   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
676   return GenerateFillingColor( aDocument, theZoneNames );
677 }
678
679 QColor HYDROGUI_Tool::GenerateFillingColor( const Handle(HYDROData_Document)& theDoc,
680                                             const QStringList&                theZoneNames )
681 {
682   QColor aFillingColor( HYDROData_ImmersibleZone::DefaultFillingColor() );
683
684   int aCounter = 0;
685   int aR = 0, aG = 0, aB = 0;
686   QStringListIterator aZoneNameIter( theZoneNames );
687   while( aZoneNameIter.hasNext() )
688   {
689     const QString& aZoneName = aZoneNameIter.next();
690     Handle(HYDROData_ImmersibleZone) aRefZone = 
691       Handle(HYDROData_ImmersibleZone)::DownCast( theDoc->FindObjectByName( aZoneName, KIND_IMMERSIBLE_ZONE ) );
692     if( !aRefZone.IsNull() )
693     {
694       QColor aRefColor = aRefZone->GetFillingColor();
695       aR += aRefColor.red();
696       aG += aRefColor.green();
697       aB += aRefColor.blue();
698       aCounter++;
699     }
700   }
701   
702   if( aCounter > 0 )
703   {
704     aFillingColor = QColor( aR / aCounter, aG / aCounter, aB / aCounter );
705   }
706
707   return aFillingColor;
708 }
709
710 QStringList HYDROGUI_Tool::FindExistingObjectsNames( const Handle(HYDROData_Document)& theDoc, 
711                                                      const ObjectKind theObjectKind,
712                                                      bool isCheckValidProfile )
713 {
714   QStringList aNames;
715
716   HYDROData_Iterator anIter( theDoc, theObjectKind );
717   for ( ; anIter.More(); anIter.Next() ) {
718     Handle(HYDROData_Entity) anObject = anIter.Current();
719
720     bool isOK = !anObject.IsNull();
721     
722     if( isOK && isCheckValidProfile )
723     {
724       Handle_HYDROData_Profile aProfile = Handle_HYDROData_Profile::DownCast( anObject );
725       if( !aProfile.IsNull() && !aProfile->IsValid() )
726         isOK = false;
727     }
728       
729     if( isOK )
730       aNames.append( anObject->GetName() );
731   }
732
733   return aNames;
734 }
735
736 QString HYDROGUI_Tool::GetCoordinateString( const double theNumber, bool isInLocale )
737 {
738   if( isInLocale )
739   {
740     static QLocale aLocale( QLocale::English, QLocale::France );
741     return aLocale.toString( theNumber, 'f', 2 );
742   }
743   else
744     return QString::number( theNumber, 'f', 2 );
745 }
746
747 Handle(Image_PixMap) HYDROGUI_Tool::Pixmap( const QImage& theImage )
748 {
749     Handle(Image_PixMap) pix;
750     if ( theImage.isNull() || theImage.format() == QImage::Format_Invalid )
751         return pix;
752
753     Handle(Image_PixMap) tmpPix = new Image_PixMap();
754     tmpPix->SetTopDown( false );
755     QImage anImage = theImage.mirrored();
756     if ( !anImage.hasAlphaChannel() && anImage.allGray() )
757     {
758         tmpPix->InitTrash( Image_PixMap::ImgGray, anImage.width(), anImage.height(), anImage.width() );
759         for ( int r = 0; r < anImage.height(); r++ )
760         {
761             Standard_Byte* aRowData = tmpPix->ChangeRow( anImage.height() - r - 1 );
762             for  ( int p = 0; p < anImage.width(); p++ )
763                 aRowData[p] = qRed( anImage.pixel( p, r ) );
764         }
765     }
766     else
767     {
768         Image_PixMap::ImgFormat aFormat;
769         if ( anImage.hasAlphaChannel() )
770         {
771             if ( anImage.format() != QImage::Format_ARGB32 )
772                 anImage = anImage.convertToFormat( QImage::Format_ARGB32 );
773             aFormat = Image_PixMap::ImgRGBA;
774         }
775         else
776         {
777             if ( anImage.format() != QImage::Format_RGB888 )
778                 anImage = anImage.convertToFormat( QImage::Format_RGB888 );
779             aFormat = Image_PixMap::ImgRGB;
780         }
781
782         tmpPix->InitWrapper( aFormat, (Standard_Byte*)anImage.bits(), anImage.width(), anImage.height(), anImage.bytesPerLine() );
783     }
784
785     if ( !tmpPix.IsNull() )
786     {
787         pix = new Image_PixMap();
788         pix->InitCopy( *tmpPix.operator->() );
789         pix->SetTopDown( tmpPix->IsTopDown() );
790     }
791
792     return pix;
793 }