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 <GraphicsView_ViewFrame.h>
39 #include <GraphicsView_ViewManager.h>
40 #include <GraphicsView_ViewPort.h>
41 #include <GraphicsView_Viewer.h>
43 #include <LightApp_Application.h>
44 #include <LightApp_GVSelector.h>
45 #include <LightApp_SelectionMgr.h>
46 #include <LightApp_UpdateFlags.h>
48 #include <SALOME_Event.h>
50 #include <SUIT_Study.h>
51 #include <SUIT_ViewManager.h>
54 #include <QApplication>
55 #include <QGraphicsSceneMouseEvent>
58 static int ViewManagerId = 0;
60 extern "C" HYDRO_EXPORT CAM_Module* createModule()
62 return new HYDROGUI_Module();
65 HYDROGUI_Module::HYDROGUI_Module()
66 : LightApp_Module( "HYDRO" ),
68 myIsUpdateEnabled( true )
72 HYDROGUI_Module::~HYDROGUI_Module()
76 int HYDROGUI_Module::getStudyId() const
78 LightApp_Application* anApp = getApp();
79 return anApp ? anApp->activeStudy()->id() : 0;
82 void HYDROGUI_Module::initialize( CAM_Application* theApp )
84 LightApp_Module::initialize( theApp );
87 createUndoRedoActions();
92 setMenuShown( false );
93 setToolShown( false );
95 myDisplayer = new HYDROGUI_Displayer( this );
98 bool HYDROGUI_Module::activateModule( SUIT_Study* theStudy )
100 bool aRes = LightApp_Module::activateModule( theStudy );
102 setMenuShown( true );
103 setToolShown( true );
107 updateCommandsStatus();
112 bool HYDROGUI_Module::deactivateModule( SUIT_Study* theStudy )
114 ViewManagerMapIterator anIter( myViewManagerMap );
115 while( anIter.hasNext() )
116 if( SUIT_ViewManager* aViewManager = anIter.next().value().first )
117 getApp()->removeViewManager( aViewManager );
118 myViewManagerMap.clear();
120 setMenuShown( false );
121 setToolShown( false );
123 return LightApp_Module::deactivateModule( theStudy );
126 void HYDROGUI_Module::windows( QMap<int, int>& theMap ) const
129 theMap.insert( LightApp_Application::WT_LogWindow, Qt::BottomDockWidgetArea );
130 theMap.insert( LightApp_Application::WT_ObjectBrowser, Qt::LeftDockWidgetArea );
133 void HYDROGUI_Module::viewManagers( QStringList& theTypesList ) const
135 theTypesList << GraphicsView_Viewer::Type();
138 void HYDROGUI_Module::contextMenuPopup( const QString& theClient,
142 HYDROGUI_DataModel* aModel = getDataModel();
144 size_t aViewId = HYDROGUI_Tool::GetActiveGraphicsViewId( this );
146 bool anIsSelection = false;
147 bool anIsVisibleInSelection = false;
148 bool anIsHiddenInSelection = false;
150 bool anIsImage = false;
151 bool anIsCompositeImage = false;
152 bool anIsMustBeUpdatedImage = false;
153 bool anIsPolyline = false;
154 bool anIsVisualState = false;
156 HYDROData_SequenceOfObjects aSeq = HYDROGUI_Tool::GetSelectedObjects( this );
157 for( Standard_Integer anIndex = 1, aLength = aSeq.Length(); anIndex <= aLength; anIndex++ )
159 Handle(HYDROData_Object) anObject = aSeq.Value( anIndex );
160 if( !anObject.IsNull() )
162 anIsSelection = true;
164 bool aVisibility = anObject->IsVisible( aViewId );
165 anIsVisibleInSelection |= aVisibility;
166 anIsHiddenInSelection |= !aVisibility;
168 if( anObject->GetKind() == KIND_IMAGE )
171 Handle(HYDROData_Image) anImage = Handle(HYDROData_Image)::DownCast( anObject );
172 if( !anImage.IsNull() )
174 anIsCompositeImage = anImage->NbReferences() > 0;
175 anIsMustBeUpdatedImage = anImage->MustBeUpdated();
178 else if( anObject->GetKind() == KIND_POLYLINE )
180 else if( anObject->GetKind() == KIND_VISUAL_STATE )
181 anIsVisualState = true;
187 theMenu->addAction( action( SaveVisualStateId ) );
188 theMenu->addSeparator();
191 if( anIsSelection && anIsMustBeUpdatedImage )
193 theMenu->addAction( action( UpdateImageId ) );
194 theMenu->addSeparator();
197 if( anIsSelection && aSeq.Length() == 1 )
201 theMenu->addAction( action( anIsCompositeImage ? EditCompositeImageId : EditImportedImageId ) );
202 theMenu->addAction( action( ObserveImageId ) );
203 theMenu->addAction( action( ExportImageId ) );
204 theMenu->addSeparator();
206 else if( anIsPolyline )
208 theMenu->addAction( action( EditPolylineId ) );
209 theMenu->addSeparator();
211 else if( anIsVisualState )
213 theMenu->addAction( action( SaveVisualStateId ) );
214 theMenu->addAction( action( LoadVisualStateId ) );
215 theMenu->addSeparator();
221 theMenu->addAction( action( DeleteId ) );
222 theMenu->addSeparator();
225 if( anIsSelection && ( anIsImage || anIsPolyline ) )
227 if( anIsHiddenInSelection )
228 theMenu->addAction( action( ShowId ) );
229 theMenu->addAction( action( ShowOnlyId ) );
230 if( anIsVisibleInSelection )
231 theMenu->addAction( action( HideId ) );
232 theMenu->addSeparator();
235 theMenu->addAction( action( ShowAllId ) );
236 theMenu->addAction( action( HideAllId ) );
237 theMenu->addSeparator();
240 void HYDROGUI_Module::update( const int flags )
242 if( !isUpdateEnabled() )
245 QApplication::setOverrideCursor( Qt::WaitCursor );
247 // To prevent calling this method recursively
248 // from one of the methods called below
249 setUpdateEnabled( false );
251 if( ( flags & UF_Model ) && getDataModel() && getApp() )
253 getDataModel()->update( getStudyId() );
255 // Temporary workaround to prevent breaking
256 // the selection in the object browser
257 qApp->processEvents();
258 getApp()->updateObjectBrowser( true );
261 // Object browser is currently updated by using UF_Model flag
262 //if( ( flags & UF_ObjBrowser ) && getApp() )
263 // getApp()->updateObjectBrowser( true );
265 if( ( flags & UF_Viewer ) )
266 updateGV( flags & UF_GV_Init,
267 flags & UF_GV_Forced );
269 if( ( flags & UF_Controls ) && getApp() )
270 getApp()->updateActions();
272 setUpdateEnabled( true );
274 QApplication::restoreOverrideCursor();
277 void HYDROGUI_Module::updateCommandsStatus()
279 LightApp_Module::updateCommandsStatus();
281 updateUndoRedoControls();
284 //action( ... )->setEnabled( ... );
287 HYDROGUI_DataModel* HYDROGUI_Module::getDataModel() const
289 return (HYDROGUI_DataModel*)dataModel();
292 HYDROGUI_Displayer* HYDROGUI_Module::getDisplayer() const
297 GraphicsView_Viewer* HYDROGUI_Module::getViewer( const int theId ) const
299 if( myViewManagerMap.contains( theId ) )
301 ViewManagerInfo anInfo = myViewManagerMap[ theId ];
302 GraphicsView_ViewManager* aViewManager =
303 dynamic_cast<GraphicsView_ViewManager*>( anInfo.first );
305 return aViewManager->getViewer();
310 int HYDROGUI_Module::getViewManagerId( SUIT_ViewManager* theViewManager )
312 ViewManagerMapIterator anIter( myViewManagerMap );
313 while( anIter.hasNext() )
315 int anId = anIter.next().key();
316 const ViewManagerInfo& anInfo = anIter.value();
317 if( anInfo.first == theViewManager )
323 HYDROGUI_Module::ViewManagerRole HYDROGUI_Module::getViewManagerRole( SUIT_ViewManager* theViewManager )
325 int anId = getViewManagerId( theViewManager );
328 const ViewManagerInfo& anInfo = myViewManagerMap[ anId ];
329 return anInfo.second;
334 void HYDROGUI_Module::setViewManagerRole( SUIT_ViewManager* theViewManager,
335 const ViewManagerRole theRole )
337 int anId = getViewManagerId( theViewManager );
340 ViewManagerInfo& anInfo = myViewManagerMap[ anId ];
341 anInfo.second = theRole;
345 CAM_DataModel* HYDROGUI_Module::createDataModel()
347 return new HYDROGUI_DataModel( this );
350 void HYDROGUI_Module::customEvent( QEvent* e )
352 int aType = e->type();
353 if ( aType == NewViewEvent )
355 SALOME_CustomEvent* ce = ( SALOME_CustomEvent* )e;
356 if( GraphicsView_ViewFrame* aViewFrame = ( GraphicsView_ViewFrame* )ce->data() )
358 if( GraphicsView_Viewer* aViewer = dynamic_cast<GraphicsView_Viewer*>( aViewFrame->getViewer() ) )
360 SUIT_ViewManager* aViewManager = aViewer->getViewManager();
361 ViewManagerRole aRole = getViewManagerRole( aViewManager );
362 if( aRole == VMR_General )
365 aViewer->activateTransform( GraphicsView_Viewer::FitAll );
367 if( GraphicsView_ViewPort* aViewPort = aViewer->getActiveViewPort() )
369 aViewPort->setInteractionFlag( GraphicsView_ViewPort::TraceBoundingRect );
370 aViewPort->setInteractionFlag( GraphicsView_ViewPort::ImmediateContextMenu );
371 aViewPort->setInteractionFlag( GraphicsView_ViewPort::ImmediateSelection );
373 //ouv: temporarily commented
374 //aViewPort->setViewLabelPosition( GraphicsView_ViewPort::VLP_BottomLeft, true );
381 bool HYDROGUI_Module::eventFilter( QObject* theObj, QEvent* theEvent )
383 QEvent::Type aType = theEvent->type();
384 if( theObj->inherits( "GraphicsView_ViewFrame" ) )
386 if( aType == QEvent::Show )
388 SALOME_CustomEvent* e = new SALOME_CustomEvent( NewViewEvent );
389 e->setData( theObj );
390 QApplication::postEvent( this, e );
391 theObj->removeEventFilter( this );
394 return LightApp_Module::eventFilter( theObj, theEvent );
397 void HYDROGUI_Module::onViewManagerAdded( SUIT_ViewManager* theViewManager )
399 LightApp_Module::onViewManagerAdded( theViewManager );
401 if( theViewManager->getType() == GraphicsView_Viewer::Type() )
403 createSelector( theViewManager ); // replace the default selector
405 connect( theViewManager, SIGNAL( viewCreated( SUIT_ViewWindow* ) ),
406 this, SLOT( onViewCreated( SUIT_ViewWindow* ) ) );
409 ViewManagerInfo anInfo( theViewManager, VMR_General );
410 myViewManagerMap.insert( ViewManagerId++, anInfo );
413 void HYDROGUI_Module::onViewManagerRemoved( SUIT_ViewManager* theViewManager )
415 LightApp_Module::onViewManagerRemoved( theViewManager );
417 createSelector( theViewManager ); // replace the default selector
419 int anId = getViewManagerId( theViewManager );
421 myViewManagerMap.remove( anId );
424 void HYDROGUI_Module::onViewCreated( SUIT_ViewWindow* theViewWindow )
426 if( theViewWindow && theViewWindow->inherits( "GraphicsView_ViewFrame" ) )
428 if( GraphicsView_ViewFrame* aViewFrame = dynamic_cast<GraphicsView_ViewFrame*>( theViewWindow ) )
430 aViewFrame->installEventFilter( this );
432 GraphicsView_ViewPort* aViewPort = aViewFrame->getViewPort();
434 connect( aViewPort, SIGNAL( vpMouseEvent( QGraphicsSceneMouseEvent* ) ),
435 this, SLOT( onViewPortMouseEvent( QGraphicsSceneMouseEvent* ) ) );
441 void HYDROGUI_Module::onViewPortMouseEvent( QGraphicsSceneMouseEvent* theEvent )
443 if( GraphicsView_ViewPort* aViewPort = qobject_cast<GraphicsView_ViewPort*>( sender() ) )
445 SUIT_ViewManager* aViewManager = 0;
447 QObject* aParent = aViewPort;
448 while( aParent = aParent->parent() )
450 if( GraphicsView_ViewFrame* aViewFrame = dynamic_cast<GraphicsView_ViewFrame*>( aParent ) )
452 if( GraphicsView_Viewer* aViewer = aViewFrame->getViewer() )
454 aViewManager = aViewer->getViewManager();
463 double aMouseX = theEvent->scenePos().x();
464 double aMouseY = theEvent->scenePos().y();
466 ViewManagerRole aRole = getViewManagerRole( aViewManager );
467 if( aRole == VMR_General )
469 int aXDeg = 0, aYDeg = 0;
470 int aXMin = 0, aYMin = 0;
471 double aXSec = 0, aYSec = 0;
472 HYDROGUI_Tool::DoubleToLambert( aMouseX, aXDeg, aXMin, aXSec );
473 HYDROGUI_Tool::DoubleToLambert( aMouseY, aYDeg, aYMin, aYSec );
475 QString aDegSymbol( QChar( 0x00B0 ) );
476 QString aXStr = QString( "%1%2 %3' %4\"" ).arg( aXDeg ).arg( aDegSymbol ).arg( aXMin ).arg( aXSec );
477 QString aYStr = QString( "%1%2 %3' %4\"" ).arg( aYDeg ).arg( aDegSymbol ).arg( aYMin ).arg( aYSec );
479 aViewPort->setViewLabelText( QString( "X: %1\nY: %2" ).arg( aXStr ).arg( aYStr ) );
481 else if( aRole == VMR_TransformImage )
482 aViewPort->setViewLabelText( QString( "X: %1\nY: %2" ).arg( (int)aMouseX ).arg( (int)aMouseY ) );
486 void HYDROGUI_Module::updateGV( const bool theIsInit,
487 const bool theIsForced )
489 if( !getDisplayer() )
492 QList<int> aViewManagerIdList;
494 // currently, all views are updated
495 ViewManagerMapIterator anIter( myViewManagerMap );
496 while( anIter.hasNext() )
498 int anId = anIter.next().key();
499 aViewManagerIdList.append( anId );
502 QListIterator<int> anIdIter( aViewManagerIdList );
503 while( anIdIter.hasNext() )
504 getDisplayer()->UpdateAll( anIdIter.next(), theIsInit, theIsForced );
507 void HYDROGUI_Module::createSelector( SUIT_ViewManager* theViewManager )
509 if( !theViewManager )
512 LightApp_SelectionMgr* aSelectionMgr = getApp()->selectionMgr();
516 QString aViewType = theViewManager->getType();
517 if( aViewType != GraphicsView_Viewer::Type() )
520 GraphicsView_ViewManager* aViewManager =
521 dynamic_cast<GraphicsView_ViewManager*>( theViewManager );
525 QList<SUIT_Selector*> aSelectorList;
526 aSelectionMgr->selectors( aViewType, aSelectorList );
528 // disable all alien selectors
529 QList<SUIT_Selector*>::iterator anIter, anIterEnd = aSelectorList.end();
530 for( anIter = aSelectorList.begin(); anIter != anIterEnd; anIter++ )
532 SUIT_Selector* aSelector = *anIter;
533 if( aSelector && !dynamic_cast<HYDROGUI_GVSelector*>( aSelector ) )
534 aSelector->setEnabled( false );
537 new HYDROGUI_GVSelector( this, aViewManager->getViewer(), aSelectionMgr );
540 bool HYDROGUI_Module::setUpdateEnabled( const bool theState )
542 bool aPrevState = myIsUpdateEnabled;
543 myIsUpdateEnabled = theState;
547 bool HYDROGUI_Module::isUpdateEnabled() const
549 return myIsUpdateEnabled;