Salome HOME
Fix for the bug #56: Show, Show only, Hide for Region and Zone under Region.
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_Tool.cxx
1 // Copyright (C) 2007-2013  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 #include "HYDROGUI_Tool.h"
24
25 #include "HYDROGUI_DataModel.h"
26 #include "HYDROGUI_DataObject.h"
27 #include "HYDROGUI_Module.h"
28 #include "HYDROGUI_Prs.h"
29
30 #include <HYDROData_Document.h>
31 #include <HYDROData_Iterator.h>
32 #include <HYDROData_ImmersibleZone.h>
33 #include <HYDROData_Tool.h>
34
35 #include <GraphicsView_Viewer.h>
36
37 #include <LightApp_Application.h>
38 #include <LightApp_DataOwner.h>
39 #include <LightApp_SelectionMgr.h>
40
41 #include <OCCViewer_ViewModel.h>
42
43 #include <QtxWorkstack.h>
44
45 #include <STD_TabDesktop.h>
46
47 #include <SUIT_Session.h>
48 #include <SUIT_Study.h>
49 #include <SUIT_ViewManager.h>
50 #include <SUIT_ViewWindow.h>
51
52 #include <QDir>
53 #include <QFileInfo>
54 #include <QDockWidget>
55 #include <QTextCodec>
56
57 // Definition of this id allows to use 'latin1' (Qt alias for 'ISO-8859-1')
58 // encoding instead of default 'System'
59 #define USE_LATIN1_ENCODING
60
61 QString HYDROGUI_Tool::ToQString( const TCollection_AsciiString& src )
62 {
63 #ifdef USE_LATIN1_ENCODING
64   QTextCodec* codec = QTextCodec::codecForName( "latin1" ); // alias for ISO-8859-1
65 #else
66   QTextCodec* codec = QTextCodec::codecForLocale();
67 #endif
68   QString res;
69   if ( !src.IsEmpty() )
70     res = codec ? codec->toUnicode( (char*)src.ToCString(), src.Length() ) :
71       QString( (char*)src.ToCString() );
72   return res;
73 }
74
75 QString HYDROGUI_Tool::ToQString( const TCollection_ExtendedString& src )
76 {
77   return QString( (QChar*)src.ToExtString(), src.Length() );
78 }
79
80 QString HYDROGUI_Tool::ToQString( const Handle(TCollection_HAsciiString)& src )
81 {
82   if( src.IsNull() )
83     return QString();
84   else
85     return ToQString( src->String() );
86 }
87
88 QString HYDROGUI_Tool::ToQString( const Handle(TCollection_HExtendedString)& src )
89 {
90   if( src.IsNull() )
91     return QString();
92   return ToQString( src->String() );
93 }
94
95 TCollection_AsciiString HYDROGUI_Tool::ToAsciiString( const QString& src )
96 {
97   TCollection_AsciiString res;
98   if( !src.isNull() )
99   {
100 #ifdef USE_LATIN1_ENCODING
101     QTextCodec* codec = QTextCodec::codecForName( "latin1" ); // alias for ISO-8859-1
102 #else
103     QTextCodec* codec = QTextCodec::codecForLocale();
104 #endif
105     if( codec )
106     {
107       QByteArray str = codec->fromUnicode( src );
108       res = TCollection_AsciiString( (Standard_CString)str.constData() );
109     }
110     else
111       res = TCollection_AsciiString( src.toLatin1().data() );
112   }
113   return res;
114 }
115
116 TCollection_ExtendedString HYDROGUI_Tool::ToExtString( const QString& src )
117 {
118   if( src.isEmpty() )
119     return TCollection_ExtendedString();
120
121   Standard_Integer len = src.length();
122   Standard_ExtString extStr = new Standard_ExtCharacter[ ( len + 1 ) * 2 ];
123   memcpy( (void*)extStr, src.unicode(), len * 2 );
124   ((short*)extStr)[ len ] = 0;
125
126   TCollection_ExtendedString trg( extStr );
127   delete [] extStr;
128   return trg;
129 }
130
131 Handle(TCollection_HAsciiString) HYDROGUI_Tool::ToHAsciiString( const QString& src )
132 {
133   return new TCollection_HAsciiString( ToAsciiString( src ) );
134 }
135
136 Handle(TCollection_HExtendedString) HYDROGUI_Tool::ToHExtString( const QString& src )
137 {
138   return new TCollection_HExtendedString( ToExtString( src ) );
139 }
140
141 int HYDROGUI_Tool::GetActiveStudyId()
142 {
143   if( SUIT_Session* aSession = SUIT_Session::session() )
144     if( SUIT_Application* anApp = aSession->activeApplication() )
145       if( SUIT_Study* aStudy = anApp->activeStudy() )
146         return aStudy->id();
147   return 0;
148 }
149
150 QString HYDROGUI_Tool::GetTempDir( const bool theToCreate )
151 {
152   QString aRes;
153
154   char* tmpdir = getenv ( "HYDRO_TMP_DIR" );
155   if ( tmpdir )
156   {
157     // try to create folder if it does not exist
158     QFileInfo fi( tmpdir );
159     if ( !fi.exists() && theToCreate )
160     {
161       if ( QDir().mkdir( tmpdir ) )
162         QFile::setPermissions( tmpdir, (QFile::Permissions)0x4FFFF );
163        QFileInfo fi( tmpdir );
164        if ( !fi.exists() || !fi.isWritable() )
165          tmpdir = 0;
166     }
167   }
168   if ( !tmpdir )
169     tmpdir = getenv ( "TEMP" );
170   if ( !tmpdir )
171     tmpdir = getenv ( "TMP" );
172   if ( !tmpdir )
173   {
174 #ifdef WNT
175     tmpdir = "C:\\";
176 #else
177     tmpdir = "/tmp";
178 #endif
179   }
180   aRes = tmpdir;
181   
182   QFileInfo fi( aRes );
183   if ( !fi.exists() || !fi.isWritable() )
184     aRes = QString::null;
185
186   return aRes;
187 }
188
189 void HYDROGUI_Tool::SetActiveViewManager( HYDROGUI_Module* theModule,
190                                           SUIT_ViewManager* theViewManager )
191 {
192   if( theViewManager )
193     if( SUIT_ViewWindow* aViewWindow = theViewManager->getActiveView() )
194       if( STD_TabDesktop* aTabDesktop = dynamic_cast<STD_TabDesktop*>( theModule->getApp()->desktop() ) )
195         if( QtxWorkstack* aWorkstack = aTabDesktop->workstack() )
196           aWorkstack->setActiveWindow( aViewWindow );
197 }
198
199 void HYDROGUI_Tool::GetPrsSubObjects( HYDROGUI_Module* theModule,
200                                       HYDROData_SequenceOfObjects& theSeq )
201 {
202   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
203   if( !aDocument.IsNull() )
204   {
205     HYDROData_Iterator anIterator( aDocument );
206     for( ; anIterator.More(); anIterator.Next() )
207     {
208       Handle(HYDROData_Entity) anObject = anIterator.Current();
209       if( !anObject.IsNull() && ( 
210         ( anObject->GetKind() == KIND_IMAGE ) ||
211         ( anObject->GetKind() == KIND_POLYLINE ) ||
212         ( anObject->GetKind() == KIND_IMMERSIBLE_ZONE ) ||
213         ( anObject->GetKind() == KIND_REGION ) ||
214         ( anObject->GetKind() == KIND_ZONE ) ) )
215       {
216         theSeq.Append( anObject );
217       }
218     }
219   }
220 }
221
222 HYDROGUI_Prs* HYDROGUI_Tool::GetPresentation( const Handle(HYDROData_Entity)& theObj,
223                                               const GraphicsView_ObjectList& theObjects )
224 {
225   if( !theObj.IsNull() )
226   {
227     GraphicsView_ObjectListIterator anIter( theObjects );
228     while( anIter.hasNext() )
229     {
230       if( HYDROGUI_Prs* aPrs = dynamic_cast<HYDROGUI_Prs*>( anIter.next() ) )
231       {
232         Handle(HYDROData_Entity) anObj = aPrs->getObject();
233         if( IsEqual( anObj, theObj ) )
234           return aPrs;
235       }
236     }
237   }
238   return NULL;
239 }
240
241 GraphicsView_ObjectList HYDROGUI_Tool::GetPrsList( GraphicsView_ViewPort* theViewPort )
242 {
243   GraphicsView_ObjectList aList;
244   if( theViewPort )
245   {
246     GraphicsView_ObjectListIterator anIter( theViewPort->getObjects() );
247     while( anIter.hasNext() )
248       if( HYDROGUI_Prs* aPrs = dynamic_cast<HYDROGUI_Prs*>( anIter.next() ) )
249         aList.append( aPrs );
250   }
251   return aList;
252 }
253
254 HYDROData_SequenceOfObjects HYDROGUI_Tool::GetSelectedObjects( HYDROGUI_Module* theModule )
255 {
256   HYDROData_SequenceOfObjects aSeq;
257
258   HYDROGUI_DataModel* aModel = theModule->getDataModel();
259
260   SUIT_SelectionMgr* aSelectionMgr = theModule->getApp()->selectionMgr();
261   SUIT_DataOwnerPtrList anOwners;
262   aSelectionMgr->selected( anOwners );
263
264   QStringList aCollectedNameList; // to avoid duplication
265   foreach( SUIT_DataOwner* aSUITOwner, anOwners )
266   {
267     if( LightApp_DataOwner* anOwner = dynamic_cast<LightApp_DataOwner*>( aSUITOwner ) )
268     {
269       Handle(HYDROData_Entity) anObject = aModel->objectByEntry( anOwner->entry() );
270       if( !anObject.IsNull() )
271       {
272         QString aName = anObject->GetName();
273         if( !aCollectedNameList.contains( aName ) )
274         {
275           aSeq.Append( anObject );
276           aCollectedNameList.append( aName );
277         }
278       }
279     }
280   }
281   return aSeq;
282 }
283
284 Handle(HYDROData_Entity) HYDROGUI_Tool::GetSelectedObject( HYDROGUI_Module* theModule )
285 {
286   HYDROData_SequenceOfObjects aSeq = GetSelectedObjects( theModule );
287   if( !aSeq.IsEmpty() )
288     return aSeq.First();
289   return NULL;
290 }
291
292 HYDROData_SequenceOfObjects HYDROGUI_Tool::GetGeometryObjects( 
293   HYDROGUI_Module* theModule )
294 {
295   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
296
297   HYDROData_SequenceOfObjects aResSeq;
298
299   HYDROData_Iterator anIter( aDocument );
300   for ( ; anIter.More(); anIter.Next() )
301   {
302     Handle(HYDROData_Entity) anObj = anIter.Current();
303     if ( !HYDROData_Tool::IsGeometryObject( anObj ) )
304       continue;
305
306     aResSeq.Append( anObj );
307   }
308   
309   return aResSeq;
310 }
311
312 ObjectKind HYDROGUI_Tool::GetSelectedPartition( HYDROGUI_Module* theModule )
313 {
314   HYDROGUI_DataModel* aModel = theModule->getDataModel();
315
316   SUIT_SelectionMgr* aSelectionMgr = theModule->getApp()->selectionMgr();
317   SUIT_DataOwnerPtrList anOwners;
318   aSelectionMgr->selected( anOwners );
319
320   if( anOwners.size() != 1 )
321     return KIND_UNKNOWN;
322
323   if( LightApp_DataOwner* anOwner = dynamic_cast<LightApp_DataOwner*>( anOwners.first().operator->() ) )
324   {
325     QString anEntry = anOwner->entry();
326     QString aPrefix = HYDROGUI_DataObject::entryPrefix();
327     if( anEntry.left( aPrefix.length() ) == aPrefix )
328     {
329       anEntry.remove( aPrefix );
330       for( ObjectKind anObjectKind = KIND_UNKNOWN + 1; anObjectKind <= KIND_LAST; anObjectKind++ )
331         if( HYDROGUI_DataModel::tr( HYDROGUI_DataModel::partitionName( anObjectKind ).toAscii() ) == anEntry )
332           return anObjectKind;
333     }
334   }
335   return KIND_UNKNOWN;
336 }
337
338 Handle(HYDROData_Entity) HYDROGUI_Tool::FindObjectByName( HYDROGUI_Module* theModule,
339                                                           const QString&   theName,
340                                                           const ObjectKind theObjectKind )
341 {
342   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
343   return HYDROData_Tool::FindObjectByName( aDocument, theName, theObjectKind );
344 }
345
346 HYDROData_SequenceOfObjects HYDROGUI_Tool::FindObjectsByNames( HYDROGUI_Module*   theModule,
347                                                                const QStringList& theNames,
348                                                                const ObjectKind   theObjectKind )
349 {
350   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
351   return HYDROData_Tool::FindObjectsByNames( aDocument, theNames, theObjectKind );
352 }
353
354 QString HYDROGUI_Tool::GenerateObjectName( HYDROGUI_Module*   theModule,
355                                            const QString&     thePrefix,
356                                            const QStringList& theUsedNames )
357 {
358   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
359   return HYDROData_Tool::GenerateObjectName( aDocument, thePrefix, theUsedNames );
360 }
361
362 size_t HYDROGUI_Tool::GetActiveViewId( HYDROGUI_Module* theModule,
363                                        const QString&   theViewId )
364 {
365   size_t aViewId = 0;
366   SUIT_ViewManager* aViewMgr = theModule->getApp()->activeViewManager();
367   if( !aViewMgr || ( !theViewId.isEmpty() && aViewMgr->getType() != theViewId ) )
368     return aViewId;
369
370   if( SUIT_ViewModel* aViewer = aViewMgr->getViewModel() )
371     aViewId = (size_t)aViewer;
372   return aViewId;
373 }
374
375 size_t HYDROGUI_Tool::GetActiveGraphicsViewId( HYDROGUI_Module* theModule )
376 {
377   return GetActiveViewId( theModule, GraphicsView_Viewer::Type() );
378 }
379
380 size_t HYDROGUI_Tool::GetActiveOCCViewId( HYDROGUI_Module* theModule )
381 {
382   return GetActiveViewId( theModule, OCCViewer_Viewer::Type() );
383 }
384
385 QList<size_t> getViewIdList( HYDROGUI_Module* theModule,
386                              const QString&   theViewId )
387 {
388   QList<size_t> aList;
389   ViewManagerList aViewMgrs;
390   theModule->getApp()->viewManagers( theViewId, aViewMgrs );
391   QListIterator<SUIT_ViewManager*> anIter( aViewMgrs );
392   while( anIter.hasNext() )
393   {
394     if( SUIT_ViewManager* aViewMgr = anIter.next() )
395     {
396       if( SUIT_ViewModel* aViewer = aViewMgr->getViewModel() )
397         aList.append( (size_t)aViewer );
398     }
399   }
400   return aList;
401 }
402
403 QList<size_t> HYDROGUI_Tool::GetGraphicsViewIdList( HYDROGUI_Module* theModule )
404 {
405   return getViewIdList( theModule, GraphicsView_Viewer::Type() );
406 }
407
408 QList<size_t> HYDROGUI_Tool::GetOCCViewIdList( HYDROGUI_Module* theModule )
409 {
410   return getViewIdList( theModule, OCCViewer_Viewer::Type() );
411 }
412
413 void HYDROGUI_Tool::GetObjectReferences( const Handle(HYDROData_Image)& theImage,
414                                          HYDROData_SequenceOfObjects& theRefObjects,
415                                          QStringList& theRefNames )
416 {
417   if( theImage.IsNull() )
418     return;
419
420   for( int anIndex = 0, aNbRef = theImage->NbReferences(); anIndex < aNbRef; anIndex++ )
421   {
422     Handle(HYDROData_Entity) aRefObj = theImage->Reference( anIndex );
423     if( !aRefObj.IsNull() && !aRefObj->IsRemoved() )
424     {
425       QString aName = aRefObj->GetName();
426       if( !theRefNames.contains( aName ) )
427       {
428         theRefObjects.Append( aRefObj );
429         theRefNames.append( aRefObj->GetName() );
430         if( aRefObj->GetKind() == KIND_IMAGE )
431         {
432           Handle(HYDROData_Image) aRefImage = Handle(HYDROData_Image)::DownCast( aRefObj );
433           if( !aRefImage.IsNull() )
434             GetObjectReferences( aRefImage, theRefObjects, theRefNames );
435         }
436       }
437     }
438   }
439 }
440
441 void HYDROGUI_Tool::GetObjectBackReferences( HYDROGUI_Module* theModule,
442                                              const Handle(HYDROData_Entity)& theObj,
443                                              HYDROData_SequenceOfObjects& theBackRefObjects,
444                                              QStringList& theBackRefNames )
445 {
446   if( theObj.IsNull() )
447     return;
448
449   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
450   if( aDocument.IsNull() )
451     return;
452
453   QString aName = theObj->GetName();
454
455   HYDROData_Iterator anIterator( aDocument, KIND_IMAGE );
456   for( ; anIterator.More(); anIterator.Next() )
457   {
458     Handle(HYDROData_Image) anImage = Handle(HYDROData_Image)::DownCast( anIterator.Current() );
459     if( !anImage.IsNull() )
460     {
461       HYDROData_SequenceOfObjects aRefObjects;
462       QStringList aRefNames;
463       GetObjectReferences( anImage, aRefObjects, aRefNames );
464       if( aRefNames.contains( aName ) )
465       {
466         theBackRefObjects.Append( anImage );
467         theBackRefNames.append( anImage->GetName() );
468       }
469     }
470   }
471 }
472
473
474 QDockWidget* HYDROGUI_Tool::WindowDock( QWidget* wid )
475 {
476   if ( !wid )
477     return 0;
478
479   QDockWidget* dock = 0;
480   QWidget* w = wid->parentWidget();
481   while ( w && !dock )
482   {
483     dock = ::qobject_cast<QDockWidget*>( w );
484     w = w->parentWidget();
485   }
486   return dock;
487 }
488
489 QColor HYDROGUI_Tool::GenerateFillingColor( HYDROGUI_Module*   theModule,
490                                             const QStringList& theZoneNames )
491 {
492   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
493   return GenerateFillingColor( aDocument, theZoneNames );
494 }
495
496 QColor HYDROGUI_Tool::GenerateFillingColor( const Handle(HYDROData_Document)& theDoc,
497                                             const QStringList&                theZoneNames )
498 {
499   QColor aFillingColor( HYDROData_ImmersibleZone::DefaultFillingColor() );
500
501   int aCounter = 0;
502   int aR = 0, aG = 0, aB = 0;
503   QStringListIterator aZoneNameIter( theZoneNames );
504   while( aZoneNameIter.hasNext() )
505   {
506     const QString& aZoneName = aZoneNameIter.next();
507     Handle(HYDROData_ImmersibleZone) aRefZone = 
508       Handle(HYDROData_ImmersibleZone)::DownCast(
509         HYDROData_Tool::FindObjectByName( theDoc, aZoneName, KIND_IMMERSIBLE_ZONE ) );
510     if( !aRefZone.IsNull() )
511     {
512       QColor aRefColor = aRefZone->GetFillingColor();
513       aR += aRefColor.red();
514       aG += aRefColor.green();
515       aB += aRefColor.blue();
516       aCounter++;
517     }
518   }
519   
520   if( aCounter > 0 )
521   {
522     aFillingColor = QColor( aR / aCounter, aG / aCounter, aB / aCounter );
523   }
524
525   return aFillingColor;
526 }
527