Salome HOME
edcfd8688268be19ee89b679fba11f375fd20d2d
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_Tool2.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_Tool2.h>
20 #include <HYDROGUI_DataModel.h>
21 #include <HYDROGUI_DataObject.h>
22 #include <HYDROGUI_Module.h>
23 #include <HYDROGUI_Prs.h>
24 #include <HYDROData_Document.h>
25 #include <HYDROData_ImmersibleZone.h>
26 #include <HYDROData_Iterator.h>
27 #include <HYDROData_Tool.h>
28
29 #include <GeometryGUI.h>
30 #include <GEOMBase.h>
31
32 #include <SalomeApp_Study.h>
33 #include <LightApp_Application.h>
34 #include <LightApp_DataOwner.h>
35 #include <LightApp_SelectionMgr.h>
36 #include <OCCViewer_ViewModel.h>
37 #include <OCCViewer_ViewFrame.h>
38 #include <SVTK_ViewModel.h>
39 #include <GraphicsView_Viewer.h>
40 #include <GraphicsView_ViewPort.h>
41 #include <STD_TabDesktop.h>
42 #include <SUIT_Session.h>
43 #include <SUIT_ViewManager.h>
44 #include <SUIT_ViewWindow.h>
45 #include <QtxActionToolMgr.h>
46 #include <QtxWorkstack.h>
47
48 int HYDROGUI_Tool::GetActiveStudyId()
49 {
50   if( SUIT_Session* aSession = SUIT_Session::session() )
51     if( SUIT_Application* anApp = aSession->activeApplication() )
52       if( SUIT_Study* aStudy = anApp->activeStudy() )
53         return aStudy->id();
54   return 0;
55 }
56
57
58
59 void HYDROGUI_Tool::SetActiveViewManager( HYDROGUI_Module* theModule,
60                                           SUIT_ViewManager* theViewManager )
61 {
62   if( theViewManager )
63     if( SUIT_ViewWindow* aViewWindow = theViewManager->getActiveView() )
64       if( STD_TabDesktop* aTabDesktop = dynamic_cast<STD_TabDesktop*>( theModule->getApp()->desktop() ) )
65         if( QtxWorkstack* aWorkstack = aTabDesktop->workstack() )
66           aWorkstack->setActiveWindow( aViewWindow );
67 }
68
69
70 bool HYDROGUI_Tool::IsObjectHasPresentation( const Handle(HYDROData_Entity)& theObject,
71                                              const QString&                  theViewerType )
72 {
73   if ( theObject.IsNull() )
74     return false;
75
76   ObjectKind anObjectKind = theObject->GetKind();
77   if ( theViewerType.isEmpty() || theViewerType == OCCViewer_Viewer::Type() )
78   {
79     if ( anObjectKind == KIND_IMAGE ||
80          anObjectKind == KIND_POLYLINEXY ||
81          anObjectKind == KIND_POLYLINE ||
82          anObjectKind == KIND_IMMERSIBLE_ZONE ||
83          anObjectKind == KIND_REGION ||
84          anObjectKind == KIND_ZONE ||
85          anObjectKind == KIND_OBSTACLE ||
86          anObjectKind == KIND_PROFILE ||
87          anObjectKind == KIND_STREAM ||
88          anObjectKind == KIND_CHANNEL ||
89          anObjectKind == KIND_DIGUE ||
90          anObjectKind == KIND_DUMMY_3D || 
91          anObjectKind == KIND_BATHYMETRY ||
92          anObjectKind == KIND_LAND_COVER_MAP
93 #ifdef DEB_GROUPS
94          || anObjectKind == KIND_SHAPES_GROUP ||
95          anObjectKind == KIND_SPLITTED_GROUP
96 #endif
97          )
98     {
99       return true;
100     }
101   }
102   
103   if ( theViewerType.isEmpty() || theViewerType == SVTK_Viewer::Type() )
104   {
105     if ( anObjectKind == KIND_BATHYMETRY )
106       return true;
107   }
108
109   if ( theViewerType.isEmpty() || theViewerType == GraphicsView_Viewer::Type() )
110   {
111     if ( anObjectKind == KIND_IMAGE ||
112          anObjectKind == KIND_POLYLINEXY )
113       return true;
114   }
115
116   return false;
117 }
118
119 void HYDROGUI_Tool::GetPrsSubObjects( HYDROGUI_Module* theModule,
120                                       HYDROData_SequenceOfObjects& theSeq )
121 {
122   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
123   if( !aDocument.IsNull() )
124   {
125     HYDROData_Iterator anIterator( aDocument );
126     for( ; anIterator.More(); anIterator.Next() )
127     {
128       Handle(HYDROData_Entity) anObject = anIterator.Current();
129       if ( !IsObjectHasPresentation( anObject ) )
130         continue;
131
132       theSeq.Append( anObject );
133     }
134   }
135 }
136
137 HYDROGUI_Prs* HYDROGUI_Tool::GetPresentation( const Handle(HYDROData_Entity)& theObj,
138                                               const GraphicsView_ObjectList& theObjects )
139 {
140   if( !theObj.IsNull() )
141   {
142     GraphicsView_ObjectListIterator anIter( theObjects );
143     while( anIter.hasNext() )
144     {
145       if( HYDROGUI_Prs* aPrs = dynamic_cast<HYDROGUI_Prs*>( anIter.next() ) )
146       {
147         Handle(HYDROData_Entity) anObj = aPrs->getObject();
148         if( IsEqual( anObj, theObj ) )
149           return aPrs;
150       }
151     }
152   }
153   return NULL;
154 }
155
156 GraphicsView_ObjectList HYDROGUI_Tool::GetPrsList( GraphicsView_ViewPort* theViewPort )
157 {
158   GraphicsView_ObjectList aList;
159   if( theViewPort )
160   {
161     GraphicsView_ObjectListIterator anIter( theViewPort->getObjects() );
162     while( anIter.hasNext() )
163       if( HYDROGUI_Prs* aPrs = dynamic_cast<HYDROGUI_Prs*>( anIter.next() ) )
164         aList.append( aPrs );
165   }
166   return aList;
167 }
168
169
170
171 HYDROData_SequenceOfObjects HYDROGUI_Tool::GetSelectedObjects( HYDROGUI_Module* theModule )
172 {
173   HYDROData_SequenceOfObjects aSeq;
174
175   HYDROGUI_DataModel* aModel = theModule->getDataModel();
176
177   SUIT_SelectionMgr* aSelectionMgr = theModule->getApp()->selectionMgr();
178   SUIT_DataOwnerPtrList anOwners;
179   aSelectionMgr->selected( anOwners );
180
181   QStringList aCollectedNameList; // to avoid duplication
182   foreach( SUIT_DataOwner* aSUITOwner, anOwners )
183   {
184     if( LightApp_DataOwner* anOwner = dynamic_cast<LightApp_DataOwner*>( aSUITOwner ) )
185     {
186       Handle(HYDROData_Entity) anObject = aModel->objectByEntry( anOwner->entry() );
187       if( !anObject.IsNull() )
188       {
189         QString aName = anObject->GetName();
190         if( !aCollectedNameList.contains( aName ) )
191         {
192           aSeq.Append( anObject );
193           aCollectedNameList.append( aName );
194         }
195       }
196     }
197   }
198   return aSeq;
199 }
200
201 Handle(HYDROData_Entity) HYDROGUI_Tool::GetSelectedObject( HYDROGUI_Module* theModule )
202 {
203   HYDROData_SequenceOfObjects aSeq = GetSelectedObjects( theModule );
204   if( !aSeq.IsEmpty() )
205     return aSeq.First();
206   return NULL;
207 }
208
209 HYDROData_SequenceOfObjects HYDROGUI_Tool::GetGeometryObjects( HYDROGUI_Module* theModule )
210 {
211   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
212
213   HYDROData_SequenceOfObjects aResSeq;
214
215   HYDROData_Iterator anIter( aDocument );
216   for ( ; anIter.More(); anIter.Next() )
217   {
218     Handle(HYDROData_Entity) anObj = anIter.Current();
219     if ( !HYDROData_Tool::IsGeometryObject( anObj ) )
220       continue;
221
222     aResSeq.Append( anObj );
223   }
224   
225   return aResSeq;
226 }
227
228 ObjectKind HYDROGUI_Tool::GetSelectedPartition( HYDROGUI_Module* theModule )
229 {
230   HYDROGUI_DataModel* aModel = theModule->getDataModel();
231
232   SUIT_SelectionMgr* aSelectionMgr = theModule->getApp()->selectionMgr();
233   SUIT_DataOwnerPtrList anOwners;
234   aSelectionMgr->selected( anOwners );
235
236   if( anOwners.size() != 1 )
237     return KIND_UNKNOWN;
238
239   if( LightApp_DataOwner* anOwner = dynamic_cast<LightApp_DataOwner*>( anOwners.first().operator->() ) )
240   {
241     QString anEntry = anOwner->entry();
242     QString aPrefix = HYDROGUI_DataObject::entryPrefix();
243     if( anEntry.left( aPrefix.length() ) == aPrefix )
244     {
245       anEntry.remove( aPrefix );
246       for( ObjectKind anObjectKind = KIND_UNKNOWN + 1; anObjectKind <= KIND_LAST; anObjectKind++ )
247         if( HYDROGUI_DataModel::tr( HYDROGUI_DataModel::partitionName( anObjectKind ).toAscii() ) == anEntry )
248           return anObjectKind;
249     }
250   }
251   return KIND_UNKNOWN;
252 }
253
254
255
256
257 QStringList HYDROGUI_Tool::GetSelectedGeomObjects( HYDROGUI_Module* theModule,
258                                                    QList<GEOM::shape_type> theTypes )
259 {
260   QStringList anEntryList;
261
262   // Get active SalomeApp_Study
263   SalomeApp_Study* aStudy = NULL;
264   if ( theModule && theModule->getApp() ) {
265     aStudy = dynamic_cast<SalomeApp_Study*>( theModule->getApp()->activeStudy() );
266   }
267   if ( !aStudy ) {
268     return anEntryList;
269   }
270
271   // Get selection
272   SUIT_SelectionMgr* aSelectionMgr = theModule->getApp()->selectionMgr();
273   SUIT_DataOwnerPtrList anOwners;
274   aSelectionMgr->selected( anOwners );
275
276   // Check if the selected objects belong to GEOM and have a shape
277   foreach( SUIT_DataOwner* aSUITOwner, anOwners )
278   {
279     if( LightApp_DataOwner* anOwner = dynamic_cast<LightApp_DataOwner*>( aSUITOwner ) )
280     {
281       QString anEntry = anOwner->entry();
282       _PTR(SObject) aSObject( aStudy->studyDS()->FindObjectID(qPrintable(anEntry)) );
283       if (aSObject) {
284          _PTR(SComponent) aSComponent = aSObject->GetFatherComponent();
285         if ( aSComponent && aSComponent->ComponentDataType() == "GEOM" ) {
286           GEOM::GEOM_Object_var aGeomObj = 
287             GEOMBase::GetObjectFromIOR( aSObject->GetIOR().c_str() );
288
289           if ( !aGeomObj->_is_nil() && aGeomObj->IsShape() && 
290                theTypes.contains( aGeomObj->GetShapeType() ) ) {
291             anEntryList << anEntry;
292           }
293         }
294       }
295     }
296   }
297
298   return anEntryList;
299 }
300
301 Handle(HYDROData_Entity) HYDROGUI_Tool::FindObjectByName( HYDROGUI_Module* theModule,
302                                                           const QString&   theName,
303                                                           const ObjectKind theObjectKind )
304 {
305   Handle(HYDROData_Entity) aResObj;
306   
307   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
308   if ( !aDocument.IsNull() )
309     aResObj = aDocument->FindObjectByName( theName, theObjectKind );
310   
311   return aResObj;
312 }
313
314 HYDROData_SequenceOfObjects HYDROGUI_Tool::FindObjectsByNames( HYDROGUI_Module*   theModule,
315                                                                const QStringList& theNames,
316                                                                const ObjectKind   theObjectKind )
317 {
318   HYDROData_SequenceOfObjects aResSeq;
319
320   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
321   if ( !aDocument.IsNull() )
322     aResSeq = aDocument->FindObjectsByNames( theNames, theObjectKind );
323
324   return aResSeq;
325 }
326
327 QString HYDROGUI_Tool::GenerateObjectName( HYDROGUI_Module*   theModule,
328                                            const QString&     thePrefix,
329                                            const QStringList& theUsedNames,
330                                            const bool         theIsTryToUsePurePrefix)
331 {
332   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
333   return HYDROData_Tool::GenerateObjectName( aDocument, thePrefix, theUsedNames, theIsTryToUsePurePrefix );
334 }
335
336 size_t HYDROGUI_Tool::GetActiveViewId( HYDROGUI_Module* theModule,
337                                        const QString&   theViewId )
338 {
339   size_t aViewId = 0;
340   SUIT_ViewManager* aViewMgr = theModule->getApp()->activeViewManager();
341   if( !aViewMgr || ( !theViewId.isEmpty() && aViewMgr->getType() != theViewId ) )
342     return aViewId;
343
344   if( SUIT_ViewModel* aViewer = aViewMgr->getViewModel() )
345     aViewId = (size_t)aViewer;
346   return aViewId;
347 }
348
349 size_t HYDROGUI_Tool::GetActiveGraphicsViewId( HYDROGUI_Module* theModule )
350 {
351   return GetActiveViewId( theModule, GraphicsView_Viewer::Type() );
352 }
353
354 size_t HYDROGUI_Tool::GetActiveOCCViewId( HYDROGUI_Module* theModule )
355 {
356   return GetActiveViewId( theModule, OCCViewer_Viewer::Type() );
357 }
358
359 QList<size_t> getViewIdList( HYDROGUI_Module* theModule,
360                              const QString&   theViewId )
361 {
362   QList<size_t> aList;
363   ViewManagerList aViewMgrs;
364   theModule->getApp()->viewManagers( theViewId, aViewMgrs );
365   QListIterator<SUIT_ViewManager*> anIter( aViewMgrs );
366   while( anIter.hasNext() )
367   {
368     if( SUIT_ViewManager* aViewMgr = anIter.next() )
369     {
370       if( SUIT_ViewModel* aViewer = aViewMgr->getViewModel() )
371         aList.append( (size_t)aViewer );
372     }
373   }
374   return aList;
375 }
376
377 QList<size_t> HYDROGUI_Tool::GetGraphicsViewIdList( HYDROGUI_Module* theModule )
378 {
379   return getViewIdList( theModule, GraphicsView_Viewer::Type() );
380 }
381
382 QList<size_t> HYDROGUI_Tool::GetOCCViewIdList( HYDROGUI_Module* theModule )
383 {
384   return getViewIdList( theModule, OCCViewer_Viewer::Type() );
385 }
386
387 void HYDROGUI_Tool::setOCCActionShown( OCCViewer_ViewFrame* theViewFrame,
388                                        const int theActionId,
389                                        const bool isShown )
390 {
391   if ( !theViewFrame )
392     return;
393
394   OCCViewer_ViewWindow* aView = theViewFrame->getView( OCCViewer_ViewFrame::MAIN_VIEW );
395   if ( aView ) {
396     aView->toolMgr()->setShown( theActionId, isShown );
397     if ( theActionId == OCCViewer_ViewWindow::MaximizedId )
398       theViewFrame->onMaximizedView( aView, true );
399   }
400 }
401
402 void HYDROGUI_Tool::setOCCActionShown( HYDROGUI_Module* theModule,
403                                        const int theActionId,
404                                        const bool isShown )
405 {
406   QList<size_t> aList;
407   ViewManagerList aViewMgrs;
408   theModule->getApp()->viewManagers( OCCViewer_Viewer::Type(), aViewMgrs );
409   QListIterator<SUIT_ViewManager*> anIter( aViewMgrs );
410   while( anIter.hasNext() )
411   {
412     if( SUIT_ViewManager* aViewMgr = anIter.next() )
413     {
414       OCCViewer_ViewFrame* aViewFrame = dynamic_cast<OCCViewer_ViewFrame*>
415                                                            ( aViewMgr->getActiveView() );
416       if ( aViewFrame )
417         setOCCActionShown( aViewFrame, theActionId, isShown );
418     }
419   }
420 }
421
422
423
424 QColor HYDROGUI_Tool::GenerateFillingColor( HYDROGUI_Module*   theModule,
425                                             const QStringList& theZoneNames )
426 {
427   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
428   return GenerateFillingColor( aDocument, theZoneNames );
429 }
430
431 QColor HYDROGUI_Tool::GenerateFillingColor( const Handle(HYDROData_Document)& theDoc,
432                                             const QStringList&                theZoneNames )
433 {
434   QColor aFillingColor( Qt::darkBlue );
435
436   int aCounter = 0;
437   int aR = 0, aG = 0, aB = 0;
438   QStringListIterator aZoneNameIter( theZoneNames );
439   while( aZoneNameIter.hasNext() )
440   {
441     const QString& aZoneName = aZoneNameIter.next();
442     Handle(HYDROData_ImmersibleZone) aRefZone = 
443       Handle(HYDROData_ImmersibleZone)::DownCast( theDoc->FindObjectByName( aZoneName, KIND_IMMERSIBLE_ZONE ) );
444     if( !aRefZone.IsNull() )
445     {
446       QColor aRefColor = aRefZone->GetFillingColor();
447       aR += aRefColor.red();
448       aG += aRefColor.green();
449       aB += aRefColor.blue();
450       aCounter++;
451     }
452   }
453   
454   if( aCounter > 0 )
455   {
456     aFillingColor = QColor( aR / aCounter, aG / aCounter, aB / aCounter );
457   }
458
459   return aFillingColor;
460 }
461
462
463
464 void HYDROGUI_Tool::DeleteGeomObjects( HYDROGUI_Module* theModule, const QStringList& theEntries )
465 {
466   QStringList anEntryList;
467
468   // Get active SalomeApp_Study
469   SalomeApp_Study* aStudy = NULL;
470   if ( theModule && theModule->getApp() ) {
471     aStudy = dynamic_cast<SalomeApp_Study*>( theModule->getApp()->activeStudy() );
472   }
473   if ( !aStudy ) {
474     return;
475   }
476  
477   // Get GEOM engine
478   GEOM::GEOM_Gen_var aGeomEngine = GeometryGUI::GetGeomGen();
479   if ( aGeomEngine->_is_nil() ) {
480     return;
481   }
482   
483   // Delete GEOM objects
484   _PTR(StudyBuilder) aStudyBuilder( aStudy->studyDS()->NewBuilder() );
485   foreach ( const QString anEntry, theEntries ) {
486     _PTR(SObject) aSObject( aStudy->studyDS()->FindObjectID( qPrintable(anEntry) ) );
487     if ( aSObject ) {
488       GEOM::GEOM_Object_var aGeomObj = 
489         GEOMBase::GetObjectFromIOR( aSObject->GetIOR().c_str() );
490
491       if ( !aGeomObj->_is_nil() ) {
492         aGeomEngine->RemoveObject( aGeomObj );
493       }
494
495       aStudyBuilder->RemoveObject( aSObject );
496     }
497   }
498 }