]> SALOME platform Git repositories - modules/shaper.git/blob - src/SHAPERGUI/SHAPERGUI_SalomeViewer.cpp
Salome HOME
58cb99b4ccb86062964ea8fd29032f541d3de0e7
[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   if (aContext.IsNull())
380     return;
381   AIS_ListOfInteractive aList;
382   aContext->DisplayedObjects(aList);
383   AIS_ListIteratorOfListOfInteractive aLIt;
384   Handle(AIS_InteractiveObject) anAISIO;
385   for (aLIt.Initialize(aList); aLIt.More(); aLIt.Next()) {
386     anAISIO = aLIt.Value();
387     Handle(Standard_Type) aType = anAISIO->DynamicType();
388     if (anAISIO->IsKind(STANDARD_TYPE(SALOME_AISShape))) {
389       aContext->Erase(anAISIO, false);
390     }
391   }
392 }
393
394 //**********************************************
395 void SHAPERGUI_SalomeViewer::setViewProjection(double theX, double theY,
396                                                double theZ, double theTwist)
397 {
398   if (!mySelector)
399     return;
400
401   SUIT_ViewManager* aMgr = mySelector->viewer()->getViewManager();
402   OCCViewer_ViewFrame* aVFrame = dynamic_cast<OCCViewer_ViewFrame*>(aMgr->getActiveView());
403   if (aVFrame) {
404     Handle(V3d_View) aView3d = aVFrame->getViewPort()->getView();
405     if (!aView3d.IsNull()) {
406       aView3d->SetProj(theX, theY, theZ);
407       aView3d->SetTwist( theTwist );
408       aView3d->FitAll(0.01, false);
409       aView3d->SetZSize(0.);
410       if (aView3d->Depth() < 0.1)
411         aView3d->DepthFitAll();
412     }
413   }
414 }
415
416 //***************************************
417 void SHAPERGUI_SalomeViewer::addSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
418 {
419   Handle(AIS_InteractiveContext) aContext = AISContext();
420   if (!aContext.IsNull()) {
421     aContext->AddFilter(theFilter);
422   }
423 }
424
425 //***************************************
426 void SHAPERGUI_SalomeViewer::removeSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
427 {
428   Handle(AIS_InteractiveContext) aContext = AISContext();
429   if (!aContext.IsNull()) {
430     aContext->RemoveFilter(theFilter);
431   }
432 }
433
434 //***************************************
435 bool SHAPERGUI_SalomeViewer::hasSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
436 {
437   bool aFoundFilter = false;
438   Handle(AIS_InteractiveContext) aContext = AISContext();
439   if (!aContext.IsNull()) {
440     const SelectMgr_ListOfFilter& aFilters = aContext->Filters();
441     SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
442     for (; aIt.More() && !aFoundFilter; aIt.Next()) {
443       aFoundFilter = theFilter.get() == aIt.Value().get();
444     }
445   }
446   return aFoundFilter;
447 }
448
449 //***************************************
450 void SHAPERGUI_SalomeViewer::clearSelectionFilters()
451 {
452   Handle(AIS_InteractiveContext) aContext = AISContext();
453   if (!aContext.IsNull()) {
454     aContext->RemoveFilters();
455   }
456 }
457
458 //***************************************
459 void SHAPERGUI_SalomeViewer::update()
460 {
461   Handle(AIS_InteractiveContext) aContext = AISContext();
462   if (!aContext.IsNull()) {
463     aContext->UpdateCurrentViewer();
464   }
465 }
466
467 //***************************************
468 void SHAPERGUI_SalomeViewer::onViewTransformed(OCCViewer_ViewWindow::OperationType theType)
469 {
470   emit viewTransformed((int) theType);
471 }
472
473 //***************************************
474 void SHAPERGUI_SalomeViewer::onViewPortMapped()
475 {
476   emit trihedronVisibilityChanged(true);
477 }
478
479 //***************************************
480 void SHAPERGUI_SalomeViewer::activateViewer(bool toActivate)
481 {
482   if (!mySelector || !mySelector->viewer())
483     return;
484   SUIT_ViewManager* aMgr = mySelector->viewer()->getViewManager();
485 #ifdef SALOME_PATCH_FOR_CTRL_WHEEL
486   OCCViewer_Viewer* aViewer = dynamic_cast<OCCViewer_Viewer*>(aMgr->getViewModel());
487   if (aViewer)
488     aViewer->setUseLocalSelection(toActivate);
489 #endif
490   QVector<SUIT_ViewWindow*> aViews = aMgr->getViews();
491   if (toActivate) {
492     foreach (SUIT_ViewWindow* aView, aViews) {
493       OCCViewer_ViewFrame* aOCCView = dynamic_cast<OCCViewer_ViewFrame*>(aView);
494       OCCViewer_ViewWindow* aWnd = aOCCView->getView(OCCViewer_ViewFrame::MAIN_VIEW);
495       connect(aWnd, SIGNAL(vpTransformationFinished(OCCViewer_ViewWindow::OperationType)),
496         this, SLOT(onViewTransformed(OCCViewer_ViewWindow::OperationType)));
497       reconnectActions(aWnd, true);
498     }
499   } else {
500     foreach (SUIT_ViewWindow* aView, aViews) {
501       OCCViewer_ViewFrame* aOCCView = dynamic_cast<OCCViewer_ViewFrame*>(aView);
502       OCCViewer_ViewWindow* aWnd = aOCCView->getView(OCCViewer_ViewFrame::MAIN_VIEW);
503       disconnect((OCCViewer_ViewWindow*)aWnd,
504                  SIGNAL(vpTransformationFinished(OCCViewer_ViewWindow::OperationType)),
505         this, SLOT(onViewTransformed(OCCViewer_ViewWindow::OperationType)));
506       reconnectActions(aWnd, false);
507     }
508   }
509 }
510
511 //void SHAPERGUI_SalomeViewer::Zfitall()
512 //{
513 //  if (!mySelector || !mySelector->viewer())
514 //    return;
515 //  SUIT_ViewManager* aMgr = mySelector->viewer()->getViewManager();
516 //  /// WORKAROUND for issue #1798. SUIT_ViewManager::closeAllViews() should nullify myActiveView
517 //  /// As a result, we need to check views count in manager
518 //  if (aMgr->getViews().size() > 0) {
519 //    OCCViewer_ViewFrame* aView = dynamic_cast<OCCViewer_ViewFrame*>(aMgr->getActiveView());
520 //    if (aView) {
521 //      OCCViewer_ViewWindow* aWnd = aView->getView(OCCViewer_ViewFrame::MAIN_VIEW);
522 //      Handle(V3d_View) aView3d = aWnd->getViewPort()->getView();
523 //      aView3d->ZFitAll();
524 //      if (aView3d->Depth() < 0.1)
525 //        aView3d->DepthFitAll();
526 //    }
527 //  }
528 //}