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 extern "C" HYDRO_EXPORT char* getModuleVersion()
73 return (char*)HYDRO_VERSION;
76 HYDROGUI_Module::HYDROGUI_Module()
77 : LightApp_Module( "HYDRO" ),
79 myIsUpdateEnabled( true )
83 HYDROGUI_Module::~HYDROGUI_Module()
87 int HYDROGUI_Module::getStudyId() const
89 LightApp_Application* anApp = getApp();
90 return anApp ? anApp->activeStudy()->id() : 0;
93 void HYDROGUI_Module::initialize( CAM_Application* theApp )
95 LightApp_Module::initialize( theApp );
98 createUndoRedoActions();
103 setMenuShown( false );
104 setToolShown( false );
106 myDisplayer = new HYDROGUI_Displayer( this );
109 bool HYDROGUI_Module::activateModule( SUIT_Study* theStudy )
111 bool aRes = LightApp_Module::activateModule( theStudy );
113 setMenuShown( true );
114 setToolShown( true );
118 updateCommandsStatus();
123 bool HYDROGUI_Module::deactivateModule( SUIT_Study* theStudy )
125 ViewManagerMapIterator anIter( myViewManagerMap );
126 while( anIter.hasNext() )
127 if( SUIT_ViewManager* aViewManager = anIter.next().value().first )
128 getApp()->removeViewManager( aViewManager );
129 myViewManagerMap.clear();
131 myObjectStateMap.clear();
133 setMenuShown( false );
134 setToolShown( false );
136 return LightApp_Module::deactivateModule( theStudy );
139 void HYDROGUI_Module::windows( QMap<int, int>& theMap ) const
142 theMap.insert( LightApp_Application::WT_LogWindow, Qt::BottomDockWidgetArea );
143 theMap.insert( LightApp_Application::WT_ObjectBrowser, Qt::LeftDockWidgetArea );
146 void HYDROGUI_Module::viewManagers( QStringList& theTypesList ) const
148 theTypesList << GraphicsView_Viewer::Type();
151 void HYDROGUI_Module::contextMenuPopup( const QString& theClient,
155 HYDROGUI_DataModel* aModel = getDataModel();
157 size_t aViewId = HYDROGUI_Tool::GetActiveGraphicsViewId( this );
159 bool anIsSelection = false;
160 bool anIsVisibleInSelection = false;
161 bool anIsHiddenInSelection = false;
163 bool anIsImage = false;
164 bool anIsImportedImage = false;
165 bool anIsCompositeImage = false;
166 bool anIsFusedImage = false;
167 bool anIsCutImage = false;
168 bool anIsSplittedImage = false;
169 bool anIsMustBeUpdatedImage = false;
170 bool anIsPolyline = false;
171 bool anIsCalculation = false;
172 bool anIsVisualState = false;
174 HYDROData_SequenceOfObjects aSeq = HYDROGUI_Tool::GetSelectedObjects( this );
175 for( Standard_Integer anIndex = 1, aLength = aSeq.Length(); anIndex <= aLength; anIndex++ )
177 Handle(HYDROData_Object) anObject = aSeq.Value( anIndex );
178 if( !anObject.IsNull() )
180 anIsSelection = true;
182 bool aVisibility = isObjectVisible( aViewId, anObject );
183 anIsVisibleInSelection |= aVisibility;
184 anIsHiddenInSelection |= !aVisibility;
186 if( anObject->GetKind() == KIND_IMAGE )
189 Handle(HYDROData_Image) anImage = Handle(HYDROData_Image)::DownCast( anObject );
190 if( !anImage.IsNull() )
192 anIsImportedImage = anImage->HasTrsfPoints() && !anImage->IsSelfSplitted();
193 anIsCompositeImage = anImage->NbReferences() > 0;
194 if( HYDROOperations_Factory* aFactory = HYDROOperations_Factory::Factory() )
196 if( ImageComposer_Operator* anOperator = aFactory->Operator( anImage ) )
198 if( dynamic_cast<ImageComposer_FuseOperator*>( anOperator ) )
199 anIsFusedImage = true;
200 else if( dynamic_cast<ImageComposer_CutOperator*>( anOperator ) )
202 else if( dynamic_cast<ImageComposer_CropOperator*>( anOperator ) )
203 anIsSplittedImage = true;
206 anIsMustBeUpdatedImage = anImage->MustBeUpdated();
209 else if( anObject->GetKind() == KIND_POLYLINE )
211 else if( anObject->GetKind() == KIND_CALCULATION )
212 anIsCalculation = true;
213 else if( anObject->GetKind() == KIND_VISUAL_STATE )
214 anIsVisualState = true;
220 theMenu->addAction( action( SaveVisualStateId ) );
221 theMenu->addSeparator();
224 if( anIsSelection && anIsMustBeUpdatedImage )
226 theMenu->addAction( action( UpdateImageId ) );
227 theMenu->addSeparator();
230 if( anIsSelection && aSeq.Length() == 1 )
234 if( anIsImportedImage )
235 theMenu->addAction( action( EditImportedImageId ) );
236 else if( anIsCompositeImage )
239 theMenu->addAction( action( EditFusedImageId ) );
240 else if( anIsCutImage )
241 theMenu->addAction( action( EditCutImageId ) );
242 else if( anIsSplittedImage )
243 theMenu->addAction( action( EditSplittedImageId ) );
246 theMenu->addAction( action( ObserveImageId ) );
247 theMenu->addAction( action( ExportImageId ) );
248 theMenu->addSeparator();
250 else if( anIsPolyline )
252 theMenu->addAction( action( EditPolylineId ) );
253 theMenu->addSeparator();
255 else if( anIsCalculation )
257 theMenu->addAction( action( EditCalculationId ) );
258 theMenu->addSeparator();
260 else if( anIsVisualState )
262 theMenu->addAction( action( SaveVisualStateId ) );
263 theMenu->addAction( action( LoadVisualStateId ) );
264 theMenu->addSeparator();
270 theMenu->addAction( action( DeleteId ) );
271 theMenu->addSeparator();
274 if( anIsSelection && ( anIsImage || anIsPolyline ) )
276 if( anIsHiddenInSelection )
277 theMenu->addAction( action( ShowId ) );
278 theMenu->addAction( action( ShowOnlyId ) );
279 if( anIsVisibleInSelection )
280 theMenu->addAction( action( HideId ) );
281 theMenu->addSeparator();
284 theMenu->addAction( action( ShowAllId ) );
285 theMenu->addAction( action( HideAllId ) );
286 theMenu->addSeparator();
289 void HYDROGUI_Module::update( const int flags )
291 if( !isUpdateEnabled() )
294 QApplication::setOverrideCursor( Qt::WaitCursor );
296 // To prevent calling this method recursively
297 // from one of the methods called below
298 setUpdateEnabled( false );
300 if( ( flags & UF_Viewer ) )
301 updateGV( flags & UF_GV_Init,
302 flags & UF_GV_Forced );
304 if( ( flags & UF_Model ) && getDataModel() && getApp() )
306 getDataModel()->update( getStudyId() );
308 // Temporary workaround to prevent breaking
309 // the selection in the object browser.
310 // Note: processEvents() should be called after updateGV(),
311 // otherwise the application crashes from time to time.
312 qApp->processEvents();
313 getApp()->updateObjectBrowser( true );
316 // Object browser is currently updated by using UF_Model flag
317 //if( ( flags & UF_ObjBrowser ) && getApp() )
318 // getApp()->updateObjectBrowser( true );
320 if( ( flags & UF_Controls ) && getApp() )
321 getApp()->updateActions();
323 setUpdateEnabled( true );
325 QApplication::restoreOverrideCursor();
328 void HYDROGUI_Module::updateCommandsStatus()
330 LightApp_Module::updateCommandsStatus();
332 updateUndoRedoControls();
335 //action( ... )->setEnabled( ... );
338 HYDROGUI_DataModel* HYDROGUI_Module::getDataModel() const
340 return (HYDROGUI_DataModel*)dataModel();
343 HYDROGUI_Displayer* HYDROGUI_Module::getDisplayer() const
348 GraphicsView_Viewer* HYDROGUI_Module::getViewer( const int theId ) const
350 if( myViewManagerMap.contains( theId ) )
352 ViewManagerInfo anInfo = myViewManagerMap[ theId ];
353 GraphicsView_ViewManager* aViewManager =
354 dynamic_cast<GraphicsView_ViewManager*>( anInfo.first );
356 return aViewManager->getViewer();
361 int HYDROGUI_Module::getViewManagerId( SUIT_ViewManager* theViewManager )
363 ViewManagerMapIterator anIter( myViewManagerMap );
364 while( anIter.hasNext() )
366 int anId = anIter.next().key();
367 const ViewManagerInfo& anInfo = anIter.value();
368 if( anInfo.first == theViewManager )
374 HYDROGUI_Module::ViewManagerRole HYDROGUI_Module::getViewManagerRole( SUIT_ViewManager* theViewManager )
376 int anId = getViewManagerId( theViewManager );
379 const ViewManagerInfo& anInfo = myViewManagerMap[ anId ];
380 return anInfo.second;
385 void HYDROGUI_Module::setViewManagerRole( SUIT_ViewManager* theViewManager,
386 const ViewManagerRole theRole )
388 int anId = getViewManagerId( theViewManager );
391 ViewManagerInfo& anInfo = myViewManagerMap[ anId ];
392 anInfo.second = theRole;
396 bool HYDROGUI_Module::isObjectVisible( const int theViewId,
397 const Handle(HYDROData_Object)& theObject )
399 if( theObject.IsNull() )
402 ViewId2Name2ObjectStateMap::const_iterator anIter1 = myObjectStateMap.find( theViewId );
403 if( anIter1 != myObjectStateMap.end() )
405 const Name2ObjectStateMap& aName2ObjectStateMap = anIter1.value();
406 Name2ObjectStateMap::const_iterator anIter2 = aName2ObjectStateMap.find( theObject->GetName());
407 if( anIter2 != aName2ObjectStateMap.end() )
409 const ObjectState& anObjectState = anIter2.value();
410 return anObjectState.Visibility;
416 void HYDROGUI_Module::setObjectVisible( const int theViewId,
417 const Handle(HYDROData_Object)& theObject,
418 const bool theState )
420 if( !theObject.IsNull() )
422 Name2ObjectStateMap& aName2ObjectStateMap = myObjectStateMap[ theViewId ];
423 ObjectState& anObjectState = aName2ObjectStateMap[ theObject->GetName() ];
424 anObjectState.Visibility = theState;
428 CAM_DataModel* HYDROGUI_Module::createDataModel()
430 return new HYDROGUI_DataModel( this );
433 void HYDROGUI_Module::customEvent( QEvent* e )
435 int aType = e->type();
436 if ( aType == NewViewEvent )
438 SALOME_CustomEvent* ce = ( SALOME_CustomEvent* )e;
439 if( GraphicsView_ViewFrame* aViewFrame = ( GraphicsView_ViewFrame* )ce->data() )
441 if( GraphicsView_Viewer* aViewer = dynamic_cast<GraphicsView_Viewer*>( aViewFrame->getViewer() ) )
443 if( GraphicsView_ViewPort* aViewPort = aViewer->getActiveViewPort() )
445 aViewPort->setInteractionFlag( GraphicsView_ViewPort::TraceBoundingRect );
446 aViewPort->setInteractionFlag( GraphicsView_ViewPort::ImmediateContextMenu );
447 aViewPort->setInteractionFlag( GraphicsView_ViewPort::ImmediateSelection );
449 //ouv: temporarily commented
450 //aViewPort->setViewLabelPosition( GraphicsView_ViewPort::VLP_BottomLeft, true );
453 SUIT_ViewManager* aViewManager = aViewer->getViewManager();
454 ViewManagerRole aRole = getViewManagerRole( aViewManager );
455 if( aRole != VMR_TransformImage )
458 aViewer->activateTransform( GraphicsView_Viewer::FitAll );
464 bool HYDROGUI_Module::eventFilter( QObject* theObj, QEvent* theEvent )
466 QEvent::Type aType = theEvent->type();
467 if( theObj->inherits( "GraphicsView_ViewFrame" ) )
469 if( aType == QEvent::Show )
471 SALOME_CustomEvent* e = new SALOME_CustomEvent( NewViewEvent );
472 e->setData( theObj );
473 QApplication::postEvent( this, e );
474 theObj->removeEventFilter( this );
477 return LightApp_Module::eventFilter( theObj, theEvent );
480 void HYDROGUI_Module::onViewManagerAdded( SUIT_ViewManager* theViewManager )
482 LightApp_Module::onViewManagerAdded( theViewManager );
484 if( theViewManager->getType() == GraphicsView_Viewer::Type() )
486 createSelector( theViewManager ); // replace the default selector
488 connect( theViewManager, SIGNAL( viewCreated( SUIT_ViewWindow* ) ),
489 this, SLOT( onViewCreated( SUIT_ViewWindow* ) ) );
492 ViewManagerInfo anInfo( theViewManager, VMR_General );
493 myViewManagerMap.insert( ViewManagerId++, anInfo );
496 void HYDROGUI_Module::onViewManagerRemoved( SUIT_ViewManager* theViewManager )
498 LightApp_Module::onViewManagerRemoved( theViewManager );
500 createSelector( theViewManager ); // replace the default selector
502 int anId = getViewManagerId( theViewManager );
504 myViewManagerMap.remove( anId );
507 void HYDROGUI_Module::onViewCreated( SUIT_ViewWindow* theViewWindow )
509 if( theViewWindow && theViewWindow->inherits( "GraphicsView_ViewFrame" ) )
511 if( GraphicsView_ViewFrame* aViewFrame = dynamic_cast<GraphicsView_ViewFrame*>( theViewWindow ) )
513 aViewFrame->installEventFilter( this );
515 GraphicsView_ViewPort* aViewPort = aViewFrame->getViewPort();
517 connect( aViewPort, SIGNAL( vpMouseEvent( QGraphicsSceneMouseEvent* ) ),
518 this, SLOT( onViewPortMouseEvent( QGraphicsSceneMouseEvent* ) ) );
524 void HYDROGUI_Module::onViewPortMouseEvent( QGraphicsSceneMouseEvent* theEvent )
526 if( GraphicsView_ViewPort* aViewPort = qobject_cast<GraphicsView_ViewPort*>( sender() ) )
528 SUIT_ViewManager* aViewManager = 0;
530 QObject* aParent = aViewPort;
531 while( aParent = aParent->parent() )
533 if( GraphicsView_ViewFrame* aViewFrame = dynamic_cast<GraphicsView_ViewFrame*>( aParent ) )
535 if( GraphicsView_Viewer* aViewer = aViewFrame->getViewer() )
537 aViewManager = aViewer->getViewManager();
546 double aMouseX = theEvent->scenePos().x();
547 double aMouseY = theEvent->scenePos().y();
549 ViewManagerRole aRole = getViewManagerRole( aViewManager );
550 if( aRole == VMR_General )
552 int aXDeg = 0, aYDeg = 0;
553 int aXMin = 0, aYMin = 0;
554 double aXSec = 0, aYSec = 0;
555 HYDROGUI_Tool::DoubleToLambert( aMouseX, aXDeg, aXMin, aXSec );
556 HYDROGUI_Tool::DoubleToLambert( aMouseY, aYDeg, aYMin, aYSec );
558 QString aDegSymbol( QChar( 0x00B0 ) );
559 QString aXStr = QString( "%1%2 %3' %4\"" ).arg( aXDeg ).arg( aDegSymbol ).arg( aXMin ).arg( aXSec );
560 QString aYStr = QString( "%1%2 %3' %4\"" ).arg( aYDeg ).arg( aDegSymbol ).arg( aYMin ).arg( aYSec );
562 aViewPort->setViewLabelText( QString( "X: %1\nY: %2" ).arg( aXStr ).arg( aYStr ) );
564 else if( aRole == VMR_TransformImage )
565 aViewPort->setViewLabelText( QString( "X: %1\nY: %2" ).arg( (int)aMouseX ).arg( (int)aMouseY ) );
569 void HYDROGUI_Module::updateGV( const bool theIsInit,
570 const bool theIsForced )
572 if( !getDisplayer() )
575 QList<int> aViewManagerIdList;
577 // currently, all views are updated
578 ViewManagerMapIterator anIter( myViewManagerMap );
579 while( anIter.hasNext() )
581 int anId = anIter.next().key();
582 aViewManagerIdList.append( anId );
585 QListIterator<int> anIdIter( aViewManagerIdList );
586 while( anIdIter.hasNext() )
587 getDisplayer()->UpdateAll( anIdIter.next(), theIsInit, theIsForced );
590 void HYDROGUI_Module::createSelector( SUIT_ViewManager* theViewManager )
592 if( !theViewManager )
595 LightApp_SelectionMgr* aSelectionMgr = getApp()->selectionMgr();
599 QString aViewType = theViewManager->getType();
600 if( aViewType != GraphicsView_Viewer::Type() )
603 GraphicsView_ViewManager* aViewManager =
604 dynamic_cast<GraphicsView_ViewManager*>( theViewManager );
608 QList<SUIT_Selector*> aSelectorList;
609 aSelectionMgr->selectors( aViewType, aSelectorList );
611 // disable all alien selectors
612 QList<SUIT_Selector*>::iterator anIter, anIterEnd = aSelectorList.end();
613 for( anIter = aSelectorList.begin(); anIter != anIterEnd; anIter++ )
615 SUIT_Selector* aSelector = *anIter;
616 if( aSelector && !dynamic_cast<HYDROGUI_GVSelector*>( aSelector ) )
617 aSelector->setEnabled( false );
620 new HYDROGUI_GVSelector( this, aViewManager->getViewer(), aSelectionMgr );
623 bool HYDROGUI_Module::setUpdateEnabled( const bool theState )
625 bool aPrevState = myIsUpdateEnabled;
626 myIsUpdateEnabled = theState;
630 bool HYDROGUI_Module::isUpdateEnabled() const
632 return myIsUpdateEnabled;