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