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