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( ObserveImageId ) );
175 theMenu->addAction( action( ExportImageId ) );
176 theMenu->addSeparator();
178 else if( anIsPolyline )
180 theMenu->addAction( action( EditPolylineId ) );
181 theMenu->addSeparator();
187 theMenu->addAction( action( DeleteId ) );
188 theMenu->addSeparator();
193 if( anIsHiddenInSelection )
194 theMenu->addAction( action( ShowId ) );
195 theMenu->addAction( action( ShowOnlyId ) );
196 if( anIsVisibleInSelection )
197 theMenu->addAction( action( HideId ) );
198 theMenu->addSeparator();
200 theMenu->addAction( action( ShowAllId ) );
201 theMenu->addAction( action( HideAllId ) );
202 theMenu->addSeparator();
205 void HYDROGUI_Module::update( const int flags )
207 if( !isUpdateEnabled() )
210 QApplication::setOverrideCursor( Qt::WaitCursor );
212 // To prevent calling this method recursively
213 // from one of the methods called below
214 setUpdateEnabled( false );
216 if( ( flags & UF_Model ) && getDataModel() )
217 getDataModel()->update( getStudyId() );
219 if( ( flags & UF_ObjBrowser ) && getApp() )
220 getApp()->updateObjectBrowser( true );
222 if( ( flags & UF_Viewer ) )
223 updateGV( flags & UF_GV_Init,
224 flags & UF_GV_Forced );
226 if( ( flags & UF_Controls ) && getApp() )
227 getApp()->updateActions();
229 setUpdateEnabled( true );
231 QApplication::restoreOverrideCursor();
234 void HYDROGUI_Module::updateCommandsStatus()
236 LightApp_Module::updateCommandsStatus();
238 updateUndoRedoControls();
241 //action( ... )->setEnabled( ... );
244 HYDROGUI_DataModel* HYDROGUI_Module::getDataModel() const
246 return (HYDROGUI_DataModel*)dataModel();
249 HYDROGUI_Displayer* HYDROGUI_Module::getDisplayer() const
254 GraphicsView_Viewer* HYDROGUI_Module::getViewer( const int theId ) const
256 if( myViewManagerMap.contains( theId ) )
258 ViewManagerInfo anInfo = myViewManagerMap[ theId ];
259 GraphicsView_ViewManager* aViewManager =
260 dynamic_cast<GraphicsView_ViewManager*>( anInfo.first );
262 return aViewManager->getViewer();
267 int HYDROGUI_Module::getViewManagerId( SUIT_ViewManager* theViewManager )
269 ViewManagerMapIterator anIter( myViewManagerMap );
270 while( anIter.hasNext() )
272 int anId = anIter.next().key();
273 const ViewManagerInfo& anInfo = anIter.value();
274 if( anInfo.first == theViewManager )
280 HYDROGUI_Module::ViewManagerRole HYDROGUI_Module::getViewManagerRole( SUIT_ViewManager* theViewManager )
282 int anId = getViewManagerId( theViewManager );
285 const ViewManagerInfo& anInfo = myViewManagerMap[ anId ];
286 return anInfo.second;
291 void HYDROGUI_Module::setViewManagerRole( SUIT_ViewManager* theViewManager,
292 const ViewManagerRole theRole )
294 int anId = getViewManagerId( theViewManager );
297 ViewManagerInfo& anInfo = myViewManagerMap[ anId ];
298 anInfo.second = theRole;
302 CAM_DataModel* HYDROGUI_Module::createDataModel()
304 return new HYDROGUI_DataModel( this );
307 void HYDROGUI_Module::customEvent( QEvent* e )
309 int aType = e->type();
310 if ( aType == NewViewEvent )
312 SALOME_CustomEvent* ce = ( SALOME_CustomEvent* )e;
313 if( GraphicsView_ViewFrame* aViewFrame = ( GraphicsView_ViewFrame* )ce->data() )
315 if( GraphicsView_Viewer* aViewer = dynamic_cast<GraphicsView_Viewer*>( aViewFrame->getViewer() ) )
317 SUIT_ViewManager* aViewManager = aViewer->getViewManager();
318 ViewManagerRole aRole = getViewManagerRole( aViewManager );
319 if( aRole == VMR_General )
322 aViewer->activateTransform( GraphicsView_Viewer::FitAll );
324 if( GraphicsView_ViewPort* aViewPort = aViewer->getActiveViewPort() )
326 aViewPort->setInteractionFlag( GraphicsView_ViewPort::TraceBoundingRect );
327 aViewPort->setInteractionFlag( GraphicsView_ViewPort::ImmediateContextMenu );
328 aViewPort->setInteractionFlag( GraphicsView_ViewPort::ImmediateSelection );
331 //aViewPort->setViewLabelPosition( GraphicsView_ViewPort::VLP_BottomLeft, true );
338 bool HYDROGUI_Module::eventFilter( QObject* theObj, QEvent* theEvent )
340 QEvent::Type aType = theEvent->type();
341 if( theObj->inherits( "GraphicsView_ViewFrame" ) )
343 if( aType == QEvent::Show )
345 SALOME_CustomEvent* e = new SALOME_CustomEvent( NewViewEvent );
346 e->setData( theObj );
347 QApplication::postEvent( this, e );
348 theObj->removeEventFilter( this );
351 return LightApp_Module::eventFilter( theObj, theEvent );
354 void HYDROGUI_Module::onViewManagerAdded( SUIT_ViewManager* theViewManager )
356 LightApp_Module::onViewManagerAdded( theViewManager );
358 if( theViewManager->getType() == GraphicsView_Viewer::Type() )
360 createSelector( theViewManager ); // replace the default selector
362 connect( theViewManager, SIGNAL( viewCreated( SUIT_ViewWindow* ) ),
363 this, SLOT( onViewCreated( SUIT_ViewWindow* ) ) );
366 ViewManagerInfo anInfo( theViewManager, VMR_General );
367 myViewManagerMap.insert( ViewManagerId++, anInfo );
370 void HYDROGUI_Module::onViewManagerRemoved( SUIT_ViewManager* theViewManager )
372 LightApp_Module::onViewManagerRemoved( theViewManager );
374 createSelector( theViewManager ); // replace the default selector
376 int anId = getViewManagerId( theViewManager );
378 myViewManagerMap.remove( anId );
381 void HYDROGUI_Module::onViewCreated( SUIT_ViewWindow* theViewWindow )
383 if( theViewWindow && theViewWindow->inherits( "GraphicsView_ViewFrame" ) )
385 if( GraphicsView_ViewFrame* aViewFrame = dynamic_cast<GraphicsView_ViewFrame*>( theViewWindow ) )
387 aViewFrame->installEventFilter( this );
389 GraphicsView_ViewPort* aViewPort = aViewFrame->getViewPort();
391 connect( aViewPort, SIGNAL( vpMouseEvent( QGraphicsSceneMouseEvent* ) ),
392 this, SLOT( onViewPortMouseEvent( QGraphicsSceneMouseEvent* ) ) );
398 void HYDROGUI_Module::onViewPortMouseEvent( QGraphicsSceneMouseEvent* theEvent )
400 if( GraphicsView_ViewPort* aViewPort = qobject_cast<GraphicsView_ViewPort*>( sender() ) )
402 SUIT_ViewManager* aViewManager = 0;
404 QObject* aParent = aViewPort;
405 while( aParent = aParent->parent() )
407 if( GraphicsView_ViewFrame* aViewFrame = dynamic_cast<GraphicsView_ViewFrame*>( aParent ) )
409 if( GraphicsView_Viewer* aViewer = aViewFrame->getViewer() )
411 aViewManager = aViewer->getViewManager();
420 double aMouseX = theEvent->scenePos().x();
421 double aMouseY = theEvent->scenePos().y();
423 ViewManagerRole aRole = getViewManagerRole( aViewManager );
424 if( aRole == VMR_General )
426 int aXDeg = 0, aYDeg = 0;
427 int aXMin = 0, aYMin = 0;
428 double aXSec = 0, aYSec = 0;
429 HYDROGUI_Tool::DoubleToLambert( aMouseX, aXDeg, aXMin, aXSec );
430 HYDROGUI_Tool::DoubleToLambert( aMouseY, aYDeg, aYMin, aYSec );
432 QString aDegSymbol( QChar( 0x00B0 ) );
433 QString aXStr = QString( "%1%2 %3' %4\"" ).arg( aXDeg ).arg( aDegSymbol ).arg( aXMin ).arg( aXSec );
434 QString aYStr = QString( "%1%2 %3' %4\"" ).arg( aYDeg ).arg( aDegSymbol ).arg( aYMin ).arg( aYSec );
436 aViewPort->setViewLabelText( QString( "X: %1\nY: %2" ).arg( aXStr ).arg( aYStr ) );
438 else if( aRole == VMR_TransformImage )
439 aViewPort->setViewLabelText( QString( "X: %1\nY: %2" ).arg( (int)aMouseX ).arg( (int)aMouseY ) );
443 void HYDROGUI_Module::updateGV( const bool theIsInit,
444 const bool theIsForced )
446 if( !getDisplayer() )
449 QList<int> aViewManagerIdList;
451 // currently, all views are updated
452 ViewManagerMapIterator anIter( myViewManagerMap );
453 while( anIter.hasNext() )
455 int anId = anIter.next().key();
456 aViewManagerIdList.append( anId );
459 QListIterator<int> anIdIter( aViewManagerIdList );
460 while( anIdIter.hasNext() )
461 getDisplayer()->UpdateAll( anIdIter.next(), theIsInit, theIsForced );
464 void HYDROGUI_Module::createSelector( SUIT_ViewManager* theViewManager )
466 if( !theViewManager )
469 LightApp_SelectionMgr* aSelectionMgr = getApp()->selectionMgr();
473 QString aViewType = theViewManager->getType();
474 if( aViewType != GraphicsView_Viewer::Type() )
477 GraphicsView_ViewManager* aViewManager =
478 dynamic_cast<GraphicsView_ViewManager*>( theViewManager );
482 QList<SUIT_Selector*> aSelectorList;
483 aSelectionMgr->selectors( aViewType, aSelectorList );
485 // disable all alien selectors
486 QList<SUIT_Selector*>::iterator anIter, anIterEnd = aSelectorList.end();
487 for( anIter = aSelectorList.begin(); anIter != anIterEnd; anIter++ )
489 SUIT_Selector* aSelector = *anIter;
490 if( aSelector && !dynamic_cast<HYDROGUI_GVSelector*>( aSelector ) )
491 aSelector->setEnabled( false );
494 new HYDROGUI_GVSelector( this, aViewManager->getViewer(), aSelectionMgr );
497 bool HYDROGUI_Module::setUpdateEnabled( const bool theState )
499 bool aPrevState = myIsUpdateEnabled;
500 myIsUpdateEnabled = theState;
504 bool HYDROGUI_Module::isUpdateEnabled() const
506 return myIsUpdateEnabled;