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