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 bool anIsSelection = false;
143 bool anIsVisibleInSelection = false;
144 bool anIsHiddenInSelection = false;
146 bool anIsImage = false;
147 bool anIsPolyline = false;
149 HYDROData_SequenceOfObjects aSeq = HYDROGUI_Tool::GetSelectedObjects( this );
150 for( Standard_Integer anIndex = 1, aLength = aSeq.Length(); anIndex <= aLength; anIndex++ )
152 Handle(HYDROData_Object) anObject = aSeq.Value( anIndex );
153 if( !anObject.IsNull() )
155 anIsSelection = true;
157 bool aVisibility = anObject->GetVisibility();
158 anIsVisibleInSelection |= aVisibility;
159 anIsHiddenInSelection |= !aVisibility;
161 if( anObject->GetKind() == KIND_IMAGE )
164 if( anObject->GetKind() == KIND_POLYLINE )
169 if( aSeq.Length() == 1 )
173 theMenu->addAction( action( EditImageId ) );
174 theMenu->addAction( action( ExportImageId ) );
175 theMenu->addSeparator();
177 else if( anIsPolyline )
179 theMenu->addAction( action( EditPolylineId ) );
180 theMenu->addSeparator();
186 theMenu->addAction( action( DeleteId ) );
187 theMenu->addSeparator();
192 if( anIsHiddenInSelection )
193 theMenu->addAction( action( ShowId ) );
194 theMenu->addAction( action( ShowOnlyId ) );
195 if( anIsVisibleInSelection )
196 theMenu->addAction( action( HideId ) );
197 theMenu->addSeparator();
199 theMenu->addAction( action( ShowAllId ) );
200 theMenu->addAction( action( HideAllId ) );
201 theMenu->addSeparator();
204 void HYDROGUI_Module::update( const int flags )
206 if( !isUpdateEnabled() )
209 QApplication::setOverrideCursor( Qt::WaitCursor );
211 // To prevent calling this method recursively
212 // from one of the methods called below
213 setUpdateEnabled( false );
215 if( ( flags & UF_Model ) && getDataModel() )
216 getDataModel()->update( getStudyId() );
218 if( ( flags & UF_ObjBrowser ) && getApp() )
219 getApp()->updateObjectBrowser( true );
221 if( ( flags & UF_Viewer ) )
222 updateGV( flags & UF_GV_Init,
223 flags & UF_GV_Forced );
225 if( ( flags & UF_Controls ) && getApp() )
226 getApp()->updateActions();
228 setUpdateEnabled( true );
230 QApplication::restoreOverrideCursor();
233 void HYDROGUI_Module::updateCommandsStatus()
235 LightApp_Module::updateCommandsStatus();
237 updateUndoRedoControls();
240 //action( ... )->setEnabled( ... );
243 HYDROGUI_DataModel* HYDROGUI_Module::getDataModel() const
245 return (HYDROGUI_DataModel*)dataModel();
248 HYDROGUI_Displayer* HYDROGUI_Module::getDisplayer() const
253 GraphicsView_Viewer* HYDROGUI_Module::getViewer( const int theId ) const
255 if( myViewManagerMap.contains( theId ) )
257 ViewManagerInfo anInfo = myViewManagerMap[ theId ];
258 GraphicsView_ViewManager* aViewManager =
259 dynamic_cast<GraphicsView_ViewManager*>( anInfo.first );
261 return aViewManager->getViewer();
266 int HYDROGUI_Module::getViewManagerId( SUIT_ViewManager* theViewManager )
268 ViewManagerMapIterator anIter( myViewManagerMap );
269 while( anIter.hasNext() )
271 int anId = anIter.next().key();
272 const ViewManagerInfo& anInfo = anIter.value();
273 if( anInfo.first == theViewManager )
279 HYDROGUI_Module::ViewManagerRole HYDROGUI_Module::getViewManagerRole( SUIT_ViewManager* theViewManager )
281 int anId = getViewManagerId( theViewManager );
284 const ViewManagerInfo& anInfo = myViewManagerMap[ anId ];
285 return anInfo.second;
290 void HYDROGUI_Module::setViewManagerRole( SUIT_ViewManager* theViewManager,
291 const ViewManagerRole theRole )
293 int anId = getViewManagerId( theViewManager );
296 ViewManagerInfo& anInfo = myViewManagerMap[ anId ];
297 anInfo.second = theRole;
301 CAM_DataModel* HYDROGUI_Module::createDataModel()
303 return new HYDROGUI_DataModel( this );
306 void HYDROGUI_Module::customEvent( QEvent* e )
308 int aType = e->type();
309 if ( aType == NewViewEvent )
311 SALOME_CustomEvent* ce = ( SALOME_CustomEvent* )e;
312 if( GraphicsView_ViewFrame* aViewFrame = ( GraphicsView_ViewFrame* )ce->data() )
314 if( GraphicsView_Viewer* aViewer = dynamic_cast<GraphicsView_Viewer*>( aViewFrame->getViewer() ) )
316 SUIT_ViewManager* aViewManager = aViewer->getViewManager();
317 ViewManagerRole aRole = getViewManagerRole( aViewManager );
318 if( aRole == VMR_General )
321 aViewer->activateTransform( GraphicsView_Viewer::FitAll );
323 if( GraphicsView_ViewPort* aViewPort = aViewer->getActiveViewPort() )
325 aViewPort->setInteractionFlag( GraphicsView_ViewPort::TraceBoundingRect );
326 aViewPort->setInteractionFlag( GraphicsView_ViewPort::ImmediateContextMenu );
327 aViewPort->setInteractionFlag( GraphicsView_ViewPort::ImmediateSelection );
330 //aViewPort->setViewLabelPosition( GraphicsView_ViewPort::VLP_BottomLeft, true );
337 bool HYDROGUI_Module::eventFilter( QObject* theObj, QEvent* theEvent )
339 QEvent::Type aType = theEvent->type();
340 if( theObj->inherits( "GraphicsView_ViewFrame" ) )
342 if( aType == QEvent::Show )
344 SALOME_CustomEvent* e = new SALOME_CustomEvent( NewViewEvent );
345 e->setData( theObj );
346 QApplication::postEvent( this, e );
347 theObj->removeEventFilter( this );
350 return LightApp_Module::eventFilter( theObj, theEvent );
353 void HYDROGUI_Module::onViewManagerAdded( SUIT_ViewManager* theViewManager )
355 LightApp_Module::onViewManagerAdded( theViewManager );
357 if( theViewManager->getType() == GraphicsView_Viewer::Type() )
359 createSelector( theViewManager ); // replace the default selector
361 connect( theViewManager, SIGNAL( viewCreated( SUIT_ViewWindow* ) ),
362 this, SLOT( onViewCreated( SUIT_ViewWindow* ) ) );
365 ViewManagerInfo anInfo( theViewManager, VMR_General );
366 myViewManagerMap.insert( ViewManagerId++, anInfo );
369 void HYDROGUI_Module::onViewManagerRemoved( SUIT_ViewManager* theViewManager )
371 LightApp_Module::onViewManagerRemoved( theViewManager );
373 createSelector( theViewManager ); // replace the default selector
375 int anId = getViewManagerId( theViewManager );
377 myViewManagerMap.remove( anId );
380 void HYDROGUI_Module::onViewCreated( SUIT_ViewWindow* theViewWindow )
382 if( theViewWindow && theViewWindow->inherits( "GraphicsView_ViewFrame" ) )
384 if( GraphicsView_ViewFrame* aViewFrame = dynamic_cast<GraphicsView_ViewFrame*>( theViewWindow ) )
386 aViewFrame->installEventFilter( this );
388 GraphicsView_ViewPort* aViewPort = aViewFrame->getViewPort();
390 connect( aViewPort, SIGNAL( vpMouseEvent( QGraphicsSceneMouseEvent* ) ),
391 this, SLOT( onViewPortMouseEvent( QGraphicsSceneMouseEvent* ) ) );
397 void HYDROGUI_Module::onViewPortMouseEvent( QGraphicsSceneMouseEvent* theEvent )
399 if( GraphicsView_ViewPort* aViewPort = qobject_cast<GraphicsView_ViewPort*>( sender() ) )
401 SUIT_ViewManager* aViewManager = 0;
403 QObject* aParent = aViewPort;
404 while( aParent = aParent->parent() )
406 if( GraphicsView_ViewFrame* aViewFrame = dynamic_cast<GraphicsView_ViewFrame*>( aParent ) )
408 if( GraphicsView_Viewer* aViewer = aViewFrame->getViewer() )
410 aViewManager = aViewer->getViewManager();
419 double aMouseX = theEvent->scenePos().x();
420 double aMouseY = theEvent->scenePos().y();
422 ViewManagerRole aRole = getViewManagerRole( aViewManager );
423 if( aRole == VMR_General )
425 int aXDeg = 0, aYDeg = 0;
426 int aXMin = 0, aYMin = 0;
427 double aXSec = 0, aYSec = 0;
428 HYDROGUI_Tool::DoubleToLambert( aMouseX, aXDeg, aXMin, aXSec );
429 HYDROGUI_Tool::DoubleToLambert( aMouseY, aYDeg, aYMin, aYSec );
431 QString aDegSymbol( QChar( 0x00B0 ) );
432 QString aXStr = QString( "%1%2 %3' %4\"" ).arg( aXDeg ).arg( aDegSymbol ).arg( aXMin ).arg( aXSec );
433 QString aYStr = QString( "%1%2 %3' %4\"" ).arg( aYDeg ).arg( aDegSymbol ).arg( aYMin ).arg( aYSec );
435 aViewPort->setViewLabelText( QString( "X: %1\nY: %2" ).arg( aXStr ).arg( aYStr ) );
437 else if( aRole == VMR_Mapping )
438 aViewPort->setViewLabelText( QString( "X: %1\nY: %2" ).arg( (int)aMouseX ).arg( (int)aMouseY ) );
442 void HYDROGUI_Module::updateGV( const bool theIsInit,
443 const bool theIsForced )
445 if( !getDisplayer() )
448 QList<int> aViewManagerIdList;
450 // currently, all views are updated
451 ViewManagerMapIterator anIter( myViewManagerMap );
452 while( anIter.hasNext() )
454 int anId = anIter.next().key();
455 aViewManagerIdList.append( anId );
458 QListIterator<int> anIdIter( aViewManagerIdList );
459 while( anIdIter.hasNext() )
460 getDisplayer()->UpdateAll( anIdIter.next(), theIsInit, theIsForced );
463 void HYDROGUI_Module::createSelector( SUIT_ViewManager* theViewManager )
465 if( !theViewManager )
468 LightApp_SelectionMgr* aSelectionMgr = getApp()->selectionMgr();
472 QString aViewType = theViewManager->getType();
473 if( aViewType != GraphicsView_Viewer::Type() )
476 GraphicsView_ViewManager* aViewManager =
477 dynamic_cast<GraphicsView_ViewManager*>( theViewManager );
481 QList<SUIT_Selector*> aSelectorList;
482 aSelectionMgr->selectors( aViewType, aSelectorList );
484 // disable all alien selectors
485 QList<SUIT_Selector*>::iterator anIter, anIterEnd = aSelectorList.end();
486 for( anIter = aSelectorList.begin(); anIter != anIterEnd; anIter++ )
488 SUIT_Selector* aSelector = *anIter;
489 if( aSelector && !dynamic_cast<HYDROGUI_GVSelector*>( aSelector ) )
490 aSelector->setEnabled( false );
493 new HYDROGUI_GVSelector( this, aViewManager->getViewer(), aSelectionMgr );
496 bool HYDROGUI_Module::setUpdateEnabled( const bool theState )
498 bool aPrevState = myIsUpdateEnabled;
499 myIsUpdateEnabled = theState;
503 bool HYDROGUI_Module::isUpdateEnabled() const
505 return myIsUpdateEnabled;