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_Study.h>
57 #include <SUIT_ViewManager.h>
60 #include <QApplication>
61 #include <QGraphicsSceneMouseEvent>
64 static int ViewManagerId = 0;
66 extern "C" HYDRO_EXPORT CAM_Module* createModule()
68 return new HYDROGUI_Module();
71 HYDROGUI_Module::HYDROGUI_Module()
72 : LightApp_Module( "HYDRO" ),
74 myIsUpdateEnabled( true )
78 HYDROGUI_Module::~HYDROGUI_Module()
82 int HYDROGUI_Module::getStudyId() const
84 LightApp_Application* anApp = getApp();
85 return anApp ? anApp->activeStudy()->id() : 0;
88 void HYDROGUI_Module::initialize( CAM_Application* theApp )
90 LightApp_Module::initialize( theApp );
93 createUndoRedoActions();
98 setMenuShown( false );
99 setToolShown( false );
101 myDisplayer = new HYDROGUI_Displayer( this );
104 bool HYDROGUI_Module::activateModule( SUIT_Study* theStudy )
106 bool aRes = LightApp_Module::activateModule( theStudy );
108 setMenuShown( true );
109 setToolShown( true );
113 updateCommandsStatus();
118 bool HYDROGUI_Module::deactivateModule( SUIT_Study* theStudy )
120 ViewManagerMapIterator anIter( myViewManagerMap );
121 while( anIter.hasNext() )
122 if( SUIT_ViewManager* aViewManager = anIter.next().value().first )
123 getApp()->removeViewManager( aViewManager );
124 myViewManagerMap.clear();
126 myObjectStateMap.clear();
128 setMenuShown( false );
129 setToolShown( false );
131 return LightApp_Module::deactivateModule( theStudy );
134 void HYDROGUI_Module::windows( QMap<int, int>& theMap ) const
137 theMap.insert( LightApp_Application::WT_LogWindow, Qt::BottomDockWidgetArea );
138 theMap.insert( LightApp_Application::WT_ObjectBrowser, Qt::LeftDockWidgetArea );
141 void HYDROGUI_Module::viewManagers( QStringList& theTypesList ) const
143 theTypesList << GraphicsView_Viewer::Type();
146 void HYDROGUI_Module::contextMenuPopup( const QString& theClient,
150 HYDROGUI_DataModel* aModel = getDataModel();
152 size_t aViewId = HYDROGUI_Tool::GetActiveGraphicsViewId( this );
154 bool anIsSelection = false;
155 bool anIsVisibleInSelection = false;
156 bool anIsHiddenInSelection = false;
158 bool anIsImage = false;
159 bool anIsImportedImage = false;
160 bool anIsCompositeImage = false;
161 bool anIsFusedImage = false;
162 bool anIsCutImage = false;
163 bool anIsSplittedImage = false;
164 bool anIsMustBeUpdatedImage = false;
165 bool anIsPolyline = false;
166 bool anIsVisualState = false;
168 HYDROData_SequenceOfObjects aSeq = HYDROGUI_Tool::GetSelectedObjects( this );
169 for( Standard_Integer anIndex = 1, aLength = aSeq.Length(); anIndex <= aLength; anIndex++ )
171 Handle(HYDROData_Object) anObject = aSeq.Value( anIndex );
172 if( !anObject.IsNull() )
174 anIsSelection = true;
176 bool aVisibility = isObjectVisible( aViewId, anObject );
177 anIsVisibleInSelection |= aVisibility;
178 anIsHiddenInSelection |= !aVisibility;
180 if( anObject->GetKind() == KIND_IMAGE )
183 Handle(HYDROData_Image) anImage = Handle(HYDROData_Image)::DownCast( anObject );
184 if( !anImage.IsNull() )
186 anIsImportedImage = anImage->HasTrsfPoints() && !anImage->IsSelfSplitted();
187 anIsCompositeImage = anImage->NbReferences() > 0;
188 if( HYDROOperations_Factory* aFactory = HYDROOperations_Factory::Factory() )
190 if( ImageComposer_Operator* anOperator = aFactory->Operator( anImage ) )
192 if( dynamic_cast<ImageComposer_FuseOperator*>( anOperator ) )
193 anIsFusedImage = true;
194 else if( dynamic_cast<ImageComposer_CutOperator*>( anOperator ) )
196 else if( dynamic_cast<ImageComposer_CropOperator*>( anOperator ) )
197 anIsSplittedImage = true;
200 anIsMustBeUpdatedImage = anImage->MustBeUpdated();
203 else if( anObject->GetKind() == KIND_POLYLINE )
205 else if( anObject->GetKind() == KIND_VISUAL_STATE )
206 anIsVisualState = true;
212 theMenu->addAction( action( SaveVisualStateId ) );
213 theMenu->addSeparator();
216 if( anIsSelection && anIsMustBeUpdatedImage )
218 theMenu->addAction( action( UpdateImageId ) );
219 theMenu->addSeparator();
222 if( anIsSelection && aSeq.Length() == 1 )
226 if( anIsImportedImage )
227 theMenu->addAction( action( EditImportedImageId ) );
228 else if( anIsCompositeImage )
231 theMenu->addAction( action( EditFusedImageId ) );
232 else if( anIsCutImage )
233 theMenu->addAction( action( EditCutImageId ) );
234 else if( anIsSplittedImage )
235 theMenu->addAction( action( EditSplittedImageId ) );
238 theMenu->addAction( action( ObserveImageId ) );
239 theMenu->addAction( action( ExportImageId ) );
240 theMenu->addSeparator();
242 else if( anIsPolyline )
244 theMenu->addAction( action( EditPolylineId ) );
245 theMenu->addSeparator();
247 else if( anIsVisualState )
249 theMenu->addAction( action( SaveVisualStateId ) );
250 theMenu->addAction( action( LoadVisualStateId ) );
251 theMenu->addSeparator();
257 theMenu->addAction( action( DeleteId ) );
258 theMenu->addSeparator();
261 if( anIsSelection && ( anIsImage || anIsPolyline ) )
263 if( anIsHiddenInSelection )
264 theMenu->addAction( action( ShowId ) );
265 theMenu->addAction( action( ShowOnlyId ) );
266 if( anIsVisibleInSelection )
267 theMenu->addAction( action( HideId ) );
268 theMenu->addSeparator();
271 theMenu->addAction( action( ShowAllId ) );
272 theMenu->addAction( action( HideAllId ) );
273 theMenu->addSeparator();
276 void HYDROGUI_Module::update( const int flags )
278 if( !isUpdateEnabled() )
281 QApplication::setOverrideCursor( Qt::WaitCursor );
283 // To prevent calling this method recursively
284 // from one of the methods called below
285 setUpdateEnabled( false );
287 if( ( flags & UF_Viewer ) )
288 updateGV( flags & UF_GV_Init,
289 flags & UF_GV_Forced );
291 if( ( flags & UF_Model ) && getDataModel() && getApp() )
293 getDataModel()->update( getStudyId() );
295 // Temporary workaround to prevent breaking
296 // the selection in the object browser.
297 // Note: processEvents() should be called after updateGV(),
298 // otherwise the application crashes from time to time.
299 qApp->processEvents();
300 getApp()->updateObjectBrowser( true );
303 // Object browser is currently updated by using UF_Model flag
304 //if( ( flags & UF_ObjBrowser ) && getApp() )
305 // getApp()->updateObjectBrowser( true );
307 if( ( flags & UF_Controls ) && getApp() )
308 getApp()->updateActions();
310 setUpdateEnabled( true );
312 QApplication::restoreOverrideCursor();
315 void HYDROGUI_Module::updateCommandsStatus()
317 LightApp_Module::updateCommandsStatus();
319 updateUndoRedoControls();
322 //action( ... )->setEnabled( ... );
325 HYDROGUI_DataModel* HYDROGUI_Module::getDataModel() const
327 return (HYDROGUI_DataModel*)dataModel();
330 HYDROGUI_Displayer* HYDROGUI_Module::getDisplayer() const
335 GraphicsView_Viewer* HYDROGUI_Module::getViewer( const int theId ) const
337 if( myViewManagerMap.contains( theId ) )
339 ViewManagerInfo anInfo = myViewManagerMap[ theId ];
340 GraphicsView_ViewManager* aViewManager =
341 dynamic_cast<GraphicsView_ViewManager*>( anInfo.first );
343 return aViewManager->getViewer();
348 int HYDROGUI_Module::getViewManagerId( SUIT_ViewManager* theViewManager )
350 ViewManagerMapIterator anIter( myViewManagerMap );
351 while( anIter.hasNext() )
353 int anId = anIter.next().key();
354 const ViewManagerInfo& anInfo = anIter.value();
355 if( anInfo.first == theViewManager )
361 HYDROGUI_Module::ViewManagerRole HYDROGUI_Module::getViewManagerRole( SUIT_ViewManager* theViewManager )
363 int anId = getViewManagerId( theViewManager );
366 const ViewManagerInfo& anInfo = myViewManagerMap[ anId ];
367 return anInfo.second;
372 void HYDROGUI_Module::setViewManagerRole( SUIT_ViewManager* theViewManager,
373 const ViewManagerRole theRole )
375 int anId = getViewManagerId( theViewManager );
378 ViewManagerInfo& anInfo = myViewManagerMap[ anId ];
379 anInfo.second = theRole;
383 bool HYDROGUI_Module::isObjectVisible( const int theViewId,
384 const Handle(HYDROData_Object)& theObject )
386 if( theObject.IsNull() )
389 ViewId2Name2ObjectStateMap::const_iterator anIter1 = myObjectStateMap.find( theViewId );
390 if( anIter1 != myObjectStateMap.end() )
392 const Name2ObjectStateMap& aName2ObjectStateMap = anIter1.value();
393 Name2ObjectStateMap::const_iterator anIter2 = aName2ObjectStateMap.find( theObject->GetName());
394 if( anIter2 != aName2ObjectStateMap.end() )
396 const ObjectState& anObjectState = anIter2.value();
397 return anObjectState.Visibility;
403 void HYDROGUI_Module::setObjectVisible( const int theViewId,
404 const Handle(HYDROData_Object)& theObject,
405 const bool theState )
407 if( !theObject.IsNull() )
409 Name2ObjectStateMap& aName2ObjectStateMap = myObjectStateMap[ theViewId ];
410 ObjectState& anObjectState = aName2ObjectStateMap[ theObject->GetName() ];
411 anObjectState.Visibility = theState;
415 CAM_DataModel* HYDROGUI_Module::createDataModel()
417 return new HYDROGUI_DataModel( this );
420 void HYDROGUI_Module::customEvent( QEvent* e )
422 int aType = e->type();
423 if ( aType == NewViewEvent )
425 SALOME_CustomEvent* ce = ( SALOME_CustomEvent* )e;
426 if( GraphicsView_ViewFrame* aViewFrame = ( GraphicsView_ViewFrame* )ce->data() )
428 if( GraphicsView_Viewer* aViewer = dynamic_cast<GraphicsView_Viewer*>( aViewFrame->getViewer() ) )
430 if( GraphicsView_ViewPort* aViewPort = aViewer->getActiveViewPort() )
432 aViewPort->setInteractionFlag( GraphicsView_ViewPort::TraceBoundingRect );
433 aViewPort->setInteractionFlag( GraphicsView_ViewPort::ImmediateContextMenu );
434 aViewPort->setInteractionFlag( GraphicsView_ViewPort::ImmediateSelection );
436 //ouv: temporarily commented
437 //aViewPort->setViewLabelPosition( GraphicsView_ViewPort::VLP_BottomLeft, true );
440 SUIT_ViewManager* aViewManager = aViewer->getViewManager();
441 ViewManagerRole aRole = getViewManagerRole( aViewManager );
442 if( aRole != VMR_TransformImage )
445 aViewer->activateTransform( GraphicsView_Viewer::FitAll );
451 bool HYDROGUI_Module::eventFilter( QObject* theObj, QEvent* theEvent )
453 QEvent::Type aType = theEvent->type();
454 if( theObj->inherits( "GraphicsView_ViewFrame" ) )
456 if( aType == QEvent::Show )
458 SALOME_CustomEvent* e = new SALOME_CustomEvent( NewViewEvent );
459 e->setData( theObj );
460 QApplication::postEvent( this, e );
461 theObj->removeEventFilter( this );
464 return LightApp_Module::eventFilter( theObj, theEvent );
467 void HYDROGUI_Module::onViewManagerAdded( SUIT_ViewManager* theViewManager )
469 LightApp_Module::onViewManagerAdded( theViewManager );
471 if( theViewManager->getType() == GraphicsView_Viewer::Type() )
473 createSelector( theViewManager ); // replace the default selector
475 connect( theViewManager, SIGNAL( viewCreated( SUIT_ViewWindow* ) ),
476 this, SLOT( onViewCreated( SUIT_ViewWindow* ) ) );
479 ViewManagerInfo anInfo( theViewManager, VMR_General );
480 myViewManagerMap.insert( ViewManagerId++, anInfo );
483 void HYDROGUI_Module::onViewManagerRemoved( SUIT_ViewManager* theViewManager )
485 LightApp_Module::onViewManagerRemoved( theViewManager );
487 createSelector( theViewManager ); // replace the default selector
489 int anId = getViewManagerId( theViewManager );
491 myViewManagerMap.remove( anId );
494 void HYDROGUI_Module::onViewCreated( SUIT_ViewWindow* theViewWindow )
496 if( theViewWindow && theViewWindow->inherits( "GraphicsView_ViewFrame" ) )
498 if( GraphicsView_ViewFrame* aViewFrame = dynamic_cast<GraphicsView_ViewFrame*>( theViewWindow ) )
500 aViewFrame->installEventFilter( this );
502 GraphicsView_ViewPort* aViewPort = aViewFrame->getViewPort();
504 connect( aViewPort, SIGNAL( vpMouseEvent( QGraphicsSceneMouseEvent* ) ),
505 this, SLOT( onViewPortMouseEvent( QGraphicsSceneMouseEvent* ) ) );
511 void HYDROGUI_Module::onViewPortMouseEvent( QGraphicsSceneMouseEvent* theEvent )
513 if( GraphicsView_ViewPort* aViewPort = qobject_cast<GraphicsView_ViewPort*>( sender() ) )
515 SUIT_ViewManager* aViewManager = 0;
517 QObject* aParent = aViewPort;
518 while( aParent = aParent->parent() )
520 if( GraphicsView_ViewFrame* aViewFrame = dynamic_cast<GraphicsView_ViewFrame*>( aParent ) )
522 if( GraphicsView_Viewer* aViewer = aViewFrame->getViewer() )
524 aViewManager = aViewer->getViewManager();
533 double aMouseX = theEvent->scenePos().x();
534 double aMouseY = theEvent->scenePos().y();
536 ViewManagerRole aRole = getViewManagerRole( aViewManager );
537 if( aRole == VMR_General )
539 int aXDeg = 0, aYDeg = 0;
540 int aXMin = 0, aYMin = 0;
541 double aXSec = 0, aYSec = 0;
542 HYDROGUI_Tool::DoubleToLambert( aMouseX, aXDeg, aXMin, aXSec );
543 HYDROGUI_Tool::DoubleToLambert( aMouseY, aYDeg, aYMin, aYSec );
545 QString aDegSymbol( QChar( 0x00B0 ) );
546 QString aXStr = QString( "%1%2 %3' %4\"" ).arg( aXDeg ).arg( aDegSymbol ).arg( aXMin ).arg( aXSec );
547 QString aYStr = QString( "%1%2 %3' %4\"" ).arg( aYDeg ).arg( aDegSymbol ).arg( aYMin ).arg( aYSec );
549 aViewPort->setViewLabelText( QString( "X: %1\nY: %2" ).arg( aXStr ).arg( aYStr ) );
551 else if( aRole == VMR_TransformImage )
552 aViewPort->setViewLabelText( QString( "X: %1\nY: %2" ).arg( (int)aMouseX ).arg( (int)aMouseY ) );
556 void HYDROGUI_Module::updateGV( const bool theIsInit,
557 const bool theIsForced )
559 if( !getDisplayer() )
562 QList<int> aViewManagerIdList;
564 // currently, all views are updated
565 ViewManagerMapIterator anIter( myViewManagerMap );
566 while( anIter.hasNext() )
568 int anId = anIter.next().key();
569 aViewManagerIdList.append( anId );
572 QListIterator<int> anIdIter( aViewManagerIdList );
573 while( anIdIter.hasNext() )
574 getDisplayer()->UpdateAll( anIdIter.next(), theIsInit, theIsForced );
577 void HYDROGUI_Module::createSelector( SUIT_ViewManager* theViewManager )
579 if( !theViewManager )
582 LightApp_SelectionMgr* aSelectionMgr = getApp()->selectionMgr();
586 QString aViewType = theViewManager->getType();
587 if( aViewType != GraphicsView_Viewer::Type() )
590 GraphicsView_ViewManager* aViewManager =
591 dynamic_cast<GraphicsView_ViewManager*>( theViewManager );
595 QList<SUIT_Selector*> aSelectorList;
596 aSelectionMgr->selectors( aViewType, aSelectorList );
598 // disable all alien selectors
599 QList<SUIT_Selector*>::iterator anIter, anIterEnd = aSelectorList.end();
600 for( anIter = aSelectorList.begin(); anIter != anIterEnd; anIter++ )
602 SUIT_Selector* aSelector = *anIter;
603 if( aSelector && !dynamic_cast<HYDROGUI_GVSelector*>( aSelector ) )
604 aSelector->setEnabled( false );
607 new HYDROGUI_GVSelector( this, aViewManager->getViewer(), aSelectionMgr );
610 bool HYDROGUI_Module::setUpdateEnabled( const bool theState )
612 bool aPrevState = myIsUpdateEnabled;
613 myIsUpdateEnabled = theState;
617 bool HYDROGUI_Module::isUpdateEnabled() const
619 return myIsUpdateEnabled;