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