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()
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 anIsVisualState = false;
173 HYDROData_SequenceOfObjects aSeq = HYDROGUI_Tool::GetSelectedObjects( this );
174 for( Standard_Integer anIndex = 1, aLength = aSeq.Length(); anIndex <= aLength; anIndex++ )
176 Handle(HYDROData_Object) anObject = aSeq.Value( anIndex );
177 if( !anObject.IsNull() )
179 anIsSelection = true;
181 bool aVisibility = isObjectVisible( aViewId, anObject );
182 anIsVisibleInSelection |= aVisibility;
183 anIsHiddenInSelection |= !aVisibility;
185 if( anObject->GetKind() == KIND_IMAGE )
188 Handle(HYDROData_Image) anImage = Handle(HYDROData_Image)::DownCast( anObject );
189 if( !anImage.IsNull() )
191 anIsImportedImage = anImage->HasTrsfPoints() && !anImage->IsSelfSplitted();
192 anIsCompositeImage = anImage->NbReferences() > 0;
193 if( HYDROOperations_Factory* aFactory = HYDROOperations_Factory::Factory() )
195 if( ImageComposer_Operator* anOperator = aFactory->Operator( anImage ) )
197 if( dynamic_cast<ImageComposer_FuseOperator*>( anOperator ) )
198 anIsFusedImage = true;
199 else if( dynamic_cast<ImageComposer_CutOperator*>( anOperator ) )
201 else if( dynamic_cast<ImageComposer_CropOperator*>( anOperator ) )
202 anIsSplittedImage = true;
205 anIsMustBeUpdatedImage = anImage->MustBeUpdated();
208 else if( anObject->GetKind() == KIND_POLYLINE )
210 else if( anObject->GetKind() == KIND_VISUAL_STATE )
211 anIsVisualState = true;
217 theMenu->addAction( action( SaveVisualStateId ) );
218 theMenu->addSeparator();
221 if( anIsSelection && anIsMustBeUpdatedImage )
223 theMenu->addAction( action( UpdateImageId ) );
224 theMenu->addSeparator();
227 if( anIsSelection && aSeq.Length() == 1 )
231 if( anIsImportedImage )
232 theMenu->addAction( action( EditImportedImageId ) );
233 else if( anIsCompositeImage )
236 theMenu->addAction( action( EditFusedImageId ) );
237 else if( anIsCutImage )
238 theMenu->addAction( action( EditCutImageId ) );
239 else if( anIsSplittedImage )
240 theMenu->addAction( action( EditSplittedImageId ) );
243 theMenu->addAction( action( ObserveImageId ) );
244 theMenu->addAction( action( ExportImageId ) );
245 theMenu->addSeparator();
247 else if( anIsPolyline )
249 theMenu->addAction( action( EditPolylineId ) );
250 theMenu->addSeparator();
252 else if( anIsVisualState )
254 theMenu->addAction( action( SaveVisualStateId ) );
255 theMenu->addAction( action( LoadVisualStateId ) );
256 theMenu->addSeparator();
262 theMenu->addAction( action( DeleteId ) );
263 theMenu->addSeparator();
266 if( anIsSelection && ( anIsImage || anIsPolyline ) )
268 if( anIsHiddenInSelection )
269 theMenu->addAction( action( ShowId ) );
270 theMenu->addAction( action( ShowOnlyId ) );
271 if( anIsVisibleInSelection )
272 theMenu->addAction( action( HideId ) );
273 theMenu->addSeparator();
276 theMenu->addAction( action( ShowAllId ) );
277 theMenu->addAction( action( HideAllId ) );
278 theMenu->addSeparator();
281 void HYDROGUI_Module::update( const int flags )
283 if( !isUpdateEnabled() )
286 QApplication::setOverrideCursor( Qt::WaitCursor );
288 // To prevent calling this method recursively
289 // from one of the methods called below
290 setUpdateEnabled( false );
292 if( ( flags & UF_Viewer ) )
293 updateGV( flags & UF_GV_Init,
294 flags & UF_GV_Forced );
296 if( ( flags & UF_Model ) && getDataModel() && getApp() )
298 getDataModel()->update( getStudyId() );
300 // Temporary workaround to prevent breaking
301 // the selection in the object browser.
302 // Note: processEvents() should be called after updateGV(),
303 // otherwise the application crashes from time to time.
304 qApp->processEvents();
305 getApp()->updateObjectBrowser( true );
308 // Object browser is currently updated by using UF_Model flag
309 //if( ( flags & UF_ObjBrowser ) && getApp() )
310 // getApp()->updateObjectBrowser( true );
312 if( ( flags & UF_Controls ) && getApp() )
313 getApp()->updateActions();
315 setUpdateEnabled( true );
317 QApplication::restoreOverrideCursor();
320 void HYDROGUI_Module::updateCommandsStatus()
322 LightApp_Module::updateCommandsStatus();
324 updateUndoRedoControls();
327 //action( ... )->setEnabled( ... );
330 HYDROGUI_DataModel* HYDROGUI_Module::getDataModel() const
332 return (HYDROGUI_DataModel*)dataModel();
335 HYDROGUI_Displayer* HYDROGUI_Module::getDisplayer() const
340 GraphicsView_Viewer* HYDROGUI_Module::getViewer( const int theId ) const
342 if( myViewManagerMap.contains( theId ) )
344 ViewManagerInfo anInfo = myViewManagerMap[ theId ];
345 GraphicsView_ViewManager* aViewManager =
346 dynamic_cast<GraphicsView_ViewManager*>( anInfo.first );
348 return aViewManager->getViewer();
353 int HYDROGUI_Module::getViewManagerId( SUIT_ViewManager* theViewManager )
355 ViewManagerMapIterator anIter( myViewManagerMap );
356 while( anIter.hasNext() )
358 int anId = anIter.next().key();
359 const ViewManagerInfo& anInfo = anIter.value();
360 if( anInfo.first == theViewManager )
366 HYDROGUI_Module::ViewManagerRole HYDROGUI_Module::getViewManagerRole( SUIT_ViewManager* theViewManager )
368 int anId = getViewManagerId( theViewManager );
371 const ViewManagerInfo& anInfo = myViewManagerMap[ anId ];
372 return anInfo.second;
377 void HYDROGUI_Module::setViewManagerRole( SUIT_ViewManager* theViewManager,
378 const ViewManagerRole theRole )
380 int anId = getViewManagerId( theViewManager );
383 ViewManagerInfo& anInfo = myViewManagerMap[ anId ];
384 anInfo.second = theRole;
388 bool HYDROGUI_Module::isObjectVisible( const int theViewId,
389 const Handle(HYDROData_Object)& theObject )
391 if( theObject.IsNull() )
394 ViewId2Name2ObjectStateMap::const_iterator anIter1 = myObjectStateMap.find( theViewId );
395 if( anIter1 != myObjectStateMap.end() )
397 const Name2ObjectStateMap& aName2ObjectStateMap = anIter1.value();
398 Name2ObjectStateMap::const_iterator anIter2 = aName2ObjectStateMap.find( theObject->GetName());
399 if( anIter2 != aName2ObjectStateMap.end() )
401 const ObjectState& anObjectState = anIter2.value();
402 return anObjectState.Visibility;
408 void HYDROGUI_Module::setObjectVisible( const int theViewId,
409 const Handle(HYDROData_Object)& theObject,
410 const bool theState )
412 if( !theObject.IsNull() )
414 Name2ObjectStateMap& aName2ObjectStateMap = myObjectStateMap[ theViewId ];
415 ObjectState& anObjectState = aName2ObjectStateMap[ theObject->GetName() ];
416 anObjectState.Visibility = theState;
420 CAM_DataModel* HYDROGUI_Module::createDataModel()
422 return new HYDROGUI_DataModel( this );
425 void HYDROGUI_Module::customEvent( QEvent* e )
427 int aType = e->type();
428 if ( aType == NewViewEvent )
430 SALOME_CustomEvent* ce = ( SALOME_CustomEvent* )e;
431 if( GraphicsView_ViewFrame* aViewFrame = ( GraphicsView_ViewFrame* )ce->data() )
433 if( GraphicsView_Viewer* aViewer = dynamic_cast<GraphicsView_Viewer*>( aViewFrame->getViewer() ) )
435 if( GraphicsView_ViewPort* aViewPort = aViewer->getActiveViewPort() )
437 aViewPort->setInteractionFlag( GraphicsView_ViewPort::TraceBoundingRect );
438 aViewPort->setInteractionFlag( GraphicsView_ViewPort::ImmediateContextMenu );
439 aViewPort->setInteractionFlag( GraphicsView_ViewPort::ImmediateSelection );
441 //ouv: temporarily commented
442 //aViewPort->setViewLabelPosition( GraphicsView_ViewPort::VLP_BottomLeft, true );
445 SUIT_ViewManager* aViewManager = aViewer->getViewManager();
446 ViewManagerRole aRole = getViewManagerRole( aViewManager );
447 if( aRole != VMR_TransformImage )
450 aViewer->activateTransform( GraphicsView_Viewer::FitAll );
456 bool HYDROGUI_Module::eventFilter( QObject* theObj, QEvent* theEvent )
458 QEvent::Type aType = theEvent->type();
459 if( theObj->inherits( "GraphicsView_ViewFrame" ) )
461 if( aType == QEvent::Show )
463 SALOME_CustomEvent* e = new SALOME_CustomEvent( NewViewEvent );
464 e->setData( theObj );
465 QApplication::postEvent( this, e );
466 theObj->removeEventFilter( this );
469 return LightApp_Module::eventFilter( theObj, theEvent );
472 void HYDROGUI_Module::onViewManagerAdded( SUIT_ViewManager* theViewManager )
474 LightApp_Module::onViewManagerAdded( theViewManager );
476 if( theViewManager->getType() == GraphicsView_Viewer::Type() )
478 createSelector( theViewManager ); // replace the default selector
480 connect( theViewManager, SIGNAL( viewCreated( SUIT_ViewWindow* ) ),
481 this, SLOT( onViewCreated( SUIT_ViewWindow* ) ) );
484 ViewManagerInfo anInfo( theViewManager, VMR_General );
485 myViewManagerMap.insert( ViewManagerId++, anInfo );
488 void HYDROGUI_Module::onViewManagerRemoved( SUIT_ViewManager* theViewManager )
490 LightApp_Module::onViewManagerRemoved( theViewManager );
492 createSelector( theViewManager ); // replace the default selector
494 int anId = getViewManagerId( theViewManager );
496 myViewManagerMap.remove( anId );
499 void HYDROGUI_Module::onViewCreated( SUIT_ViewWindow* theViewWindow )
501 if( theViewWindow && theViewWindow->inherits( "GraphicsView_ViewFrame" ) )
503 if( GraphicsView_ViewFrame* aViewFrame = dynamic_cast<GraphicsView_ViewFrame*>( theViewWindow ) )
505 aViewFrame->installEventFilter( this );
507 GraphicsView_ViewPort* aViewPort = aViewFrame->getViewPort();
509 connect( aViewPort, SIGNAL( vpMouseEvent( QGraphicsSceneMouseEvent* ) ),
510 this, SLOT( onViewPortMouseEvent( QGraphicsSceneMouseEvent* ) ) );
516 void HYDROGUI_Module::onViewPortMouseEvent( QGraphicsSceneMouseEvent* theEvent )
518 if( GraphicsView_ViewPort* aViewPort = qobject_cast<GraphicsView_ViewPort*>( sender() ) )
520 SUIT_ViewManager* aViewManager = 0;
522 QObject* aParent = aViewPort;
523 while( aParent = aParent->parent() )
525 if( GraphicsView_ViewFrame* aViewFrame = dynamic_cast<GraphicsView_ViewFrame*>( aParent ) )
527 if( GraphicsView_Viewer* aViewer = aViewFrame->getViewer() )
529 aViewManager = aViewer->getViewManager();
538 double aMouseX = theEvent->scenePos().x();
539 double aMouseY = theEvent->scenePos().y();
541 ViewManagerRole aRole = getViewManagerRole( aViewManager );
542 if( aRole == VMR_General )
544 int aXDeg = 0, aYDeg = 0;
545 int aXMin = 0, aYMin = 0;
546 double aXSec = 0, aYSec = 0;
547 HYDROGUI_Tool::DoubleToLambert( aMouseX, aXDeg, aXMin, aXSec );
548 HYDROGUI_Tool::DoubleToLambert( aMouseY, aYDeg, aYMin, aYSec );
550 QString aDegSymbol( QChar( 0x00B0 ) );
551 QString aXStr = QString( "%1%2 %3' %4\"" ).arg( aXDeg ).arg( aDegSymbol ).arg( aXMin ).arg( aXSec );
552 QString aYStr = QString( "%1%2 %3' %4\"" ).arg( aYDeg ).arg( aDegSymbol ).arg( aYMin ).arg( aYSec );
554 aViewPort->setViewLabelText( QString( "X: %1\nY: %2" ).arg( aXStr ).arg( aYStr ) );
556 else if( aRole == VMR_TransformImage )
557 aViewPort->setViewLabelText( QString( "X: %1\nY: %2" ).arg( (int)aMouseX ).arg( (int)aMouseY ) );
561 void HYDROGUI_Module::updateGV( const bool theIsInit,
562 const bool theIsForced )
564 if( !getDisplayer() )
567 QList<int> aViewManagerIdList;
569 // currently, all views are updated
570 ViewManagerMapIterator anIter( myViewManagerMap );
571 while( anIter.hasNext() )
573 int anId = anIter.next().key();
574 aViewManagerIdList.append( anId );
577 QListIterator<int> anIdIter( aViewManagerIdList );
578 while( anIdIter.hasNext() )
579 getDisplayer()->UpdateAll( anIdIter.next(), theIsInit, theIsForced );
582 void HYDROGUI_Module::createSelector( SUIT_ViewManager* theViewManager )
584 if( !theViewManager )
587 LightApp_SelectionMgr* aSelectionMgr = getApp()->selectionMgr();
591 QString aViewType = theViewManager->getType();
592 if( aViewType != GraphicsView_Viewer::Type() )
595 GraphicsView_ViewManager* aViewManager =
596 dynamic_cast<GraphicsView_ViewManager*>( theViewManager );
600 QList<SUIT_Selector*> aSelectorList;
601 aSelectionMgr->selectors( aViewType, aSelectorList );
603 // disable all alien selectors
604 QList<SUIT_Selector*>::iterator anIter, anIterEnd = aSelectorList.end();
605 for( anIter = aSelectorList.begin(); anIter != anIterEnd; anIter++ )
607 SUIT_Selector* aSelector = *anIter;
608 if( aSelector && !dynamic_cast<HYDROGUI_GVSelector*>( aSelector ) )
609 aSelector->setEnabled( false );
612 new HYDROGUI_GVSelector( this, aViewManager->getViewer(), aSelectionMgr );
615 bool HYDROGUI_Module::setUpdateEnabled( const bool theState )
617 bool aPrevState = myIsUpdateEnabled;
618 myIsUpdateEnabled = theState;
622 bool HYDROGUI_Module::isUpdateEnabled() const
624 return myIsUpdateEnabled;