Salome HOME
b00b69648a4d2c965abbbcded9c0c4eaacd2abba
[modules/shaper.git] / src / SHAPERGUI / SHAPERGUI_SalomeViewer.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 #include "SHAPERGUI_SalomeViewer.h"
4 #include "SHAPERGUI_OCCSelector.h"
5
6 #include <OCCViewer_ViewPort3d.h>
7 #include <OCCViewer_ViewFrame.h>
8
9 #include <SUIT_ViewManager.h>
10
11 #include <QtxActionToolMgr.h>
12
13 #include <SelectMgr_ListIteratorOfListOfFilter.hxx>
14
15 #include <QMouseEvent>
16 #include <QContextMenuEvent>
17
18 SHAPERGUI_SalomeView::SHAPERGUI_SalomeView(OCCViewer_Viewer* theViewer)
19 : ModuleBase_IViewWindow(), myCurrentView(0)
20 {
21   myViewer = theViewer;
22 }
23
24
25 Handle(V3d_View) SHAPERGUI_SalomeView::v3dView() const
26 {
27   Handle(V3d_View) aView;
28   if (myCurrentView) {
29     OCCViewer_ViewWindow* aWnd = static_cast<OCCViewer_ViewWindow*>(myCurrentView);
30     aView = aWnd->getViewPort()->getView();
31   }
32   return aView;
33 }
34
35 QWidget* SHAPERGUI_SalomeView::viewPort() const
36 {
37   QWidget* aViewPort = 0;
38   if (myCurrentView) {
39     OCCViewer_ViewWindow* aWnd = static_cast<OCCViewer_ViewWindow*>(myCurrentView);
40     aViewPort = aWnd->getViewPort();
41   }
42   return aViewPort;
43 }
44
45 //**********************************************
46 //**********************************************
47 //**********************************************
48
49
50
51 SHAPERGUI_SalomeViewer::SHAPERGUI_SalomeViewer(QObject* theParent)
52     : ModuleBase_IViewer(theParent),
53       mySelector(0), myView(0), myIsSelectionChanged(false)
54 {
55 }
56
57 SHAPERGUI_SalomeViewer::~SHAPERGUI_SalomeViewer()
58 {
59   if (myView)
60     delete myView;
61 }
62
63
64 //**********************************************
65 Handle(AIS_InteractiveContext) SHAPERGUI_SalomeViewer::AISContext() const
66 {
67   if (mySelector && mySelector->viewer())
68     return mySelector->viewer()->getAISContext();
69   Handle(AIS_InteractiveContext) aNull;
70   return aNull;
71 }
72
73 //**********************************************
74 Handle(V3d_Viewer) SHAPERGUI_SalomeViewer::v3dViewer() const
75 {
76   if (mySelector)
77     return mySelector->viewer()->getViewer3d();
78   return Handle(V3d_Viewer)();
79 }
80
81 //**********************************************
82 Handle(AIS_Trihedron) SHAPERGUI_SalomeViewer::trihedron() const
83 {
84   return mySelector->viewer()->getTrihedron();
85 }
86
87 //**********************************************
88 Handle(V3d_View) SHAPERGUI_SalomeViewer::activeView() const
89 {
90   if (mySelector) {
91     OCCViewer_Viewer* aViewer = mySelector->viewer();
92     SUIT_ViewManager* aMgr = aViewer->getViewManager();
93     OCCViewer_ViewWindow* aWnd = static_cast<OCCViewer_ViewWindow*>(aMgr->getActiveView());
94     return aWnd->getViewPort()->getView();
95   }
96   return Handle(V3d_View)();
97 }
98
99 //**********************************************
100 QWidget* SHAPERGUI_SalomeViewer::activeViewPort() const
101 {
102   QWidget* aViewPort;
103   if (mySelector) {
104     OCCViewer_Viewer* aViewer = mySelector->viewer();
105     SUIT_ViewManager* aMgr = aViewer->getViewManager();
106     OCCViewer_ViewWindow* aWnd = static_cast<OCCViewer_ViewWindow*>(aMgr->getActiveView());
107     aViewPort = aWnd->getViewPort();
108   }
109   return aViewPort;
110 }
111
112 //**********************************************
113 void SHAPERGUI_SalomeViewer::setSelector(SHAPERGUI_OCCSelector* theSel)
114 {
115   if (mySelector) {
116     if (mySelector == theSel)
117       return;
118     else {
119       mySelector->viewer()->getViewManager()->disconnect(this);
120       OCCViewer_Viewer* aViewer = mySelector->viewer();
121       if (aViewer)
122         aViewer->disconnect(this);
123     }
124   }
125   mySelector = theSel;
126   if (!mySelector)
127     return;
128   OCCViewer_Viewer* aViewer = mySelector->viewer();
129   SUIT_ViewManager* aMgr = aViewer->getViewManager();
130
131   myView = new SHAPERGUI_SalomeView(mySelector->viewer());
132
133   // TODO: Provide ModuleBase_IViewWindow interface
134   connect(aMgr, SIGNAL(lastViewClosed(SUIT_ViewManager*)), this, SIGNAL(lastViewClosed()));
135
136   connect(aMgr, SIGNAL(tryCloseView(SUIT_ViewWindow*)), 
137           this, SLOT(onTryCloseView(SUIT_ViewWindow*)));
138   connect(aMgr, SIGNAL(deleteView(SUIT_ViewWindow*)), 
139           this, SLOT(onDeleteView(SUIT_ViewWindow*)));
140   connect(aMgr, SIGNAL(viewCreated(SUIT_ViewWindow*)), 
141           this, SLOT(onViewCreated(SUIT_ViewWindow*)));
142   connect(aMgr, SIGNAL(activated(SUIT_ViewManager*)), 
143           this, SLOT(onActivated(SUIT_ViewManager*)));
144
145   connect(aMgr, SIGNAL(mousePress(SUIT_ViewWindow*, QMouseEvent*)), this,
146           SLOT(onMousePress(SUIT_ViewWindow*, QMouseEvent*)));
147   connect(aMgr, SIGNAL(mouseRelease(SUIT_ViewWindow*, QMouseEvent*)), this,
148           SLOT(onMouseRelease(SUIT_ViewWindow*, QMouseEvent*)));
149   connect(aMgr, SIGNAL(mouseDoubleClick(SUIT_ViewWindow*, QMouseEvent*)), this,
150           SLOT(onMouseDoubleClick(SUIT_ViewWindow*, QMouseEvent*)));
151   connect(aMgr, SIGNAL(mouseMove(SUIT_ViewWindow*, QMouseEvent*)), this,
152           SLOT(onMouseMove(SUIT_ViewWindow*, QMouseEvent*)));
153
154   connect(aMgr, SIGNAL(keyPress(SUIT_ViewWindow*, QKeyEvent*)), this,
155           SLOT(onKeyPress(SUIT_ViewWindow*, QKeyEvent*)));
156   connect(aMgr, SIGNAL(keyRelease(SUIT_ViewWindow*, QKeyEvent*)), this,
157           SLOT(onKeyRelease(SUIT_ViewWindow*, QKeyEvent*)));
158
159   connect(aViewer, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
160 }
161
162 //**********************************************
163 void SHAPERGUI_SalomeViewer::onSelectionChanged()
164 {
165   // Selection event must be sent only after mouse release
166   myIsSelectionChanged = true;
167 }
168
169 //**********************************************
170 void SHAPERGUI_SalomeViewer::onMousePress(SUIT_ViewWindow* theView, QMouseEvent* theEvent)
171 {
172   myView->setCurrentView(theView);
173   emit mousePress(myView, theEvent);
174 }
175
176 //**********************************************
177 void SHAPERGUI_SalomeViewer::onMouseRelease(SUIT_ViewWindow* theView, QMouseEvent* theEvent)
178 {
179   myView->setCurrentView(theView);
180   emit mouseRelease(myView, theEvent);
181   if (myIsSelectionChanged) {
182     emit selectionChanged();
183     myIsSelectionChanged = false;
184   }
185 }
186
187 //**********************************************
188 void SHAPERGUI_SalomeViewer::onMouseDoubleClick(SUIT_ViewWindow* theView, QMouseEvent* theEvent)
189 {
190   myView->setCurrentView(theView);
191   emit mouseDoubleClick(myView, theEvent);
192 }
193
194 //**********************************************
195 void SHAPERGUI_SalomeViewer::onMouseMove(SUIT_ViewWindow* theView, QMouseEvent* theEvent)
196 {
197   myView->setCurrentView(theView);
198   emit mouseMove(myView, theEvent);
199 }
200
201 //**********************************************
202 bool SHAPERGUI_SalomeViewer::canDragByMouse() const
203 {
204   OCCViewer_Viewer* aViewer = mySelector->viewer();
205   SUIT_ViewWindow* aWnd = aViewer->getViewManager()->getActiveView();
206   OCCViewer_ViewWindow* aViewWnd = dynamic_cast<OCCViewer_ViewWindow*>(aWnd);
207   if (aViewWnd) {
208     return (aViewWnd->interactionStyle() == 0);
209   }
210   return true;
211 }
212
213
214 //**********************************************
215 void SHAPERGUI_SalomeViewer::onKeyPress(SUIT_ViewWindow* theView, QKeyEvent* theEvent)
216 {
217   emit keyPress(myView, theEvent);
218 }
219
220 //**********************************************
221 void SHAPERGUI_SalomeViewer::onKeyRelease(SUIT_ViewWindow* theView, QKeyEvent* theEvent)
222 {
223   emit keyRelease(myView, theEvent);
224 }
225
226 //**********************************************
227 void SHAPERGUI_SalomeViewer::onTryCloseView(SUIT_ViewWindow*)
228 {
229   emit tryCloseView(myView);
230 }
231
232 //**********************************************
233 void SHAPERGUI_SalomeViewer::onDeleteView(SUIT_ViewWindow*)
234 {
235   if(myWindowScale.contains(myView->v3dView()))
236     myWindowScale.remove(myView->v3dView());
237   emit deleteView(myView);
238 }
239
240 //**********************************************
241 void SHAPERGUI_SalomeViewer::onViewCreated(SUIT_ViewWindow* theView)
242 {
243   myView->setCurrentView(theView);
244
245   OCCViewer_ViewFrame* aView = dynamic_cast<OCCViewer_ViewFrame*>(theView);
246
247   OCCViewer_ViewWindow* aWnd = aView->getView(OCCViewer_ViewFrame::MAIN_VIEW);
248   if (aWnd) {
249     connect(aWnd, SIGNAL(vpTransformationFinished(OCCViewer_ViewWindow::OperationType)),
250       this, SLOT(onViewTransformed(OCCViewer_ViewWindow::OperationType)));
251     OCCViewer_ViewPort3d* aViewPort = aWnd->getViewPort();
252     if (aViewPort)
253       connect(aViewPort, SIGNAL(vpMapped(OCCViewer_ViewPort3d*)), this, SLOT(onViewPortMapped()));
254   }
255   reconnectActions(aWnd, true);
256
257   myWindowScale.insert(aView->getViewPort()->getView(), 
258                        aView->getViewPort()->getView()->Camera()->Scale());
259
260   emit viewCreated(myView);
261
262
263 }
264
265 //**********************************************
266 void SHAPERGUI_SalomeViewer::onActivated(SUIT_ViewManager* theMgr)
267 {
268   myView->setCurrentView(theMgr->getActiveView());
269   emit activated(myView);
270 }
271
272 //**********************************************
273 void SHAPERGUI_SalomeViewer::enableSelection(bool isEnabled)
274 {
275   if (mySelector)
276     if (mySelector->viewer()->isSelectionEnabled() != isEnabled)
277       mySelector->viewer()->enableSelection(isEnabled);
278   // The enableSelection() in SALOME 7.5 cause of forced Viewer update(we have blinking)
279   // After this is corrected, the first row should be recommented, the last - removed
280     //mySelector->viewer()->setInteractionStyle(isEnabled ? SUIT_ViewModel::STANDARD
281     //                                                    : SUIT_ViewModel::KEY_FREE);
282 }
283
284 //**********************************************
285 bool SHAPERGUI_SalomeViewer::isSelectionEnabled() const
286 {
287   if (mySelector)
288     return mySelector->viewer()->isSelectionEnabled();
289   return false;
290 }
291
292 //**********************************************
293 void SHAPERGUI_SalomeViewer::enableMultiselection(bool isEnable)
294 {
295   if (mySelector)
296     mySelector->viewer()->enableMultiselection(isEnable);
297 }
298
299 //**********************************************
300 bool SHAPERGUI_SalomeViewer::isMultiSelectionEnabled() const
301 {
302   if (mySelector)
303     return mySelector->viewer()->isMultiSelectionEnabled();
304   return false;
305 }
306
307 //**********************************************
308 bool SHAPERGUI_SalomeViewer::enableDrawMode(bool isEnabled)
309 {
310   // TODO: Has to be replaced when SALOME patch become available
311   if (mySelector)
312     return mySelector->viewer()->enableDrawMode(isEnabled);
313   return false;
314 }
315
316 //**********************************************
317 void SHAPERGUI_SalomeViewer::reconnectActions(SUIT_ViewWindow* theWindow,
318                                             const bool theUseSHAPERSlot)
319 {
320   OCCViewer_ViewWindow* aWindow = dynamic_cast<OCCViewer_ViewWindow*>(theWindow);
321   if (!aWindow)
322     return;
323
324   QAction* anAction = theWindow->toolMgr()->action(OCCViewer_ViewWindow::TrihedronShowId);
325   if (!anAction)
326     return;
327
328   if (theUseSHAPERSlot) {
329     anAction->disconnect(anAction, SIGNAL(toggled(bool)),
330                          theWindow, SLOT(onTrihedronShow(bool)));
331     anAction->connect(anAction, SIGNAL(toggled(bool)),
332                       this, SIGNAL(trihedronVisibilityChanged(bool)));
333   }
334   else {
335     anAction->connect(anAction, SIGNAL(toggled(bool)),
336                       theWindow, SLOT(onTrihedronShow(bool)));
337     anAction->disconnect(anAction, SIGNAL(toggled(bool)),
338                          this, SIGNAL(trihedronVisibilityChanged(bool)));
339   }
340 }
341
342 //**********************************************
343 void SHAPERGUI_SalomeViewer::fitAll()
344 {
345   if (mySelector) {
346     SUIT_ViewManager* aMgr = mySelector->viewer()->getViewManager();
347     OCCViewer_ViewFrame* aVFrame = dynamic_cast<OCCViewer_ViewFrame*>(aMgr->getActiveView());
348     if (aVFrame) {
349       aVFrame->onFitAll();
350     }
351   }
352 }
353
354 //**********************************************
355 void SHAPERGUI_SalomeViewer::setViewProjection(double theX, double theY, 
356                                                double theZ, double theTwist)
357 {
358   if (!mySelector) 
359     return;
360
361   SUIT_ViewManager* aMgr = mySelector->viewer()->getViewManager();
362   OCCViewer_ViewFrame* aVFrame = dynamic_cast<OCCViewer_ViewFrame*>(aMgr->getActiveView());
363   if (aVFrame) {
364     Handle(V3d_View) aView3d = aVFrame->getViewPort()->getView();
365     if (!aView3d.IsNull()) {
366       aView3d->SetProj(theX, theY, theZ);
367       aView3d->SetTwist( theTwist );
368       aView3d->FitAll(0.01, false);
369       aView3d->SetZSize(0.);
370       if (aView3d->Depth() < 0.1)
371         aView3d->DepthFitAll();
372     }
373   }
374 }
375
376 //***************************************
377 void SHAPERGUI_SalomeViewer::addSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
378 {
379   Handle(AIS_InteractiveContext) aContext = AISContext();
380   if (!aContext.IsNull()) {
381     aContext->AddFilter(theFilter);
382   }
383 }
384
385 //***************************************
386 void SHAPERGUI_SalomeViewer::removeSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
387 {
388   Handle(AIS_InteractiveContext) aContext = AISContext();
389   if (!aContext.IsNull()) {
390     aContext->RemoveFilter(theFilter);
391   }
392 }
393
394 //***************************************
395 bool SHAPERGUI_SalomeViewer::hasSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
396 {
397   bool aFoundFilter = false;
398   Handle(AIS_InteractiveContext) aContext = AISContext();
399   if (!aContext.IsNull()) {
400     const SelectMgr_ListOfFilter& aFilters = aContext->Filters();
401     SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
402     for (; aIt.More() && !aFoundFilter; aIt.Next()) {
403       aFoundFilter = theFilter.Access() == aIt.Value().Access();
404     }
405   }
406   return aFoundFilter;
407 }
408
409 //***************************************
410 void SHAPERGUI_SalomeViewer::clearSelectionFilters()
411 {
412   Handle(AIS_InteractiveContext) aContext = AISContext();
413   if (!aContext.IsNull()) {
414     aContext->RemoveFilters();
415   }
416 }
417
418 //***************************************
419 void SHAPERGUI_SalomeViewer::update()
420 {
421   Handle(AIS_InteractiveContext) aContext = AISContext();
422   if (!aContext.IsNull()) {
423     aContext->UpdateCurrentViewer();
424   }
425 }
426
427 //***************************************
428 void SHAPERGUI_SalomeViewer::onViewTransformed(OCCViewer_ViewWindow::OperationType theType)
429 {
430   emit viewTransformed((int) theType);
431 }
432
433 //***************************************
434 void SHAPERGUI_SalomeViewer::onViewPortMapped()
435 {
436   emit trihedronVisibilityChanged(true);
437 }
438
439 //***************************************
440 void SHAPERGUI_SalomeViewer::activateViewer(bool toActivate)
441 {
442   if (!mySelector || !mySelector->viewer())
443     return;
444   SUIT_ViewManager* aMgr = mySelector->viewer()->getViewManager();
445   QVector<SUIT_ViewWindow*> aViews = aMgr->getViews();
446   if (toActivate) {
447     foreach (SUIT_ViewWindow* aView, aViews) {
448       OCCViewer_ViewFrame* aOCCView = dynamic_cast<OCCViewer_ViewFrame*>(aView);
449       OCCViewer_ViewWindow* aWnd = aOCCView->getView(OCCViewer_ViewFrame::MAIN_VIEW);
450       connect(aWnd, SIGNAL(vpTransformationFinished(OCCViewer_ViewWindow::OperationType)),
451         this, SLOT(onViewTransformed(OCCViewer_ViewWindow::OperationType)));
452       reconnectActions(aWnd, true);
453     }
454   } else {
455     foreach (SUIT_ViewWindow* aView, aViews) {
456       OCCViewer_ViewFrame* aOCCView = dynamic_cast<OCCViewer_ViewFrame*>(aView);
457       OCCViewer_ViewWindow* aWnd = aOCCView->getView(OCCViewer_ViewFrame::MAIN_VIEW);
458       disconnect((OCCViewer_ViewWindow*)aWnd, 
459                  SIGNAL(vpTransformationFinished(OCCViewer_ViewWindow::OperationType)),
460         this, SLOT(onViewTransformed(OCCViewer_ViewWindow::OperationType)));
461       reconnectActions(aWnd, false);
462     }
463   }
464 }
465
466 void SHAPERGUI_SalomeViewer::Zfitall()
467 {
468   if (!mySelector || !mySelector->viewer())
469     return;
470   SUIT_ViewManager* aMgr = mySelector->viewer()->getViewManager();
471   OCCViewer_ViewFrame* aView = dynamic_cast<OCCViewer_ViewFrame*>(aMgr->getActiveView());
472   if (aView) {
473     OCCViewer_ViewWindow* aWnd = aView->getView(OCCViewer_ViewFrame::MAIN_VIEW);
474     Handle(V3d_View) aView3d = aWnd->getViewPort()->getView();
475     aView3d->ZFitAll();
476     if (aView3d->Depth() < 0.1)
477       aView3d->DepthFitAll();
478   }
479 }