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