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 myObjectStateMap.clear();
122 setMenuShown( false );
123 setToolShown( false );
125 return LightApp_Module::deactivateModule( theStudy );
128 void HYDROGUI_Module::windows( QMap<int, int>& theMap ) const
131 theMap.insert( LightApp_Application::WT_LogWindow, Qt::BottomDockWidgetArea );
132 theMap.insert( LightApp_Application::WT_ObjectBrowser, Qt::LeftDockWidgetArea );
135 void HYDROGUI_Module::viewManagers( QStringList& theTypesList ) const
137 theTypesList << GraphicsView_Viewer::Type();
140 void HYDROGUI_Module::contextMenuPopup( const QString& theClient,
144 HYDROGUI_DataModel* aModel = getDataModel();
146 size_t aViewId = HYDROGUI_Tool::GetActiveGraphicsViewId( this );
148 bool anIsSelection = false;
149 bool anIsVisibleInSelection = false;
150 bool anIsHiddenInSelection = false;
152 bool anIsImage = false;
153 bool anIsCompositeImage = false;
154 bool anIsMustBeUpdatedImage = false;
155 bool anIsPolyline = false;
156 bool anIsVisualState = false;
158 HYDROData_SequenceOfObjects aSeq = HYDROGUI_Tool::GetSelectedObjects( this );
159 for( Standard_Integer anIndex = 1, aLength = aSeq.Length(); anIndex <= aLength; anIndex++ )
161 Handle(HYDROData_Object) anObject = aSeq.Value( anIndex );
162 if( !anObject.IsNull() )
164 anIsSelection = true;
166 bool aVisibility = isObjectVisible( aViewId, anObject );
167 anIsVisibleInSelection |= aVisibility;
168 anIsHiddenInSelection |= !aVisibility;
170 if( anObject->GetKind() == KIND_IMAGE )
173 Handle(HYDROData_Image) anImage = Handle(HYDROData_Image)::DownCast( anObject );
174 if( !anImage.IsNull() )
176 anIsCompositeImage = anImage->NbReferences() > 0;
177 anIsMustBeUpdatedImage = anImage->MustBeUpdated();
180 else if( anObject->GetKind() == KIND_POLYLINE )
182 else if( anObject->GetKind() == KIND_VISUAL_STATE )
183 anIsVisualState = true;
189 theMenu->addAction( action( SaveVisualStateId ) );
190 theMenu->addSeparator();
193 if( anIsSelection && anIsMustBeUpdatedImage )
195 theMenu->addAction( action( UpdateImageId ) );
196 theMenu->addSeparator();
199 if( anIsSelection && aSeq.Length() == 1 )
203 theMenu->addAction( action( anIsCompositeImage ? EditCompositeImageId : EditImportedImageId ) );
204 theMenu->addAction( action( ObserveImageId ) );
205 theMenu->addAction( action( ExportImageId ) );
206 theMenu->addSeparator();
208 else if( anIsPolyline )
210 theMenu->addAction( action( EditPolylineId ) );
211 theMenu->addSeparator();
213 else if( anIsVisualState )
215 theMenu->addAction( action( SaveVisualStateId ) );
216 theMenu->addAction( action( LoadVisualStateId ) );
217 theMenu->addSeparator();
223 theMenu->addAction( action( DeleteId ) );
224 theMenu->addSeparator();
227 if( anIsSelection && ( anIsImage || anIsPolyline ) )
229 if( anIsHiddenInSelection )
230 theMenu->addAction( action( ShowId ) );
231 theMenu->addAction( action( ShowOnlyId ) );
232 if( anIsVisibleInSelection )
233 theMenu->addAction( action( HideId ) );
234 theMenu->addSeparator();
237 theMenu->addAction( action( ShowAllId ) );
238 theMenu->addAction( action( HideAllId ) );
239 theMenu->addSeparator();
242 void HYDROGUI_Module::update( const int flags )
244 if( !isUpdateEnabled() )
247 QApplication::setOverrideCursor( Qt::WaitCursor );
249 // To prevent calling this method recursively
250 // from one of the methods called below
251 setUpdateEnabled( false );
253 if( ( flags & UF_Viewer ) )
254 updateGV( flags & UF_GV_Init,
255 flags & UF_GV_Forced );
257 if( ( flags & UF_Model ) && getDataModel() && getApp() )
259 getDataModel()->update( getStudyId() );
261 // Temporary workaround to prevent breaking
262 // the selection in the object browser.
263 // Note: processEvents() should be called after updateGV(),
264 // otherwise the application crashes from time to time.
265 qApp->processEvents();
266 getApp()->updateObjectBrowser( true );
269 // Object browser is currently updated by using UF_Model flag
270 //if( ( flags & UF_ObjBrowser ) && getApp() )
271 // getApp()->updateObjectBrowser( true );
273 if( ( flags & UF_Controls ) && getApp() )
274 getApp()->updateActions();
276 setUpdateEnabled( true );
278 QApplication::restoreOverrideCursor();
281 void HYDROGUI_Module::updateCommandsStatus()
283 LightApp_Module::updateCommandsStatus();
285 updateUndoRedoControls();
288 //action( ... )->setEnabled( ... );
291 HYDROGUI_DataModel* HYDROGUI_Module::getDataModel() const
293 return (HYDROGUI_DataModel*)dataModel();
296 HYDROGUI_Displayer* HYDROGUI_Module::getDisplayer() const
301 GraphicsView_Viewer* HYDROGUI_Module::getViewer( const int theId ) const
303 if( myViewManagerMap.contains( theId ) )
305 ViewManagerInfo anInfo = myViewManagerMap[ theId ];
306 GraphicsView_ViewManager* aViewManager =
307 dynamic_cast<GraphicsView_ViewManager*>( anInfo.first );
309 return aViewManager->getViewer();
314 int HYDROGUI_Module::getViewManagerId( SUIT_ViewManager* theViewManager )
316 ViewManagerMapIterator anIter( myViewManagerMap );
317 while( anIter.hasNext() )
319 int anId = anIter.next().key();
320 const ViewManagerInfo& anInfo = anIter.value();
321 if( anInfo.first == theViewManager )
327 HYDROGUI_Module::ViewManagerRole HYDROGUI_Module::getViewManagerRole( SUIT_ViewManager* theViewManager )
329 int anId = getViewManagerId( theViewManager );
332 const ViewManagerInfo& anInfo = myViewManagerMap[ anId ];
333 return anInfo.second;
338 void HYDROGUI_Module::setViewManagerRole( SUIT_ViewManager* theViewManager,
339 const ViewManagerRole theRole )
341 int anId = getViewManagerId( theViewManager );
344 ViewManagerInfo& anInfo = myViewManagerMap[ anId ];
345 anInfo.second = theRole;
349 bool HYDROGUI_Module::isObjectVisible( const int theViewId,
350 const Handle(HYDROData_Object)& theObject )
352 if( theObject.IsNull() )
355 ViewId2Name2ObjectStateMap::const_iterator anIter1 = myObjectStateMap.find( theViewId );
356 if( anIter1 != myObjectStateMap.end() )
358 const Name2ObjectStateMap& aName2ObjectStateMap = anIter1.value();
359 Name2ObjectStateMap::const_iterator anIter2 = aName2ObjectStateMap.find( theObject->GetName());
360 if( anIter2 != aName2ObjectStateMap.end() )
362 const ObjectState& anObjectState = anIter2.value();
363 return anObjectState.Visibility;
369 void HYDROGUI_Module::setObjectVisible( const int theViewId,
370 const Handle(HYDROData_Object)& theObject,
371 const bool theState )
373 if( !theObject.IsNull() )
375 Name2ObjectStateMap& aName2ObjectStateMap = myObjectStateMap[ theViewId ];
376 ObjectState& anObjectState = aName2ObjectStateMap[ theObject->GetName() ];
377 anObjectState.Visibility = theState;
381 CAM_DataModel* HYDROGUI_Module::createDataModel()
383 return new HYDROGUI_DataModel( this );
386 void HYDROGUI_Module::customEvent( QEvent* e )
388 int aType = e->type();
389 if ( aType == NewViewEvent )
391 SALOME_CustomEvent* ce = ( SALOME_CustomEvent* )e;
392 if( GraphicsView_ViewFrame* aViewFrame = ( GraphicsView_ViewFrame* )ce->data() )
394 if( GraphicsView_Viewer* aViewer = dynamic_cast<GraphicsView_Viewer*>( aViewFrame->getViewer() ) )
396 if( GraphicsView_ViewPort* aViewPort = aViewer->getActiveViewPort() )
398 aViewPort->setInteractionFlag( GraphicsView_ViewPort::TraceBoundingRect );
399 aViewPort->setInteractionFlag( GraphicsView_ViewPort::ImmediateContextMenu );
400 aViewPort->setInteractionFlag( GraphicsView_ViewPort::ImmediateSelection );
402 //ouv: temporarily commented
403 //aViewPort->setViewLabelPosition( GraphicsView_ViewPort::VLP_BottomLeft, true );
406 SUIT_ViewManager* aViewManager = aViewer->getViewManager();
407 ViewManagerRole aRole = getViewManagerRole( aViewManager );
408 if( aRole != VMR_TransformImage )
411 aViewer->activateTransform( GraphicsView_Viewer::FitAll );
417 bool HYDROGUI_Module::eventFilter( QObject* theObj, QEvent* theEvent )
419 QEvent::Type aType = theEvent->type();
420 if( theObj->inherits( "GraphicsView_ViewFrame" ) )
422 if( aType == QEvent::Show )
424 SALOME_CustomEvent* e = new SALOME_CustomEvent( NewViewEvent );
425 e->setData( theObj );
426 QApplication::postEvent( this, e );
427 theObj->removeEventFilter( this );
430 return LightApp_Module::eventFilter( theObj, theEvent );
433 void HYDROGUI_Module::onViewManagerAdded( SUIT_ViewManager* theViewManager )
435 LightApp_Module::onViewManagerAdded( theViewManager );
437 if( theViewManager->getType() == GraphicsView_Viewer::Type() )
439 createSelector( theViewManager ); // replace the default selector
441 connect( theViewManager, SIGNAL( viewCreated( SUIT_ViewWindow* ) ),
442 this, SLOT( onViewCreated( SUIT_ViewWindow* ) ) );
445 ViewManagerInfo anInfo( theViewManager, VMR_General );
446 myViewManagerMap.insert( ViewManagerId++, anInfo );
449 void HYDROGUI_Module::onViewManagerRemoved( SUIT_ViewManager* theViewManager )
451 LightApp_Module::onViewManagerRemoved( theViewManager );
453 createSelector( theViewManager ); // replace the default selector
455 int anId = getViewManagerId( theViewManager );
457 myViewManagerMap.remove( anId );
460 void HYDROGUI_Module::onViewCreated( SUIT_ViewWindow* theViewWindow )
462 if( theViewWindow && theViewWindow->inherits( "GraphicsView_ViewFrame" ) )
464 if( GraphicsView_ViewFrame* aViewFrame = dynamic_cast<GraphicsView_ViewFrame*>( theViewWindow ) )
466 aViewFrame->installEventFilter( this );
468 GraphicsView_ViewPort* aViewPort = aViewFrame->getViewPort();
470 connect( aViewPort, SIGNAL( vpMouseEvent( QGraphicsSceneMouseEvent* ) ),
471 this, SLOT( onViewPortMouseEvent( QGraphicsSceneMouseEvent* ) ) );
477 void HYDROGUI_Module::onViewPortMouseEvent( QGraphicsSceneMouseEvent* theEvent )
479 if( GraphicsView_ViewPort* aViewPort = qobject_cast<GraphicsView_ViewPort*>( sender() ) )
481 SUIT_ViewManager* aViewManager = 0;
483 QObject* aParent = aViewPort;
484 while( aParent = aParent->parent() )
486 if( GraphicsView_ViewFrame* aViewFrame = dynamic_cast<GraphicsView_ViewFrame*>( aParent ) )
488 if( GraphicsView_Viewer* aViewer = aViewFrame->getViewer() )
490 aViewManager = aViewer->getViewManager();
499 double aMouseX = theEvent->scenePos().x();
500 double aMouseY = theEvent->scenePos().y();
502 ViewManagerRole aRole = getViewManagerRole( aViewManager );
503 if( aRole == VMR_General )
505 int aXDeg = 0, aYDeg = 0;
506 int aXMin = 0, aYMin = 0;
507 double aXSec = 0, aYSec = 0;
508 HYDROGUI_Tool::DoubleToLambert( aMouseX, aXDeg, aXMin, aXSec );
509 HYDROGUI_Tool::DoubleToLambert( aMouseY, aYDeg, aYMin, aYSec );
511 QString aDegSymbol( QChar( 0x00B0 ) );
512 QString aXStr = QString( "%1%2 %3' %4\"" ).arg( aXDeg ).arg( aDegSymbol ).arg( aXMin ).arg( aXSec );
513 QString aYStr = QString( "%1%2 %3' %4\"" ).arg( aYDeg ).arg( aDegSymbol ).arg( aYMin ).arg( aYSec );
515 aViewPort->setViewLabelText( QString( "X: %1\nY: %2" ).arg( aXStr ).arg( aYStr ) );
517 else if( aRole == VMR_TransformImage )
518 aViewPort->setViewLabelText( QString( "X: %1\nY: %2" ).arg( (int)aMouseX ).arg( (int)aMouseY ) );
522 void HYDROGUI_Module::updateGV( const bool theIsInit,
523 const bool theIsForced )
525 if( !getDisplayer() )
528 QList<int> aViewManagerIdList;
530 // currently, all views are updated
531 ViewManagerMapIterator anIter( myViewManagerMap );
532 while( anIter.hasNext() )
534 int anId = anIter.next().key();
535 aViewManagerIdList.append( anId );
538 QListIterator<int> anIdIter( aViewManagerIdList );
539 while( anIdIter.hasNext() )
540 getDisplayer()->UpdateAll( anIdIter.next(), theIsInit, theIsForced );
543 void HYDROGUI_Module::createSelector( SUIT_ViewManager* theViewManager )
545 if( !theViewManager )
548 LightApp_SelectionMgr* aSelectionMgr = getApp()->selectionMgr();
552 QString aViewType = theViewManager->getType();
553 if( aViewType != GraphicsView_Viewer::Type() )
556 GraphicsView_ViewManager* aViewManager =
557 dynamic_cast<GraphicsView_ViewManager*>( theViewManager );
561 QList<SUIT_Selector*> aSelectorList;
562 aSelectionMgr->selectors( aViewType, aSelectorList );
564 // disable all alien selectors
565 QList<SUIT_Selector*>::iterator anIter, anIterEnd = aSelectorList.end();
566 for( anIter = aSelectorList.begin(); anIter != anIterEnd; anIter++ )
568 SUIT_Selector* aSelector = *anIter;
569 if( aSelector && !dynamic_cast<HYDROGUI_GVSelector*>( aSelector ) )
570 aSelector->setEnabled( false );
573 new HYDROGUI_GVSelector( this, aViewManager->getViewer(), aSelectionMgr );
576 bool HYDROGUI_Module::setUpdateEnabled( const bool theState )
578 bool aPrevState = myIsUpdateEnabled;
579 myIsUpdateEnabled = theState;
583 bool HYDROGUI_Module::isUpdateEnabled() const
585 return myIsUpdateEnabled;