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