1 // Copyright (C) 2007-2013 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 #include "HYDROGUI_Module.h"
26 #include "HYDROGUI_DataModel.h"
27 #include "HYDROGUI_Displayer.h"
28 #include "HYDROGUI_GVSelector.h"
29 #include "HYDROGUI_InputPanel.h"
30 #include "HYDROGUI_ObjSelector.h"
31 #include "HYDROGUI_Operations.h"
32 #include "HYDROGUI_PrsImage.h"
33 #include "HYDROGUI_Tool.h"
34 #include "HYDROGUI_UpdateFlags.h"
36 #include <HYDROData_Image.h>
38 #include <HYDROOperations_Factory.h>
40 #include <GraphicsView_ViewFrame.h>
41 #include <GraphicsView_ViewManager.h>
42 #include <GraphicsView_ViewPort.h>
43 #include <GraphicsView_Viewer.h>
45 #include <ImageComposer_CutOperator.h>
46 #include <ImageComposer_CropOperator.h>
47 #include <ImageComposer_FuseOperator.h>
49 #include <LightApp_Application.h>
50 #include <LightApp_GVSelector.h>
51 #include <LightApp_SelectionMgr.h>
52 #include <LightApp_UpdateFlags.h>
54 #include <SALOME_Event.h>
56 #include <SUIT_Desktop.h>
57 #include <SUIT_Study.h>
58 #include <SUIT_ViewManager.h>
61 #include <QApplication>
62 #include <QGraphicsSceneMouseEvent>
65 static int ViewManagerId = 0;
67 extern "C" HYDRO_EXPORT CAM_Module* createModule()
69 return new HYDROGUI_Module();
72 extern "C" HYDRO_EXPORT char* getModuleVersion()
74 return (char*)HYDRO_VERSION;
77 HYDROGUI_Module::HYDROGUI_Module()
78 : LightApp_Module( "HYDRO" ),
80 myIsUpdateEnabled( true )
84 HYDROGUI_Module::~HYDROGUI_Module()
88 int HYDROGUI_Module::getStudyId() const
90 LightApp_Application* anApp = getApp();
91 return anApp ? anApp->activeStudy()->id() : 0;
94 void HYDROGUI_Module::initialize( CAM_Application* theApp )
96 LightApp_Module::initialize( theApp );
99 createUndoRedoActions();
104 setMenuShown( false );
105 setToolShown( false );
107 myDisplayer = new HYDROGUI_Displayer( this );
110 bool HYDROGUI_Module::activateModule( SUIT_Study* theStudy )
112 bool aRes = LightApp_Module::activateModule( theStudy );
114 LightApp_Application* anApp = getApp();
115 SUIT_Desktop* aDesktop = anApp->desktop();
117 getApp()->setEditEnabled( false ); // hide SalomeApp copy/paste actions
119 setMenuShown( true );
120 setToolShown( true );
122 #ifndef DISABLE_PYCONSOLE
123 aDesktop->tabifyDockWidget( HYDROGUI_Tool::WindowDock( anApp->getWindow( LightApp_Application::WT_PyConsole ) ),
124 HYDROGUI_Tool::WindowDock( anApp->getWindow( LightApp_Application::WT_LogWindow ) ) );
129 updateCommandsStatus();
134 bool HYDROGUI_Module::deactivateModule( SUIT_Study* theStudy )
136 ViewManagerMapIterator anIter( myViewManagerMap );
137 while( anIter.hasNext() )
138 if( SUIT_ViewManager* aViewManager = anIter.next().value().first )
139 getApp()->removeViewManager( aViewManager );
140 myViewManagerMap.clear();
142 myObjectStateMap.clear();
144 // clear the data model's list of copying objects
145 HYDROGUI_DataModel::changeCopyingObjects( HYDROData_SequenceOfObjects() );
147 setMenuShown( false );
148 setToolShown( false );
150 getApp()->setEditEnabled( true ); // show SalomeApp copy/paste actions
152 return LightApp_Module::deactivateModule( theStudy );
155 void HYDROGUI_Module::windows( QMap<int, int>& theMap ) const
158 theMap.insert( LightApp_Application::WT_LogWindow, Qt::BottomDockWidgetArea );
159 #ifndef DISABLE_PYCONSOLE
160 theMap.insert( LightApp_Application::WT_PyConsole, Qt::BottomDockWidgetArea );
162 theMap.insert( LightApp_Application::WT_ObjectBrowser, Qt::LeftDockWidgetArea );
165 void HYDROGUI_Module::viewManagers( QStringList& theTypesList ) const
167 theTypesList << GraphicsView_Viewer::Type();
170 void HYDROGUI_Module::contextMenuPopup( const QString& theClient,
174 HYDROGUI_DataModel* aModel = getDataModel();
176 bool anIsObjectBrowser = theClient == "ObjectBrowser";
177 bool anIsGraphicsView = theClient == "GraphicsView";
178 if( !anIsObjectBrowser && !anIsGraphicsView )
181 size_t aViewId = HYDROGUI_Tool::GetActiveGraphicsViewId( this );
183 bool anIsSelectedDataObjects = false;
184 bool anIsVisibleInSelection = false;
185 bool anIsHiddenInSelection = false;
187 bool anIsImage = false;
188 bool anIsImportedImage = false;
189 bool anIsCompositeImage = false;
190 bool anIsFusedImage = false;
191 bool anIsCutImage = false;
192 bool anIsSplittedImage = false;
193 bool anIsMustBeUpdatedImage = false;
194 bool anIsPolyline = false;
195 bool anIsCalculation = false;
196 bool anIsZone = false;
197 bool anIsVisualState = false;
199 // check the selected data model objects
200 HYDROData_SequenceOfObjects aSeq = HYDROGUI_Tool::GetSelectedObjects( this );
201 for( Standard_Integer anIndex = 1, aLength = aSeq.Length(); anIndex <= aLength; anIndex++ )
203 Handle(HYDROData_Object) anObject = aSeq.Value( anIndex );
204 if( !anObject.IsNull() )
206 anIsSelectedDataObjects = true;
208 bool aVisibility = isObjectVisible( aViewId, anObject );
209 anIsVisibleInSelection |= aVisibility;
210 anIsHiddenInSelection |= !aVisibility;
212 if( anObject->GetKind() == KIND_IMAGE )
215 Handle(HYDROData_Image) anImage = Handle(HYDROData_Image)::DownCast( anObject );
216 if( !anImage.IsNull() )
218 anIsImportedImage = anImage->HasTrsfPoints() && !anImage->IsSelfSplitted();
219 anIsCompositeImage = anImage->NbReferences() > 0;
220 if( HYDROOperations_Factory* aFactory = HYDROOperations_Factory::Factory() )
222 if( ImageComposer_Operator* anOperator = aFactory->Operator( anImage ) )
224 QString anOperatorName = anOperator->name();
225 if( anOperatorName == ImageComposer_FuseOperator::Type() )
226 anIsFusedImage = true;
227 else if( anOperatorName == ImageComposer_CutOperator::Type() )
229 else if( anOperatorName == ImageComposer_CropOperator::Type() )
230 anIsSplittedImage = true;
233 anIsMustBeUpdatedImage = anImage->MustBeUpdated();
236 else if( anObject->GetKind() == KIND_POLYLINE )
238 else if( anObject->GetKind() == KIND_CALCULATION )
239 anIsCalculation = true;
240 else if( anObject->GetKind() == KIND_ZONE )
242 else if( anObject->GetKind() == KIND_VISUAL_STATE )
243 anIsVisualState = true;
247 // check the selected partitions
248 if( !anIsSelectedDataObjects && anIsObjectBrowser )
250 ObjectKind aSelectedPartition = HYDROGUI_Tool::GetSelectedPartition( this );
251 if( aSelectedPartition != KIND_UNKNOWN )
253 switch( aSelectedPartition )
256 theMenu->addAction( action( ImportImageId ) );
259 theMenu->addAction( action( CreatePolylineId ) );
261 case KIND_VISUAL_STATE:
262 theMenu->addAction( action( SaveVisualStateId ) );
264 case KIND_BATHYMETRY:
265 theMenu->addAction( action( ImportBathymetryId ) );
267 case KIND_CALCULATION:
268 theMenu->addAction( action( CreateCalculationId ) );
271 theMenu->addAction( action( CreateZoneId ) );
274 theMenu->addSeparator();
278 if( anIsSelectedDataObjects && anIsMustBeUpdatedImage )
280 theMenu->addAction( action( UpdateImageId ) );
281 theMenu->addSeparator();
284 if( anIsSelectedDataObjects && aSeq.Length() == 1 )
288 if( anIsImportedImage )
289 theMenu->addAction( action( EditImportedImageId ) );
290 else if( anIsCompositeImage )
293 theMenu->addAction( action( EditFusedImageId ) );
294 else if( anIsCutImage )
295 theMenu->addAction( action( EditCutImageId ) );
296 else if( anIsSplittedImage )
297 theMenu->addAction( action( EditSplittedImageId ) );
300 theMenu->addAction( action( ObserveImageId ) );
301 theMenu->addAction( action( ExportImageId ) );
302 theMenu->addSeparator();
304 theMenu->addAction( action( FuseImagesId ) );
305 theMenu->addAction( action( CutImagesId ) );
306 theMenu->addAction( action( SplitImageId ) );
307 theMenu->addSeparator();
309 else if( anIsPolyline )
311 theMenu->addAction( action( EditPolylineId ) );
312 theMenu->addSeparator();
314 else if( anIsCalculation )
316 theMenu->addAction( action( EditCalculationId ) );
317 theMenu->addSeparator();
321 theMenu->addAction( action( EditZoneId ) );
322 theMenu->addSeparator();
324 else if( anIsVisualState && anIsObjectBrowser )
326 theMenu->addAction( action( SaveVisualStateId ) );
327 theMenu->addAction( action( LoadVisualStateId ) );
328 theMenu->addSeparator();
332 if( anIsSelectedDataObjects )
334 theMenu->addAction( action( DeleteId ) );
335 theMenu->addSeparator();
338 if( anIsSelectedDataObjects && ( anIsImage || anIsPolyline || anIsZone ) )
340 if( anIsHiddenInSelection )
341 theMenu->addAction( action( ShowId ) );
342 theMenu->addAction( action( ShowOnlyId ) );
343 if( anIsVisibleInSelection )
344 theMenu->addAction( action( HideId ) );
345 theMenu->addSeparator();
348 if( anIsObjectBrowser || anIsGraphicsView )
350 theMenu->addAction( action( ShowAllId ) );
351 theMenu->addAction( action( HideAllId ) );
352 theMenu->addSeparator();
356 void HYDROGUI_Module::update( const int flags )
358 if( !isUpdateEnabled() )
361 QApplication::setOverrideCursor( Qt::WaitCursor );
363 // To prevent calling this method recursively
364 // from one of the methods called below
365 setUpdateEnabled( false );
367 if( ( flags & UF_Viewer ) )
368 updateGV( flags & UF_GV_Init,
369 flags & UF_GV_Forced );
371 if( ( flags & UF_Model ) && getDataModel() && getApp() )
373 getDataModel()->update( getStudyId() );
375 // Temporary workaround to prevent breaking
376 // the selection in the object browser.
377 // Note: processEvents() should be called after updateGV(),
378 // otherwise the application crashes from time to time.
379 qApp->processEvents();
380 getApp()->updateObjectBrowser( true );
383 // Object browser is currently updated by using UF_Model flag
384 //if( ( flags & UF_ObjBrowser ) && getApp() )
385 // getApp()->updateObjectBrowser( true );
387 if( ( flags & UF_Controls ) && getApp() )
388 getApp()->updateActions();
390 setUpdateEnabled( true );
392 QApplication::restoreOverrideCursor();
395 void HYDROGUI_Module::updateCommandsStatus()
397 LightApp_Module::updateCommandsStatus();
399 updateUndoRedoControls();
401 action( CopyId )->setEnabled( getDataModel()->canCopy() );
402 action( PasteId )->setEnabled( getDataModel()->canPaste() );
405 void HYDROGUI_Module::selectionChanged()
407 LightApp_Module::selectionChanged();
408 updateCommandsStatus();
411 HYDROGUI_DataModel* HYDROGUI_Module::getDataModel() const
413 return (HYDROGUI_DataModel*)dataModel();
416 HYDROGUI_Displayer* HYDROGUI_Module::getDisplayer() const
421 GraphicsView_Viewer* HYDROGUI_Module::getViewer( const int theId ) const
423 if( myViewManagerMap.contains( theId ) )
425 ViewManagerInfo anInfo = myViewManagerMap[ theId ];
426 GraphicsView_ViewManager* aViewManager =
427 dynamic_cast<GraphicsView_ViewManager*>( anInfo.first );
429 return aViewManager->getViewer();
434 int HYDROGUI_Module::getViewManagerId( SUIT_ViewManager* theViewManager )
436 ViewManagerMapIterator anIter( myViewManagerMap );
437 while( anIter.hasNext() )
439 int anId = anIter.next().key();
440 const ViewManagerInfo& anInfo = anIter.value();
441 if( anInfo.first == theViewManager )
447 HYDROGUI_Module::ViewManagerRole HYDROGUI_Module::getViewManagerRole( SUIT_ViewManager* theViewManager )
449 int anId = getViewManagerId( theViewManager );
452 const ViewManagerInfo& anInfo = myViewManagerMap[ anId ];
453 return anInfo.second;
458 void HYDROGUI_Module::setViewManagerRole( SUIT_ViewManager* theViewManager,
459 const ViewManagerRole theRole )
461 int anId = getViewManagerId( theViewManager );
464 ViewManagerInfo& anInfo = myViewManagerMap[ anId ];
465 anInfo.second = theRole;
469 bool HYDROGUI_Module::isObjectVisible( const int theViewId,
470 const Handle(HYDROData_Object)& theObject )
472 if( theObject.IsNull() )
475 ViewId2Name2ObjectStateMap::const_iterator anIter1 = myObjectStateMap.find( theViewId );
476 if( anIter1 != myObjectStateMap.end() )
478 const Name2ObjectStateMap& aName2ObjectStateMap = anIter1.value();
479 Name2ObjectStateMap::const_iterator anIter2 = aName2ObjectStateMap.find( theObject->GetName());
480 if( anIter2 != aName2ObjectStateMap.end() )
482 const ObjectState& anObjectState = anIter2.value();
483 return anObjectState.Visibility;
489 void HYDROGUI_Module::setObjectVisible( const int theViewId,
490 const Handle(HYDROData_Object)& theObject,
491 const bool theState )
493 if( !theObject.IsNull() )
495 Name2ObjectStateMap& aName2ObjectStateMap = myObjectStateMap[ theViewId ];
496 ObjectState& anObjectState = aName2ObjectStateMap[ theObject->GetName() ];
497 anObjectState.Visibility = theState;
501 CAM_DataModel* HYDROGUI_Module::createDataModel()
503 return new HYDROGUI_DataModel( this );
506 void HYDROGUI_Module::customEvent( QEvent* e )
508 int aType = e->type();
509 if ( aType == NewViewEvent )
511 SALOME_CustomEvent* ce = ( SALOME_CustomEvent* )e;
512 if( GraphicsView_ViewFrame* aViewFrame = ( GraphicsView_ViewFrame* )ce->data() )
514 if( GraphicsView_Viewer* aViewer = dynamic_cast<GraphicsView_Viewer*>( aViewFrame->getViewer() ) )
516 if( GraphicsView_ViewPort* aViewPort = aViewer->getActiveViewPort() )
518 aViewPort->setInteractionFlag( GraphicsView_ViewPort::TraceBoundingRect );
519 aViewPort->setInteractionFlag( GraphicsView_ViewPort::ImmediateContextMenu );
520 aViewPort->setInteractionFlag( GraphicsView_ViewPort::ImmediateSelection );
522 //ouv: temporarily commented
523 //aViewPort->setViewLabelPosition( GraphicsView_ViewPort::VLP_BottomLeft, true );
526 SUIT_ViewManager* aViewManager = aViewer->getViewManager();
527 ViewManagerRole aRole = getViewManagerRole( aViewManager );
528 if( aRole != VMR_TransformImage )
531 aViewer->activateTransform( GraphicsView_Viewer::FitAll );
537 bool HYDROGUI_Module::eventFilter( QObject* theObj, QEvent* theEvent )
539 QEvent::Type aType = theEvent->type();
540 if( theObj->inherits( "GraphicsView_ViewFrame" ) )
542 if( aType == QEvent::Show )
544 SALOME_CustomEvent* e = new SALOME_CustomEvent( NewViewEvent );
545 e->setData( theObj );
546 QApplication::postEvent( this, e );
547 theObj->removeEventFilter( this );
550 return LightApp_Module::eventFilter( theObj, theEvent );
553 void HYDROGUI_Module::onViewManagerAdded( SUIT_ViewManager* theViewManager )
555 LightApp_Module::onViewManagerAdded( theViewManager );
557 if( theViewManager->getType() == GraphicsView_Viewer::Type() )
559 createSelector( theViewManager ); // replace the default selector
561 connect( theViewManager, SIGNAL( viewCreated( SUIT_ViewWindow* ) ),
562 this, SLOT( onViewCreated( SUIT_ViewWindow* ) ) );
565 ViewManagerInfo anInfo( theViewManager, VMR_General );
566 myViewManagerMap.insert( ViewManagerId++, anInfo );
569 void HYDROGUI_Module::onViewManagerRemoved( SUIT_ViewManager* theViewManager )
571 LightApp_Module::onViewManagerRemoved( theViewManager );
573 createSelector( theViewManager ); // replace the default selector
575 int anId = getViewManagerId( theViewManager );
577 myViewManagerMap.remove( anId );
580 void HYDROGUI_Module::onViewCreated( SUIT_ViewWindow* theViewWindow )
582 if( theViewWindow && theViewWindow->inherits( "GraphicsView_ViewFrame" ) )
584 if( GraphicsView_ViewFrame* aViewFrame = dynamic_cast<GraphicsView_ViewFrame*>( theViewWindow ) )
586 aViewFrame->installEventFilter( this );
588 GraphicsView_ViewPort* aViewPort = aViewFrame->getViewPort();
590 connect( aViewPort, SIGNAL( vpMouseEvent( QGraphicsSceneMouseEvent* ) ),
591 this, SLOT( onViewPortMouseEvent( QGraphicsSceneMouseEvent* ) ) );
597 void HYDROGUI_Module::onViewPortMouseEvent( QGraphicsSceneMouseEvent* theEvent )
599 if( GraphicsView_ViewPort* aViewPort = qobject_cast<GraphicsView_ViewPort*>( sender() ) )
601 SUIT_ViewManager* aViewManager = 0;
603 QObject* aParent = aViewPort;
604 while( aParent = aParent->parent() )
606 if( GraphicsView_ViewFrame* aViewFrame = dynamic_cast<GraphicsView_ViewFrame*>( aParent ) )
608 if( GraphicsView_Viewer* aViewer = aViewFrame->getViewer() )
610 aViewManager = aViewer->getViewManager();
619 double aMouseX = theEvent->scenePos().x();
620 double aMouseY = theEvent->scenePos().y();
622 ViewManagerRole aRole = getViewManagerRole( aViewManager );
623 if( aRole == VMR_General )
625 int aXDeg = 0, aYDeg = 0;
626 int aXMin = 0, aYMin = 0;
627 double aXSec = 0, aYSec = 0;
628 HYDROGUI_Tool::DoubleToLambert( aMouseX, aXDeg, aXMin, aXSec );
629 HYDROGUI_Tool::DoubleToLambert( aMouseY, aYDeg, aYMin, aYSec );
631 QString aDegSymbol( QChar( 0x00B0 ) );
632 QString aXStr = QString( "%1%2 %3' %4\"" ).arg( aXDeg ).arg( aDegSymbol ).arg( aXMin ).arg( aXSec );
633 QString aYStr = QString( "%1%2 %3' %4\"" ).arg( aYDeg ).arg( aDegSymbol ).arg( aYMin ).arg( aYSec );
635 aViewPort->setViewLabelText( QString( "X: %1\nY: %2" ).arg( aXStr ).arg( aYStr ) );
637 else if( aRole == VMR_TransformImage )
638 aViewPort->setViewLabelText( QString( "X: %1\nY: %2" ).arg( (int)aMouseX ).arg( (int)aMouseY ) );
642 void HYDROGUI_Module::updateGV( const bool theIsInit,
643 const bool theIsForced )
645 if( !getDisplayer() )
648 QList<int> aViewManagerIdList;
650 // currently, all views are updated
651 ViewManagerMapIterator anIter( myViewManagerMap );
652 while( anIter.hasNext() )
654 int anId = anIter.next().key();
655 aViewManagerIdList.append( anId );
658 QListIterator<int> anIdIter( aViewManagerIdList );
659 while( anIdIter.hasNext() )
660 getDisplayer()->UpdateAll( anIdIter.next(), theIsInit, theIsForced );
663 void HYDROGUI_Module::createSelector( SUIT_ViewManager* theViewManager )
665 if( !theViewManager )
668 LightApp_SelectionMgr* aSelectionMgr = getApp()->selectionMgr();
672 QString aViewType = theViewManager->getType();
673 if( aViewType != GraphicsView_Viewer::Type() )
676 GraphicsView_ViewManager* aViewManager =
677 dynamic_cast<GraphicsView_ViewManager*>( theViewManager );
681 QList<SUIT_Selector*> aSelectorList;
682 aSelectionMgr->selectors( aViewType, aSelectorList );
684 // disable all alien selectors
685 QList<SUIT_Selector*>::iterator anIter, anIterEnd = aSelectorList.end();
686 for( anIter = aSelectorList.begin(); anIter != anIterEnd; anIter++ )
688 SUIT_Selector* aSelector = *anIter;
689 if( aSelector && !dynamic_cast<HYDROGUI_GVSelector*>( aSelector ) )
690 aSelector->setEnabled( false );
693 new HYDROGUI_GVSelector( this, aViewManager->getViewer(), aSelectionMgr );
696 bool HYDROGUI_Module::setUpdateEnabled( const bool theState )
698 bool aPrevState = myIsUpdateEnabled;
699 myIsUpdateEnabled = theState;
703 bool HYDROGUI_Module::isUpdateEnabled() const
705 return myIsUpdateEnabled;