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 <GraphicsView_ViewFrame.h>
37 #include <GraphicsView_ViewManager.h>
38 #include <GraphicsView_ViewPort.h>
39 #include <GraphicsView_Viewer.h>
41 #include <LightApp_Application.h>
42 #include <LightApp_GVSelector.h>
43 #include <LightApp_SelectionMgr.h>
44 #include <LightApp_UpdateFlags.h>
46 #include <SALOME_Event.h>
48 #include <SUIT_Study.h>
49 #include <SUIT_ViewManager.h>
52 #include <QApplication>
53 #include <QGraphicsSceneMouseEvent>
56 static int ViewManagerId = 0;
58 extern "C" HYDRO_EXPORT CAM_Module* createModule()
60 return new HYDROGUI_Module();
63 HYDROGUI_Module::HYDROGUI_Module()
64 : LightApp_Module( "HYDRO" ),
66 myIsUpdateEnabled( true )
70 HYDROGUI_Module::~HYDROGUI_Module()
74 int HYDROGUI_Module::getStudyId() const
76 LightApp_Application* anApp = getApp();
77 return anApp ? anApp->activeStudy()->id() : 0;
80 void HYDROGUI_Module::initialize( CAM_Application* theApp )
82 LightApp_Module::initialize( theApp );
85 createUndoRedoActions();
90 setMenuShown( false );
91 setToolShown( false );
93 myDisplayer = new HYDROGUI_Displayer( this );
96 bool HYDROGUI_Module::activateModule( SUIT_Study* theStudy )
98 bool aRes = LightApp_Module::activateModule( theStudy );
100 setMenuShown( true );
101 setToolShown( true );
105 updateCommandsStatus();
110 bool HYDROGUI_Module::deactivateModule( SUIT_Study* theStudy )
112 ViewManagerMapIterator anIter( myViewManagerMap );
113 while( anIter.hasNext() )
114 if( SUIT_ViewManager* aViewManager = anIter.next().value().first )
115 getApp()->removeViewManager( aViewManager );
116 myViewManagerMap.clear();
118 setMenuShown( false );
119 setToolShown( false );
121 return LightApp_Module::deactivateModule( theStudy );
124 void HYDROGUI_Module::windows( QMap<int, int>& theMap ) const
127 theMap.insert( LightApp_Application::WT_LogWindow, Qt::BottomDockWidgetArea );
128 theMap.insert( LightApp_Application::WT_ObjectBrowser, Qt::LeftDockWidgetArea );
131 void HYDROGUI_Module::viewManagers( QStringList& theTypesList ) const
133 theTypesList << GraphicsView_Viewer::Type();
136 void HYDROGUI_Module::contextMenuPopup( const QString& theClient,
140 HYDROGUI_DataModel* aModel = getDataModel();
142 int aViewId = HYDROGUI_Tool::GetActiveGraphicsViewId( this );
144 bool anIsSelection = false;
145 bool anIsVisibleInSelection = false;
146 bool anIsHiddenInSelection = false;
148 bool anIsImage = false;
149 bool anIsPolyline = false;
150 bool anIsVisualState = false;
152 HYDROData_SequenceOfObjects aSeq = HYDROGUI_Tool::GetSelectedObjects( this );
153 for( Standard_Integer anIndex = 1, aLength = aSeq.Length(); anIndex <= aLength; anIndex++ )
155 Handle(HYDROData_Object) anObject = aSeq.Value( anIndex );
156 if( !anObject.IsNull() )
158 anIsSelection = true;
160 bool aVisibility = anObject->IsVisible( aViewId );
161 anIsVisibleInSelection |= aVisibility;
162 anIsHiddenInSelection |= !aVisibility;
164 if( anObject->GetKind() == KIND_IMAGE )
166 else if( anObject->GetKind() == KIND_POLYLINE )
168 else if( anObject->GetKind() == KIND_VISUAL_STATE )
169 anIsVisualState = true;
175 theMenu->addAction( action( SaveVisualStateId ) );
176 theMenu->addSeparator();
178 else if( anIsSelection && aSeq.Length() == 1 )
182 theMenu->addAction( action( EditImageId ) );
183 theMenu->addAction( action( ObserveImageId ) );
184 theMenu->addAction( action( ExportImageId ) );
185 theMenu->addSeparator();
187 else if( anIsPolyline )
189 theMenu->addAction( action( EditPolylineId ) );
190 theMenu->addSeparator();
192 else if( anIsVisualState )
194 theMenu->addAction( action( SaveVisualStateId ) );
195 theMenu->addAction( action( LoadVisualStateId ) );
196 theMenu->addSeparator();
202 theMenu->addAction( action( DeleteId ) );
203 theMenu->addSeparator();
206 if( anIsSelection && ( anIsImage || anIsPolyline ) )
208 if( anIsHiddenInSelection )
209 theMenu->addAction( action( ShowId ) );
210 theMenu->addAction( action( ShowOnlyId ) );
211 if( anIsVisibleInSelection )
212 theMenu->addAction( action( HideId ) );
213 theMenu->addSeparator();
216 theMenu->addAction( action( ShowAllId ) );
217 theMenu->addAction( action( HideAllId ) );
218 theMenu->addSeparator();
221 void HYDROGUI_Module::update( const int flags )
223 if( !isUpdateEnabled() )
226 QApplication::setOverrideCursor( Qt::WaitCursor );
228 // To prevent calling this method recursively
229 // from one of the methods called below
230 setUpdateEnabled( false );
232 if( ( flags & UF_Model ) && getDataModel() )
233 getDataModel()->update( getStudyId() );
235 if( ( flags & UF_ObjBrowser ) && getApp() )
236 getApp()->updateObjectBrowser( true );
238 if( ( flags & UF_Viewer ) )
239 updateGV( flags & UF_GV_Init,
240 flags & UF_GV_Forced );
242 if( ( flags & UF_Controls ) && getApp() )
243 getApp()->updateActions();
245 setUpdateEnabled( true );
247 QApplication::restoreOverrideCursor();
250 void HYDROGUI_Module::updateCommandsStatus()
252 LightApp_Module::updateCommandsStatus();
254 updateUndoRedoControls();
257 //action( ... )->setEnabled( ... );
260 HYDROGUI_DataModel* HYDROGUI_Module::getDataModel() const
262 return (HYDROGUI_DataModel*)dataModel();
265 HYDROGUI_Displayer* HYDROGUI_Module::getDisplayer() const
270 GraphicsView_Viewer* HYDROGUI_Module::getViewer( const int theId ) const
272 if( myViewManagerMap.contains( theId ) )
274 ViewManagerInfo anInfo = myViewManagerMap[ theId ];
275 GraphicsView_ViewManager* aViewManager =
276 dynamic_cast<GraphicsView_ViewManager*>( anInfo.first );
278 return aViewManager->getViewer();
283 int HYDROGUI_Module::getViewManagerId( SUIT_ViewManager* theViewManager )
285 ViewManagerMapIterator anIter( myViewManagerMap );
286 while( anIter.hasNext() )
288 int anId = anIter.next().key();
289 const ViewManagerInfo& anInfo = anIter.value();
290 if( anInfo.first == theViewManager )
296 HYDROGUI_Module::ViewManagerRole HYDROGUI_Module::getViewManagerRole( SUIT_ViewManager* theViewManager )
298 int anId = getViewManagerId( theViewManager );
301 const ViewManagerInfo& anInfo = myViewManagerMap[ anId ];
302 return anInfo.second;
307 void HYDROGUI_Module::setViewManagerRole( SUIT_ViewManager* theViewManager,
308 const ViewManagerRole theRole )
310 int anId = getViewManagerId( theViewManager );
313 ViewManagerInfo& anInfo = myViewManagerMap[ anId ];
314 anInfo.second = theRole;
318 CAM_DataModel* HYDROGUI_Module::createDataModel()
320 return new HYDROGUI_DataModel( this );
323 void HYDROGUI_Module::customEvent( QEvent* e )
325 int aType = e->type();
326 if ( aType == NewViewEvent )
328 SALOME_CustomEvent* ce = ( SALOME_CustomEvent* )e;
329 if( GraphicsView_ViewFrame* aViewFrame = ( GraphicsView_ViewFrame* )ce->data() )
331 if( GraphicsView_Viewer* aViewer = dynamic_cast<GraphicsView_Viewer*>( aViewFrame->getViewer() ) )
333 SUIT_ViewManager* aViewManager = aViewer->getViewManager();
334 ViewManagerRole aRole = getViewManagerRole( aViewManager );
335 if( aRole == VMR_General )
338 aViewer->activateTransform( GraphicsView_Viewer::FitAll );
340 if( GraphicsView_ViewPort* aViewPort = aViewer->getActiveViewPort() )
342 aViewPort->setInteractionFlag( GraphicsView_ViewPort::TraceBoundingRect );
343 aViewPort->setInteractionFlag( GraphicsView_ViewPort::ImmediateContextMenu );
344 aViewPort->setInteractionFlag( GraphicsView_ViewPort::ImmediateSelection );
347 //aViewPort->setViewLabelPosition( GraphicsView_ViewPort::VLP_BottomLeft, true );
354 bool HYDROGUI_Module::eventFilter( QObject* theObj, QEvent* theEvent )
356 QEvent::Type aType = theEvent->type();
357 if( theObj->inherits( "GraphicsView_ViewFrame" ) )
359 if( aType == QEvent::Show )
361 SALOME_CustomEvent* e = new SALOME_CustomEvent( NewViewEvent );
362 e->setData( theObj );
363 QApplication::postEvent( this, e );
364 theObj->removeEventFilter( this );
367 return LightApp_Module::eventFilter( theObj, theEvent );
370 void HYDROGUI_Module::onViewManagerAdded( SUIT_ViewManager* theViewManager )
372 LightApp_Module::onViewManagerAdded( theViewManager );
374 if( theViewManager->getType() == GraphicsView_Viewer::Type() )
376 createSelector( theViewManager ); // replace the default selector
378 connect( theViewManager, SIGNAL( viewCreated( SUIT_ViewWindow* ) ),
379 this, SLOT( onViewCreated( SUIT_ViewWindow* ) ) );
382 ViewManagerInfo anInfo( theViewManager, VMR_General );
383 myViewManagerMap.insert( ViewManagerId++, anInfo );
386 void HYDROGUI_Module::onViewManagerRemoved( SUIT_ViewManager* theViewManager )
388 LightApp_Module::onViewManagerRemoved( theViewManager );
390 createSelector( theViewManager ); // replace the default selector
392 int anId = getViewManagerId( theViewManager );
394 myViewManagerMap.remove( anId );
397 void HYDROGUI_Module::onViewCreated( SUIT_ViewWindow* theViewWindow )
399 if( theViewWindow && theViewWindow->inherits( "GraphicsView_ViewFrame" ) )
401 if( GraphicsView_ViewFrame* aViewFrame = dynamic_cast<GraphicsView_ViewFrame*>( theViewWindow ) )
403 aViewFrame->installEventFilter( this );
405 GraphicsView_ViewPort* aViewPort = aViewFrame->getViewPort();
407 connect( aViewPort, SIGNAL( vpMouseEvent( QGraphicsSceneMouseEvent* ) ),
408 this, SLOT( onViewPortMouseEvent( QGraphicsSceneMouseEvent* ) ) );
414 void HYDROGUI_Module::onViewPortMouseEvent( QGraphicsSceneMouseEvent* theEvent )
416 if( GraphicsView_ViewPort* aViewPort = qobject_cast<GraphicsView_ViewPort*>( sender() ) )
418 SUIT_ViewManager* aViewManager = 0;
420 QObject* aParent = aViewPort;
421 while( aParent = aParent->parent() )
423 if( GraphicsView_ViewFrame* aViewFrame = dynamic_cast<GraphicsView_ViewFrame*>( aParent ) )
425 if( GraphicsView_Viewer* aViewer = aViewFrame->getViewer() )
427 aViewManager = aViewer->getViewManager();
436 double aMouseX = theEvent->scenePos().x();
437 double aMouseY = theEvent->scenePos().y();
439 ViewManagerRole aRole = getViewManagerRole( aViewManager );
440 if( aRole == VMR_General )
442 int aXDeg = 0, aYDeg = 0;
443 int aXMin = 0, aYMin = 0;
444 double aXSec = 0, aYSec = 0;
445 HYDROGUI_Tool::DoubleToLambert( aMouseX, aXDeg, aXMin, aXSec );
446 HYDROGUI_Tool::DoubleToLambert( aMouseY, aYDeg, aYMin, aYSec );
448 QString aDegSymbol( QChar( 0x00B0 ) );
449 QString aXStr = QString( "%1%2 %3' %4\"" ).arg( aXDeg ).arg( aDegSymbol ).arg( aXMin ).arg( aXSec );
450 QString aYStr = QString( "%1%2 %3' %4\"" ).arg( aYDeg ).arg( aDegSymbol ).arg( aYMin ).arg( aYSec );
452 aViewPort->setViewLabelText( QString( "X: %1\nY: %2" ).arg( aXStr ).arg( aYStr ) );
454 else if( aRole == VMR_TransformImage )
455 aViewPort->setViewLabelText( QString( "X: %1\nY: %2" ).arg( (int)aMouseX ).arg( (int)aMouseY ) );
459 void HYDROGUI_Module::updateGV( const bool theIsInit,
460 const bool theIsForced )
462 if( !getDisplayer() )
465 QList<int> aViewManagerIdList;
467 // currently, all views are updated
468 ViewManagerMapIterator anIter( myViewManagerMap );
469 while( anIter.hasNext() )
471 int anId = anIter.next().key();
472 aViewManagerIdList.append( anId );
475 QListIterator<int> anIdIter( aViewManagerIdList );
476 while( anIdIter.hasNext() )
477 getDisplayer()->UpdateAll( anIdIter.next(), theIsInit, theIsForced );
480 void HYDROGUI_Module::createSelector( SUIT_ViewManager* theViewManager )
482 if( !theViewManager )
485 LightApp_SelectionMgr* aSelectionMgr = getApp()->selectionMgr();
489 QString aViewType = theViewManager->getType();
490 if( aViewType != GraphicsView_Viewer::Type() )
493 GraphicsView_ViewManager* aViewManager =
494 dynamic_cast<GraphicsView_ViewManager*>( theViewManager );
498 QList<SUIT_Selector*> aSelectorList;
499 aSelectionMgr->selectors( aViewType, aSelectorList );
501 // disable all alien selectors
502 QList<SUIT_Selector*>::iterator anIter, anIterEnd = aSelectorList.end();
503 for( anIter = aSelectorList.begin(); anIter != anIterEnd; anIter++ )
505 SUIT_Selector* aSelector = *anIter;
506 if( aSelector && !dynamic_cast<HYDROGUI_GVSelector*>( aSelector ) )
507 aSelector->setEnabled( false );
510 new HYDROGUI_GVSelector( this, aViewManager->getViewer(), aSelectionMgr );
513 bool HYDROGUI_Module::setUpdateEnabled( const bool theState )
515 bool aPrevState = myIsUpdateEnabled;
516 myIsUpdateEnabled = theState;
520 bool HYDROGUI_Module::isUpdateEnabled() const
522 return myIsUpdateEnabled;