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_DataOwner.h>
43 #include <LightApp_GVSelector.h>
44 #include <LightApp_SelectionMgr.h>
45 #include <LightApp_UpdateFlags.h>
47 #include <SALOME_Event.h>
49 #include <SUIT_Study.h>
50 #include <SUIT_ViewManager.h>
53 #include <QApplication>
54 #include <QGraphicsSceneMouseEvent>
57 static int ViewManagerId = 0;
59 extern "C" HYDRO_EXPORT CAM_Module* createModule()
61 return new HYDROGUI_Module();
64 HYDROGUI_Module::HYDROGUI_Module()
65 : LightApp_Module( "HYDRO" ),
67 myIsUpdateEnabled( true )
71 HYDROGUI_Module::~HYDROGUI_Module()
75 int HYDROGUI_Module::getStudyId() const
77 LightApp_Application* anApp = getApp();
78 return anApp ? anApp->activeStudy()->id() : 0;
81 void HYDROGUI_Module::initialize( CAM_Application* theApp )
83 LightApp_Module::initialize( theApp );
86 createUndoRedoActions();
91 setMenuShown( false );
92 setToolShown( false );
94 myDisplayer = new HYDROGUI_Displayer( this );
97 bool HYDROGUI_Module::activateModule( SUIT_Study* theStudy )
99 bool aRes = LightApp_Module::activateModule( theStudy );
101 setMenuShown( true );
102 setToolShown( true );
106 updateCommandsStatus();
111 bool HYDROGUI_Module::deactivateModule( SUIT_Study* theStudy )
113 ViewManagerMapIterator anIter( myViewManagerMap );
114 while( anIter.hasNext() )
115 if( SUIT_ViewManager* aViewManager = anIter.next().value().first )
116 getApp()->removeViewManager( aViewManager );
117 myViewManagerMap.clear();
119 setMenuShown( false );
120 setToolShown( false );
122 return LightApp_Module::deactivateModule( theStudy );
125 void HYDROGUI_Module::windows( QMap<int, int>& theMap ) const
128 theMap.insert( LightApp_Application::WT_LogWindow, Qt::BottomDockWidgetArea );
129 theMap.insert( LightApp_Application::WT_ObjectBrowser, Qt::LeftDockWidgetArea );
132 void HYDROGUI_Module::viewManagers( QStringList& theTypesList ) const
134 theTypesList << GraphicsView_Viewer::Type();
137 void HYDROGUI_Module::contextMenuPopup( const QString& theClient,
141 HYDROGUI_DataModel* aModel = getDataModel();
143 LightApp_SelectionMgr* aSelectionMgr = getApp()->selectionMgr();
147 SUIT_DataOwnerPtrList anOwners;
148 aSelectionMgr->selected( anOwners );
150 bool anIsSelection = false;
151 bool anIsVisibleInSelection = false;
152 bool anIsHiddenInSelection = false;
154 bool anIsImage = false;
155 bool anIsPolyline = false;
157 foreach( SUIT_DataOwner* aSUITOwner, anOwners )
159 if( LightApp_DataOwner* anOwner = dynamic_cast<LightApp_DataOwner*>( aSUITOwner ) )
161 Handle(HYDROData_Object) anObject = aModel->objectByEntry( anOwner->entry() );
162 if( !anObject.IsNull() )
164 anIsSelection = true;
166 bool aVisibility = anObject->GetVisibility();
167 anIsVisibleInSelection |= aVisibility;
168 anIsHiddenInSelection |= !aVisibility;
170 if( anObject->GetKind() == KIND_IMAGE )
173 if( anObject->GetKind() == KIND_POLYLINE )
179 if( anOwners.count() == 1 && anIsImage )
181 theMenu->addAction( action( EditImageId ) );
182 theMenu->addSeparator();
185 if( anOwners.count() == 1 && anIsPolyline )
187 theMenu->addAction( action( EditPolylineId ) );
188 theMenu->addSeparator();
193 theMenu->addAction( action( DeleteId ) );
194 theMenu->addSeparator();
199 if( anIsHiddenInSelection )
200 theMenu->addAction( action( ShowId ) );
201 theMenu->addAction( action( ShowOnlyId ) );
202 if( anIsVisibleInSelection )
203 theMenu->addAction( action( HideId ) );
204 theMenu->addSeparator();
206 theMenu->addAction( action( ShowAllId ) );
207 theMenu->addAction( action( HideAllId ) );
208 theMenu->addSeparator();
211 void HYDROGUI_Module::update( const int flags )
213 if( !isUpdateEnabled() )
216 QApplication::setOverrideCursor( Qt::WaitCursor );
218 // To prevent calling this method recursively
219 // from one of the methods called below
220 setUpdateEnabled( false );
222 if( ( flags & UF_Model ) && getDataModel() )
224 getDataModel()->update( getStudyId() );
229 if( ( flags & UF_ObjBrowser ) && getObjectBrowser() )
230 updateObjectBrowser();
234 if( ( flags & UF_Viewer ) )
235 updateGV( flags & UF_GV_Init,
236 flags & UF_GV_Forced );
238 if( ( flags & UF_Controls ) && getApp() )
239 getApp()->updateActions();
241 setUpdateEnabled( true );
243 QApplication::restoreOverrideCursor();
246 void HYDROGUI_Module::updateCommandsStatus()
248 LightApp_Module::updateCommandsStatus();
250 updateUndoRedoControls();
253 //action( ... )->setEnabled( ... );
256 HYDROGUI_DataModel* HYDROGUI_Module::getDataModel() const
258 return (HYDROGUI_DataModel*)dataModel();
261 HYDROGUI_Displayer* HYDROGUI_Module::getDisplayer() const
266 GraphicsView_Viewer* HYDROGUI_Module::getViewer( const int theId ) const
268 if( myViewManagerMap.contains( theId ) )
270 ViewManagerInfo anInfo = myViewManagerMap[ theId ];
271 GraphicsView_ViewManager* aViewManager =
272 dynamic_cast<GraphicsView_ViewManager*>( anInfo.first );
274 return aViewManager->getViewer();
279 int HYDROGUI_Module::getViewManagerId( SUIT_ViewManager* theViewManager )
281 ViewManagerMapIterator anIter( myViewManagerMap );
282 while( anIter.hasNext() )
284 int anId = anIter.next().key();
285 const ViewManagerInfo& anInfo = anIter.value();
286 if( anInfo.first == theViewManager )
292 HYDROGUI_Module::ViewManagerRole HYDROGUI_Module::getViewManagerRole( SUIT_ViewManager* theViewManager )
294 int anId = getViewManagerId( theViewManager );
297 const ViewManagerInfo& anInfo = myViewManagerMap[ anId ];
298 return anInfo.second;
303 void HYDROGUI_Module::setViewManagerRole( SUIT_ViewManager* theViewManager,
304 const ViewManagerRole theRole )
306 int anId = getViewManagerId( theViewManager );
309 ViewManagerInfo& anInfo = myViewManagerMap[ anId ];
310 anInfo.second = theRole;
314 CAM_DataModel* HYDROGUI_Module::createDataModel()
316 return new HYDROGUI_DataModel( this );
319 void HYDROGUI_Module::customEvent( QEvent* e )
321 int aType = e->type();
322 if ( aType == NewViewEvent )
324 SALOME_CustomEvent* ce = ( SALOME_CustomEvent* )e;
325 if( GraphicsView_ViewFrame* aViewFrame = ( GraphicsView_ViewFrame* )ce->data() )
327 if( GraphicsView_Viewer* aViewer = dynamic_cast<GraphicsView_Viewer*>( aViewFrame->getViewer() ) )
329 SUIT_ViewManager* aViewManager = aViewer->getViewManager();
330 ViewManagerRole aRole = getViewManagerRole( aViewManager );
331 if( aRole == VMR_General )
334 aViewer->activateTransform( GraphicsView_Viewer::FitAll );
336 if( GraphicsView_ViewPort* aViewPort = aViewer->getActiveViewPort() )
338 aViewPort->setInteractionFlag( GraphicsView_ViewPort::TraceBoundingRect );
339 aViewPort->setInteractionFlag( GraphicsView_ViewPort::ImmediateContextMenu );
340 aViewPort->setInteractionFlag( GraphicsView_ViewPort::ImmediateSelection );
343 //aViewPort->setViewLabelPosition( GraphicsView_ViewPort::VLP_BottomLeft, true );
350 bool HYDROGUI_Module::eventFilter( QObject* theObj, QEvent* theEvent )
352 QEvent::Type aType = theEvent->type();
353 if( theObj->inherits( "GraphicsView_ViewFrame" ) )
355 if( aType == QEvent::Show )
357 SALOME_CustomEvent* e = new SALOME_CustomEvent( NewViewEvent );
358 e->setData( theObj );
359 QApplication::postEvent( this, e );
360 theObj->removeEventFilter( this );
363 return LightApp_Module::eventFilter( theObj, theEvent );
366 void HYDROGUI_Module::onViewManagerAdded( SUIT_ViewManager* theViewManager )
368 LightApp_Module::onViewManagerAdded( theViewManager );
370 if( theViewManager->getType() == GraphicsView_Viewer::Type() )
372 createSelector( theViewManager ); // replace the default selector
374 connect( theViewManager, SIGNAL( viewCreated( SUIT_ViewWindow* ) ),
375 this, SLOT( onViewCreated( SUIT_ViewWindow* ) ) );
378 ViewManagerInfo anInfo( theViewManager, VMR_General );
379 myViewManagerMap.insert( ViewManagerId++, anInfo );
382 void HYDROGUI_Module::onViewManagerRemoved( SUIT_ViewManager* theViewManager )
384 LightApp_Module::onViewManagerRemoved( theViewManager );
386 createSelector( theViewManager ); // replace the default selector
388 int anId = getViewManagerId( theViewManager );
390 myViewManagerMap.remove( anId );
393 void HYDROGUI_Module::onViewCreated( SUIT_ViewWindow* theViewWindow )
395 if( theViewWindow && theViewWindow->inherits( "GraphicsView_ViewFrame" ) )
397 if( GraphicsView_ViewFrame* aViewFrame = dynamic_cast<GraphicsView_ViewFrame*>( theViewWindow ) )
399 aViewFrame->installEventFilter( this );
401 GraphicsView_ViewPort* aViewPort = aViewFrame->getViewPort();
403 connect( aViewPort, SIGNAL( vpMouseEvent( QGraphicsSceneMouseEvent* ) ),
404 this, SLOT( onViewPortMouseEvent( QGraphicsSceneMouseEvent* ) ) );
410 void HYDROGUI_Module::onViewPortMouseEvent( QGraphicsSceneMouseEvent* theEvent )
412 if( GraphicsView_ViewPort* aViewPort = qobject_cast<GraphicsView_ViewPort*>( sender() ) )
414 SUIT_ViewManager* aViewManager = 0;
416 QObject* aParent = aViewPort;
417 while( aParent = aParent->parent() )
419 if( GraphicsView_ViewFrame* aViewFrame = dynamic_cast<GraphicsView_ViewFrame*>( aParent ) )
421 if( GraphicsView_Viewer* aViewer = aViewFrame->getViewer() )
423 aViewManager = aViewer->getViewManager();
432 double aMouseX = theEvent->scenePos().x();
433 double aMouseY = theEvent->scenePos().y();
435 ViewManagerRole aRole = getViewManagerRole( aViewManager );
436 if( aRole == VMR_General )
438 int aXDeg = 0, aYDeg = 0;
439 int aXMin = 0, aYMin = 0;
440 double aXSec = 0, aYSec = 0;
441 HYDROGUI_Tool::DoubleToLambert( aMouseX, aXDeg, aXMin, aXSec );
442 HYDROGUI_Tool::DoubleToLambert( aMouseY, aYDeg, aYMin, aYSec );
444 QString aDegSymbol( QChar( 0x00B0 ) );
445 QString aXStr = QString( "%1%2 %3' %4\"" ).arg( aXDeg ).arg( aDegSymbol ).arg( aXMin ).arg( aXSec );
446 QString aYStr = QString( "%1%2 %3' %4\"" ).arg( aYDeg ).arg( aDegSymbol ).arg( aYMin ).arg( aYSec );
448 aViewPort->setViewLabelText( QString( "X: %1\nY: %2" ).arg( aXStr ).arg( aYStr ) );
450 else if( aRole == VMR_Mapping )
451 aViewPort->setViewLabelText( QString( "X: %1\nY: %2" ).arg( (int)aMouseX ).arg( (int)aMouseY ) );
455 void HYDROGUI_Module::updateGV( const bool theIsInit,
456 const bool theIsForced )
458 if( !getDisplayer() )
461 QList<int> aViewManagerIdList;
463 // currently, all views are updated
464 ViewManagerMapIterator anIter( myViewManagerMap );
465 while( anIter.hasNext() )
467 int anId = anIter.next().key();
468 aViewManagerIdList.append( anId );
471 QListIterator<int> anIdIter( aViewManagerIdList );
472 while( anIdIter.hasNext() )
473 getDisplayer()->UpdateAll( anIdIter.next(), theIsInit, theIsForced );
476 void HYDROGUI_Module::createSelector( SUIT_ViewManager* theViewManager )
478 if( !theViewManager )
481 LightApp_SelectionMgr* aSelectionMgr = getApp()->selectionMgr();
485 QString aViewType = theViewManager->getType();
486 if( aViewType != GraphicsView_Viewer::Type() )
489 GraphicsView_ViewManager* aViewManager =
490 dynamic_cast<GraphicsView_ViewManager*>( theViewManager );
494 QList<SUIT_Selector*> aSelectorList;
495 aSelectionMgr->selectors( aViewType, aSelectorList );
497 // disable all alien selectors
498 QList<SUIT_Selector*>::iterator anIter, anIterEnd = aSelectorList.end();
499 for( anIter = aSelectorList.begin(); anIter != anIterEnd; anIter++ )
501 SUIT_Selector* aSelector = *anIter;
502 if( aSelector && !dynamic_cast<HYDROGUI_GVSelector*>( aSelector ) )
503 aSelector->setEnabled( false );
506 new HYDROGUI_GVSelector( this, aViewManager->getViewer(), aSelectionMgr );
509 bool HYDROGUI_Module::setUpdateEnabled( const bool theState )
511 bool aPrevState = myIsUpdateEnabled;
512 myIsUpdateEnabled = theState;
516 bool HYDROGUI_Module::isUpdateEnabled() const
518 return myIsUpdateEnabled;