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