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