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>
9 #include <SUIT_ViewManager.h>
11 #include <QtxActionToolMgr.h>
13 #include <SelectMgr_ListIteratorOfListOfFilter.hxx>
15 #include <QMouseEvent>
16 #include <QContextMenuEvent>
18 SHAPERGUI_SalomeView::SHAPERGUI_SalomeView(OCCViewer_Viewer* theViewer)
19 : ModuleBase_IViewWindow(), myCurrentView(0)
25 Handle(V3d_View) SHAPERGUI_SalomeView::v3dView() const
27 Handle(V3d_View) aView;
29 OCCViewer_ViewWindow* aWnd = static_cast<OCCViewer_ViewWindow*>(myCurrentView);
30 aView = aWnd->getViewPort()->getView();
35 QWidget* SHAPERGUI_SalomeView::viewPort() const
37 QWidget* aViewPort = 0;
39 OCCViewer_ViewWindow* aWnd = static_cast<OCCViewer_ViewWindow*>(myCurrentView);
40 aViewPort = aWnd->getViewPort();
45 //**********************************************
46 //**********************************************
47 //**********************************************
51 SHAPERGUI_SalomeViewer::SHAPERGUI_SalomeViewer(QObject* theParent)
52 : ModuleBase_IViewer(theParent),
53 mySelector(0), myView(0), myIsSelectionChanged(false)
57 SHAPERGUI_SalomeViewer::~SHAPERGUI_SalomeViewer()
64 //**********************************************
65 Handle(AIS_InteractiveContext) SHAPERGUI_SalomeViewer::AISContext() const
67 if (mySelector && mySelector->viewer())
68 return mySelector->viewer()->getAISContext();
69 Handle(AIS_InteractiveContext) aNull;
73 //**********************************************
74 Handle(V3d_Viewer) SHAPERGUI_SalomeViewer::v3dViewer() const
77 return mySelector->viewer()->getViewer3d();
78 return Handle(V3d_Viewer)();
81 //**********************************************
82 Handle(AIS_Trihedron) SHAPERGUI_SalomeViewer::trihedron() const
84 return mySelector->viewer()->getTrihedron();
87 //**********************************************
88 Handle(V3d_View) SHAPERGUI_SalomeViewer::activeView() const
91 OCCViewer_Viewer* aViewer = mySelector->viewer();
92 SUIT_ViewManager* aMgr = aViewer->getViewManager();
93 OCCViewer_ViewWindow* aWnd = static_cast<OCCViewer_ViewWindow*>(aMgr->getActiveView());
94 return aWnd->getViewPort()->getView();
96 return Handle(V3d_View)();
99 //**********************************************
100 QWidget* SHAPERGUI_SalomeViewer::activeViewPort() const
104 OCCViewer_Viewer* aViewer = mySelector->viewer();
105 SUIT_ViewManager* aMgr = aViewer->getViewManager();
106 OCCViewer_ViewWindow* aWnd = static_cast<OCCViewer_ViewWindow*>(aMgr->getActiveView());
107 aViewPort = aWnd->getViewPort();
112 //**********************************************
113 void SHAPERGUI_SalomeViewer::setSelector(SHAPERGUI_OCCSelector* theSel)
116 if (mySelector == theSel)
119 mySelector->viewer()->getViewManager()->disconnect(this);
120 OCCViewer_Viewer* aViewer = mySelector->viewer();
122 aViewer->disconnect(this);
128 OCCViewer_Viewer* aViewer = mySelector->viewer();
129 SUIT_ViewManager* aMgr = aViewer->getViewManager();
131 myView = new SHAPERGUI_SalomeView(mySelector->viewer());
133 // TODO: Provide ModuleBase_IViewWindow interface
134 connect(aMgr, SIGNAL(lastViewClosed(SUIT_ViewManager*)), this, SIGNAL(lastViewClosed()));
136 connect(aMgr, SIGNAL(tryCloseView(SUIT_ViewWindow*)),
137 this, SLOT(onTryCloseView(SUIT_ViewWindow*)));
138 connect(aMgr, SIGNAL(deleteView(SUIT_ViewWindow*)),
139 this, SLOT(onDeleteView(SUIT_ViewWindow*)));
140 connect(aMgr, SIGNAL(viewCreated(SUIT_ViewWindow*)),
141 this, SLOT(onViewCreated(SUIT_ViewWindow*)));
142 connect(aMgr, SIGNAL(activated(SUIT_ViewManager*)),
143 this, SLOT(onActivated(SUIT_ViewManager*)));
145 connect(aMgr, SIGNAL(mousePress(SUIT_ViewWindow*, QMouseEvent*)), this,
146 SLOT(onMousePress(SUIT_ViewWindow*, QMouseEvent*)));
147 connect(aMgr, SIGNAL(mouseRelease(SUIT_ViewWindow*, QMouseEvent*)), this,
148 SLOT(onMouseRelease(SUIT_ViewWindow*, QMouseEvent*)));
149 connect(aMgr, SIGNAL(mouseDoubleClick(SUIT_ViewWindow*, QMouseEvent*)), this,
150 SLOT(onMouseDoubleClick(SUIT_ViewWindow*, QMouseEvent*)));
151 connect(aMgr, SIGNAL(mouseMove(SUIT_ViewWindow*, QMouseEvent*)), this,
152 SLOT(onMouseMove(SUIT_ViewWindow*, QMouseEvent*)));
154 connect(aMgr, SIGNAL(keyPress(SUIT_ViewWindow*, QKeyEvent*)), this,
155 SLOT(onKeyPress(SUIT_ViewWindow*, QKeyEvent*)));
156 connect(aMgr, SIGNAL(keyRelease(SUIT_ViewWindow*, QKeyEvent*)), this,
157 SLOT(onKeyRelease(SUIT_ViewWindow*, QKeyEvent*)));
159 connect(aViewer, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
162 //**********************************************
163 void SHAPERGUI_SalomeViewer::onSelectionChanged()
165 // Selection event must be sent only after mouse release
166 myIsSelectionChanged = true;
169 //**********************************************
170 void SHAPERGUI_SalomeViewer::onMousePress(SUIT_ViewWindow* theView, QMouseEvent* theEvent)
172 myView->setCurrentView(theView);
173 emit mousePress(myView, theEvent);
176 //**********************************************
177 void SHAPERGUI_SalomeViewer::onMouseRelease(SUIT_ViewWindow* theView, QMouseEvent* theEvent)
179 myView->setCurrentView(theView);
180 emit mouseRelease(myView, theEvent);
181 if (myIsSelectionChanged) {
182 emit selectionChanged();
183 myIsSelectionChanged = false;
187 //**********************************************
188 void SHAPERGUI_SalomeViewer::onMouseDoubleClick(SUIT_ViewWindow* theView, QMouseEvent* theEvent)
190 myView->setCurrentView(theView);
191 emit mouseDoubleClick(myView, theEvent);
194 //**********************************************
195 void SHAPERGUI_SalomeViewer::onMouseMove(SUIT_ViewWindow* theView, QMouseEvent* theEvent)
197 myView->setCurrentView(theView);
198 emit mouseMove(myView, theEvent);
201 //**********************************************
202 bool SHAPERGUI_SalomeViewer::canDragByMouse() const
204 OCCViewer_Viewer* aViewer = mySelector->viewer();
205 SUIT_ViewWindow* aWnd = aViewer->getViewManager()->getActiveView();
206 OCCViewer_ViewWindow* aViewWnd = dynamic_cast<OCCViewer_ViewWindow*>(aWnd);
208 return (aViewWnd->interactionStyle() == 0);
214 //**********************************************
215 void SHAPERGUI_SalomeViewer::onKeyPress(SUIT_ViewWindow* theView, QKeyEvent* theEvent)
217 emit keyPress(myView, theEvent);
220 //**********************************************
221 void SHAPERGUI_SalomeViewer::onKeyRelease(SUIT_ViewWindow* theView, QKeyEvent* theEvent)
223 emit keyRelease(myView, theEvent);
226 //**********************************************
227 void SHAPERGUI_SalomeViewer::onTryCloseView(SUIT_ViewWindow*)
229 emit tryCloseView(myView);
232 //**********************************************
233 void SHAPERGUI_SalomeViewer::onDeleteView(SUIT_ViewWindow*)
235 if(myWindowScale.contains(myView->v3dView()))
236 myWindowScale.remove(myView->v3dView());
237 emit deleteView(myView);
240 //**********************************************
241 void SHAPERGUI_SalomeViewer::onViewCreated(SUIT_ViewWindow* theView)
243 myView->setCurrentView(theView);
245 OCCViewer_ViewFrame* aView = dynamic_cast<OCCViewer_ViewFrame*>(theView);
247 OCCViewer_ViewWindow* aWnd = aView->getView(OCCViewer_ViewFrame::MAIN_VIEW);
249 connect(aWnd, SIGNAL(vpTransformationFinished(OCCViewer_ViewWindow::OperationType)),
250 this, SLOT(onViewTransformed(OCCViewer_ViewWindow::OperationType)));
251 OCCViewer_ViewPort3d* aViewPort = aWnd->getViewPort();
253 connect(aViewPort, SIGNAL(vpMapped(OCCViewer_ViewPort3d*)), this, SLOT(onViewPortMapped()));
255 reconnectActions(aWnd, true);
257 myWindowScale.insert (aView->getViewPort()->getView(), aView->getViewPort()->getView()->Camera()->Scale());
259 emit viewCreated(myView);
264 //**********************************************
265 void SHAPERGUI_SalomeViewer::onActivated(SUIT_ViewManager* theMgr)
267 myView->setCurrentView(theMgr->getActiveView());
268 emit activated(myView);
271 //**********************************************
272 void SHAPERGUI_SalomeViewer::enableSelection(bool isEnabled)
275 if (mySelector->viewer()->isSelectionEnabled() != isEnabled)
276 mySelector->viewer()->enableSelection(isEnabled);
277 // The enableSelection() in SALOME 7.5 cause of forced Viewer update(we have blinking)
278 // After this is corrected, the first row should be recommented, the last - removed
279 //mySelector->viewer()->setInteractionStyle(isEnabled ? SUIT_ViewModel::STANDARD
280 // : SUIT_ViewModel::KEY_FREE);
283 //**********************************************
284 bool SHAPERGUI_SalomeViewer::isSelectionEnabled() const
287 return mySelector->viewer()->isSelectionEnabled();
291 //**********************************************
292 void SHAPERGUI_SalomeViewer::enableMultiselection(bool isEnable)
295 mySelector->viewer()->enableMultiselection(isEnable);
298 //**********************************************
299 bool SHAPERGUI_SalomeViewer::isMultiSelectionEnabled() const
302 return mySelector->viewer()->isMultiSelectionEnabled();
306 //**********************************************
307 bool SHAPERGUI_SalomeViewer::enableDrawMode(bool isEnabled)
309 // TODO: Has to be replaced when SALOME patch become available
311 return mySelector->viewer()->enableDrawMode(isEnabled);
315 //**********************************************
316 void SHAPERGUI_SalomeViewer::reconnectActions(SUIT_ViewWindow* theWindow,
317 const bool theUseSHAPERSlot)
319 OCCViewer_ViewWindow* aWindow = dynamic_cast<OCCViewer_ViewWindow*>(theWindow);
323 QAction* anAction = theWindow->toolMgr()->action(OCCViewer_ViewWindow::TrihedronShowId);
327 if (theUseSHAPERSlot) {
328 anAction->disconnect(anAction, SIGNAL(toggled(bool)),
329 theWindow, SLOT(onTrihedronShow(bool)));
330 anAction->connect(anAction, SIGNAL(toggled(bool)),
331 this, SIGNAL(trihedronVisibilityChanged(bool)));
334 anAction->connect(anAction, SIGNAL(toggled(bool)),
335 theWindow, SLOT(onTrihedronShow(bool)));
336 anAction->disconnect(anAction, SIGNAL(toggled(bool)),
337 this, SIGNAL(trihedronVisibilityChanged(bool)));
341 //**********************************************
342 void SHAPERGUI_SalomeViewer::fitAll()
345 SUIT_ViewManager* aMgr = mySelector->viewer()->getViewManager();
346 OCCViewer_ViewFrame* aVFrame = dynamic_cast<OCCViewer_ViewFrame*>(aMgr->getActiveView());
353 //**********************************************
354 void SHAPERGUI_SalomeViewer::setViewProjection(double theX, double theY, double theZ, double theTwist)
359 SUIT_ViewManager* aMgr = mySelector->viewer()->getViewManager();
360 OCCViewer_ViewFrame* aVFrame = dynamic_cast<OCCViewer_ViewFrame*>(aMgr->getActiveView());
362 Handle(V3d_View) aView3d = aVFrame->getViewPort()->getView();
363 if (!aView3d.IsNull()) {
364 aView3d->SetProj(theX, theY, theZ);
365 aView3d->SetTwist( theTwist );
366 aView3d->FitAll(0.01, false);
367 aView3d->SetZSize(0.);
368 if (aView3d->Depth() < 0.1)
369 aView3d->DepthFitAll();
374 //***************************************
375 void SHAPERGUI_SalomeViewer::addSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
377 Handle(AIS_InteractiveContext) aContext = AISContext();
378 if (!aContext.IsNull()) {
379 aContext->AddFilter(theFilter);
383 //***************************************
384 void SHAPERGUI_SalomeViewer::removeSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
386 Handle(AIS_InteractiveContext) aContext = AISContext();
387 if (!aContext.IsNull()) {
388 aContext->RemoveFilter(theFilter);
392 //***************************************
393 bool SHAPERGUI_SalomeViewer::hasSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
395 bool aFoundFilter = false;
396 Handle(AIS_InteractiveContext) aContext = AISContext();
397 if (!aContext.IsNull()) {
398 const SelectMgr_ListOfFilter& aFilters = aContext->Filters();
399 SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
400 for (; aIt.More() && !aFoundFilter; aIt.Next()) {
401 aFoundFilter = theFilter.Access() == aIt.Value().Access();
407 //***************************************
408 void SHAPERGUI_SalomeViewer::clearSelectionFilters()
410 Handle(AIS_InteractiveContext) aContext = AISContext();
411 if (!aContext.IsNull()) {
412 aContext->RemoveFilters();
416 //***************************************
417 void SHAPERGUI_SalomeViewer::update()
419 Handle(AIS_InteractiveContext) aContext = AISContext();
420 if (!aContext.IsNull()) {
421 aContext->UpdateCurrentViewer();
425 //***************************************
426 void SHAPERGUI_SalomeViewer::onViewTransformed(OCCViewer_ViewWindow::OperationType theType)
428 emit viewTransformed((int) theType);
431 //***************************************
432 void SHAPERGUI_SalomeViewer::onViewPortMapped()
434 emit trihedronVisibilityChanged(true);
437 //***************************************
438 void SHAPERGUI_SalomeViewer::activateViewer(bool toActivate)
440 if (!mySelector || !mySelector->viewer())
442 SUIT_ViewManager* aMgr = mySelector->viewer()->getViewManager();
443 QVector<SUIT_ViewWindow*> aViews = aMgr->getViews();
445 foreach (SUIT_ViewWindow* aView, aViews) {
446 OCCViewer_ViewFrame* aOCCView = dynamic_cast<OCCViewer_ViewFrame*>(aView);
447 OCCViewer_ViewWindow* aWnd = aOCCView->getView(OCCViewer_ViewFrame::MAIN_VIEW);
448 connect(aWnd, SIGNAL(vpTransformationFinished(OCCViewer_ViewWindow::OperationType)),
449 this, SLOT(onViewTransformed(OCCViewer_ViewWindow::OperationType)));
450 reconnectActions(aWnd, true);
453 foreach (SUIT_ViewWindow* aView, aViews) {
454 OCCViewer_ViewFrame* aOCCView = dynamic_cast<OCCViewer_ViewFrame*>(aView);
455 OCCViewer_ViewWindow* aWnd = aOCCView->getView(OCCViewer_ViewFrame::MAIN_VIEW);
456 disconnect((OCCViewer_ViewWindow*)aWnd, SIGNAL(vpTransformationFinished(OCCViewer_ViewWindow::OperationType)),
457 this, SLOT(onViewTransformed(OCCViewer_ViewWindow::OperationType)));
458 reconnectActions(aWnd, false);
463 void SHAPERGUI_SalomeViewer::Zfitall()
465 if (!mySelector || !mySelector->viewer())
467 SUIT_ViewManager* aMgr = mySelector->viewer()->getViewManager();
468 OCCViewer_ViewFrame* aView = dynamic_cast<OCCViewer_ViewFrame*>(aMgr->getActiveView());
470 OCCViewer_ViewWindow* aWnd = aView->getView(OCCViewer_ViewFrame::MAIN_VIEW);
471 Handle(V3d_View) aView3d = aWnd->getViewPort()->getView();
473 if (aView3d->Depth() < 0.1)
474 aView3d->DepthFitAll();