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