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 anIsVisualState = false;
198 // check the selected data model objects
199 HYDROData_SequenceOfObjects aSeq = HYDROGUI_Tool::GetSelectedObjects( this );
200 for( Standard_Integer anIndex = 1, aLength = aSeq.Length(); anIndex <= aLength; anIndex++ )
202 Handle(HYDROData_Object) anObject = aSeq.Value( anIndex );
203 if( !anObject.IsNull() )
205 anIsSelectedDataObjects = true;
207 bool aVisibility = isObjectVisible( aViewId, anObject );
208 anIsVisibleInSelection |= aVisibility;
209 anIsHiddenInSelection |= !aVisibility;
211 if( anObject->GetKind() == KIND_IMAGE )
214 Handle(HYDROData_Image) anImage = Handle(HYDROData_Image)::DownCast( anObject );
215 if( !anImage.IsNull() )
217 anIsImportedImage = anImage->HasTrsfPoints() && !anImage->IsSelfSplitted();
218 anIsCompositeImage = anImage->NbReferences() > 0;
219 if( HYDROOperations_Factory* aFactory = HYDROOperations_Factory::Factory() )
221 if( ImageComposer_Operator* anOperator = aFactory->Operator( anImage ) )
223 QString anOperatorName = anOperator->name();
224 if( anOperatorName == ImageComposer_FuseOperator::Type() )
225 anIsFusedImage = true;
226 else if( anOperatorName == ImageComposer_CutOperator::Type() )
228 else if( anOperatorName == ImageComposer_CropOperator::Type() )
229 anIsSplittedImage = true;
232 anIsMustBeUpdatedImage = anImage->MustBeUpdated();
235 else if( anObject->GetKind() == KIND_POLYLINE )
237 else if( anObject->GetKind() == KIND_CALCULATION )
238 anIsCalculation = true;
239 else if( anObject->GetKind() == KIND_VISUAL_STATE )
240 anIsVisualState = true;
244 // check the selected partitions
245 if( !anIsSelectedDataObjects && anIsObjectBrowser )
247 ObjectKind aSelectedPartition = HYDROGUI_Tool::GetSelectedPartition( this );
248 if( aSelectedPartition != KIND_UNKNOWN )
250 switch( aSelectedPartition )
253 theMenu->addAction( action( ImportImageId ) );
256 theMenu->addAction( action( CreatePolylineId ) );
258 case KIND_VISUAL_STATE:
259 theMenu->addAction( action( SaveVisualStateId ) );
261 case KIND_BATHYMETRY:
262 theMenu->addAction( action( ImportBathymetryId ) );
264 case KIND_CALCULATION:
265 theMenu->addAction( action( CreateCalculationId ) );
268 theMenu->addSeparator();
272 if( anIsSelectedDataObjects && anIsMustBeUpdatedImage )
274 theMenu->addAction( action( UpdateImageId ) );
275 theMenu->addSeparator();
278 if( anIsSelectedDataObjects && aSeq.Length() == 1 )
282 if( anIsImportedImage )
283 theMenu->addAction( action( EditImportedImageId ) );
284 else if( anIsCompositeImage )
287 theMenu->addAction( action( EditFusedImageId ) );
288 else if( anIsCutImage )
289 theMenu->addAction( action( EditCutImageId ) );
290 else if( anIsSplittedImage )
291 theMenu->addAction( action( EditSplittedImageId ) );
294 theMenu->addAction( action( ObserveImageId ) );
295 theMenu->addAction( action( ExportImageId ) );
296 theMenu->addSeparator();
298 theMenu->addAction( action( FuseImagesId ) );
299 theMenu->addAction( action( CutImagesId ) );
300 theMenu->addAction( action( SplitImageId ) );
301 theMenu->addSeparator();
303 else if( anIsPolyline )
305 theMenu->addAction( action( EditPolylineId ) );
306 theMenu->addSeparator();
308 else if( anIsCalculation )
310 theMenu->addAction( action( EditCalculationId ) );
311 theMenu->addSeparator();
313 else if( anIsVisualState && anIsObjectBrowser )
315 theMenu->addAction( action( SaveVisualStateId ) );
316 theMenu->addAction( action( LoadVisualStateId ) );
317 theMenu->addSeparator();
321 if( anIsSelectedDataObjects )
323 theMenu->addAction( action( DeleteId ) );
324 theMenu->addSeparator();
327 if( anIsSelectedDataObjects && ( anIsImage || anIsPolyline ) )
329 if( anIsHiddenInSelection )
330 theMenu->addAction( action( ShowId ) );
331 theMenu->addAction( action( ShowOnlyId ) );
332 if( anIsVisibleInSelection )
333 theMenu->addAction( action( HideId ) );
334 theMenu->addSeparator();
337 if( anIsObjectBrowser || anIsGraphicsView )
339 theMenu->addAction( action( ShowAllId ) );
340 theMenu->addAction( action( HideAllId ) );
341 theMenu->addSeparator();
345 void HYDROGUI_Module::update( const int flags )
347 if( !isUpdateEnabled() )
350 QApplication::setOverrideCursor( Qt::WaitCursor );
352 // To prevent calling this method recursively
353 // from one of the methods called below
354 setUpdateEnabled( false );
356 if( ( flags & UF_Viewer ) )
357 updateGV( flags & UF_GV_Init,
358 flags & UF_GV_Forced );
360 if( ( flags & UF_Model ) && getDataModel() && getApp() )
362 getDataModel()->update( getStudyId() );
364 // Temporary workaround to prevent breaking
365 // the selection in the object browser.
366 // Note: processEvents() should be called after updateGV(),
367 // otherwise the application crashes from time to time.
368 qApp->processEvents();
369 getApp()->updateObjectBrowser( true );
372 // Object browser is currently updated by using UF_Model flag
373 //if( ( flags & UF_ObjBrowser ) && getApp() )
374 // getApp()->updateObjectBrowser( true );
376 if( ( flags & UF_Controls ) && getApp() )
377 getApp()->updateActions();
379 setUpdateEnabled( true );
381 QApplication::restoreOverrideCursor();
384 void HYDROGUI_Module::updateCommandsStatus()
386 LightApp_Module::updateCommandsStatus();
388 updateUndoRedoControls();
390 action( CopyId )->setEnabled( getDataModel()->canCopy() );
391 action( PasteId )->setEnabled( getDataModel()->canPaste() );
394 void HYDROGUI_Module::selectionChanged()
396 LightApp_Module::selectionChanged();
397 updateCommandsStatus();
400 HYDROGUI_DataModel* HYDROGUI_Module::getDataModel() const
402 return (HYDROGUI_DataModel*)dataModel();
405 HYDROGUI_Displayer* HYDROGUI_Module::getDisplayer() const
410 GraphicsView_Viewer* HYDROGUI_Module::getViewer( const int theId ) const
412 if( myViewManagerMap.contains( theId ) )
414 ViewManagerInfo anInfo = myViewManagerMap[ theId ];
415 GraphicsView_ViewManager* aViewManager =
416 dynamic_cast<GraphicsView_ViewManager*>( anInfo.first );
418 return aViewManager->getViewer();
423 int HYDROGUI_Module::getViewManagerId( SUIT_ViewManager* theViewManager )
425 ViewManagerMapIterator anIter( myViewManagerMap );
426 while( anIter.hasNext() )
428 int anId = anIter.next().key();
429 const ViewManagerInfo& anInfo = anIter.value();
430 if( anInfo.first == theViewManager )
436 HYDROGUI_Module::ViewManagerRole HYDROGUI_Module::getViewManagerRole( SUIT_ViewManager* theViewManager )
438 int anId = getViewManagerId( theViewManager );
441 const ViewManagerInfo& anInfo = myViewManagerMap[ anId ];
442 return anInfo.second;
447 void HYDROGUI_Module::setViewManagerRole( SUIT_ViewManager* theViewManager,
448 const ViewManagerRole theRole )
450 int anId = getViewManagerId( theViewManager );
453 ViewManagerInfo& anInfo = myViewManagerMap[ anId ];
454 anInfo.second = theRole;
458 bool HYDROGUI_Module::isObjectVisible( const int theViewId,
459 const Handle(HYDROData_Object)& theObject )
461 if( theObject.IsNull() )
464 ViewId2Name2ObjectStateMap::const_iterator anIter1 = myObjectStateMap.find( theViewId );
465 if( anIter1 != myObjectStateMap.end() )
467 const Name2ObjectStateMap& aName2ObjectStateMap = anIter1.value();
468 Name2ObjectStateMap::const_iterator anIter2 = aName2ObjectStateMap.find( theObject->GetName());
469 if( anIter2 != aName2ObjectStateMap.end() )
471 const ObjectState& anObjectState = anIter2.value();
472 return anObjectState.Visibility;
478 void HYDROGUI_Module::setObjectVisible( const int theViewId,
479 const Handle(HYDROData_Object)& theObject,
480 const bool theState )
482 if( !theObject.IsNull() )
484 Name2ObjectStateMap& aName2ObjectStateMap = myObjectStateMap[ theViewId ];
485 ObjectState& anObjectState = aName2ObjectStateMap[ theObject->GetName() ];
486 anObjectState.Visibility = theState;
490 CAM_DataModel* HYDROGUI_Module::createDataModel()
492 return new HYDROGUI_DataModel( this );
495 void HYDROGUI_Module::customEvent( QEvent* e )
497 int aType = e->type();
498 if ( aType == NewViewEvent )
500 SALOME_CustomEvent* ce = ( SALOME_CustomEvent* )e;
501 if( GraphicsView_ViewFrame* aViewFrame = ( GraphicsView_ViewFrame* )ce->data() )
503 if( GraphicsView_Viewer* aViewer = dynamic_cast<GraphicsView_Viewer*>( aViewFrame->getViewer() ) )
505 if( GraphicsView_ViewPort* aViewPort = aViewer->getActiveViewPort() )
507 aViewPort->setInteractionFlag( GraphicsView_ViewPort::TraceBoundingRect );
508 aViewPort->setInteractionFlag( GraphicsView_ViewPort::ImmediateContextMenu );
509 aViewPort->setInteractionFlag( GraphicsView_ViewPort::ImmediateSelection );
511 //ouv: temporarily commented
512 //aViewPort->setViewLabelPosition( GraphicsView_ViewPort::VLP_BottomLeft, true );
515 SUIT_ViewManager* aViewManager = aViewer->getViewManager();
516 ViewManagerRole aRole = getViewManagerRole( aViewManager );
517 if( aRole != VMR_TransformImage )
520 aViewer->activateTransform( GraphicsView_Viewer::FitAll );
526 bool HYDROGUI_Module::eventFilter( QObject* theObj, QEvent* theEvent )
528 QEvent::Type aType = theEvent->type();
529 if( theObj->inherits( "GraphicsView_ViewFrame" ) )
531 if( aType == QEvent::Show )
533 SALOME_CustomEvent* e = new SALOME_CustomEvent( NewViewEvent );
534 e->setData( theObj );
535 QApplication::postEvent( this, e );
536 theObj->removeEventFilter( this );
539 return LightApp_Module::eventFilter( theObj, theEvent );
542 void HYDROGUI_Module::onViewManagerAdded( SUIT_ViewManager* theViewManager )
544 LightApp_Module::onViewManagerAdded( theViewManager );
546 if( theViewManager->getType() == GraphicsView_Viewer::Type() )
548 createSelector( theViewManager ); // replace the default selector
550 connect( theViewManager, SIGNAL( viewCreated( SUIT_ViewWindow* ) ),
551 this, SLOT( onViewCreated( SUIT_ViewWindow* ) ) );
554 ViewManagerInfo anInfo( theViewManager, VMR_General );
555 myViewManagerMap.insert( ViewManagerId++, anInfo );
558 void HYDROGUI_Module::onViewManagerRemoved( SUIT_ViewManager* theViewManager )
560 LightApp_Module::onViewManagerRemoved( theViewManager );
562 createSelector( theViewManager ); // replace the default selector
564 int anId = getViewManagerId( theViewManager );
566 myViewManagerMap.remove( anId );
569 void HYDROGUI_Module::onViewCreated( SUIT_ViewWindow* theViewWindow )
571 if( theViewWindow && theViewWindow->inherits( "GraphicsView_ViewFrame" ) )
573 if( GraphicsView_ViewFrame* aViewFrame = dynamic_cast<GraphicsView_ViewFrame*>( theViewWindow ) )
575 aViewFrame->installEventFilter( this );
577 GraphicsView_ViewPort* aViewPort = aViewFrame->getViewPort();
579 connect( aViewPort, SIGNAL( vpMouseEvent( QGraphicsSceneMouseEvent* ) ),
580 this, SLOT( onViewPortMouseEvent( QGraphicsSceneMouseEvent* ) ) );
586 void HYDROGUI_Module::onViewPortMouseEvent( QGraphicsSceneMouseEvent* theEvent )
588 if( GraphicsView_ViewPort* aViewPort = qobject_cast<GraphicsView_ViewPort*>( sender() ) )
590 SUIT_ViewManager* aViewManager = 0;
592 QObject* aParent = aViewPort;
593 while( aParent = aParent->parent() )
595 if( GraphicsView_ViewFrame* aViewFrame = dynamic_cast<GraphicsView_ViewFrame*>( aParent ) )
597 if( GraphicsView_Viewer* aViewer = aViewFrame->getViewer() )
599 aViewManager = aViewer->getViewManager();
608 double aMouseX = theEvent->scenePos().x();
609 double aMouseY = theEvent->scenePos().y();
611 ViewManagerRole aRole = getViewManagerRole( aViewManager );
612 if( aRole == VMR_General )
614 int aXDeg = 0, aYDeg = 0;
615 int aXMin = 0, aYMin = 0;
616 double aXSec = 0, aYSec = 0;
617 HYDROGUI_Tool::DoubleToLambert( aMouseX, aXDeg, aXMin, aXSec );
618 HYDROGUI_Tool::DoubleToLambert( aMouseY, aYDeg, aYMin, aYSec );
620 QString aDegSymbol( QChar( 0x00B0 ) );
621 QString aXStr = QString( "%1%2 %3' %4\"" ).arg( aXDeg ).arg( aDegSymbol ).arg( aXMin ).arg( aXSec );
622 QString aYStr = QString( "%1%2 %3' %4\"" ).arg( aYDeg ).arg( aDegSymbol ).arg( aYMin ).arg( aYSec );
624 aViewPort->setViewLabelText( QString( "X: %1\nY: %2" ).arg( aXStr ).arg( aYStr ) );
626 else if( aRole == VMR_TransformImage )
627 aViewPort->setViewLabelText( QString( "X: %1\nY: %2" ).arg( (int)aMouseX ).arg( (int)aMouseY ) );
631 void HYDROGUI_Module::updateGV( const bool theIsInit,
632 const bool theIsForced )
634 if( !getDisplayer() )
637 QList<int> aViewManagerIdList;
639 // currently, all views are updated
640 ViewManagerMapIterator anIter( myViewManagerMap );
641 while( anIter.hasNext() )
643 int anId = anIter.next().key();
644 aViewManagerIdList.append( anId );
647 QListIterator<int> anIdIter( aViewManagerIdList );
648 while( anIdIter.hasNext() )
649 getDisplayer()->UpdateAll( anIdIter.next(), theIsInit, theIsForced );
652 void HYDROGUI_Module::createSelector( SUIT_ViewManager* theViewManager )
654 if( !theViewManager )
657 LightApp_SelectionMgr* aSelectionMgr = getApp()->selectionMgr();
661 QString aViewType = theViewManager->getType();
662 if( aViewType != GraphicsView_Viewer::Type() )
665 GraphicsView_ViewManager* aViewManager =
666 dynamic_cast<GraphicsView_ViewManager*>( theViewManager );
670 QList<SUIT_Selector*> aSelectorList;
671 aSelectionMgr->selectors( aViewType, aSelectorList );
673 // disable all alien selectors
674 QList<SUIT_Selector*>::iterator anIter, anIterEnd = aSelectorList.end();
675 for( anIter = aSelectorList.begin(); anIter != anIterEnd; anIter++ )
677 SUIT_Selector* aSelector = *anIter;
678 if( aSelector && !dynamic_cast<HYDROGUI_GVSelector*>( aSelector ) )
679 aSelector->setEnabled( false );
682 new HYDROGUI_GVSelector( this, aViewManager->getViewer(), aSelectionMgr );
685 bool HYDROGUI_Module::setUpdateEnabled( const bool theState )
687 bool aPrevState = myIsUpdateEnabled;
688 myIsUpdateEnabled = theState;
692 bool HYDROGUI_Module::isUpdateEnabled() const
694 return myIsUpdateEnabled;