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 setMenuShown( false );
121 setToolShown( false );
123 return LightApp_Module::deactivateModule( theStudy );
126 void HYDROGUI_Module::windows( QMap<int, int>& theMap ) const
129 theMap.insert( LightApp_Application::WT_LogWindow, Qt::BottomDockWidgetArea );
130 theMap.insert( LightApp_Application::WT_ObjectBrowser, Qt::LeftDockWidgetArea );
133 void HYDROGUI_Module::viewManagers( QStringList& theTypesList ) const
135 theTypesList << GraphicsView_Viewer::Type();
138 void HYDROGUI_Module::contextMenuPopup( const QString& theClient,
142 HYDROGUI_DataModel* aModel = getDataModel();
144 size_t aViewId = HYDROGUI_Tool::GetActiveGraphicsViewId( this );
146 bool anIsSelection = false;
147 bool anIsVisibleInSelection = false;
148 bool anIsHiddenInSelection = false;
150 bool anIsImage = false;
151 bool anIsCompositeImage = false;
152 bool anIsPolyline = false;
153 bool anIsVisualState = false;
155 HYDROData_SequenceOfObjects aSeq = HYDROGUI_Tool::GetSelectedObjects( this );
156 for( Standard_Integer anIndex = 1, aLength = aSeq.Length(); anIndex <= aLength; anIndex++ )
158 Handle(HYDROData_Object) anObject = aSeq.Value( anIndex );
159 if( !anObject.IsNull() )
161 anIsSelection = true;
163 bool aVisibility = anObject->IsVisible( aViewId );
164 anIsVisibleInSelection |= aVisibility;
165 anIsHiddenInSelection |= !aVisibility;
167 if( anObject->GetKind() == KIND_IMAGE )
170 Handle(HYDROData_Image) anImage = Handle(HYDROData_Image)::DownCast( anObject );
171 if( !anImage.IsNull() )
172 anIsCompositeImage = anImage->NbReferences() > 0;
174 else if( anObject->GetKind() == KIND_POLYLINE )
176 else if( anObject->GetKind() == KIND_VISUAL_STATE )
177 anIsVisualState = true;
183 theMenu->addAction( action( SaveVisualStateId ) );
184 theMenu->addSeparator();
186 else if( anIsSelection && aSeq.Length() == 1 )
190 theMenu->addAction( action( anIsCompositeImage ? EditCompositeImageId : EditImportedImageId ) );
191 theMenu->addAction( action( ObserveImageId ) );
192 theMenu->addAction( action( ExportImageId ) );
193 theMenu->addSeparator();
195 else if( anIsPolyline )
197 theMenu->addAction( action( EditPolylineId ) );
198 theMenu->addSeparator();
200 else if( anIsVisualState )
202 theMenu->addAction( action( SaveVisualStateId ) );
203 theMenu->addAction( action( LoadVisualStateId ) );
204 theMenu->addSeparator();
210 theMenu->addAction( action( DeleteId ) );
211 theMenu->addSeparator();
214 if( anIsSelection && ( anIsImage || anIsPolyline ) )
216 if( anIsHiddenInSelection )
217 theMenu->addAction( action( ShowId ) );
218 theMenu->addAction( action( ShowOnlyId ) );
219 if( anIsVisibleInSelection )
220 theMenu->addAction( action( HideId ) );
221 theMenu->addSeparator();
224 theMenu->addAction( action( ShowAllId ) );
225 theMenu->addAction( action( HideAllId ) );
226 theMenu->addSeparator();
229 void HYDROGUI_Module::update( const int flags )
231 if( !isUpdateEnabled() )
234 QApplication::setOverrideCursor( Qt::WaitCursor );
236 // To prevent calling this method recursively
237 // from one of the methods called below
238 setUpdateEnabled( false );
240 if( ( flags & UF_Model ) && getDataModel() )
241 getDataModel()->update( getStudyId() );
243 if( ( flags & UF_ObjBrowser ) && getApp() )
244 getApp()->updateObjectBrowser( true );
246 if( ( flags & UF_Viewer ) )
247 updateGV( flags & UF_GV_Init,
248 flags & UF_GV_Forced );
250 if( ( flags & UF_Controls ) && getApp() )
251 getApp()->updateActions();
253 setUpdateEnabled( true );
255 QApplication::restoreOverrideCursor();
258 void HYDROGUI_Module::updateCommandsStatus()
260 LightApp_Module::updateCommandsStatus();
262 updateUndoRedoControls();
265 //action( ... )->setEnabled( ... );
268 HYDROGUI_DataModel* HYDROGUI_Module::getDataModel() const
270 return (HYDROGUI_DataModel*)dataModel();
273 HYDROGUI_Displayer* HYDROGUI_Module::getDisplayer() const
278 GraphicsView_Viewer* HYDROGUI_Module::getViewer( const int theId ) const
280 if( myViewManagerMap.contains( theId ) )
282 ViewManagerInfo anInfo = myViewManagerMap[ theId ];
283 GraphicsView_ViewManager* aViewManager =
284 dynamic_cast<GraphicsView_ViewManager*>( anInfo.first );
286 return aViewManager->getViewer();
291 int HYDROGUI_Module::getViewManagerId( SUIT_ViewManager* theViewManager )
293 ViewManagerMapIterator anIter( myViewManagerMap );
294 while( anIter.hasNext() )
296 int anId = anIter.next().key();
297 const ViewManagerInfo& anInfo = anIter.value();
298 if( anInfo.first == theViewManager )
304 HYDROGUI_Module::ViewManagerRole HYDROGUI_Module::getViewManagerRole( SUIT_ViewManager* theViewManager )
306 int anId = getViewManagerId( theViewManager );
309 const ViewManagerInfo& anInfo = myViewManagerMap[ anId ];
310 return anInfo.second;
315 void HYDROGUI_Module::setViewManagerRole( SUIT_ViewManager* theViewManager,
316 const ViewManagerRole theRole )
318 int anId = getViewManagerId( theViewManager );
321 ViewManagerInfo& anInfo = myViewManagerMap[ anId ];
322 anInfo.second = theRole;
326 CAM_DataModel* HYDROGUI_Module::createDataModel()
328 return new HYDROGUI_DataModel( this );
331 void HYDROGUI_Module::customEvent( QEvent* e )
333 int aType = e->type();
334 if ( aType == NewViewEvent )
336 SALOME_CustomEvent* ce = ( SALOME_CustomEvent* )e;
337 if( GraphicsView_ViewFrame* aViewFrame = ( GraphicsView_ViewFrame* )ce->data() )
339 if( GraphicsView_Viewer* aViewer = dynamic_cast<GraphicsView_Viewer*>( aViewFrame->getViewer() ) )
341 SUIT_ViewManager* aViewManager = aViewer->getViewManager();
342 ViewManagerRole aRole = getViewManagerRole( aViewManager );
343 if( aRole == VMR_General )
346 aViewer->activateTransform( GraphicsView_Viewer::FitAll );
348 if( GraphicsView_ViewPort* aViewPort = aViewer->getActiveViewPort() )
350 aViewPort->setInteractionFlag( GraphicsView_ViewPort::TraceBoundingRect );
351 aViewPort->setInteractionFlag( GraphicsView_ViewPort::ImmediateContextMenu );
352 aViewPort->setInteractionFlag( GraphicsView_ViewPort::ImmediateSelection );
355 //aViewPort->setViewLabelPosition( GraphicsView_ViewPort::VLP_BottomLeft, true );
362 bool HYDROGUI_Module::eventFilter( QObject* theObj, QEvent* theEvent )
364 QEvent::Type aType = theEvent->type();
365 if( theObj->inherits( "GraphicsView_ViewFrame" ) )
367 if( aType == QEvent::Show )
369 SALOME_CustomEvent* e = new SALOME_CustomEvent( NewViewEvent );
370 e->setData( theObj );
371 QApplication::postEvent( this, e );
372 theObj->removeEventFilter( this );
375 return LightApp_Module::eventFilter( theObj, theEvent );
378 void HYDROGUI_Module::onViewManagerAdded( SUIT_ViewManager* theViewManager )
380 LightApp_Module::onViewManagerAdded( theViewManager );
382 if( theViewManager->getType() == GraphicsView_Viewer::Type() )
384 createSelector( theViewManager ); // replace the default selector
386 connect( theViewManager, SIGNAL( viewCreated( SUIT_ViewWindow* ) ),
387 this, SLOT( onViewCreated( SUIT_ViewWindow* ) ) );
390 ViewManagerInfo anInfo( theViewManager, VMR_General );
391 myViewManagerMap.insert( ViewManagerId++, anInfo );
394 void HYDROGUI_Module::onViewManagerRemoved( SUIT_ViewManager* theViewManager )
396 LightApp_Module::onViewManagerRemoved( theViewManager );
398 createSelector( theViewManager ); // replace the default selector
400 int anId = getViewManagerId( theViewManager );
402 myViewManagerMap.remove( anId );
405 void HYDROGUI_Module::onViewCreated( SUIT_ViewWindow* theViewWindow )
407 if( theViewWindow && theViewWindow->inherits( "GraphicsView_ViewFrame" ) )
409 if( GraphicsView_ViewFrame* aViewFrame = dynamic_cast<GraphicsView_ViewFrame*>( theViewWindow ) )
411 aViewFrame->installEventFilter( this );
413 GraphicsView_ViewPort* aViewPort = aViewFrame->getViewPort();
415 connect( aViewPort, SIGNAL( vpMouseEvent( QGraphicsSceneMouseEvent* ) ),
416 this, SLOT( onViewPortMouseEvent( QGraphicsSceneMouseEvent* ) ) );
422 void HYDROGUI_Module::onViewPortMouseEvent( QGraphicsSceneMouseEvent* theEvent )
424 if( GraphicsView_ViewPort* aViewPort = qobject_cast<GraphicsView_ViewPort*>( sender() ) )
426 SUIT_ViewManager* aViewManager = 0;
428 QObject* aParent = aViewPort;
429 while( aParent = aParent->parent() )
431 if( GraphicsView_ViewFrame* aViewFrame = dynamic_cast<GraphicsView_ViewFrame*>( aParent ) )
433 if( GraphicsView_Viewer* aViewer = aViewFrame->getViewer() )
435 aViewManager = aViewer->getViewManager();
444 double aMouseX = theEvent->scenePos().x();
445 double aMouseY = theEvent->scenePos().y();
447 ViewManagerRole aRole = getViewManagerRole( aViewManager );
448 if( aRole == VMR_General )
450 int aXDeg = 0, aYDeg = 0;
451 int aXMin = 0, aYMin = 0;
452 double aXSec = 0, aYSec = 0;
453 HYDROGUI_Tool::DoubleToLambert( aMouseX, aXDeg, aXMin, aXSec );
454 HYDROGUI_Tool::DoubleToLambert( aMouseY, aYDeg, aYMin, aYSec );
456 QString aDegSymbol( QChar( 0x00B0 ) );
457 QString aXStr = QString( "%1%2 %3' %4\"" ).arg( aXDeg ).arg( aDegSymbol ).arg( aXMin ).arg( aXSec );
458 QString aYStr = QString( "%1%2 %3' %4\"" ).arg( aYDeg ).arg( aDegSymbol ).arg( aYMin ).arg( aYSec );
460 aViewPort->setViewLabelText( QString( "X: %1\nY: %2" ).arg( aXStr ).arg( aYStr ) );
462 else if( aRole == VMR_TransformImage )
463 aViewPort->setViewLabelText( QString( "X: %1\nY: %2" ).arg( (int)aMouseX ).arg( (int)aMouseY ) );
467 void HYDROGUI_Module::updateGV( const bool theIsInit,
468 const bool theIsForced )
470 if( !getDisplayer() )
473 QList<int> aViewManagerIdList;
475 // currently, all views are updated
476 ViewManagerMapIterator anIter( myViewManagerMap );
477 while( anIter.hasNext() )
479 int anId = anIter.next().key();
480 aViewManagerIdList.append( anId );
483 QListIterator<int> anIdIter( aViewManagerIdList );
484 while( anIdIter.hasNext() )
485 getDisplayer()->UpdateAll( anIdIter.next(), theIsInit, theIsForced );
488 void HYDROGUI_Module::createSelector( SUIT_ViewManager* theViewManager )
490 if( !theViewManager )
493 LightApp_SelectionMgr* aSelectionMgr = getApp()->selectionMgr();
497 QString aViewType = theViewManager->getType();
498 if( aViewType != GraphicsView_Viewer::Type() )
501 GraphicsView_ViewManager* aViewManager =
502 dynamic_cast<GraphicsView_ViewManager*>( theViewManager );
506 QList<SUIT_Selector*> aSelectorList;
507 aSelectionMgr->selectors( aViewType, aSelectorList );
509 // disable all alien selectors
510 QList<SUIT_Selector*>::iterator anIter, anIterEnd = aSelectorList.end();
511 for( anIter = aSelectorList.begin(); anIter != anIterEnd; anIter++ )
513 SUIT_Selector* aSelector = *anIter;
514 if( aSelector && !dynamic_cast<HYDROGUI_GVSelector*>( aSelector ) )
515 aSelector->setEnabled( false );
518 new HYDROGUI_GVSelector( this, aViewManager->getViewer(), aSelectionMgr );
521 bool HYDROGUI_Module::setUpdateEnabled( const bool theState )
523 bool aPrevState = myIsUpdateEnabled;
524 myIsUpdateEnabled = theState;
528 bool HYDROGUI_Module::isUpdateEnabled() const
530 return myIsUpdateEnabled;