1 // Copyright (C) 2014-2017 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include "SHAPERGUI_SalomeViewer.h"
22 #include "SHAPERGUI_OCCSelector.h"
24 #include <OCCViewer_ViewPort3d.h>
25 #include <OCCViewer_ViewFrame.h>
26 #include <SOCC_ViewModel.h>
27 #include <SUIT_ViewManager.h>
29 #include <QtxActionToolMgr.h>
30 #include <SALOME_AISShape.hxx>
32 #include <SelectMgr_ListIteratorOfListOfFilter.hxx>
34 #include <QMouseEvent>
35 #include <QContextMenuEvent>
37 #define SALOME_PATCH_FOR_CTRL_WHEEL
39 SHAPERGUI_SalomeView::SHAPERGUI_SalomeView(OCCViewer_Viewer* theViewer)
40 : ModuleBase_IViewWindow(), myCurrentView(0)
46 Handle(V3d_View) SHAPERGUI_SalomeView::v3dView() const
48 Handle(V3d_View) aView;
50 OCCViewer_ViewWindow* aWnd = static_cast<OCCViewer_ViewWindow*>(myCurrentView);
51 aView = aWnd->getViewPort()->getView();
56 QWidget* SHAPERGUI_SalomeView::viewPort() const
58 QWidget* aViewPort = 0;
60 OCCViewer_ViewWindow* aWnd = static_cast<OCCViewer_ViewWindow*>(myCurrentView);
61 aViewPort = aWnd->getViewPort();
66 //**********************************************
67 //**********************************************
68 //**********************************************
72 SHAPERGUI_SalomeViewer::SHAPERGUI_SalomeViewer(QObject* theParent)
73 : ModuleBase_IViewer(theParent),
74 mySelector(0), myView(0), myIsSelectionChanged(false)
78 SHAPERGUI_SalomeViewer::~SHAPERGUI_SalomeViewer()
85 //**********************************************
86 Handle(AIS_InteractiveContext) SHAPERGUI_SalomeViewer::AISContext() const
88 if (mySelector && mySelector->viewer())
89 return mySelector->viewer()->getAISContext();
90 Handle(AIS_InteractiveContext) aNull;
94 //**********************************************
95 Handle(V3d_Viewer) SHAPERGUI_SalomeViewer::v3dViewer() const
98 return mySelector->viewer()->getViewer3d();
99 return Handle(V3d_Viewer)();
102 //**********************************************
103 Handle(AIS_Trihedron) SHAPERGUI_SalomeViewer::trihedron() const
105 return mySelector->viewer()->getTrihedron();
108 //**********************************************
109 Handle(V3d_View) SHAPERGUI_SalomeViewer::activeView() const
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();
117 return Handle(V3d_View)();
120 //**********************************************
121 QWidget* SHAPERGUI_SalomeViewer::activeViewPort() const
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();
133 //**********************************************
134 void SHAPERGUI_SalomeViewer::setSelector(SHAPERGUI_OCCSelector* theSel)
137 if (mySelector == theSel)
140 mySelector->viewer()->getViewManager()->disconnect(this);
141 OCCViewer_Viewer* aViewer = mySelector->viewer();
143 aViewer->disconnect(this);
149 OCCViewer_Viewer* aViewer = mySelector->viewer();
150 SUIT_ViewManager* aMgr = aViewer->getViewManager();
152 myView = new SHAPERGUI_SalomeView(mySelector->viewer());
154 // TODO: Provide ModuleBase_IViewWindow interface
155 connect(aMgr, SIGNAL(lastViewClosed(SUIT_ViewManager*)), this, SIGNAL(lastViewClosed()));
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*)));
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*)));
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*)));
180 connect(aViewer, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
183 //**********************************************
184 void SHAPERGUI_SalomeViewer::onSelectionChanged()
186 // Selection event must be sent only after mouse release
187 myIsSelectionChanged = true;
190 //**********************************************
191 void SHAPERGUI_SalomeViewer::onMousePress(SUIT_ViewWindow* theView, QMouseEvent* theEvent)
193 myView->setCurrentView(theView);
194 emit mousePress(myView, theEvent);
197 //**********************************************
198 void SHAPERGUI_SalomeViewer::onMouseRelease(SUIT_ViewWindow* theView, QMouseEvent* theEvent)
200 myView->setCurrentView(theView);
201 emit mouseRelease(myView, theEvent);
202 if (myIsSelectionChanged) {
203 emit selectionChanged();
204 myIsSelectionChanged = false;
208 //**********************************************
209 void SHAPERGUI_SalomeViewer::onMouseDoubleClick(SUIT_ViewWindow* theView, QMouseEvent* theEvent)
211 myView->setCurrentView(theView);
212 emit mouseDoubleClick(myView, theEvent);
215 //**********************************************
216 void SHAPERGUI_SalomeViewer::onMouseMove(SUIT_ViewWindow* theView, QMouseEvent* theEvent)
218 myView->setCurrentView(theView);
219 emit mouseMove(myView, theEvent);
222 //**********************************************
223 bool SHAPERGUI_SalomeViewer::canDragByMouse() const
225 OCCViewer_Viewer* aViewer = mySelector->viewer();
226 SUIT_ViewWindow* aWnd = aViewer->getViewManager()->getActiveView();
227 OCCViewer_ViewWindow* aViewWnd = dynamic_cast<OCCViewer_ViewWindow*>(aWnd);
229 return (aViewWnd->interactionStyle() == 0);
235 //**********************************************
236 void SHAPERGUI_SalomeViewer::onKeyPress(SUIT_ViewWindow* theView, QKeyEvent* theEvent)
238 emit keyPress(myView, theEvent);
241 //**********************************************
242 void SHAPERGUI_SalomeViewer::onKeyRelease(SUIT_ViewWindow* theView, QKeyEvent* theEvent)
244 emit keyRelease(myView, theEvent);
247 //**********************************************
248 void SHAPERGUI_SalomeViewer::onTryCloseView(SUIT_ViewWindow*)
250 emit tryCloseView(myView);
253 //**********************************************
254 void SHAPERGUI_SalomeViewer::onDeleteView(SUIT_ViewWindow*)
256 if(myWindowScale.contains(myView->v3dView()))
257 myWindowScale.remove(myView->v3dView());
258 emit deleteView(myView);
261 //**********************************************
262 void SHAPERGUI_SalomeViewer::onViewCreated(SUIT_ViewWindow* theView)
264 myView->setCurrentView(theView);
266 OCCViewer_ViewFrame* aView = dynamic_cast<OCCViewer_ViewFrame*>(theView);
268 OCCViewer_ViewWindow* aWnd = aView->getView(OCCViewer_ViewFrame::MAIN_VIEW);
270 connect(aWnd, SIGNAL(vpTransformationFinished(OCCViewer_ViewWindow::OperationType)),
271 this, SLOT(onViewTransformed(OCCViewer_ViewWindow::OperationType)));
272 OCCViewer_ViewPort3d* aViewPort = aWnd->getViewPort();
274 connect(aViewPort, SIGNAL(vpMapped(OCCViewer_ViewPort3d*)), this, SLOT(onViewPortMapped()));
276 reconnectActions(aWnd, true);
278 myWindowScale.insert(aView->getViewPort()->getView(),
279 aView->getViewPort()->getView()->Camera()->Scale());
281 emit viewCreated(myView);
286 //**********************************************
287 void SHAPERGUI_SalomeViewer::onActivated(SUIT_ViewManager* theMgr)
289 myView->setCurrentView(theMgr->getActiveView());
290 emit activated(myView);
293 //**********************************************
294 void SHAPERGUI_SalomeViewer::enableSelection(bool isEnabled)
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);
305 //**********************************************
306 bool SHAPERGUI_SalomeViewer::isSelectionEnabled() const
309 return mySelector->viewer()->isSelectionEnabled();
313 //**********************************************
314 void SHAPERGUI_SalomeViewer::enableMultiselection(bool isEnable)
317 mySelector->viewer()->enableMultiselection(isEnable);
320 //**********************************************
321 bool SHAPERGUI_SalomeViewer::isMultiSelectionEnabled() const
324 return mySelector->viewer()->isMultiSelectionEnabled();
328 //**********************************************
329 bool SHAPERGUI_SalomeViewer::enableDrawMode(bool isEnabled)
331 // TODO: Has to be replaced when SALOME patch become available
333 return mySelector->viewer()->enableDrawMode(isEnabled);
337 //**********************************************
338 void SHAPERGUI_SalomeViewer::reconnectActions(SUIT_ViewWindow* theWindow,
339 const bool theUseSHAPERSlot)
341 OCCViewer_ViewWindow* aWindow = dynamic_cast<OCCViewer_ViewWindow*>(theWindow);
345 QAction* anAction = theWindow->toolMgr()->action(OCCViewer_ViewWindow::TrihedronShowId);
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)));
356 anAction->connect(anAction, SIGNAL(toggled(bool)),
357 theWindow, SLOT(onTrihedronShow(bool)));
358 anAction->disconnect(anAction, SIGNAL(toggled(bool)),
359 this, SIGNAL(trihedronVisibilityChanged(bool)));
363 //**********************************************
364 void SHAPERGUI_SalomeViewer::fitAll()
367 SUIT_ViewManager* aMgr = mySelector->viewer()->getViewManager();
368 OCCViewer_ViewFrame* aVFrame = dynamic_cast<OCCViewer_ViewFrame*>(aMgr->getActiveView());
375 //**********************************************
376 void SHAPERGUI_SalomeViewer::eraseAll()
378 Handle(AIS_InteractiveContext) aContext = AISContext();
379 if (aContext.IsNull())
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);
394 //**********************************************
395 void SHAPERGUI_SalomeViewer::setViewProjection(double theX, double theY,
396 double theZ, double theTwist)
401 SUIT_ViewManager* aMgr = mySelector->viewer()->getViewManager();
402 OCCViewer_ViewFrame* aVFrame = dynamic_cast<OCCViewer_ViewFrame*>(aMgr->getActiveView());
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();
416 //***************************************
417 void SHAPERGUI_SalomeViewer::addSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
419 Handle(AIS_InteractiveContext) aContext = AISContext();
420 if (!aContext.IsNull()) {
421 aContext->AddFilter(theFilter);
425 //***************************************
426 void SHAPERGUI_SalomeViewer::removeSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
428 Handle(AIS_InteractiveContext) aContext = AISContext();
429 if (!aContext.IsNull()) {
430 aContext->RemoveFilter(theFilter);
434 //***************************************
435 bool SHAPERGUI_SalomeViewer::hasSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
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();
449 //***************************************
450 void SHAPERGUI_SalomeViewer::clearSelectionFilters()
452 Handle(AIS_InteractiveContext) aContext = AISContext();
453 if (!aContext.IsNull()) {
454 aContext->RemoveFilters();
458 //***************************************
459 void SHAPERGUI_SalomeViewer::update()
461 Handle(AIS_InteractiveContext) aContext = AISContext();
462 if (!aContext.IsNull()) {
463 aContext->UpdateCurrentViewer();
467 //***************************************
468 void SHAPERGUI_SalomeViewer::onViewTransformed(OCCViewer_ViewWindow::OperationType theType)
470 emit viewTransformed((int) theType);
473 //***************************************
474 void SHAPERGUI_SalomeViewer::onViewPortMapped()
476 emit trihedronVisibilityChanged(true);
479 //***************************************
480 void SHAPERGUI_SalomeViewer::activateViewer(bool toActivate)
482 if (!mySelector || !mySelector->viewer())
484 SUIT_ViewManager* aMgr = mySelector->viewer()->getViewManager();
485 #ifdef SALOME_PATCH_FOR_CTRL_WHEEL
486 OCCViewer_Viewer* aViewer = dynamic_cast<OCCViewer_Viewer*>(aMgr->getViewModel());
488 aViewer->setUseLocalSelection(toActivate);
490 QVector<SUIT_ViewWindow*> aViews = aMgr->getViews();
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);
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);
511 //void SHAPERGUI_SalomeViewer::Zfitall()
513 // if (!mySelector || !mySelector->viewer())
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());
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();