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.
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.
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
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
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>
29 #include <GeometryGUI.h>
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>
48 int HYDROGUI_Tool::GetActiveStudyId()
50 if( SUIT_Session* aSession = SUIT_Session::session() )
51 if( SUIT_Application* anApp = aSession->activeApplication() )
52 if( SUIT_Study* aStudy = anApp->activeStudy() )
57 static bool GetCalCaseSubT(const QString& aHydroPref,
58 const QString& anEntry,
59 const QString& SubTPostFix,
60 HYDROGUI_DataModel* aModel,
61 Handle(HYDROData_CalculationCase)& theOutCalCase)
63 int aFiB = anEntry.lastIndexOf(SubTPostFix, -1, Qt::CaseInsensitive);
66 QString RightTruncEntry = anEntry.left(anEntry.length() - SubTPostFix.length());
67 Handle(HYDROData_CalculationCase) CalCase =
68 Handle(HYDROData_CalculationCase)::DownCast (aModel->objectByEntry( RightTruncEntry, KIND_CALCULATION ));
71 theOutCalCase = CalCase;
80 void HYDROGUI_Tool::SetActiveViewManager( HYDROGUI_Module* theModule,
81 SUIT_ViewManager* 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 );
91 bool HYDROGUI_Tool::IsObjectHasPresentation( const Handle(HYDROData_Entity)& theObject,
92 const QString& theViewerType )
94 if ( theObject.IsNull() )
97 ObjectKind anObjectKind = theObject->GetKind();
98 if ( theViewerType.isEmpty() || theViewerType == OCCViewer_Viewer::Type() )
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
116 || anObjectKind == KIND_SHAPES_GROUP ||
117 anObjectKind == KIND_SPLIT_GROUP
125 if ( theViewerType.isEmpty() || theViewerType == SVTK_Viewer::Type() )
127 if ( anObjectKind == KIND_BATHYMETRY )
131 if ( theViewerType.isEmpty() || theViewerType == GraphicsView_Viewer::Type() )
133 if ( anObjectKind == KIND_IMAGE ||
134 anObjectKind == KIND_POLYLINEXY )
141 void HYDROGUI_Tool::GetPrsSubObjects( HYDROGUI_Module* theModule,
142 HYDROData_SequenceOfObjects& theSeq )
144 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
145 if( !aDocument.IsNull() )
147 HYDROData_Iterator anIterator( aDocument );
148 for( ; anIterator.More(); anIterator.Next() )
150 Handle(HYDROData_Entity) anObject = anIterator.Current();
151 if ( !IsObjectHasPresentation( anObject ) )
154 theSeq.Append( anObject );
159 HYDROGUI_Prs* HYDROGUI_Tool::GetPresentation( const Handle(HYDROData_Entity)& theObj,
160 const GraphicsView_ObjectList& theObjects )
162 if( !theObj.IsNull() )
164 GraphicsView_ObjectListIterator anIter( theObjects );
165 while( anIter.hasNext() )
167 if( HYDROGUI_Prs* aPrs = dynamic_cast<HYDROGUI_Prs*>( anIter.next() ) )
169 Handle(HYDROData_Entity) anObj = aPrs->getObject();
170 if( IsEqual( anObj, theObj ) )
178 GraphicsView_ObjectList HYDROGUI_Tool::GetPrsList( GraphicsView_ViewPort* theViewPort )
180 GraphicsView_ObjectList aList;
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 );
193 HYDROData_SequenceOfObjects HYDROGUI_Tool::GetSelectedObjects( HYDROGUI_Module* theModule )
195 HYDROData_SequenceOfObjects aSeq;
197 HYDROGUI_DataModel* aModel = theModule->getDataModel();
199 SUIT_SelectionMgr* aSelectionMgr = theModule->getApp()->selectionMgr();
200 SUIT_DataOwnerPtrList anOwners;
201 aSelectionMgr->selected( anOwners );
203 QStringList aCollectedNameList; // to avoid duplication
204 foreach( SUIT_DataOwner* aSUITOwner, anOwners )
206 if( LightApp_DataOwner* anOwner = dynamic_cast<LightApp_DataOwner*>( aSUITOwner ) )
208 Handle(HYDROData_Entity) anObject = aModel->objectByEntry( anOwner->entry() );
209 if( !anObject.IsNull() )
211 QString aName = anObject->GetName();
212 if( !aCollectedNameList.contains( aName ) )
214 aSeq.Append( anObject );
215 aCollectedNameList.append( aName );
223 Handle(HYDROData_Entity) HYDROGUI_Tool::GetSelectedObject( HYDROGUI_Module* theModule )
225 HYDROData_SequenceOfObjects aSeq = GetSelectedObjects( theModule );
226 if( !aSeq.IsEmpty() )
231 HYDROData_SequenceOfObjects HYDROGUI_Tool::GetGeometryObjects( HYDROGUI_Module* theModule )
233 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
235 HYDROData_SequenceOfObjects aResSeq;
237 HYDROData_Iterator anIter( aDocument );
238 for ( ; anIter.More(); anIter.Next() )
240 Handle(HYDROData_Entity) anObj = anIter.Current();
241 if ( !HYDROData_Tool::IsGeometryObject( anObj ) )
244 aResSeq.Append( anObj );
250 ObjectKind HYDROGUI_Tool::GetSelectedPartition( HYDROGUI_Module* theModule )
252 HYDROGUI_DataModel* aModel = theModule->getDataModel();
254 SUIT_SelectionMgr* aSelectionMgr = theModule->getApp()->selectionMgr();
255 SUIT_DataOwnerPtrList anOwners;
256 aSelectionMgr->selected( anOwners );
258 if( anOwners.size() != 1 )
261 if( LightApp_DataOwner* anOwner = dynamic_cast<LightApp_DataOwner*>( anOwners.first().operator->() ) )
263 QString anEntry = anOwner->entry();
264 QString aPrefix = HYDROGUI_DataObject::entryPrefix();
265 if( anEntry.left( aPrefix.length() ) == aPrefix )
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 )
276 bool HYDROGUI_Tool::IsSelectedPartOfCalcCase( HYDROGUI_Module* theModule, Handle(HYDROData_CalculationCase)& theOutCalCase,
279 HYDROGUI_DataModel* aModel = theModule->getDataModel();
280 SUIT_SelectionMgr* aSelectionMgr = theModule->getApp()->selectionMgr();
281 SUIT_DataOwnerPtrList anOwners;
282 aSelectionMgr->selected( anOwners );
284 if( anOwners.size() != 1 )
287 LightApp_DataOwner* anOwner = dynamic_cast<LightApp_DataOwner*>( anOwners.first().operator->() );
291 QString anEntry = anOwner->entry();
292 QString aHydroPref = "_" + HYDROGUI_DataObject::entryPrefix();
294 QString aPostFixBoundary = aHydroPref + HYDROGUI_DataModel::tr("CASE_BOUNDARY");
295 if (GetCalCaseSubT(anEntry, anEntry, aPostFixBoundary, aModel, theOutCalCase ))
297 theOutPart = HYDROGUI_DataModel::tr("CASE_BOUNDARY");
298 theOutPart.toUpper();
302 QString aPostFixAO = aHydroPref +
303 HYDROGUI_DataModel::tr( HYDROGUI_DataModel::partitionName( KIND_ARTIFICIAL_OBJECT ).toLatin1().constData());
304 if (GetCalCaseSubT(anEntry, anEntry, aPostFixAO, aModel, theOutCalCase ))
306 aPostFixAO.remove(0, aHydroPref.length());
307 theOutPart = aPostFixAO;
311 QString aPostFixNO = aHydroPref +
312 HYDROGUI_DataModel::tr( HYDROGUI_DataModel::partitionName( KIND_NATURAL_OBJECT ).toLatin1().constData());
313 if (GetCalCaseSubT(anEntry, anEntry, aPostFixNO, aModel, theOutCalCase ))
315 aPostFixNO.remove(0, aHydroPref.length());
316 theOutPart = aPostFixNO;
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 ))
324 aPostFixLCM.remove(0, aHydroPref.length());
325 theOutPart = aPostFixLCM;
329 QString aPostFixReg = aHydroPref +
330 HYDROGUI_DataModel::tr( HYDROGUI_DataModel::partitionName( KIND_REGION ).toLatin1().constData());
331 if (GetCalCaseSubT(anEntry, anEntry, aPostFixReg, aModel, theOutCalCase ))
333 aPostFixReg.remove(0, aHydroPref.length());
334 theOutPart = aPostFixReg;
342 QStringList HYDROGUI_Tool::GetSelectedGeomObjects( HYDROGUI_Module* theModule,
343 QList<GEOM::shape_type> theTypes )
345 QStringList anEntryList;
347 // Get active SalomeApp_Study
348 SalomeApp_Study* aStudy = NULL;
349 if ( theModule && theModule->getApp() ) {
350 aStudy = dynamic_cast<SalomeApp_Study*>( theModule->getApp()->activeStudy() );
357 SUIT_SelectionMgr* aSelectionMgr = theModule->getApp()->selectionMgr();
358 SUIT_DataOwnerPtrList anOwners;
359 aSelectionMgr->selected( anOwners );
361 // Check if the selected objects belong to GEOM and have a shape
362 foreach( SUIT_DataOwner* aSUITOwner, anOwners )
364 if( LightApp_DataOwner* anOwner = dynamic_cast<LightApp_DataOwner*>( aSUITOwner ) )
366 QString anEntry = anOwner->entry();
367 _PTR(SObject) aSObject( aStudy->studyDS()->FindObjectID(qPrintable(anEntry)) );
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() );
374 if ( !aGeomObj->_is_nil() && aGeomObj->IsShape() &&
375 theTypes.contains( aGeomObj->GetShapeType() ) ) {
376 anEntryList << anEntry;
386 Handle(HYDROData_Entity) HYDROGUI_Tool::FindObjectByName( HYDROGUI_Module* theModule,
387 const QString& theName,
388 const ObjectKind theObjectKind )
390 Handle(HYDROData_Entity) aResObj;
392 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
393 if ( !aDocument.IsNull() )
394 aResObj = aDocument->FindObjectByName( theName, theObjectKind );
399 HYDROData_SequenceOfObjects HYDROGUI_Tool::FindObjectsByNames( HYDROGUI_Module* theModule,
400 const QStringList& theNames,
401 const ObjectKind theObjectKind )
403 HYDROData_SequenceOfObjects aResSeq;
405 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
406 if ( !aDocument.IsNull() )
407 aResSeq = aDocument->FindObjectsByNames( theNames, theObjectKind );
412 QString HYDROGUI_Tool::GenerateObjectName( HYDROGUI_Module* theModule,
413 const QString& thePrefix,
414 const QStringList& theUsedNames,
415 const bool theIsTryToUsePurePrefix)
417 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
418 return HYDROData_Tool::GenerateObjectName( aDocument, thePrefix, theUsedNames, theIsTryToUsePurePrefix );
421 size_t HYDROGUI_Tool::GetActiveViewId( HYDROGUI_Module* theModule,
422 const QString& theViewId )
425 SUIT_ViewManager* aViewMgr = theModule->getApp()->activeViewManager();
426 if( !aViewMgr || ( !theViewId.isEmpty() && aViewMgr->getType() != theViewId ) )
429 if( SUIT_ViewModel* aViewer = aViewMgr->getViewModel() )
430 aViewId = (size_t)aViewer;
434 size_t HYDROGUI_Tool::GetActiveGraphicsViewId( HYDROGUI_Module* theModule )
436 return GetActiveViewId( theModule, GraphicsView_Viewer::Type() );
439 size_t HYDROGUI_Tool::GetActiveOCCViewId( HYDROGUI_Module* theModule )
441 return GetActiveViewId( theModule, OCCViewer_Viewer::Type() );
444 QList<size_t> getViewIdList( HYDROGUI_Module* theModule,
445 const QString& theViewId )
448 ViewManagerList aViewMgrs;
449 theModule->getApp()->viewManagers( theViewId, aViewMgrs );
450 QListIterator<SUIT_ViewManager*> anIter( aViewMgrs );
451 while( anIter.hasNext() )
453 if( SUIT_ViewManager* aViewMgr = anIter.next() )
455 if( SUIT_ViewModel* aViewer = aViewMgr->getViewModel() )
456 aList.append( (size_t)aViewer );
462 QList<size_t> HYDROGUI_Tool::GetGraphicsViewIdList( HYDROGUI_Module* theModule )
464 return getViewIdList( theModule, GraphicsView_Viewer::Type() );
467 QList<size_t> HYDROGUI_Tool::GetOCCViewIdList( HYDROGUI_Module* theModule )
469 return getViewIdList( theModule, OCCViewer_Viewer::Type() );
472 void HYDROGUI_Tool::setOCCActionShown( OCCViewer_ViewFrame* theViewFrame,
473 const int theActionId,
479 OCCViewer_ViewWindow* aView = theViewFrame->getView( OCCViewer_ViewFrame::MAIN_VIEW );
481 aView->toolMgr()->setShown( theActionId, isShown );
482 if ( theActionId == OCCViewer_ViewWindow::MaximizedId )
483 theViewFrame->onMaximizedView( aView, true );
487 void HYDROGUI_Tool::setOCCActionShown( HYDROGUI_Module* theModule,
488 const int theActionId,
492 ViewManagerList aViewMgrs;
493 theModule->getApp()->viewManagers( OCCViewer_Viewer::Type(), aViewMgrs );
494 QListIterator<SUIT_ViewManager*> anIter( aViewMgrs );
495 while( anIter.hasNext() )
497 if( SUIT_ViewManager* aViewMgr = anIter.next() )
499 OCCViewer_ViewFrame* aViewFrame = dynamic_cast<OCCViewer_ViewFrame*>
500 ( aViewMgr->getActiveView() );
502 setOCCActionShown( aViewFrame, theActionId, isShown );
509 QColor HYDROGUI_Tool::GenerateFillingColor( HYDROGUI_Module* theModule,
510 const QStringList& theZoneNames )
512 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
513 return GenerateFillingColor( aDocument, theZoneNames );
516 QColor HYDROGUI_Tool::GenerateFillingColor( const Handle(HYDROData_Document)& theDoc,
517 const QStringList& theZoneNames )
519 QColor aFillingColor( Qt::darkBlue );
522 int aR = 0, aG = 0, aB = 0;
523 QStringListIterator aZoneNameIter( theZoneNames );
524 while( aZoneNameIter.hasNext() )
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() )
531 QColor aRefColor = aRefZone->GetFillingColor();
532 aR += aRefColor.red();
533 aG += aRefColor.green();
534 aB += aRefColor.blue();
541 aFillingColor = QColor( aR / aCounter, aG / aCounter, aB / aCounter );
544 return aFillingColor;
549 void HYDROGUI_Tool::DeleteGeomObjects( HYDROGUI_Module* theModule, const QStringList& theEntries )
551 QStringList anEntryList;
553 // Get active SalomeApp_Study
554 SalomeApp_Study* aStudy = NULL;
555 if ( theModule && theModule->getApp() ) {
556 aStudy = dynamic_cast<SalomeApp_Study*>( theModule->getApp()->activeStudy() );
563 GEOM::GEOM_Gen_var aGeomEngine = GeometryGUI::GetGeomGen();
564 if ( aGeomEngine->_is_nil() ) {
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) ) );
573 GEOM::GEOM_Object_var aGeomObj =
574 GEOMBase::GetObjectFromIOR( aSObject->GetIOR().c_str() );
576 if ( !aGeomObj->_is_nil() ) {
577 aGeomEngine->RemoveObject( aGeomObj );
580 aStudyBuilder->RemoveObject( aSObject );