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() )
59 void HYDROGUI_Tool::SetActiveViewManager( HYDROGUI_Module* theModule,
60 SUIT_ViewManager* 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 );
70 bool HYDROGUI_Tool::IsObjectHasPresentation( const Handle(HYDROData_Entity)& theObject,
71 const QString& theViewerType )
73 if ( theObject.IsNull() )
76 ObjectKind anObjectKind = theObject->GetKind();
77 if ( theViewerType.isEmpty() || theViewerType == OCCViewer_Viewer::Type() )
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
94 || anObjectKind == KIND_SHAPES_GROUP ||
95 anObjectKind == KIND_SPLIT_GROUP
103 if ( theViewerType.isEmpty() || theViewerType == SVTK_Viewer::Type() )
105 if ( anObjectKind == KIND_BATHYMETRY )
109 if ( theViewerType.isEmpty() || theViewerType == GraphicsView_Viewer::Type() )
111 if ( anObjectKind == KIND_IMAGE ||
112 anObjectKind == KIND_POLYLINEXY )
119 void HYDROGUI_Tool::GetPrsSubObjects( HYDROGUI_Module* theModule,
120 HYDROData_SequenceOfObjects& theSeq )
122 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
123 if( !aDocument.IsNull() )
125 HYDROData_Iterator anIterator( aDocument );
126 for( ; anIterator.More(); anIterator.Next() )
128 Handle(HYDROData_Entity) anObject = anIterator.Current();
129 if ( !IsObjectHasPresentation( anObject ) )
132 theSeq.Append( anObject );
137 HYDROGUI_Prs* HYDROGUI_Tool::GetPresentation( const Handle(HYDROData_Entity)& theObj,
138 const GraphicsView_ObjectList& theObjects )
140 if( !theObj.IsNull() )
142 GraphicsView_ObjectListIterator anIter( theObjects );
143 while( anIter.hasNext() )
145 if( HYDROGUI_Prs* aPrs = dynamic_cast<HYDROGUI_Prs*>( anIter.next() ) )
147 Handle(HYDROData_Entity) anObj = aPrs->getObject();
148 if( IsEqual( anObj, theObj ) )
156 GraphicsView_ObjectList HYDROGUI_Tool::GetPrsList( GraphicsView_ViewPort* theViewPort )
158 GraphicsView_ObjectList aList;
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 );
171 HYDROData_SequenceOfObjects HYDROGUI_Tool::GetSelectedObjects( HYDROGUI_Module* theModule )
173 HYDROData_SequenceOfObjects aSeq;
175 HYDROGUI_DataModel* aModel = theModule->getDataModel();
177 SUIT_SelectionMgr* aSelectionMgr = theModule->getApp()->selectionMgr();
178 SUIT_DataOwnerPtrList anOwners;
179 aSelectionMgr->selected( anOwners );
181 QStringList aCollectedNameList; // to avoid duplication
182 foreach( SUIT_DataOwner* aSUITOwner, anOwners )
184 if( LightApp_DataOwner* anOwner = dynamic_cast<LightApp_DataOwner*>( aSUITOwner ) )
186 Handle(HYDROData_Entity) anObject = aModel->objectByEntry( anOwner->entry() );
187 if( !anObject.IsNull() )
189 QString aName = anObject->GetName();
190 if( !aCollectedNameList.contains( aName ) )
192 aSeq.Append( anObject );
193 aCollectedNameList.append( aName );
201 Handle(HYDROData_Entity) HYDROGUI_Tool::GetSelectedObject( HYDROGUI_Module* theModule )
203 HYDROData_SequenceOfObjects aSeq = GetSelectedObjects( theModule );
204 if( !aSeq.IsEmpty() )
209 HYDROData_SequenceOfObjects HYDROGUI_Tool::GetGeometryObjects( HYDROGUI_Module* theModule )
211 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
213 HYDROData_SequenceOfObjects aResSeq;
215 HYDROData_Iterator anIter( aDocument );
216 for ( ; anIter.More(); anIter.Next() )
218 Handle(HYDROData_Entity) anObj = anIter.Current();
219 if ( !HYDROData_Tool::IsGeometryObject( anObj ) )
222 aResSeq.Append( anObj );
228 ObjectKind HYDROGUI_Tool::GetSelectedPartition( HYDROGUI_Module* theModule )
230 HYDROGUI_DataModel* aModel = theModule->getDataModel();
232 SUIT_SelectionMgr* aSelectionMgr = theModule->getApp()->selectionMgr();
233 SUIT_DataOwnerPtrList anOwners;
234 aSelectionMgr->selected( anOwners );
236 if( anOwners.size() != 1 )
239 if( LightApp_DataOwner* anOwner = dynamic_cast<LightApp_DataOwner*>( anOwners.first().operator->() ) )
241 QString anEntry = anOwner->entry();
242 QString aPrefix = HYDROGUI_DataObject::entryPrefix();
243 if( anEntry.left( aPrefix.length() ) == aPrefix )
245 anEntry.remove( aPrefix );
246 for( ObjectKind anObjectKind = KIND_UNKNOWN + 1; anObjectKind <= KIND_LAST; anObjectKind++ )
247 if( HYDROGUI_DataModel::tr( HYDROGUI_DataModel::partitionName( anObjectKind ).toLatin1() ) == anEntry )
257 QStringList HYDROGUI_Tool::GetSelectedGeomObjects( HYDROGUI_Module* theModule,
258 QList<GEOM::shape_type> theTypes )
260 QStringList anEntryList;
262 // Get active SalomeApp_Study
263 SalomeApp_Study* aStudy = NULL;
264 if ( theModule && theModule->getApp() ) {
265 aStudy = dynamic_cast<SalomeApp_Study*>( theModule->getApp()->activeStudy() );
272 SUIT_SelectionMgr* aSelectionMgr = theModule->getApp()->selectionMgr();
273 SUIT_DataOwnerPtrList anOwners;
274 aSelectionMgr->selected( anOwners );
276 // Check if the selected objects belong to GEOM and have a shape
277 foreach( SUIT_DataOwner* aSUITOwner, anOwners )
279 if( LightApp_DataOwner* anOwner = dynamic_cast<LightApp_DataOwner*>( aSUITOwner ) )
281 QString anEntry = anOwner->entry();
282 _PTR(SObject) aSObject( aStudy->studyDS()->FindObjectID(qPrintable(anEntry)) );
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() );
289 if ( !aGeomObj->_is_nil() && aGeomObj->IsShape() &&
290 theTypes.contains( aGeomObj->GetShapeType() ) ) {
291 anEntryList << anEntry;
301 Handle(HYDROData_Entity) HYDROGUI_Tool::FindObjectByName( HYDROGUI_Module* theModule,
302 const QString& theName,
303 const ObjectKind theObjectKind )
305 Handle(HYDROData_Entity) aResObj;
307 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
308 if ( !aDocument.IsNull() )
309 aResObj = aDocument->FindObjectByName( theName, theObjectKind );
314 HYDROData_SequenceOfObjects HYDROGUI_Tool::FindObjectsByNames( HYDROGUI_Module* theModule,
315 const QStringList& theNames,
316 const ObjectKind theObjectKind )
318 HYDROData_SequenceOfObjects aResSeq;
320 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
321 if ( !aDocument.IsNull() )
322 aResSeq = aDocument->FindObjectsByNames( theNames, theObjectKind );
327 QString HYDROGUI_Tool::GenerateObjectName( HYDROGUI_Module* theModule,
328 const QString& thePrefix,
329 const QStringList& theUsedNames,
330 const bool theIsTryToUsePurePrefix)
332 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
333 return HYDROData_Tool::GenerateObjectName( aDocument, thePrefix, theUsedNames, theIsTryToUsePurePrefix );
336 size_t HYDROGUI_Tool::GetActiveViewId( HYDROGUI_Module* theModule,
337 const QString& theViewId )
340 SUIT_ViewManager* aViewMgr = theModule->getApp()->activeViewManager();
341 if( !aViewMgr || ( !theViewId.isEmpty() && aViewMgr->getType() != theViewId ) )
344 if( SUIT_ViewModel* aViewer = aViewMgr->getViewModel() )
345 aViewId = (size_t)aViewer;
349 size_t HYDROGUI_Tool::GetActiveGraphicsViewId( HYDROGUI_Module* theModule )
351 return GetActiveViewId( theModule, GraphicsView_Viewer::Type() );
354 size_t HYDROGUI_Tool::GetActiveOCCViewId( HYDROGUI_Module* theModule )
356 return GetActiveViewId( theModule, OCCViewer_Viewer::Type() );
359 QList<size_t> getViewIdList( HYDROGUI_Module* theModule,
360 const QString& theViewId )
363 ViewManagerList aViewMgrs;
364 theModule->getApp()->viewManagers( theViewId, aViewMgrs );
365 QListIterator<SUIT_ViewManager*> anIter( aViewMgrs );
366 while( anIter.hasNext() )
368 if( SUIT_ViewManager* aViewMgr = anIter.next() )
370 if( SUIT_ViewModel* aViewer = aViewMgr->getViewModel() )
371 aList.append( (size_t)aViewer );
377 QList<size_t> HYDROGUI_Tool::GetGraphicsViewIdList( HYDROGUI_Module* theModule )
379 return getViewIdList( theModule, GraphicsView_Viewer::Type() );
382 QList<size_t> HYDROGUI_Tool::GetOCCViewIdList( HYDROGUI_Module* theModule )
384 return getViewIdList( theModule, OCCViewer_Viewer::Type() );
387 void HYDROGUI_Tool::setOCCActionShown( OCCViewer_ViewFrame* theViewFrame,
388 const int theActionId,
394 OCCViewer_ViewWindow* aView = theViewFrame->getView( OCCViewer_ViewFrame::MAIN_VIEW );
396 aView->toolMgr()->setShown( theActionId, isShown );
397 if ( theActionId == OCCViewer_ViewWindow::MaximizedId )
398 theViewFrame->onMaximizedView( aView, true );
402 void HYDROGUI_Tool::setOCCActionShown( HYDROGUI_Module* theModule,
403 const int theActionId,
407 ViewManagerList aViewMgrs;
408 theModule->getApp()->viewManagers( OCCViewer_Viewer::Type(), aViewMgrs );
409 QListIterator<SUIT_ViewManager*> anIter( aViewMgrs );
410 while( anIter.hasNext() )
412 if( SUIT_ViewManager* aViewMgr = anIter.next() )
414 OCCViewer_ViewFrame* aViewFrame = dynamic_cast<OCCViewer_ViewFrame*>
415 ( aViewMgr->getActiveView() );
417 setOCCActionShown( aViewFrame, theActionId, isShown );
424 QColor HYDROGUI_Tool::GenerateFillingColor( HYDROGUI_Module* theModule,
425 const QStringList& theZoneNames )
427 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
428 return GenerateFillingColor( aDocument, theZoneNames );
431 QColor HYDROGUI_Tool::GenerateFillingColor( const Handle(HYDROData_Document)& theDoc,
432 const QStringList& theZoneNames )
434 QColor aFillingColor( Qt::darkBlue );
437 int aR = 0, aG = 0, aB = 0;
438 QStringListIterator aZoneNameIter( theZoneNames );
439 while( aZoneNameIter.hasNext() )
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() )
446 QColor aRefColor = aRefZone->GetFillingColor();
447 aR += aRefColor.red();
448 aG += aRefColor.green();
449 aB += aRefColor.blue();
456 aFillingColor = QColor( aR / aCounter, aG / aCounter, aB / aCounter );
459 return aFillingColor;
464 void HYDROGUI_Tool::DeleteGeomObjects( HYDROGUI_Module* theModule, const QStringList& theEntries )
466 QStringList anEntryList;
468 // Get active SalomeApp_Study
469 SalomeApp_Study* aStudy = NULL;
470 if ( theModule && theModule->getApp() ) {
471 aStudy = dynamic_cast<SalomeApp_Study*>( theModule->getApp()->activeStudy() );
478 GEOM::GEOM_Gen_var aGeomEngine = GeometryGUI::GetGeomGen();
479 if ( aGeomEngine->_is_nil() ) {
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) ) );
488 GEOM::GEOM_Object_var aGeomObj =
489 GEOMBase::GetObjectFromIOR( aSObject->GetIOR().c_str() );
491 if ( !aGeomObj->_is_nil() ) {
492 aGeomEngine->RemoveObject( aGeomObj );
495 aStudyBuilder->RemoveObject( aSObject );