Salome HOME
91276860faab946b4b7ec09861d9499581d18e47
[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   {
224     getDataModel()->update( getStudyId() );
225   }
226   else
227   {
228     /* to do
229     if( ( flags & UF_ObjBrowser ) && getObjectBrowser() )
230       updateObjectBrowser();
231     */
232   }
233
234   if( ( flags & UF_Viewer ) )
235     updateGV( flags & UF_GV_Init,
236               flags & UF_GV_Forced );
237
238   if( ( flags & UF_Controls ) && getApp() )
239     getApp()->updateActions();
240
241   setUpdateEnabled( true );
242
243   QApplication::restoreOverrideCursor();
244 }
245
246 void HYDROGUI_Module::updateCommandsStatus()
247 {
248   LightApp_Module::updateCommandsStatus();
249
250   updateUndoRedoControls();
251
252   // to do
253   //action( ... )->setEnabled( ... );
254 }
255
256 HYDROGUI_DataModel* HYDROGUI_Module::getDataModel() const
257 {
258   return (HYDROGUI_DataModel*)dataModel();
259 }
260
261 HYDROGUI_Displayer* HYDROGUI_Module::getDisplayer() const
262 {
263   return myDisplayer;
264 }
265
266 GraphicsView_Viewer* HYDROGUI_Module::getViewer( const int theId ) const
267 {
268   if( myViewManagerMap.contains( theId ) )
269   {
270     ViewManagerInfo anInfo = myViewManagerMap[ theId ];
271     GraphicsView_ViewManager* aViewManager =
272       dynamic_cast<GraphicsView_ViewManager*>( anInfo.first );
273     if( aViewManager )
274       return aViewManager->getViewer();
275   }
276   return NULL;
277 }
278
279 int HYDROGUI_Module::getViewManagerId( SUIT_ViewManager* theViewManager )
280 {
281   ViewManagerMapIterator anIter( myViewManagerMap );
282   while( anIter.hasNext() )
283   {
284     int anId = anIter.next().key();
285     const ViewManagerInfo& anInfo = anIter.value();
286     if( anInfo.first == theViewManager )
287       return anId;
288   }
289   return -1;
290 }
291
292 HYDROGUI_Module::ViewManagerRole HYDROGUI_Module::getViewManagerRole( SUIT_ViewManager* theViewManager )
293 {
294   int anId = getViewManagerId( theViewManager );
295   if( anId != -1 )
296   {
297     const ViewManagerInfo& anInfo = myViewManagerMap[ anId ];
298     return anInfo.second;
299   }
300   return VMR_Unknown;
301 }
302
303 void HYDROGUI_Module::setViewManagerRole( SUIT_ViewManager* theViewManager,
304                                           const ViewManagerRole theRole )
305 {
306   int anId = getViewManagerId( theViewManager );
307   if( anId != -1 )
308   {
309     ViewManagerInfo& anInfo = myViewManagerMap[ anId ];
310     anInfo.second = theRole;
311   }
312 }
313
314 CAM_DataModel* HYDROGUI_Module::createDataModel()
315 {
316   return new HYDROGUI_DataModel( this );
317 }
318
319 void HYDROGUI_Module::customEvent( QEvent* e )
320 {
321   int aType = e->type();
322   if ( aType == NewViewEvent )
323   {
324     SALOME_CustomEvent* ce = ( SALOME_CustomEvent* )e;
325     if( GraphicsView_ViewFrame* aViewFrame = ( GraphicsView_ViewFrame* )ce->data() )
326     {
327       if( GraphicsView_Viewer* aViewer = dynamic_cast<GraphicsView_Viewer*>( aViewFrame->getViewer() ) )
328       {
329         SUIT_ViewManager* aViewManager = aViewer->getViewManager();
330         ViewManagerRole aRole = getViewManagerRole( aViewManager );
331         if( aRole == VMR_General )
332           update( UF_Viewer );
333
334         aViewer->activateTransform( GraphicsView_Viewer::FitAll );
335
336         if( GraphicsView_ViewPort* aViewPort = aViewer->getActiveViewPort() )
337         {
338           aViewPort->setInteractionFlag( GraphicsView_ViewPort::TraceBoundingRect );
339           aViewPort->setInteractionFlag( GraphicsView_ViewPort::ImmediateContextMenu );
340           aViewPort->setInteractionFlag( GraphicsView_ViewPort::ImmediateSelection );
341
342           // ouv: tmp
343           //aViewPort->setViewLabelPosition( GraphicsView_ViewPort::VLP_BottomLeft, true );
344         }
345       }
346     }
347   }
348 }
349
350 bool HYDROGUI_Module::eventFilter( QObject* theObj, QEvent* theEvent )
351 {
352   QEvent::Type aType = theEvent->type();
353   if( theObj->inherits( "GraphicsView_ViewFrame" ) )
354   {
355     if( aType == QEvent::Show )
356     {
357       SALOME_CustomEvent* e = new SALOME_CustomEvent( NewViewEvent );
358       e->setData( theObj );
359       QApplication::postEvent( this, e );
360       theObj->removeEventFilter( this );
361     }
362   }
363   return LightApp_Module::eventFilter( theObj, theEvent );
364 }
365
366 void HYDROGUI_Module::onViewManagerAdded( SUIT_ViewManager* theViewManager )
367 {
368   LightApp_Module::onViewManagerAdded( theViewManager );
369
370   if( theViewManager->getType() == GraphicsView_Viewer::Type() )
371   { 
372     createSelector( theViewManager ); // replace the default selector
373
374     connect( theViewManager, SIGNAL( viewCreated( SUIT_ViewWindow* ) ),
375              this, SLOT( onViewCreated( SUIT_ViewWindow* ) ) );
376   }
377
378   ViewManagerInfo anInfo( theViewManager, VMR_General );
379   myViewManagerMap.insert( ViewManagerId++, anInfo );
380 }
381
382 void HYDROGUI_Module::onViewManagerRemoved( SUIT_ViewManager* theViewManager )
383 {
384   LightApp_Module::onViewManagerRemoved( theViewManager );
385
386   createSelector( theViewManager ); // replace the default selector
387
388   int anId = getViewManagerId( theViewManager );
389   if( anId != -1 )
390     myViewManagerMap.remove( anId );
391 }
392
393 void HYDROGUI_Module::onViewCreated( SUIT_ViewWindow* theViewWindow )
394 {
395   if( theViewWindow && theViewWindow->inherits( "GraphicsView_ViewFrame" ) )
396   {
397     if( GraphicsView_ViewFrame* aViewFrame = dynamic_cast<GraphicsView_ViewFrame*>( theViewWindow ) )
398     {
399       aViewFrame->installEventFilter( this );
400
401       GraphicsView_ViewPort* aViewPort = aViewFrame->getViewPort();
402
403       connect( aViewPort, SIGNAL( vpMouseEvent( QGraphicsSceneMouseEvent* ) ),
404                this, SLOT( onViewPortMouseEvent( QGraphicsSceneMouseEvent* ) ) );
405       return;
406     }
407   }
408 }
409
410 void HYDROGUI_Module::onViewPortMouseEvent( QGraphicsSceneMouseEvent* theEvent )
411 {
412   if( GraphicsView_ViewPort* aViewPort = qobject_cast<GraphicsView_ViewPort*>( sender() ) )
413   {
414     SUIT_ViewManager* aViewManager = 0;
415
416     QObject* aParent = aViewPort;
417     while( aParent = aParent->parent() )
418     {
419       if( GraphicsView_ViewFrame* aViewFrame = dynamic_cast<GraphicsView_ViewFrame*>( aParent ) )
420       {
421         if( GraphicsView_Viewer* aViewer = aViewFrame->getViewer() )
422         {
423           aViewManager = aViewer->getViewManager();
424           break;
425         }
426       }
427     }
428
429     if( !aViewManager )
430       return;
431
432     double aMouseX = theEvent->scenePos().x();
433     double aMouseY = theEvent->scenePos().y();
434
435     ViewManagerRole aRole = getViewManagerRole( aViewManager );
436     if( aRole == VMR_General )
437     {
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 );
443
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 );
447
448       aViewPort->setViewLabelText( QString( "X: %1\nY: %2" ).arg( aXStr ).arg( aYStr ) );
449     }
450     else if( aRole == VMR_Mapping )
451       aViewPort->setViewLabelText( QString( "X: %1\nY: %2" ).arg( (int)aMouseX ).arg( (int)aMouseY ) );
452   }
453 }
454
455 void HYDROGUI_Module::updateGV( const bool theIsInit,
456                                 const bool theIsForced )
457 {
458   if( !getDisplayer() )
459     return;
460
461   QList<int> aViewManagerIdList;
462
463   // currently, all views are updated
464   ViewManagerMapIterator anIter( myViewManagerMap );
465   while( anIter.hasNext() )
466   {
467     int anId = anIter.next().key();
468     aViewManagerIdList.append( anId );
469   }
470
471   QListIterator<int> anIdIter( aViewManagerIdList );
472   while( anIdIter.hasNext() )
473     getDisplayer()->UpdateAll( anIdIter.next(), theIsInit, theIsForced );
474 }
475
476 void HYDROGUI_Module::createSelector( SUIT_ViewManager* theViewManager )
477 {
478   if( !theViewManager )
479     return;
480
481   LightApp_SelectionMgr* aSelectionMgr = getApp()->selectionMgr();
482   if( !aSelectionMgr )
483     return;
484
485   QString aViewType = theViewManager->getType();
486   if( aViewType != GraphicsView_Viewer::Type() )
487     return;
488
489   GraphicsView_ViewManager* aViewManager =
490     dynamic_cast<GraphicsView_ViewManager*>( theViewManager );
491   if( !aViewManager )
492     return;
493
494   QList<SUIT_Selector*> aSelectorList;
495   aSelectionMgr->selectors( aViewType, aSelectorList );
496
497   // disable all alien selectors
498   QList<SUIT_Selector*>::iterator anIter, anIterEnd = aSelectorList.end();
499   for( anIter = aSelectorList.begin(); anIter != anIterEnd; anIter++ )
500   {
501     SUIT_Selector* aSelector = *anIter;
502     if( aSelector && !dynamic_cast<HYDROGUI_GVSelector*>( aSelector ) )
503       aSelector->setEnabled( false );
504   }
505
506   new HYDROGUI_GVSelector( this, aViewManager->getViewer(), aSelectionMgr );
507 }
508
509 bool HYDROGUI_Module::setUpdateEnabled( const bool theState )
510 {
511   bool aPrevState = myIsUpdateEnabled;
512   myIsUpdateEnabled = theState;
513   return aPrevState;
514 }
515
516 bool HYDROGUI_Module::isUpdateEnabled() const
517 {
518   return myIsUpdateEnabled;
519 }