Salome HOME
7d2197a1c64f54c60400a1b7255a0a459dc6f4c1
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_Module.cxx
1 // Copyright (C) 2007-2013  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
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.
10 //
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.
15 //
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
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 #include "HYDROGUI_Module.h"
24
25 #include "HYDROGUI.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"
35
36 #include <GraphicsView_ViewFrame.h>
37 #include <GraphicsView_ViewManager.h>
38 #include <GraphicsView_ViewPort.h>
39 #include <GraphicsView_Viewer.h>
40
41 #include <LightApp_Application.h>
42 #include <LightApp_GVSelector.h>
43 #include <LightApp_SelectionMgr.h>
44 #include <LightApp_UpdateFlags.h>
45
46 #include <SALOME_Event.h>
47
48 #include <SUIT_Study.h>
49 #include <SUIT_ViewManager.h>
50
51 #include <QAction>
52 #include <QApplication>
53 #include <QGraphicsSceneMouseEvent>
54 #include <QMenu>
55
56 static int ViewManagerId = 0;
57
58 extern "C" HYDRO_EXPORT CAM_Module* createModule()
59 {
60   return new HYDROGUI_Module();
61 }
62
63 HYDROGUI_Module::HYDROGUI_Module()
64 : LightApp_Module( "HYDRO" ),
65   myDisplayer( 0 ),
66   myIsUpdateEnabled( true )
67 {
68 }
69
70 HYDROGUI_Module::~HYDROGUI_Module()
71 {
72 }
73
74 int HYDROGUI_Module::getStudyId() const
75 {
76   LightApp_Application* anApp = getApp();
77   return anApp ? anApp->activeStudy()->id() : 0;
78 }
79
80 void HYDROGUI_Module::initialize( CAM_Application* theApp )
81 {
82   LightApp_Module::initialize( theApp );
83
84   createActions();
85   createUndoRedoActions();
86   createMenus();
87   createPopups();
88   createToolbars();
89
90   setMenuShown( false );
91   setToolShown( false );
92
93   myDisplayer = new HYDROGUI_Displayer( this );
94 }
95
96 bool HYDROGUI_Module::activateModule( SUIT_Study* theStudy )
97 {
98   bool aRes = LightApp_Module::activateModule( theStudy );
99
100   setMenuShown( true );
101   setToolShown( true );
102
103   update( UF_All );
104
105   updateCommandsStatus();
106
107   return aRes;
108 }
109
110 bool HYDROGUI_Module::deactivateModule( SUIT_Study* theStudy )
111 {
112   ViewManagerMapIterator anIter( myViewManagerMap );
113   while( anIter.hasNext() )
114     if( SUIT_ViewManager* aViewManager = anIter.next().value().first )
115       getApp()->removeViewManager( aViewManager );
116   myViewManagerMap.clear();
117
118   setMenuShown( false );
119   setToolShown( false );
120
121   return LightApp_Module::deactivateModule( theStudy );
122 }
123
124 void HYDROGUI_Module::windows( QMap<int, int>& theMap ) const
125 {
126   theMap.clear();
127   theMap.insert( LightApp_Application::WT_LogWindow,     Qt::BottomDockWidgetArea );
128   theMap.insert( LightApp_Application::WT_ObjectBrowser, Qt::LeftDockWidgetArea   );
129 }
130
131 void HYDROGUI_Module::viewManagers( QStringList& theTypesList ) const
132 {
133   theTypesList << GraphicsView_Viewer::Type();
134 }
135
136 void HYDROGUI_Module::contextMenuPopup( const QString& theClient,
137                                         QMenu* theMenu,
138                                         QString& theTitle )
139 {
140   HYDROGUI_DataModel* aModel = getDataModel();
141
142   size_t aViewId = HYDROGUI_Tool::GetActiveGraphicsViewId( this );
143
144   bool anIsSelection = false;
145   bool anIsVisibleInSelection = false;
146   bool anIsHiddenInSelection = false;
147
148   bool anIsImage = false;
149   bool anIsPolyline = false;
150   bool anIsVisualState = false;
151
152   HYDROData_SequenceOfObjects aSeq = HYDROGUI_Tool::GetSelectedObjects( this );
153   for( Standard_Integer anIndex = 1, aLength = aSeq.Length(); anIndex <= aLength; anIndex++ )
154   {
155     Handle(HYDROData_Object) anObject = aSeq.Value( anIndex );
156     if( !anObject.IsNull() )
157     {
158       anIsSelection = true;
159
160       bool aVisibility = anObject->IsVisible( aViewId );
161       anIsVisibleInSelection |= aVisibility;
162       anIsHiddenInSelection |= !aVisibility;
163
164       if( anObject->GetKind() == KIND_IMAGE )
165         anIsImage = true;
166       else if( anObject->GetKind() == KIND_POLYLINE )
167         anIsPolyline = true;
168       else if( anObject->GetKind() == KIND_VISUAL_STATE )
169         anIsVisualState = true;
170     }
171   }
172
173   if( aSeq.IsEmpty() )
174   {
175     theMenu->addAction( action( SaveVisualStateId ) );
176     theMenu->addSeparator();
177   }
178   else if( anIsSelection && aSeq.Length() == 1 )
179   {
180     if( anIsImage )
181     {
182       theMenu->addAction( action( EditImageId ) );
183       theMenu->addAction( action( ObserveImageId ) );
184       theMenu->addAction( action( ExportImageId ) );
185       theMenu->addSeparator();
186     }
187     else if( anIsPolyline )
188     {
189       theMenu->addAction( action( EditPolylineId ) );
190       theMenu->addSeparator();
191     }
192     else if( anIsVisualState )
193     {
194       theMenu->addAction( action( SaveVisualStateId ) );
195       theMenu->addAction( action( LoadVisualStateId ) );
196       theMenu->addSeparator();
197     }
198   }
199
200   if( anIsSelection )
201   {
202     theMenu->addAction( action( DeleteId ) );
203     theMenu->addSeparator();
204   }
205
206   if( anIsSelection && ( anIsImage || anIsPolyline ) )
207   {
208     if( anIsHiddenInSelection )
209       theMenu->addAction( action( ShowId ) );
210     theMenu->addAction( action( ShowOnlyId ) );
211     if( anIsVisibleInSelection )
212       theMenu->addAction( action( HideId ) );
213     theMenu->addSeparator();
214   }
215
216   theMenu->addAction( action( ShowAllId ) );
217   theMenu->addAction( action( HideAllId ) );
218   theMenu->addSeparator();
219 }
220
221 void HYDROGUI_Module::update( const int flags )
222 {
223   if( !isUpdateEnabled() )
224     return;
225
226   QApplication::setOverrideCursor( Qt::WaitCursor );
227
228   // To prevent calling this method recursively
229   // from one of the methods called below
230   setUpdateEnabled( false );
231
232   if( ( flags & UF_Model ) && getDataModel() )
233     getDataModel()->update( getStudyId() );
234
235   if( ( flags & UF_ObjBrowser ) && getApp() )
236     getApp()->updateObjectBrowser( true );
237
238   if( ( flags & UF_Viewer ) )
239     updateGV( flags & UF_GV_Init,
240               flags & UF_GV_Forced );
241
242   if( ( flags & UF_Controls ) && getApp() )
243     getApp()->updateActions();
244
245   setUpdateEnabled( true );
246
247   QApplication::restoreOverrideCursor();
248 }
249
250 void HYDROGUI_Module::updateCommandsStatus()
251 {
252   LightApp_Module::updateCommandsStatus();
253
254   updateUndoRedoControls();
255
256   // to do
257   //action( ... )->setEnabled( ... );
258 }
259
260 HYDROGUI_DataModel* HYDROGUI_Module::getDataModel() const
261 {
262   return (HYDROGUI_DataModel*)dataModel();
263 }
264
265 HYDROGUI_Displayer* HYDROGUI_Module::getDisplayer() const
266 {
267   return myDisplayer;
268 }
269
270 GraphicsView_Viewer* HYDROGUI_Module::getViewer( const int theId ) const
271 {
272   if( myViewManagerMap.contains( theId ) )
273   {
274     ViewManagerInfo anInfo = myViewManagerMap[ theId ];
275     GraphicsView_ViewManager* aViewManager =
276       dynamic_cast<GraphicsView_ViewManager*>( anInfo.first );
277     if( aViewManager )
278       return aViewManager->getViewer();
279   }
280   return NULL;
281 }
282
283 int HYDROGUI_Module::getViewManagerId( SUIT_ViewManager* theViewManager )
284 {
285   ViewManagerMapIterator anIter( myViewManagerMap );
286   while( anIter.hasNext() )
287   {
288     int anId = anIter.next().key();
289     const ViewManagerInfo& anInfo = anIter.value();
290     if( anInfo.first == theViewManager )
291       return anId;
292   }
293   return -1;
294 }
295
296 HYDROGUI_Module::ViewManagerRole HYDROGUI_Module::getViewManagerRole( SUIT_ViewManager* theViewManager )
297 {
298   int anId = getViewManagerId( theViewManager );
299   if( anId != -1 )
300   {
301     const ViewManagerInfo& anInfo = myViewManagerMap[ anId ];
302     return anInfo.second;
303   }
304   return VMR_Unknown;
305 }
306
307 void HYDROGUI_Module::setViewManagerRole( SUIT_ViewManager* theViewManager,
308                                           const ViewManagerRole theRole )
309 {
310   int anId = getViewManagerId( theViewManager );
311   if( anId != -1 )
312   {
313     ViewManagerInfo& anInfo = myViewManagerMap[ anId ];
314     anInfo.second = theRole;
315   }
316 }
317
318 CAM_DataModel* HYDROGUI_Module::createDataModel()
319 {
320   return new HYDROGUI_DataModel( this );
321 }
322
323 void HYDROGUI_Module::customEvent( QEvent* e )
324 {
325   int aType = e->type();
326   if ( aType == NewViewEvent )
327   {
328     SALOME_CustomEvent* ce = ( SALOME_CustomEvent* )e;
329     if( GraphicsView_ViewFrame* aViewFrame = ( GraphicsView_ViewFrame* )ce->data() )
330     {
331       if( GraphicsView_Viewer* aViewer = dynamic_cast<GraphicsView_Viewer*>( aViewFrame->getViewer() ) )
332       {
333         SUIT_ViewManager* aViewManager = aViewer->getViewManager();
334         ViewManagerRole aRole = getViewManagerRole( aViewManager );
335         if( aRole == VMR_General )
336           update( UF_Viewer );
337
338         aViewer->activateTransform( GraphicsView_Viewer::FitAll );
339
340         if( GraphicsView_ViewPort* aViewPort = aViewer->getActiveViewPort() )
341         {
342           aViewPort->setInteractionFlag( GraphicsView_ViewPort::TraceBoundingRect );
343           aViewPort->setInteractionFlag( GraphicsView_ViewPort::ImmediateContextMenu );
344           aViewPort->setInteractionFlag( GraphicsView_ViewPort::ImmediateSelection );
345
346           // ouv: tmp
347           //aViewPort->setViewLabelPosition( GraphicsView_ViewPort::VLP_BottomLeft, true );
348         }
349       }
350     }
351   }
352 }
353
354 bool HYDROGUI_Module::eventFilter( QObject* theObj, QEvent* theEvent )
355 {
356   QEvent::Type aType = theEvent->type();
357   if( theObj->inherits( "GraphicsView_ViewFrame" ) )
358   {
359     if( aType == QEvent::Show )
360     {
361       SALOME_CustomEvent* e = new SALOME_CustomEvent( NewViewEvent );
362       e->setData( theObj );
363       QApplication::postEvent( this, e );
364       theObj->removeEventFilter( this );
365     }
366   }
367   return LightApp_Module::eventFilter( theObj, theEvent );
368 }
369
370 void HYDROGUI_Module::onViewManagerAdded( SUIT_ViewManager* theViewManager )
371 {
372   LightApp_Module::onViewManagerAdded( theViewManager );
373
374   if( theViewManager->getType() == GraphicsView_Viewer::Type() )
375   { 
376     createSelector( theViewManager ); // replace the default selector
377
378     connect( theViewManager, SIGNAL( viewCreated( SUIT_ViewWindow* ) ),
379              this, SLOT( onViewCreated( SUIT_ViewWindow* ) ) );
380   }
381
382   ViewManagerInfo anInfo( theViewManager, VMR_General );
383   myViewManagerMap.insert( ViewManagerId++, anInfo );
384 }
385
386 void HYDROGUI_Module::onViewManagerRemoved( SUIT_ViewManager* theViewManager )
387 {
388   LightApp_Module::onViewManagerRemoved( theViewManager );
389
390   createSelector( theViewManager ); // replace the default selector
391
392   int anId = getViewManagerId( theViewManager );
393   if( anId != -1 )
394     myViewManagerMap.remove( anId );
395 }
396
397 void HYDROGUI_Module::onViewCreated( SUIT_ViewWindow* theViewWindow )
398 {
399   if( theViewWindow && theViewWindow->inherits( "GraphicsView_ViewFrame" ) )
400   {
401     if( GraphicsView_ViewFrame* aViewFrame = dynamic_cast<GraphicsView_ViewFrame*>( theViewWindow ) )
402     {
403       aViewFrame->installEventFilter( this );
404
405       GraphicsView_ViewPort* aViewPort = aViewFrame->getViewPort();
406
407       connect( aViewPort, SIGNAL( vpMouseEvent( QGraphicsSceneMouseEvent* ) ),
408                this, SLOT( onViewPortMouseEvent( QGraphicsSceneMouseEvent* ) ) );
409       return;
410     }
411   }
412 }
413
414 void HYDROGUI_Module::onViewPortMouseEvent( QGraphicsSceneMouseEvent* theEvent )
415 {
416   if( GraphicsView_ViewPort* aViewPort = qobject_cast<GraphicsView_ViewPort*>( sender() ) )
417   {
418     SUIT_ViewManager* aViewManager = 0;
419
420     QObject* aParent = aViewPort;
421     while( aParent = aParent->parent() )
422     {
423       if( GraphicsView_ViewFrame* aViewFrame = dynamic_cast<GraphicsView_ViewFrame*>( aParent ) )
424       {
425         if( GraphicsView_Viewer* aViewer = aViewFrame->getViewer() )
426         {
427           aViewManager = aViewer->getViewManager();
428           break;
429         }
430       }
431     }
432
433     if( !aViewManager )
434       return;
435
436     double aMouseX = theEvent->scenePos().x();
437     double aMouseY = theEvent->scenePos().y();
438
439     ViewManagerRole aRole = getViewManagerRole( aViewManager );
440     if( aRole == VMR_General )
441     {
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 );
447
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 );
451
452       aViewPort->setViewLabelText( QString( "X: %1\nY: %2" ).arg( aXStr ).arg( aYStr ) );
453     }
454     else if( aRole == VMR_TransformImage )
455       aViewPort->setViewLabelText( QString( "X: %1\nY: %2" ).arg( (int)aMouseX ).arg( (int)aMouseY ) );
456   }
457 }
458
459 void HYDROGUI_Module::updateGV( const bool theIsInit,
460                                 const bool theIsForced )
461 {
462   if( !getDisplayer() )
463     return;
464
465   QList<int> aViewManagerIdList;
466
467   // currently, all views are updated
468   ViewManagerMapIterator anIter( myViewManagerMap );
469   while( anIter.hasNext() )
470   {
471     int anId = anIter.next().key();
472     aViewManagerIdList.append( anId );
473   }
474
475   QListIterator<int> anIdIter( aViewManagerIdList );
476   while( anIdIter.hasNext() )
477     getDisplayer()->UpdateAll( anIdIter.next(), theIsInit, theIsForced );
478 }
479
480 void HYDROGUI_Module::createSelector( SUIT_ViewManager* theViewManager )
481 {
482   if( !theViewManager )
483     return;
484
485   LightApp_SelectionMgr* aSelectionMgr = getApp()->selectionMgr();
486   if( !aSelectionMgr )
487     return;
488
489   QString aViewType = theViewManager->getType();
490   if( aViewType != GraphicsView_Viewer::Type() )
491     return;
492
493   GraphicsView_ViewManager* aViewManager =
494     dynamic_cast<GraphicsView_ViewManager*>( theViewManager );
495   if( !aViewManager )
496     return;
497
498   QList<SUIT_Selector*> aSelectorList;
499   aSelectionMgr->selectors( aViewType, aSelectorList );
500
501   // disable all alien selectors
502   QList<SUIT_Selector*>::iterator anIter, anIterEnd = aSelectorList.end();
503   for( anIter = aSelectorList.begin(); anIter != anIterEnd; anIter++ )
504   {
505     SUIT_Selector* aSelector = *anIter;
506     if( aSelector && !dynamic_cast<HYDROGUI_GVSelector*>( aSelector ) )
507       aSelector->setEnabled( false );
508   }
509
510   new HYDROGUI_GVSelector( this, aViewManager->getViewer(), aSelectionMgr );
511 }
512
513 bool HYDROGUI_Module::setUpdateEnabled( const bool theState )
514 {
515   bool aPrevState = myIsUpdateEnabled;
516   myIsUpdateEnabled = theState;
517   return aPrevState;
518 }
519
520 bool HYDROGUI_Module::isUpdateEnabled() const
521 {
522   return myIsUpdateEnabled;
523 }