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