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_DataOwner.h>
51 #include <LightApp_GVSelector.h>
52 #include <LightApp_SelectionMgr.h>
53 #include <LightApp_UpdateFlags.h>
55 #include <SALOME_Event.h>
57 #include <SUIT_Desktop.h>
58 #include <SUIT_Study.h>
59 #include <SUIT_ViewManager.h>
62 #include <QApplication>
63 #include <QGraphicsSceneMouseEvent>
66 static int ViewManagerId = 0;
68 extern "C" HYDRO_EXPORT CAM_Module* createModule()
70 return new HYDROGUI_Module();
73 extern "C" HYDRO_EXPORT char* getModuleVersion()
75 return (char*)HYDRO_VERSION;
78 HYDROGUI_Module::HYDROGUI_Module()
79 : LightApp_Module( "HYDRO" ),
81 myIsUpdateEnabled( true )
85 HYDROGUI_Module::~HYDROGUI_Module()
89 int HYDROGUI_Module::getStudyId() const
91 LightApp_Application* anApp = getApp();
92 return anApp ? anApp->activeStudy()->id() : 0;
95 void HYDROGUI_Module::initialize( CAM_Application* theApp )
97 LightApp_Module::initialize( theApp );
100 createUndoRedoActions();
105 setMenuShown( false );
106 setToolShown( false );
108 myDisplayer = new HYDROGUI_Displayer( this );
111 bool HYDROGUI_Module::activateModule( SUIT_Study* theStudy )
113 bool aRes = LightApp_Module::activateModule( theStudy );
115 LightApp_Application* anApp = getApp();
116 SUIT_Desktop* aDesktop = anApp->desktop();
118 getApp()->setEditEnabled( false ); // hide SalomeApp copy/paste actions
120 setMenuShown( true );
121 setToolShown( true );
123 #ifndef DISABLE_PYCONSOLE
124 aDesktop->tabifyDockWidget( HYDROGUI_Tool::WindowDock( anApp->getWindow( LightApp_Application::WT_PyConsole ) ),
125 HYDROGUI_Tool::WindowDock( anApp->getWindow( LightApp_Application::WT_LogWindow ) ) );
130 updateCommandsStatus();
135 bool HYDROGUI_Module::deactivateModule( SUIT_Study* theStudy )
137 ViewManagerMapIterator anIter( myViewManagerMap );
138 while( anIter.hasNext() )
139 if( SUIT_ViewManager* aViewManager = anIter.next().value().first )
140 getApp()->removeViewManager( aViewManager );
141 myViewManagerMap.clear();
143 myObjectStateMap.clear();
145 // clear the data model's list of copying objects
146 HYDROGUI_DataModel::changeCopyingObjects( HYDROData_SequenceOfObjects() );
148 setMenuShown( false );
149 setToolShown( false );
151 getApp()->setEditEnabled( true ); // show SalomeApp copy/paste actions
153 return LightApp_Module::deactivateModule( theStudy );
156 void HYDROGUI_Module::windows( QMap<int, int>& theMap ) const
159 theMap.insert( LightApp_Application::WT_LogWindow, Qt::BottomDockWidgetArea );
160 #ifndef DISABLE_PYCONSOLE
161 theMap.insert( LightApp_Application::WT_PyConsole, Qt::BottomDockWidgetArea );
163 theMap.insert( LightApp_Application::WT_ObjectBrowser, Qt::LeftDockWidgetArea );
166 void HYDROGUI_Module::viewManagers( QStringList& theTypesList ) const
168 theTypesList << GraphicsView_Viewer::Type();
171 void HYDROGUI_Module::contextMenuPopup( const QString& theClient,
175 HYDROGUI_DataModel* aModel = getDataModel();
177 bool anIsObjectBrowser = theClient == "ObjectBrowser";
178 bool anIsGraphicsView = theClient == "GraphicsView";
179 if( !anIsObjectBrowser && !anIsGraphicsView )
182 size_t aViewId = HYDROGUI_Tool::GetActiveGraphicsViewId( this );
184 bool anIsSelectedDataObjects = false;
185 bool anIsVisibleInSelection = false;
186 bool anIsHiddenInSelection = false;
188 bool anIsImage = false;
189 bool anIsImportedImage = false;
190 bool anIsCompositeImage = false;
191 bool anIsFusedImage = false;
192 bool anIsCutImage = false;
193 bool anIsSplittedImage = false;
194 bool anIsMustBeUpdatedImage = false;
195 bool anIsPolyline = false;
196 bool anIsCalculation = false;
197 bool anIsZone = false;
198 bool anIsVisualState = false;
200 // check the selected data model objects
201 HYDROData_SequenceOfObjects aSeq = HYDROGUI_Tool::GetSelectedObjects( this );
202 for( Standard_Integer anIndex = 1, aLength = aSeq.Length(); anIndex <= aLength; anIndex++ )
204 Handle(HYDROData_Object) anObject = aSeq.Value( anIndex );
205 if( !anObject.IsNull() )
207 anIsSelectedDataObjects = true;
209 bool aVisibility = isObjectVisible( aViewId, anObject );
210 anIsVisibleInSelection |= aVisibility;
211 anIsHiddenInSelection |= !aVisibility;
213 if( anObject->GetKind() == KIND_IMAGE )
216 Handle(HYDROData_Image) anImage = Handle(HYDROData_Image)::DownCast( anObject );
217 if( !anImage.IsNull() )
219 anIsImportedImage = anImage->HasTrsfPoints() && !anImage->IsSelfSplitted();
220 anIsCompositeImage = anImage->NbReferences() > 0;
221 if( HYDROOperations_Factory* aFactory = HYDROOperations_Factory::Factory() )
223 if( ImageComposer_Operator* anOperator = aFactory->Operator( anImage ) )
225 QString anOperatorName = anOperator->name();
226 if( anOperatorName == ImageComposer_FuseOperator::Type() )
227 anIsFusedImage = true;
228 else if( anOperatorName == ImageComposer_CutOperator::Type() )
230 else if( anOperatorName == ImageComposer_CropOperator::Type() )
231 anIsSplittedImage = true;
234 anIsMustBeUpdatedImage = anImage->MustBeUpdated();
237 else if( anObject->GetKind() == KIND_POLYLINE )
239 else if( anObject->GetKind() == KIND_CALCULATION )
240 anIsCalculation = true;
241 else if( anObject->GetKind() == KIND_ZONE )
243 else if( anObject->GetKind() == KIND_VISUAL_STATE )
244 anIsVisualState = true;
248 // check the selected partitions
249 if( !anIsSelectedDataObjects && anIsObjectBrowser )
251 ObjectKind aSelectedPartition = HYDROGUI_Tool::GetSelectedPartition( this );
252 if( aSelectedPartition != KIND_UNKNOWN )
254 switch( aSelectedPartition )
257 theMenu->addAction( action( ImportImageId ) );
260 theMenu->addAction( action( CreatePolylineId ) );
262 case KIND_VISUAL_STATE:
263 theMenu->addAction( action( SaveVisualStateId ) );
265 case KIND_BATHYMETRY:
266 theMenu->addAction( action( ImportBathymetryId ) );
268 case KIND_CALCULATION:
269 theMenu->addAction( action( CreateCalculationId ) );
272 theMenu->addAction( action( CreateZoneId ) );
275 theMenu->addSeparator();
279 if( anIsSelectedDataObjects && anIsMustBeUpdatedImage )
281 theMenu->addAction( action( UpdateImageId ) );
282 theMenu->addSeparator();
285 if( anIsSelectedDataObjects && aSeq.Length() == 1 )
289 if( anIsImportedImage )
290 theMenu->addAction( action( EditImportedImageId ) );
291 else if( anIsCompositeImage )
294 theMenu->addAction( action( EditFusedImageId ) );
295 else if( anIsCutImage )
296 theMenu->addAction( action( EditCutImageId ) );
297 else if( anIsSplittedImage )
298 theMenu->addAction( action( EditSplittedImageId ) );
301 theMenu->addAction( action( ObserveImageId ) );
302 theMenu->addAction( action( ExportImageId ) );
303 theMenu->addSeparator();
305 theMenu->addAction( action( FuseImagesId ) );
306 theMenu->addAction( action( CutImagesId ) );
307 theMenu->addAction( action( SplitImageId ) );
308 theMenu->addSeparator();
310 else if( anIsPolyline )
312 theMenu->addAction( action( EditPolylineId ) );
313 theMenu->addSeparator();
315 else if( anIsCalculation )
317 theMenu->addAction( action( EditCalculationId ) );
318 theMenu->addSeparator();
322 theMenu->addAction( action( EditZoneId ) );
323 theMenu->addSeparator();
325 else if( anIsVisualState && anIsObjectBrowser )
327 theMenu->addAction( action( SaveVisualStateId ) );
328 theMenu->addAction( action( LoadVisualStateId ) );
329 theMenu->addSeparator();
333 if( anIsSelectedDataObjects )
335 theMenu->addAction( action( DeleteId ) );
336 theMenu->addSeparator();
339 if( anIsSelectedDataObjects && ( anIsImage || anIsPolyline || anIsZone ) )
341 if( anIsHiddenInSelection )
342 theMenu->addAction( action( ShowId ) );
343 theMenu->addAction( action( ShowOnlyId ) );
344 if( anIsVisibleInSelection )
345 theMenu->addAction( action( HideId ) );
346 theMenu->addSeparator();
349 if( anIsObjectBrowser || anIsGraphicsView )
351 theMenu->addAction( action( ShowAllId ) );
352 theMenu->addAction( action( HideAllId ) );
353 theMenu->addSeparator();
357 void HYDROGUI_Module::update( const int flags )
359 if( !isUpdateEnabled() )
362 QApplication::setOverrideCursor( Qt::WaitCursor );
364 // To prevent calling this method recursively
365 // from one of the methods called below
366 setUpdateEnabled( false );
368 // store selected objects
369 QStringList aSelectedEntries = storeSelection();
371 if( ( flags & UF_Viewer ) )
372 updateGV( flags & UF_GV_Init,
373 flags & UF_GV_Forced );
375 if( ( flags & UF_Model ) && getDataModel() && getApp() )
377 getDataModel()->update( getStudyId() );
379 // Temporary workaround to prevent breaking
380 // the selection in the object browser.
381 // Note: processEvents() should be called after updateGV(),
382 // otherwise the application crashes from time to time.
383 qApp->processEvents();
384 getApp()->updateObjectBrowser( true );
387 // Object browser is currently updated by using UF_Model flag
388 //if( ( flags & UF_ObjBrowser ) && getApp() )
389 // getApp()->updateObjectBrowser( true );
391 if( ( flags & UF_Controls ) && getApp() )
392 getApp()->updateActions();
394 // restore selected objects
395 restoreSelection( aSelectedEntries );
397 setUpdateEnabled( true );
399 QApplication::restoreOverrideCursor();
402 void HYDROGUI_Module::updateCommandsStatus()
404 LightApp_Module::updateCommandsStatus();
406 updateUndoRedoControls();
408 action( CopyId )->setEnabled( getDataModel()->canCopy() );
409 action( PasteId )->setEnabled( getDataModel()->canPaste() );
412 void HYDROGUI_Module::selectionChanged()
414 LightApp_Module::selectionChanged();
415 updateCommandsStatus();
418 HYDROGUI_DataModel* HYDROGUI_Module::getDataModel() const
420 return (HYDROGUI_DataModel*)dataModel();
423 HYDROGUI_Displayer* HYDROGUI_Module::getDisplayer() const
428 GraphicsView_Viewer* HYDROGUI_Module::getViewer( const int theId ) const
430 if( myViewManagerMap.contains( theId ) )
432 ViewManagerInfo anInfo = myViewManagerMap[ theId ];
433 GraphicsView_ViewManager* aViewManager =
434 dynamic_cast<GraphicsView_ViewManager*>( anInfo.first );
436 return aViewManager->getViewer();
441 int HYDROGUI_Module::getViewManagerId( SUIT_ViewManager* theViewManager )
443 ViewManagerMapIterator anIter( myViewManagerMap );
444 while( anIter.hasNext() )
446 int anId = anIter.next().key();
447 const ViewManagerInfo& anInfo = anIter.value();
448 if( anInfo.first == theViewManager )
454 HYDROGUI_Module::ViewManagerRole HYDROGUI_Module::getViewManagerRole( SUIT_ViewManager* theViewManager )
456 int anId = getViewManagerId( theViewManager );
459 const ViewManagerInfo& anInfo = myViewManagerMap[ anId ];
460 return anInfo.second;
465 void HYDROGUI_Module::setViewManagerRole( SUIT_ViewManager* theViewManager,
466 const ViewManagerRole theRole )
468 int anId = getViewManagerId( theViewManager );
471 ViewManagerInfo& anInfo = myViewManagerMap[ anId ];
472 anInfo.second = theRole;
476 bool HYDROGUI_Module::isObjectVisible( const int theViewId,
477 const Handle(HYDROData_Object)& theObject )
479 if( theObject.IsNull() )
482 ViewId2Name2ObjectStateMap::const_iterator anIter1 = myObjectStateMap.find( theViewId );
483 if( anIter1 != myObjectStateMap.end() )
485 const Name2ObjectStateMap& aName2ObjectStateMap = anIter1.value();
486 Name2ObjectStateMap::const_iterator anIter2 = aName2ObjectStateMap.find( theObject->GetName());
487 if( anIter2 != aName2ObjectStateMap.end() )
489 const ObjectState& anObjectState = anIter2.value();
490 return anObjectState.Visibility;
496 void HYDROGUI_Module::setObjectVisible( const int theViewId,
497 const Handle(HYDROData_Object)& theObject,
498 const bool theState )
500 if( !theObject.IsNull() )
502 Name2ObjectStateMap& aName2ObjectStateMap = myObjectStateMap[ theViewId ];
503 ObjectState& anObjectState = aName2ObjectStateMap[ theObject->GetName() ];
504 anObjectState.Visibility = theState;
508 CAM_DataModel* HYDROGUI_Module::createDataModel()
510 return new HYDROGUI_DataModel( this );
513 void HYDROGUI_Module::customEvent( QEvent* e )
515 int aType = e->type();
516 if ( aType == NewViewEvent )
518 SALOME_CustomEvent* ce = ( SALOME_CustomEvent* )e;
519 if( GraphicsView_ViewFrame* aViewFrame = ( GraphicsView_ViewFrame* )ce->data() )
521 if( GraphicsView_Viewer* aViewer = dynamic_cast<GraphicsView_Viewer*>( aViewFrame->getViewer() ) )
523 if( GraphicsView_ViewPort* aViewPort = aViewer->getActiveViewPort() )
525 aViewPort->setInteractionFlag( GraphicsView_ViewPort::TraceBoundingRect );
526 aViewPort->setInteractionFlag( GraphicsView_ViewPort::ImmediateContextMenu );
527 aViewPort->setInteractionFlag( GraphicsView_ViewPort::ImmediateSelection );
529 //ouv: temporarily commented
530 //aViewPort->setViewLabelPosition( GraphicsView_ViewPort::VLP_BottomLeft, true );
533 SUIT_ViewManager* aViewManager = aViewer->getViewManager();
534 ViewManagerRole aRole = getViewManagerRole( aViewManager );
535 if( aRole != VMR_TransformImage )
538 aViewer->activateTransform( GraphicsView_Viewer::FitAll );
544 bool HYDROGUI_Module::eventFilter( QObject* theObj, QEvent* theEvent )
546 QEvent::Type aType = theEvent->type();
547 if( theObj->inherits( "GraphicsView_ViewFrame" ) )
549 if( aType == QEvent::Show )
551 SALOME_CustomEvent* e = new SALOME_CustomEvent( NewViewEvent );
552 e->setData( theObj );
553 QApplication::postEvent( this, e );
554 theObj->removeEventFilter( this );
557 return LightApp_Module::eventFilter( theObj, theEvent );
560 void HYDROGUI_Module::onViewManagerAdded( SUIT_ViewManager* theViewManager )
562 LightApp_Module::onViewManagerAdded( theViewManager );
564 if( theViewManager->getType() == GraphicsView_Viewer::Type() )
566 createSelector( theViewManager ); // replace the default selector
568 connect( theViewManager, SIGNAL( viewCreated( SUIT_ViewWindow* ) ),
569 this, SLOT( onViewCreated( SUIT_ViewWindow* ) ) );
572 ViewManagerInfo anInfo( theViewManager, VMR_General );
573 myViewManagerMap.insert( ViewManagerId++, anInfo );
576 void HYDROGUI_Module::onViewManagerRemoved( SUIT_ViewManager* theViewManager )
578 LightApp_Module::onViewManagerRemoved( theViewManager );
580 createSelector( theViewManager ); // replace the default selector
582 int anId = getViewManagerId( theViewManager );
584 myViewManagerMap.remove( anId );
587 void HYDROGUI_Module::onViewCreated( SUIT_ViewWindow* theViewWindow )
589 if( theViewWindow && theViewWindow->inherits( "GraphicsView_ViewFrame" ) )
591 if( GraphicsView_ViewFrame* aViewFrame = dynamic_cast<GraphicsView_ViewFrame*>( theViewWindow ) )
593 aViewFrame->installEventFilter( this );
595 GraphicsView_ViewPort* aViewPort = aViewFrame->getViewPort();
597 connect( aViewPort, SIGNAL( vpMouseEvent( QGraphicsSceneMouseEvent* ) ),
598 this, SLOT( onViewPortMouseEvent( QGraphicsSceneMouseEvent* ) ) );
604 void HYDROGUI_Module::onViewPortMouseEvent( QGraphicsSceneMouseEvent* theEvent )
606 if( GraphicsView_ViewPort* aViewPort = qobject_cast<GraphicsView_ViewPort*>( sender() ) )
608 SUIT_ViewManager* aViewManager = 0;
610 QObject* aParent = aViewPort;
611 while( aParent = aParent->parent() )
613 if( GraphicsView_ViewFrame* aViewFrame = dynamic_cast<GraphicsView_ViewFrame*>( aParent ) )
615 if( GraphicsView_Viewer* aViewer = aViewFrame->getViewer() )
617 aViewManager = aViewer->getViewManager();
626 double aMouseX = theEvent->scenePos().x();
627 double aMouseY = theEvent->scenePos().y();
629 ViewManagerRole aRole = getViewManagerRole( aViewManager );
630 if( aRole == VMR_General )
632 int aXDeg = 0, aYDeg = 0;
633 int aXMin = 0, aYMin = 0;
634 double aXSec = 0, aYSec = 0;
635 HYDROGUI_Tool::DoubleToLambert( aMouseX, aXDeg, aXMin, aXSec );
636 HYDROGUI_Tool::DoubleToLambert( aMouseY, aYDeg, aYMin, aYSec );
638 QString aDegSymbol( QChar( 0x00B0 ) );
639 QString aXStr = QString( "%1%2 %3' %4\"" ).arg( aXDeg ).arg( aDegSymbol ).arg( aXMin ).arg( aXSec );
640 QString aYStr = QString( "%1%2 %3' %4\"" ).arg( aYDeg ).arg( aDegSymbol ).arg( aYMin ).arg( aYSec );
642 aViewPort->setViewLabelText( QString( "X: %1\nY: %2" ).arg( aXStr ).arg( aYStr ) );
644 else if( aRole == VMR_TransformImage )
645 aViewPort->setViewLabelText( QString( "X: %1\nY: %2" ).arg( (int)aMouseX ).arg( (int)aMouseY ) );
649 void HYDROGUI_Module::updateGV( const bool theIsInit,
650 const bool theIsForced )
652 if( !getDisplayer() )
655 QList<int> aViewManagerIdList;
657 // currently, all views are updated
658 ViewManagerMapIterator anIter( myViewManagerMap );
659 while( anIter.hasNext() )
661 int anId = anIter.next().key();
662 aViewManagerIdList.append( anId );
665 QListIterator<int> anIdIter( aViewManagerIdList );
666 while( anIdIter.hasNext() )
667 getDisplayer()->UpdateAll( anIdIter.next(), theIsInit, theIsForced );
670 void HYDROGUI_Module::createSelector( SUIT_ViewManager* theViewManager )
672 if( !theViewManager )
675 LightApp_SelectionMgr* aSelectionMgr = getApp()->selectionMgr();
679 QString aViewType = theViewManager->getType();
680 if( aViewType != GraphicsView_Viewer::Type() )
683 GraphicsView_ViewManager* aViewManager =
684 dynamic_cast<GraphicsView_ViewManager*>( theViewManager );
688 QList<SUIT_Selector*> aSelectorList;
689 aSelectionMgr->selectors( aViewType, aSelectorList );
691 // disable all alien selectors
692 QList<SUIT_Selector*>::iterator anIter, anIterEnd = aSelectorList.end();
693 for( anIter = aSelectorList.begin(); anIter != anIterEnd; anIter++ )
695 SUIT_Selector* aSelector = *anIter;
696 if( aSelector && !dynamic_cast<HYDROGUI_GVSelector*>( aSelector ) )
697 aSelector->setEnabled( false );
700 new HYDROGUI_GVSelector( this, aViewManager->getViewer(), aSelectionMgr );
703 bool HYDROGUI_Module::setUpdateEnabled( const bool theState )
705 bool aPrevState = myIsUpdateEnabled;
706 myIsUpdateEnabled = theState;
710 bool HYDROGUI_Module::isUpdateEnabled() const
712 return myIsUpdateEnabled;
715 QStringList HYDROGUI_Module::storeSelection() const
717 QStringList anEntryList;
718 if( LightApp_SelectionMgr* aSelectionMgr = getApp()->selectionMgr() )
720 SUIT_DataOwnerPtrList aList( true );
721 aSelectionMgr->selected( aList );
723 SUIT_DataOwnerPtrList::iterator anIter;
724 for( anIter = aList.begin(); anIter != aList.end(); anIter++ )
726 const LightApp_DataOwner* anOwner =
727 dynamic_cast<const LightApp_DataOwner*>( (*anIter).operator->() );
729 anEntryList.append( anOwner->entry() );
735 void HYDROGUI_Module::restoreSelection( const QStringList& theEntryList )
737 if( LightApp_SelectionMgr* aSelectionMgr = getApp()->selectionMgr() )
739 SUIT_DataOwnerPtrList aList( true );
740 for( int anIndex = 0, aSize = theEntryList.size(); anIndex < aSize; anIndex++ )
741 aList.append( SUIT_DataOwnerPtr( new LightApp_DataOwner( theEntryList[ anIndex ] ) ) );
742 aSelectionMgr->setSelected( aList );