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