Salome HOME
567dab1ff2faa7035bdf8c1d5b144dfe538bb209
[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   bool anIsSelection = false;
143   bool anIsVisibleInSelection = false;
144   bool anIsHiddenInSelection = false;
145
146   bool anIsImage = false;
147   bool anIsPolyline = false;
148
149   HYDROData_SequenceOfObjects aSeq = HYDROGUI_Tool::GetSelectedObjects( this );
150   for( Standard_Integer anIndex = 1, aLength = aSeq.Length(); anIndex <= aLength; anIndex++ )
151   {
152     Handle(HYDROData_Object) anObject = aSeq.Value( anIndex );
153     if( !anObject.IsNull() )
154     {
155       anIsSelection = true;
156
157       bool aVisibility = anObject->GetVisibility();
158       anIsVisibleInSelection |= aVisibility;
159       anIsHiddenInSelection |= !aVisibility;
160
161       if( anObject->GetKind() == KIND_IMAGE )
162         anIsImage = true;
163
164       if( anObject->GetKind() == KIND_POLYLINE )
165         anIsPolyline = true;
166     }
167   }
168
169   if( aSeq.Length() == 1 )
170   {
171     if( anIsImage )
172     {
173       theMenu->addAction( action( EditImageId ) );
174       theMenu->addAction( action( ExportImageId ) );
175       theMenu->addSeparator();
176     }
177     else if( anIsPolyline )
178     {
179       theMenu->addAction( action( EditPolylineId ) );
180       theMenu->addSeparator();
181     }
182   }
183
184   if( anIsSelection )
185   {
186     theMenu->addAction( action( DeleteId ) );
187     theMenu->addSeparator();
188   }
189
190   if( anIsSelection )
191   {
192     if( anIsHiddenInSelection )
193       theMenu->addAction( action( ShowId ) );
194     theMenu->addAction( action( ShowOnlyId ) );
195     if( anIsVisibleInSelection )
196       theMenu->addAction( action( HideId ) );
197     theMenu->addSeparator();
198   }
199   theMenu->addAction( action( ShowAllId ) );
200   theMenu->addAction( action( HideAllId ) );
201   theMenu->addSeparator();
202 }
203
204 void HYDROGUI_Module::update( const int flags )
205 {
206   if( !isUpdateEnabled() )
207     return;
208
209   QApplication::setOverrideCursor( Qt::WaitCursor );
210
211   // To prevent calling this method recursively
212   // from one of the methods called below
213   setUpdateEnabled( false );
214
215   if( ( flags & UF_Model ) && getDataModel() )
216     getDataModel()->update( getStudyId() );
217
218   if( ( flags & UF_ObjBrowser ) && getApp() )
219     getApp()->updateObjectBrowser( true );
220
221   if( ( flags & UF_Viewer ) )
222     updateGV( flags & UF_GV_Init,
223               flags & UF_GV_Forced );
224
225   if( ( flags & UF_Controls ) && getApp() )
226     getApp()->updateActions();
227
228   setUpdateEnabled( true );
229
230   QApplication::restoreOverrideCursor();
231 }
232
233 void HYDROGUI_Module::updateCommandsStatus()
234 {
235   LightApp_Module::updateCommandsStatus();
236
237   updateUndoRedoControls();
238
239   // to do
240   //action( ... )->setEnabled( ... );
241 }
242
243 HYDROGUI_DataModel* HYDROGUI_Module::getDataModel() const
244 {
245   return (HYDROGUI_DataModel*)dataModel();
246 }
247
248 HYDROGUI_Displayer* HYDROGUI_Module::getDisplayer() const
249 {
250   return myDisplayer;
251 }
252
253 GraphicsView_Viewer* HYDROGUI_Module::getViewer( const int theId ) const
254 {
255   if( myViewManagerMap.contains( theId ) )
256   {
257     ViewManagerInfo anInfo = myViewManagerMap[ theId ];
258     GraphicsView_ViewManager* aViewManager =
259       dynamic_cast<GraphicsView_ViewManager*>( anInfo.first );
260     if( aViewManager )
261       return aViewManager->getViewer();
262   }
263   return NULL;
264 }
265
266 int HYDROGUI_Module::getViewManagerId( SUIT_ViewManager* theViewManager )
267 {
268   ViewManagerMapIterator anIter( myViewManagerMap );
269   while( anIter.hasNext() )
270   {
271     int anId = anIter.next().key();
272     const ViewManagerInfo& anInfo = anIter.value();
273     if( anInfo.first == theViewManager )
274       return anId;
275   }
276   return -1;
277 }
278
279 HYDROGUI_Module::ViewManagerRole HYDROGUI_Module::getViewManagerRole( SUIT_ViewManager* theViewManager )
280 {
281   int anId = getViewManagerId( theViewManager );
282   if( anId != -1 )
283   {
284     const ViewManagerInfo& anInfo = myViewManagerMap[ anId ];
285     return anInfo.second;
286   }
287   return VMR_Unknown;
288 }
289
290 void HYDROGUI_Module::setViewManagerRole( SUIT_ViewManager* theViewManager,
291                                           const ViewManagerRole theRole )
292 {
293   int anId = getViewManagerId( theViewManager );
294   if( anId != -1 )
295   {
296     ViewManagerInfo& anInfo = myViewManagerMap[ anId ];
297     anInfo.second = theRole;
298   }
299 }
300
301 CAM_DataModel* HYDROGUI_Module::createDataModel()
302 {
303   return new HYDROGUI_DataModel( this );
304 }
305
306 void HYDROGUI_Module::customEvent( QEvent* e )
307 {
308   int aType = e->type();
309   if ( aType == NewViewEvent )
310   {
311     SALOME_CustomEvent* ce = ( SALOME_CustomEvent* )e;
312     if( GraphicsView_ViewFrame* aViewFrame = ( GraphicsView_ViewFrame* )ce->data() )
313     {
314       if( GraphicsView_Viewer* aViewer = dynamic_cast<GraphicsView_Viewer*>( aViewFrame->getViewer() ) )
315       {
316         SUIT_ViewManager* aViewManager = aViewer->getViewManager();
317         ViewManagerRole aRole = getViewManagerRole( aViewManager );
318         if( aRole == VMR_General )
319           update( UF_Viewer );
320
321         aViewer->activateTransform( GraphicsView_Viewer::FitAll );
322
323         if( GraphicsView_ViewPort* aViewPort = aViewer->getActiveViewPort() )
324         {
325           aViewPort->setInteractionFlag( GraphicsView_ViewPort::TraceBoundingRect );
326           aViewPort->setInteractionFlag( GraphicsView_ViewPort::ImmediateContextMenu );
327           aViewPort->setInteractionFlag( GraphicsView_ViewPort::ImmediateSelection );
328
329           // ouv: tmp
330           //aViewPort->setViewLabelPosition( GraphicsView_ViewPort::VLP_BottomLeft, true );
331         }
332       }
333     }
334   }
335 }
336
337 bool HYDROGUI_Module::eventFilter( QObject* theObj, QEvent* theEvent )
338 {
339   QEvent::Type aType = theEvent->type();
340   if( theObj->inherits( "GraphicsView_ViewFrame" ) )
341   {
342     if( aType == QEvent::Show )
343     {
344       SALOME_CustomEvent* e = new SALOME_CustomEvent( NewViewEvent );
345       e->setData( theObj );
346       QApplication::postEvent( this, e );
347       theObj->removeEventFilter( this );
348     }
349   }
350   return LightApp_Module::eventFilter( theObj, theEvent );
351 }
352
353 void HYDROGUI_Module::onViewManagerAdded( SUIT_ViewManager* theViewManager )
354 {
355   LightApp_Module::onViewManagerAdded( theViewManager );
356
357   if( theViewManager->getType() == GraphicsView_Viewer::Type() )
358   { 
359     createSelector( theViewManager ); // replace the default selector
360
361     connect( theViewManager, SIGNAL( viewCreated( SUIT_ViewWindow* ) ),
362              this, SLOT( onViewCreated( SUIT_ViewWindow* ) ) );
363   }
364
365   ViewManagerInfo anInfo( theViewManager, VMR_General );
366   myViewManagerMap.insert( ViewManagerId++, anInfo );
367 }
368
369 void HYDROGUI_Module::onViewManagerRemoved( SUIT_ViewManager* theViewManager )
370 {
371   LightApp_Module::onViewManagerRemoved( theViewManager );
372
373   createSelector( theViewManager ); // replace the default selector
374
375   int anId = getViewManagerId( theViewManager );
376   if( anId != -1 )
377     myViewManagerMap.remove( anId );
378 }
379
380 void HYDROGUI_Module::onViewCreated( SUIT_ViewWindow* theViewWindow )
381 {
382   if( theViewWindow && theViewWindow->inherits( "GraphicsView_ViewFrame" ) )
383   {
384     if( GraphicsView_ViewFrame* aViewFrame = dynamic_cast<GraphicsView_ViewFrame*>( theViewWindow ) )
385     {
386       aViewFrame->installEventFilter( this );
387
388       GraphicsView_ViewPort* aViewPort = aViewFrame->getViewPort();
389
390       connect( aViewPort, SIGNAL( vpMouseEvent( QGraphicsSceneMouseEvent* ) ),
391                this, SLOT( onViewPortMouseEvent( QGraphicsSceneMouseEvent* ) ) );
392       return;
393     }
394   }
395 }
396
397 void HYDROGUI_Module::onViewPortMouseEvent( QGraphicsSceneMouseEvent* theEvent )
398 {
399   if( GraphicsView_ViewPort* aViewPort = qobject_cast<GraphicsView_ViewPort*>( sender() ) )
400   {
401     SUIT_ViewManager* aViewManager = 0;
402
403     QObject* aParent = aViewPort;
404     while( aParent = aParent->parent() )
405     {
406       if( GraphicsView_ViewFrame* aViewFrame = dynamic_cast<GraphicsView_ViewFrame*>( aParent ) )
407       {
408         if( GraphicsView_Viewer* aViewer = aViewFrame->getViewer() )
409         {
410           aViewManager = aViewer->getViewManager();
411           break;
412         }
413       }
414     }
415
416     if( !aViewManager )
417       return;
418
419     double aMouseX = theEvent->scenePos().x();
420     double aMouseY = theEvent->scenePos().y();
421
422     ViewManagerRole aRole = getViewManagerRole( aViewManager );
423     if( aRole == VMR_General )
424     {
425       int aXDeg = 0, aYDeg = 0;
426       int aXMin = 0, aYMin = 0;
427       double aXSec = 0, aYSec = 0;
428       HYDROGUI_Tool::DoubleToLambert( aMouseX, aXDeg, aXMin, aXSec );
429       HYDROGUI_Tool::DoubleToLambert( aMouseY, aYDeg, aYMin, aYSec );
430
431       QString aDegSymbol( QChar( 0x00B0 ) );
432       QString aXStr = QString( "%1%2 %3' %4\"" ).arg( aXDeg ).arg( aDegSymbol ).arg( aXMin ).arg( aXSec );
433       QString aYStr = QString( "%1%2 %3' %4\"" ).arg( aYDeg ).arg( aDegSymbol ).arg( aYMin ).arg( aYSec );
434
435       aViewPort->setViewLabelText( QString( "X: %1\nY: %2" ).arg( aXStr ).arg( aYStr ) );
436     }
437     else if( aRole == VMR_Mapping )
438       aViewPort->setViewLabelText( QString( "X: %1\nY: %2" ).arg( (int)aMouseX ).arg( (int)aMouseY ) );
439   }
440 }
441
442 void HYDROGUI_Module::updateGV( const bool theIsInit,
443                                 const bool theIsForced )
444 {
445   if( !getDisplayer() )
446     return;
447
448   QList<int> aViewManagerIdList;
449
450   // currently, all views are updated
451   ViewManagerMapIterator anIter( myViewManagerMap );
452   while( anIter.hasNext() )
453   {
454     int anId = anIter.next().key();
455     aViewManagerIdList.append( anId );
456   }
457
458   QListIterator<int> anIdIter( aViewManagerIdList );
459   while( anIdIter.hasNext() )
460     getDisplayer()->UpdateAll( anIdIter.next(), theIsInit, theIsForced );
461 }
462
463 void HYDROGUI_Module::createSelector( SUIT_ViewManager* theViewManager )
464 {
465   if( !theViewManager )
466     return;
467
468   LightApp_SelectionMgr* aSelectionMgr = getApp()->selectionMgr();
469   if( !aSelectionMgr )
470     return;
471
472   QString aViewType = theViewManager->getType();
473   if( aViewType != GraphicsView_Viewer::Type() )
474     return;
475
476   GraphicsView_ViewManager* aViewManager =
477     dynamic_cast<GraphicsView_ViewManager*>( theViewManager );
478   if( !aViewManager )
479     return;
480
481   QList<SUIT_Selector*> aSelectorList;
482   aSelectionMgr->selectors( aViewType, aSelectorList );
483
484   // disable all alien selectors
485   QList<SUIT_Selector*>::iterator anIter, anIterEnd = aSelectorList.end();
486   for( anIter = aSelectorList.begin(); anIter != anIterEnd; anIter++ )
487   {
488     SUIT_Selector* aSelector = *anIter;
489     if( aSelector && !dynamic_cast<HYDROGUI_GVSelector*>( aSelector ) )
490       aSelector->setEnabled( false );
491   }
492
493   new HYDROGUI_GVSelector( this, aViewManager->getViewer(), aSelectionMgr );
494 }
495
496 bool HYDROGUI_Module::setUpdateEnabled( const bool theState )
497 {
498   bool aPrevState = myIsUpdateEnabled;
499   myIsUpdateEnabled = theState;
500   return aPrevState;
501 }
502
503 bool HYDROGUI_Module::isUpdateEnabled() const
504 {
505   return myIsUpdateEnabled;
506 }