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