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 size_t aViewId = HYDROGUI_Tool::GetActiveGraphicsViewId( this );
178 bool anIsSelection = false;
179 bool anIsVisibleInSelection = false;
180 bool anIsHiddenInSelection = false;
182 bool anIsImage = false;
183 bool anIsImportedImage = false;
184 bool anIsCompositeImage = false;
185 bool anIsFusedImage = false;
186 bool anIsCutImage = false;
187 bool anIsSplittedImage = false;
188 bool anIsMustBeUpdatedImage = false;
189 bool anIsPolyline = false;
190 bool anIsCalculation = false;
191 bool anIsVisualState = false;
193 HYDROData_SequenceOfObjects aSeq = HYDROGUI_Tool::GetSelectedObjects( this );
194 for( Standard_Integer anIndex = 1, aLength = aSeq.Length(); anIndex <= aLength; anIndex++ )
196 Handle(HYDROData_Object) anObject = aSeq.Value( anIndex );
197 if( !anObject.IsNull() )
199 anIsSelection = true;
201 bool aVisibility = isObjectVisible( aViewId, anObject );
202 anIsVisibleInSelection |= aVisibility;
203 anIsHiddenInSelection |= !aVisibility;
205 if( anObject->GetKind() == KIND_IMAGE )
208 Handle(HYDROData_Image) anImage = Handle(HYDROData_Image)::DownCast( anObject );
209 if( !anImage.IsNull() )
211 anIsImportedImage = anImage->HasTrsfPoints() && !anImage->IsSelfSplitted();
212 anIsCompositeImage = anImage->NbReferences() > 0;
213 if( HYDROOperations_Factory* aFactory = HYDROOperations_Factory::Factory() )
215 if( ImageComposer_Operator* anOperator = aFactory->Operator( anImage ) )
217 QString anOperatorName = anOperator->name();
218 if( anOperatorName == ImageComposer_FuseOperator::Type() )
219 anIsFusedImage = true;
220 else if( anOperatorName == ImageComposer_CutOperator::Type() )
222 else if( anOperatorName == ImageComposer_CropOperator::Type() )
223 anIsSplittedImage = true;
226 anIsMustBeUpdatedImage = anImage->MustBeUpdated();
229 else if( anObject->GetKind() == KIND_POLYLINE )
231 else if( anObject->GetKind() == KIND_CALCULATION )
232 anIsCalculation = true;
233 else if( anObject->GetKind() == KIND_VISUAL_STATE )
234 anIsVisualState = true;
240 theMenu->addAction( action( SaveVisualStateId ) );
241 theMenu->addSeparator();
244 if( anIsSelection && anIsMustBeUpdatedImage )
246 theMenu->addAction( action( UpdateImageId ) );
247 theMenu->addSeparator();
250 if( anIsSelection && aSeq.Length() == 1 )
254 if( anIsImportedImage )
255 theMenu->addAction( action( EditImportedImageId ) );
256 else if( anIsCompositeImage )
259 theMenu->addAction( action( EditFusedImageId ) );
260 else if( anIsCutImage )
261 theMenu->addAction( action( EditCutImageId ) );
262 else if( anIsSplittedImage )
263 theMenu->addAction( action( EditSplittedImageId ) );
266 theMenu->addAction( action( ObserveImageId ) );
267 theMenu->addAction( action( ExportImageId ) );
268 theMenu->addSeparator();
270 else if( anIsPolyline )
272 theMenu->addAction( action( EditPolylineId ) );
273 theMenu->addSeparator();
275 else if( anIsCalculation )
277 theMenu->addAction( action( EditCalculationId ) );
278 theMenu->addSeparator();
280 else if( anIsVisualState )
282 theMenu->addAction( action( SaveVisualStateId ) );
283 theMenu->addAction( action( LoadVisualStateId ) );
284 theMenu->addSeparator();
290 theMenu->addAction( action( DeleteId ) );
291 theMenu->addSeparator();
294 if( anIsSelection && ( anIsImage || anIsPolyline ) )
296 if( anIsHiddenInSelection )
297 theMenu->addAction( action( ShowId ) );
298 theMenu->addAction( action( ShowOnlyId ) );
299 if( anIsVisibleInSelection )
300 theMenu->addAction( action( HideId ) );
301 theMenu->addSeparator();
304 theMenu->addAction( action( ShowAllId ) );
305 theMenu->addAction( action( HideAllId ) );
306 theMenu->addSeparator();
309 void HYDROGUI_Module::update( const int flags )
311 if( !isUpdateEnabled() )
314 QApplication::setOverrideCursor( Qt::WaitCursor );
316 // To prevent calling this method recursively
317 // from one of the methods called below
318 setUpdateEnabled( false );
320 if( ( flags & UF_Viewer ) )
321 updateGV( flags & UF_GV_Init,
322 flags & UF_GV_Forced );
324 if( ( flags & UF_Model ) && getDataModel() && getApp() )
326 getDataModel()->update( getStudyId() );
328 // Temporary workaround to prevent breaking
329 // the selection in the object browser.
330 // Note: processEvents() should be called after updateGV(),
331 // otherwise the application crashes from time to time.
332 qApp->processEvents();
333 getApp()->updateObjectBrowser( true );
336 // Object browser is currently updated by using UF_Model flag
337 //if( ( flags & UF_ObjBrowser ) && getApp() )
338 // getApp()->updateObjectBrowser( true );
340 if( ( flags & UF_Controls ) && getApp() )
341 getApp()->updateActions();
343 setUpdateEnabled( true );
345 QApplication::restoreOverrideCursor();
348 void HYDROGUI_Module::updateCommandsStatus()
350 LightApp_Module::updateCommandsStatus();
352 updateUndoRedoControls();
354 action( CopyId )->setEnabled( getDataModel()->canCopy() );
355 action( PasteId )->setEnabled( getDataModel()->canPaste() );
358 void HYDROGUI_Module::selectionChanged()
360 LightApp_Module::selectionChanged();
361 updateCommandsStatus();
364 HYDROGUI_DataModel* HYDROGUI_Module::getDataModel() const
366 return (HYDROGUI_DataModel*)dataModel();
369 HYDROGUI_Displayer* HYDROGUI_Module::getDisplayer() const
374 GraphicsView_Viewer* HYDROGUI_Module::getViewer( const int theId ) const
376 if( myViewManagerMap.contains( theId ) )
378 ViewManagerInfo anInfo = myViewManagerMap[ theId ];
379 GraphicsView_ViewManager* aViewManager =
380 dynamic_cast<GraphicsView_ViewManager*>( anInfo.first );
382 return aViewManager->getViewer();
387 int HYDROGUI_Module::getViewManagerId( SUIT_ViewManager* theViewManager )
389 ViewManagerMapIterator anIter( myViewManagerMap );
390 while( anIter.hasNext() )
392 int anId = anIter.next().key();
393 const ViewManagerInfo& anInfo = anIter.value();
394 if( anInfo.first == theViewManager )
400 HYDROGUI_Module::ViewManagerRole HYDROGUI_Module::getViewManagerRole( SUIT_ViewManager* theViewManager )
402 int anId = getViewManagerId( theViewManager );
405 const ViewManagerInfo& anInfo = myViewManagerMap[ anId ];
406 return anInfo.second;
411 void HYDROGUI_Module::setViewManagerRole( SUIT_ViewManager* theViewManager,
412 const ViewManagerRole theRole )
414 int anId = getViewManagerId( theViewManager );
417 ViewManagerInfo& anInfo = myViewManagerMap[ anId ];
418 anInfo.second = theRole;
422 bool HYDROGUI_Module::isObjectVisible( const int theViewId,
423 const Handle(HYDROData_Object)& theObject )
425 if( theObject.IsNull() )
428 ViewId2Name2ObjectStateMap::const_iterator anIter1 = myObjectStateMap.find( theViewId );
429 if( anIter1 != myObjectStateMap.end() )
431 const Name2ObjectStateMap& aName2ObjectStateMap = anIter1.value();
432 Name2ObjectStateMap::const_iterator anIter2 = aName2ObjectStateMap.find( theObject->GetName());
433 if( anIter2 != aName2ObjectStateMap.end() )
435 const ObjectState& anObjectState = anIter2.value();
436 return anObjectState.Visibility;
442 void HYDROGUI_Module::setObjectVisible( const int theViewId,
443 const Handle(HYDROData_Object)& theObject,
444 const bool theState )
446 if( !theObject.IsNull() )
448 Name2ObjectStateMap& aName2ObjectStateMap = myObjectStateMap[ theViewId ];
449 ObjectState& anObjectState = aName2ObjectStateMap[ theObject->GetName() ];
450 anObjectState.Visibility = theState;
454 CAM_DataModel* HYDROGUI_Module::createDataModel()
456 return new HYDROGUI_DataModel( this );
459 void HYDROGUI_Module::customEvent( QEvent* e )
461 int aType = e->type();
462 if ( aType == NewViewEvent )
464 SALOME_CustomEvent* ce = ( SALOME_CustomEvent* )e;
465 if( GraphicsView_ViewFrame* aViewFrame = ( GraphicsView_ViewFrame* )ce->data() )
467 if( GraphicsView_Viewer* aViewer = dynamic_cast<GraphicsView_Viewer*>( aViewFrame->getViewer() ) )
469 if( GraphicsView_ViewPort* aViewPort = aViewer->getActiveViewPort() )
471 aViewPort->setInteractionFlag( GraphicsView_ViewPort::TraceBoundingRect );
472 aViewPort->setInteractionFlag( GraphicsView_ViewPort::ImmediateContextMenu );
473 aViewPort->setInteractionFlag( GraphicsView_ViewPort::ImmediateSelection );
475 //ouv: temporarily commented
476 //aViewPort->setViewLabelPosition( GraphicsView_ViewPort::VLP_BottomLeft, true );
479 SUIT_ViewManager* aViewManager = aViewer->getViewManager();
480 ViewManagerRole aRole = getViewManagerRole( aViewManager );
481 if( aRole != VMR_TransformImage )
484 aViewer->activateTransform( GraphicsView_Viewer::FitAll );
490 bool HYDROGUI_Module::eventFilter( QObject* theObj, QEvent* theEvent )
492 QEvent::Type aType = theEvent->type();
493 if( theObj->inherits( "GraphicsView_ViewFrame" ) )
495 if( aType == QEvent::Show )
497 SALOME_CustomEvent* e = new SALOME_CustomEvent( NewViewEvent );
498 e->setData( theObj );
499 QApplication::postEvent( this, e );
500 theObj->removeEventFilter( this );
503 return LightApp_Module::eventFilter( theObj, theEvent );
506 void HYDROGUI_Module::onViewManagerAdded( SUIT_ViewManager* theViewManager )
508 LightApp_Module::onViewManagerAdded( theViewManager );
510 if( theViewManager->getType() == GraphicsView_Viewer::Type() )
512 createSelector( theViewManager ); // replace the default selector
514 connect( theViewManager, SIGNAL( viewCreated( SUIT_ViewWindow* ) ),
515 this, SLOT( onViewCreated( SUIT_ViewWindow* ) ) );
518 ViewManagerInfo anInfo( theViewManager, VMR_General );
519 myViewManagerMap.insert( ViewManagerId++, anInfo );
522 void HYDROGUI_Module::onViewManagerRemoved( SUIT_ViewManager* theViewManager )
524 LightApp_Module::onViewManagerRemoved( theViewManager );
526 createSelector( theViewManager ); // replace the default selector
528 int anId = getViewManagerId( theViewManager );
530 myViewManagerMap.remove( anId );
533 void HYDROGUI_Module::onViewCreated( SUIT_ViewWindow* theViewWindow )
535 if( theViewWindow && theViewWindow->inherits( "GraphicsView_ViewFrame" ) )
537 if( GraphicsView_ViewFrame* aViewFrame = dynamic_cast<GraphicsView_ViewFrame*>( theViewWindow ) )
539 aViewFrame->installEventFilter( this );
541 GraphicsView_ViewPort* aViewPort = aViewFrame->getViewPort();
543 connect( aViewPort, SIGNAL( vpMouseEvent( QGraphicsSceneMouseEvent* ) ),
544 this, SLOT( onViewPortMouseEvent( QGraphicsSceneMouseEvent* ) ) );
550 void HYDROGUI_Module::onViewPortMouseEvent( QGraphicsSceneMouseEvent* theEvent )
552 if( GraphicsView_ViewPort* aViewPort = qobject_cast<GraphicsView_ViewPort*>( sender() ) )
554 SUIT_ViewManager* aViewManager = 0;
556 QObject* aParent = aViewPort;
557 while( aParent = aParent->parent() )
559 if( GraphicsView_ViewFrame* aViewFrame = dynamic_cast<GraphicsView_ViewFrame*>( aParent ) )
561 if( GraphicsView_Viewer* aViewer = aViewFrame->getViewer() )
563 aViewManager = aViewer->getViewManager();
572 double aMouseX = theEvent->scenePos().x();
573 double aMouseY = theEvent->scenePos().y();
575 ViewManagerRole aRole = getViewManagerRole( aViewManager );
576 if( aRole == VMR_General )
578 int aXDeg = 0, aYDeg = 0;
579 int aXMin = 0, aYMin = 0;
580 double aXSec = 0, aYSec = 0;
581 HYDROGUI_Tool::DoubleToLambert( aMouseX, aXDeg, aXMin, aXSec );
582 HYDROGUI_Tool::DoubleToLambert( aMouseY, aYDeg, aYMin, aYSec );
584 QString aDegSymbol( QChar( 0x00B0 ) );
585 QString aXStr = QString( "%1%2 %3' %4\"" ).arg( aXDeg ).arg( aDegSymbol ).arg( aXMin ).arg( aXSec );
586 QString aYStr = QString( "%1%2 %3' %4\"" ).arg( aYDeg ).arg( aDegSymbol ).arg( aYMin ).arg( aYSec );
588 aViewPort->setViewLabelText( QString( "X: %1\nY: %2" ).arg( aXStr ).arg( aYStr ) );
590 else if( aRole == VMR_TransformImage )
591 aViewPort->setViewLabelText( QString( "X: %1\nY: %2" ).arg( (int)aMouseX ).arg( (int)aMouseY ) );
595 void HYDROGUI_Module::updateGV( const bool theIsInit,
596 const bool theIsForced )
598 if( !getDisplayer() )
601 QList<int> aViewManagerIdList;
603 // currently, all views are updated
604 ViewManagerMapIterator anIter( myViewManagerMap );
605 while( anIter.hasNext() )
607 int anId = anIter.next().key();
608 aViewManagerIdList.append( anId );
611 QListIterator<int> anIdIter( aViewManagerIdList );
612 while( anIdIter.hasNext() )
613 getDisplayer()->UpdateAll( anIdIter.next(), theIsInit, theIsForced );
616 void HYDROGUI_Module::createSelector( SUIT_ViewManager* theViewManager )
618 if( !theViewManager )
621 LightApp_SelectionMgr* aSelectionMgr = getApp()->selectionMgr();
625 QString aViewType = theViewManager->getType();
626 if( aViewType != GraphicsView_Viewer::Type() )
629 GraphicsView_ViewManager* aViewManager =
630 dynamic_cast<GraphicsView_ViewManager*>( theViewManager );
634 QList<SUIT_Selector*> aSelectorList;
635 aSelectionMgr->selectors( aViewType, aSelectorList );
637 // disable all alien selectors
638 QList<SUIT_Selector*>::iterator anIter, anIterEnd = aSelectorList.end();
639 for( anIter = aSelectorList.begin(); anIter != anIterEnd; anIter++ )
641 SUIT_Selector* aSelector = *anIter;
642 if( aSelector && !dynamic_cast<HYDROGUI_GVSelector*>( aSelector ) )
643 aSelector->setEnabled( false );
646 new HYDROGUI_GVSelector( this, aViewManager->getViewer(), aSelectionMgr );
649 bool HYDROGUI_Module::setUpdateEnabled( const bool theState )
651 bool aPrevState = myIsUpdateEnabled;
652 myIsUpdateEnabled = theState;
656 bool HYDROGUI_Module::isUpdateEnabled() const
658 return myIsUpdateEnabled;