1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 #include "SHAPERGUI_SalomeViewer.h"
4 #include "SHAPERGUI_OCCSelector.h"
6 #include <OCCViewer_ViewPort3d.h>
7 #include <OCCViewer_ViewFrame.h>
8 #include <SOCC_ViewModel.h>
9 #include <SUIT_ViewManager.h>
11 #include <QtxActionToolMgr.h>
13 #include <SelectMgr_ListIteratorOfListOfFilter.hxx>
15 #include <QMouseEvent>
16 #include <QContextMenuEvent>
18 //#define SALOME_PATCH_FOR_CTRL_WHEEL
20 SHAPERGUI_SalomeView::SHAPERGUI_SalomeView(OCCViewer_Viewer* theViewer)
21 : ModuleBase_IViewWindow(), myCurrentView(0)
27 Handle(V3d_View) SHAPERGUI_SalomeView::v3dView() const
29 Handle(V3d_View) aView;
31 OCCViewer_ViewWindow* aWnd = static_cast<OCCViewer_ViewWindow*>(myCurrentView);
32 aView = aWnd->getViewPort()->getView();
37 QWidget* SHAPERGUI_SalomeView::viewPort() const
39 QWidget* aViewPort = 0;
41 OCCViewer_ViewWindow* aWnd = static_cast<OCCViewer_ViewWindow*>(myCurrentView);
42 aViewPort = aWnd->getViewPort();
47 //**********************************************
48 //**********************************************
49 //**********************************************
53 SHAPERGUI_SalomeViewer::SHAPERGUI_SalomeViewer(QObject* theParent)
54 : ModuleBase_IViewer(theParent),
55 mySelector(0), myView(0), myIsSelectionChanged(false)
59 SHAPERGUI_SalomeViewer::~SHAPERGUI_SalomeViewer()
66 //**********************************************
67 Handle(AIS_InteractiveContext) SHAPERGUI_SalomeViewer::AISContext() const
69 if (mySelector && mySelector->viewer())
70 return mySelector->viewer()->getAISContext();
71 Handle(AIS_InteractiveContext) aNull;
75 //**********************************************
76 Handle(V3d_Viewer) SHAPERGUI_SalomeViewer::v3dViewer() const
79 return mySelector->viewer()->getViewer3d();
80 return Handle(V3d_Viewer)();
83 //**********************************************
84 Handle(AIS_Trihedron) SHAPERGUI_SalomeViewer::trihedron() const
86 return mySelector->viewer()->getTrihedron();
89 //**********************************************
90 Handle(V3d_View) SHAPERGUI_SalomeViewer::activeView() const
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();
98 return Handle(V3d_View)();
101 //**********************************************
102 QWidget* SHAPERGUI_SalomeViewer::activeViewPort() const
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();
114 //**********************************************
115 void SHAPERGUI_SalomeViewer::setSelector(SHAPERGUI_OCCSelector* theSel)
118 if (mySelector == theSel)
121 mySelector->viewer()->getViewManager()->disconnect(this);
122 OCCViewer_Viewer* aViewer = mySelector->viewer();
124 aViewer->disconnect(this);
130 OCCViewer_Viewer* aViewer = mySelector->viewer();
131 SUIT_ViewManager* aMgr = aViewer->getViewManager();
133 myView = new SHAPERGUI_SalomeView(mySelector->viewer());
135 // TODO: Provide ModuleBase_IViewWindow interface
136 connect(aMgr, SIGNAL(lastViewClosed(SUIT_ViewManager*)), this, SIGNAL(lastViewClosed()));
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*)));
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*)));
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*)));
161 connect(aViewer, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
164 //**********************************************
165 void SHAPERGUI_SalomeViewer::onSelectionChanged()
167 // Selection event must be sent only after mouse release
168 myIsSelectionChanged = true;
171 //**********************************************
172 void SHAPERGUI_SalomeViewer::onMousePress(SUIT_ViewWindow* theView, QMouseEvent* theEvent)
174 myView->setCurrentView(theView);
175 emit mousePress(myView, theEvent);
178 //**********************************************
179 void SHAPERGUI_SalomeViewer::onMouseRelease(SUIT_ViewWindow* theView, QMouseEvent* theEvent)
181 myView->setCurrentView(theView);
182 emit mouseRelease(myView, theEvent);
183 if (myIsSelectionChanged) {
184 emit selectionChanged();
185 myIsSelectionChanged = false;
189 //**********************************************
190 void SHAPERGUI_SalomeViewer::onMouseDoubleClick(SUIT_ViewWindow* theView, QMouseEvent* theEvent)
192 myView->setCurrentView(theView);
193 emit mouseDoubleClick(myView, theEvent);
196 //**********************************************
197 void SHAPERGUI_SalomeViewer::onMouseMove(SUIT_ViewWindow* theView, QMouseEvent* theEvent)
199 myView->setCurrentView(theView);
200 emit mouseMove(myView, theEvent);
203 //**********************************************
204 bool SHAPERGUI_SalomeViewer::canDragByMouse() const
206 OCCViewer_Viewer* aViewer = mySelector->viewer();
207 SUIT_ViewWindow* aWnd = aViewer->getViewManager()->getActiveView();
208 OCCViewer_ViewWindow* aViewWnd = dynamic_cast<OCCViewer_ViewWindow*>(aWnd);
210 return (aViewWnd->interactionStyle() == 0);
216 //**********************************************
217 void SHAPERGUI_SalomeViewer::onKeyPress(SUIT_ViewWindow* theView, QKeyEvent* theEvent)
219 emit keyPress(myView, theEvent);
222 //**********************************************
223 void SHAPERGUI_SalomeViewer::onKeyRelease(SUIT_ViewWindow* theView, QKeyEvent* theEvent)
225 emit keyRelease(myView, theEvent);
228 //**********************************************
229 void SHAPERGUI_SalomeViewer::onTryCloseView(SUIT_ViewWindow*)
231 emit tryCloseView(myView);
234 //**********************************************
235 void SHAPERGUI_SalomeViewer::onDeleteView(SUIT_ViewWindow*)
237 if(myWindowScale.contains(myView->v3dView()))
238 myWindowScale.remove(myView->v3dView());
239 emit deleteView(myView);
242 //**********************************************
243 void SHAPERGUI_SalomeViewer::onViewCreated(SUIT_ViewWindow* theView)
245 myView->setCurrentView(theView);
247 OCCViewer_ViewFrame* aView = dynamic_cast<OCCViewer_ViewFrame*>(theView);
249 OCCViewer_ViewWindow* aWnd = aView->getView(OCCViewer_ViewFrame::MAIN_VIEW);
251 connect(aWnd, SIGNAL(vpTransformationFinished(OCCViewer_ViewWindow::OperationType)),
252 this, SLOT(onViewTransformed(OCCViewer_ViewWindow::OperationType)));
253 OCCViewer_ViewPort3d* aViewPort = aWnd->getViewPort();
255 connect(aViewPort, SIGNAL(vpMapped(OCCViewer_ViewPort3d*)), this, SLOT(onViewPortMapped()));
257 reconnectActions(aWnd, true);
259 myWindowScale.insert(aView->getViewPort()->getView(),
260 aView->getViewPort()->getView()->Camera()->Scale());
262 emit viewCreated(myView);
267 //**********************************************
268 void SHAPERGUI_SalomeViewer::onActivated(SUIT_ViewManager* theMgr)
270 myView->setCurrentView(theMgr->getActiveView());
271 emit activated(myView);
274 //**********************************************
275 void SHAPERGUI_SalomeViewer::enableSelection(bool isEnabled)
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);
286 //**********************************************
287 bool SHAPERGUI_SalomeViewer::isSelectionEnabled() const
290 return mySelector->viewer()->isSelectionEnabled();
294 //**********************************************
295 void SHAPERGUI_SalomeViewer::enableMultiselection(bool isEnable)
298 mySelector->viewer()->enableMultiselection(isEnable);
301 //**********************************************
302 bool SHAPERGUI_SalomeViewer::isMultiSelectionEnabled() const
305 return mySelector->viewer()->isMultiSelectionEnabled();
309 //**********************************************
310 bool SHAPERGUI_SalomeViewer::enableDrawMode(bool isEnabled)
312 // TODO: Has to be replaced when SALOME patch become available
314 return mySelector->viewer()->enableDrawMode(isEnabled);
318 //**********************************************
319 void SHAPERGUI_SalomeViewer::reconnectActions(SUIT_ViewWindow* theWindow,
320 const bool theUseSHAPERSlot)
322 OCCViewer_ViewWindow* aWindow = dynamic_cast<OCCViewer_ViewWindow*>(theWindow);
326 QAction* anAction = theWindow->toolMgr()->action(OCCViewer_ViewWindow::TrihedronShowId);
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)));
337 anAction->connect(anAction, SIGNAL(toggled(bool)),
338 theWindow, SLOT(onTrihedronShow(bool)));
339 anAction->disconnect(anAction, SIGNAL(toggled(bool)),
340 this, SIGNAL(trihedronVisibilityChanged(bool)));
344 //**********************************************
345 void SHAPERGUI_SalomeViewer::fitAll()
348 SUIT_ViewManager* aMgr = mySelector->viewer()->getViewManager();
349 OCCViewer_ViewFrame* aVFrame = dynamic_cast<OCCViewer_ViewFrame*>(aMgr->getActiveView());
356 //**********************************************
357 void SHAPERGUI_SalomeViewer::eraseAll()
359 SOCC_Viewer* aViewer = dynamic_cast<SOCC_Viewer*>(myView->viewer());
360 aViewer->EraseAll(0);
363 //**********************************************
364 void SHAPERGUI_SalomeViewer::setViewProjection(double theX, double theY,
365 double theZ, double theTwist)
370 SUIT_ViewManager* aMgr = mySelector->viewer()->getViewManager();
371 OCCViewer_ViewFrame* aVFrame = dynamic_cast<OCCViewer_ViewFrame*>(aMgr->getActiveView());
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();
385 //***************************************
386 void SHAPERGUI_SalomeViewer::addSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
388 Handle(AIS_InteractiveContext) aContext = AISContext();
389 if (!aContext.IsNull()) {
390 aContext->AddFilter(theFilter);
394 //***************************************
395 void SHAPERGUI_SalomeViewer::removeSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
397 Handle(AIS_InteractiveContext) aContext = AISContext();
398 if (!aContext.IsNull()) {
399 aContext->RemoveFilter(theFilter);
403 //***************************************
404 bool SHAPERGUI_SalomeViewer::hasSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
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();
418 //***************************************
419 void SHAPERGUI_SalomeViewer::clearSelectionFilters()
421 Handle(AIS_InteractiveContext) aContext = AISContext();
422 if (!aContext.IsNull()) {
423 aContext->RemoveFilters();
427 //***************************************
428 void SHAPERGUI_SalomeViewer::update()
430 Handle(AIS_InteractiveContext) aContext = AISContext();
431 if (!aContext.IsNull()) {
432 aContext->UpdateCurrentViewer();
436 //***************************************
437 void SHAPERGUI_SalomeViewer::onViewTransformed(OCCViewer_ViewWindow::OperationType theType)
439 emit viewTransformed((int) theType);
442 //***************************************
443 void SHAPERGUI_SalomeViewer::onViewPortMapped()
445 emit trihedronVisibilityChanged(true);
448 //***************************************
449 void SHAPERGUI_SalomeViewer::activateViewer(bool toActivate)
451 if (!mySelector || !mySelector->viewer())
453 SUIT_ViewManager* aMgr = mySelector->viewer()->getViewManager();
454 #ifdef SALOME_PATCH_FOR_CTRL_WHEEL
455 OCCViewer_Viewer* aViewer = dynamic_cast<OCCViewer_Viewer*>(aMgr->getViewModel());
457 aViewer->setUseLocalSelection(toActivate);
459 QVector<SUIT_ViewWindow*> aViews = aMgr->getViews();
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);
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);
480 //void SHAPERGUI_SalomeViewer::Zfitall()
482 // if (!mySelector || !mySelector->viewer())
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());
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();