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_LAND_COVER_MAP
115 || anObjectKind == KIND_SHAPES_GROUP ||
116 anObjectKind == KIND_SPLIT_GROUP
124 if ( theViewerType.isEmpty() || theViewerType == SVTK_Viewer::Type() )
126 if ( anObjectKind == KIND_BATHYMETRY )
130 if ( theViewerType.isEmpty() || theViewerType == GraphicsView_Viewer::Type() )
132 if ( anObjectKind == KIND_IMAGE ||
133 anObjectKind == KIND_POLYLINEXY )
140 void HYDROGUI_Tool::GetPrsSubObjects( HYDROGUI_Module* theModule,
141 HYDROData_SequenceOfObjects& theSeq )
143 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
144 if( !aDocument.IsNull() )
146 HYDROData_Iterator anIterator( aDocument );
147 for( ; anIterator.More(); anIterator.Next() )
149 Handle(HYDROData_Entity) anObject = anIterator.Current();
150 if ( !IsObjectHasPresentation( anObject ) )
153 theSeq.Append( anObject );
158 HYDROGUI_Prs* HYDROGUI_Tool::GetPresentation( const Handle(HYDROData_Entity)& theObj,
159 const GraphicsView_ObjectList& theObjects )
161 if( !theObj.IsNull() )
163 GraphicsView_ObjectListIterator anIter( theObjects );
164 while( anIter.hasNext() )
166 if( HYDROGUI_Prs* aPrs = dynamic_cast<HYDROGUI_Prs*>( anIter.next() ) )
168 Handle(HYDROData_Entity) anObj = aPrs->getObject();
169 if( IsEqual( anObj, theObj ) )
177 GraphicsView_ObjectList HYDROGUI_Tool::GetPrsList( GraphicsView_ViewPort* theViewPort )
179 GraphicsView_ObjectList aList;
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 );
192 HYDROData_SequenceOfObjects HYDROGUI_Tool::GetSelectedObjects( HYDROGUI_Module* theModule )
194 HYDROData_SequenceOfObjects aSeq;
196 HYDROGUI_DataModel* aModel = theModule->getDataModel();
198 SUIT_SelectionMgr* aSelectionMgr = theModule->getApp()->selectionMgr();
199 SUIT_DataOwnerPtrList anOwners;
200 aSelectionMgr->selected( anOwners );
202 QStringList aCollectedNameList; // to avoid duplication
203 foreach( SUIT_DataOwner* aSUITOwner, anOwners )
205 if( LightApp_DataOwner* anOwner = dynamic_cast<LightApp_DataOwner*>( aSUITOwner ) )
207 Handle(HYDROData_Entity) anObject = aModel->objectByEntry( anOwner->entry() );
208 if( !anObject.IsNull() )
210 QString aName = anObject->GetName();
211 if( !aCollectedNameList.contains( aName ) )
213 aSeq.Append( anObject );
214 aCollectedNameList.append( aName );
222 Handle(HYDROData_Entity) HYDROGUI_Tool::GetSelectedObject( HYDROGUI_Module* theModule )
224 HYDROData_SequenceOfObjects aSeq = GetSelectedObjects( theModule );
225 if( !aSeq.IsEmpty() )
230 HYDROData_SequenceOfObjects HYDROGUI_Tool::GetGeometryObjects( HYDROGUI_Module* theModule )
232 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
234 HYDROData_SequenceOfObjects aResSeq;
236 HYDROData_Iterator anIter( aDocument );
237 for ( ; anIter.More(); anIter.Next() )
239 Handle(HYDROData_Entity) anObj = anIter.Current();
240 if ( !HYDROData_Tool::IsGeometryObject( anObj ) )
243 aResSeq.Append( anObj );
249 ObjectKind HYDROGUI_Tool::GetSelectedPartition( HYDROGUI_Module* theModule )
251 HYDROGUI_DataModel* aModel = theModule->getDataModel();
253 SUIT_SelectionMgr* aSelectionMgr = theModule->getApp()->selectionMgr();
254 SUIT_DataOwnerPtrList anOwners;
255 aSelectionMgr->selected( anOwners );
257 if( anOwners.size() != 1 )
260 if( LightApp_DataOwner* anOwner = dynamic_cast<LightApp_DataOwner*>( anOwners.first().operator->() ) )
262 QString anEntry = anOwner->entry();
263 QString aPrefix = HYDROGUI_DataObject::entryPrefix();
264 if( anEntry.left( aPrefix.length() ) == aPrefix )
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 )
275 bool HYDROGUI_Tool::IsSelectedPartOfCalcCase( HYDROGUI_Module* theModule, Handle(HYDROData_CalculationCase)& theOutCalCase,
278 HYDROGUI_DataModel* aModel = theModule->getDataModel();
279 SUIT_SelectionMgr* aSelectionMgr = theModule->getApp()->selectionMgr();
280 SUIT_DataOwnerPtrList anOwners;
281 aSelectionMgr->selected( anOwners );
283 if( anOwners.size() != 1 )
286 LightApp_DataOwner* anOwner = dynamic_cast<LightApp_DataOwner*>( anOwners.first().operator->() );
290 QString anEntry = anOwner->entry();
291 QString aHydroPref = "_" + HYDROGUI_DataObject::entryPrefix();
293 QString aPostFixBoundary = aHydroPref + HYDROGUI_DataModel::tr("CASE_BOUNDARY");
294 if (GetCalCaseSubT(anEntry, anEntry, aPostFixBoundary, aModel, theOutCalCase ))
296 theOutPart = HYDROGUI_DataModel::tr("CASE_BOUNDARY");
297 theOutPart.toUpper();
301 QString aPostFixAO = aHydroPref +
302 HYDROGUI_DataModel::tr( HYDROGUI_DataModel::partitionName( KIND_ARTIFICIAL_OBJECT ).toLatin1().constData());
303 if (GetCalCaseSubT(anEntry, anEntry, aPostFixAO, aModel, theOutCalCase ))
305 aPostFixAO.remove(0, aHydroPref.length());
306 theOutPart = aPostFixAO;
310 QString aPostFixNO = aHydroPref +
311 HYDROGUI_DataModel::tr( HYDROGUI_DataModel::partitionName( KIND_NATURAL_OBJECT ).toLatin1().constData());
312 if (GetCalCaseSubT(anEntry, anEntry, aPostFixNO, aModel, theOutCalCase ))
314 aPostFixNO.remove(0, aHydroPref.length());
315 theOutPart = aPostFixNO;
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 ))
323 aPostFixLCM.remove(0, aHydroPref.length());
324 theOutPart = aPostFixLCM;
328 QString aPostFixReg = aHydroPref +
329 HYDROGUI_DataModel::tr( HYDROGUI_DataModel::partitionName( KIND_REGION ).toLatin1().constData());
330 if (GetCalCaseSubT(anEntry, anEntry, aPostFixReg, aModel, theOutCalCase ))
332 aPostFixReg.remove(0, aHydroPref.length());
333 theOutPart = aPostFixReg;
341 QStringList HYDROGUI_Tool::GetSelectedGeomObjects( HYDROGUI_Module* theModule,
342 QList<GEOM::shape_type> theTypes )
344 QStringList anEntryList;
346 // Get active SalomeApp_Study
347 SalomeApp_Study* aStudy = NULL;
348 if ( theModule && theModule->getApp() ) {
349 aStudy = dynamic_cast<SalomeApp_Study*>( theModule->getApp()->activeStudy() );
356 SUIT_SelectionMgr* aSelectionMgr = theModule->getApp()->selectionMgr();
357 SUIT_DataOwnerPtrList anOwners;
358 aSelectionMgr->selected( anOwners );
360 // Check if the selected objects belong to GEOM and have a shape
361 foreach( SUIT_DataOwner* aSUITOwner, anOwners )
363 if( LightApp_DataOwner* anOwner = dynamic_cast<LightApp_DataOwner*>( aSUITOwner ) )
365 QString anEntry = anOwner->entry();
366 _PTR(SObject) aSObject( aStudy->studyDS()->FindObjectID(qPrintable(anEntry)) );
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() );
373 if ( !aGeomObj->_is_nil() && aGeomObj->IsShape() &&
374 theTypes.contains( aGeomObj->GetShapeType() ) ) {
375 anEntryList << anEntry;
385 Handle(HYDROData_Entity) HYDROGUI_Tool::FindObjectByName( HYDROGUI_Module* theModule,
386 const QString& theName,
387 const ObjectKind theObjectKind )
389 Handle(HYDROData_Entity) aResObj;
391 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
392 if ( !aDocument.IsNull() )
393 aResObj = aDocument->FindObjectByName( theName, theObjectKind );
398 HYDROData_SequenceOfObjects HYDROGUI_Tool::FindObjectsByNames( HYDROGUI_Module* theModule,
399 const QStringList& theNames,
400 const ObjectKind theObjectKind )
402 HYDROData_SequenceOfObjects aResSeq;
404 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
405 if ( !aDocument.IsNull() )
406 aResSeq = aDocument->FindObjectsByNames( theNames, theObjectKind );
411 QString HYDROGUI_Tool::GenerateObjectName( HYDROGUI_Module* theModule,
412 const QString& thePrefix,
413 const QStringList& theUsedNames,
414 const bool theIsTryToUsePurePrefix)
416 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
417 return HYDROData_Tool::GenerateObjectName( aDocument, thePrefix, theUsedNames, theIsTryToUsePurePrefix );
420 size_t HYDROGUI_Tool::GetActiveViewId( HYDROGUI_Module* theModule,
421 const QString& theViewId )
424 SUIT_ViewManager* aViewMgr = theModule->getApp()->activeViewManager();
425 if( !aViewMgr || ( !theViewId.isEmpty() && aViewMgr->getType() != theViewId ) )
428 if( SUIT_ViewModel* aViewer = aViewMgr->getViewModel() )
429 aViewId = (size_t)aViewer;
433 size_t HYDROGUI_Tool::GetActiveGraphicsViewId( HYDROGUI_Module* theModule )
435 return GetActiveViewId( theModule, GraphicsView_Viewer::Type() );
438 size_t HYDROGUI_Tool::GetActiveOCCViewId( HYDROGUI_Module* theModule )
440 return GetActiveViewId( theModule, OCCViewer_Viewer::Type() );
443 QList<size_t> getViewIdList( HYDROGUI_Module* theModule,
444 const QString& theViewId )
447 ViewManagerList aViewMgrs;
448 theModule->getApp()->viewManagers( theViewId, aViewMgrs );
449 QListIterator<SUIT_ViewManager*> anIter( aViewMgrs );
450 while( anIter.hasNext() )
452 if( SUIT_ViewManager* aViewMgr = anIter.next() )
454 if( SUIT_ViewModel* aViewer = aViewMgr->getViewModel() )
455 aList.append( (size_t)aViewer );
461 QList<size_t> HYDROGUI_Tool::GetGraphicsViewIdList( HYDROGUI_Module* theModule )
463 return getViewIdList( theModule, GraphicsView_Viewer::Type() );
466 QList<size_t> HYDROGUI_Tool::GetOCCViewIdList( HYDROGUI_Module* theModule )
468 return getViewIdList( theModule, OCCViewer_Viewer::Type() );
471 void HYDROGUI_Tool::setOCCActionShown( OCCViewer_ViewFrame* theViewFrame,
472 const int theActionId,
478 OCCViewer_ViewWindow* aView = theViewFrame->getView( OCCViewer_ViewFrame::MAIN_VIEW );
480 aView->toolMgr()->setShown( theActionId, isShown );
481 if ( theActionId == OCCViewer_ViewWindow::MaximizedId )
482 theViewFrame->onMaximizedView( aView, true );
486 void HYDROGUI_Tool::setOCCActionShown( HYDROGUI_Module* theModule,
487 const int theActionId,
491 ViewManagerList aViewMgrs;
492 theModule->getApp()->viewManagers( OCCViewer_Viewer::Type(), aViewMgrs );
493 QListIterator<SUIT_ViewManager*> anIter( aViewMgrs );
494 while( anIter.hasNext() )
496 if( SUIT_ViewManager* aViewMgr = anIter.next() )
498 OCCViewer_ViewFrame* aViewFrame = dynamic_cast<OCCViewer_ViewFrame*>
499 ( aViewMgr->getActiveView() );
501 setOCCActionShown( aViewFrame, theActionId, isShown );
508 QColor HYDROGUI_Tool::GenerateFillingColor( HYDROGUI_Module* theModule,
509 const QStringList& theZoneNames )
511 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
512 return GenerateFillingColor( aDocument, theZoneNames );
515 QColor HYDROGUI_Tool::GenerateFillingColor( const Handle(HYDROData_Document)& theDoc,
516 const QStringList& theZoneNames )
518 QColor aFillingColor( Qt::darkBlue );
521 int aR = 0, aG = 0, aB = 0;
522 QStringListIterator aZoneNameIter( theZoneNames );
523 while( aZoneNameIter.hasNext() )
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() )
530 QColor aRefColor = aRefZone->GetFillingColor();
531 aR += aRefColor.red();
532 aG += aRefColor.green();
533 aB += aRefColor.blue();
540 aFillingColor = QColor( aR / aCounter, aG / aCounter, aB / aCounter );
543 return aFillingColor;
548 void HYDROGUI_Tool::DeleteGeomObjects( HYDROGUI_Module* theModule, const QStringList& theEntries )
550 QStringList anEntryList;
552 // Get active SalomeApp_Study
553 SalomeApp_Study* aStudy = NULL;
554 if ( theModule && theModule->getApp() ) {
555 aStudy = dynamic_cast<SalomeApp_Study*>( theModule->getApp()->activeStudy() );
562 GEOM::GEOM_Gen_var aGeomEngine = GeometryGUI::GetGeomGen();
563 if ( aGeomEngine->_is_nil() ) {
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) ) );
572 GEOM::GEOM_Object_var aGeomObj =
573 GEOMBase::GetObjectFromIOR( aSObject->GetIOR().c_str() );
575 if ( !aGeomObj->_is_nil() ) {
576 aGeomEngine->RemoveObject( aGeomObj );
579 aStudyBuilder->RemoveObject( aSObject );