1 // Copyright (C) 2014-2020 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 email : webmaster.salome@opencascade.com
20 #include "SHAPERGUI_SalomeViewer.h"
21 #include "SHAPERGUI_OCCSelector.h"
23 #include <OCCViewer_ViewPort3d.h>
24 #include <OCCViewer_ViewFrame.h>
25 #include <SOCC_ViewModel.h>
26 #include <SUIT_ViewManager.h>
28 #include <QtxActionToolMgr.h>
29 #include <SALOME_AISShape.hxx>
31 #include <SelectMgr_ListIteratorOfListOfFilter.hxx>
33 #include <QMouseEvent>
34 #include <QContextMenuEvent>
37 #if OCC_VERSION_HEX < 0x070400
38 #define SALOME_PATCH_FOR_CTRL_WHEEL
41 SHAPERGUI_SalomeView::SHAPERGUI_SalomeView(OCCViewer_Viewer* theViewer)
42 : ModuleBase_IViewWindow(), myCurrentView(0)
48 Handle(V3d_View) SHAPERGUI_SalomeView::v3dView() const
50 Handle(V3d_View) aView;
52 OCCViewer_ViewWindow* aWnd = static_cast<OCCViewer_ViewWindow*>(myCurrentView);
53 aView = aWnd->getViewPort()->getView();
58 QWidget* SHAPERGUI_SalomeView::viewPort() const
60 QWidget* aViewPort = 0;
62 OCCViewer_ViewWindow* aWnd = static_cast<OCCViewer_ViewWindow*>(myCurrentView);
63 aViewPort = aWnd->getViewPort();
68 //**********************************************
69 //**********************************************
70 //**********************************************
74 SHAPERGUI_SalomeViewer::SHAPERGUI_SalomeViewer(QObject* theParent)
75 : ModuleBase_IViewer(theParent),
76 mySelector(0), myView(0), myIsSelectionChanged(false)
80 SHAPERGUI_SalomeViewer::~SHAPERGUI_SalomeViewer()
87 //**********************************************
88 Handle(AIS_InteractiveContext) SHAPERGUI_SalomeViewer::AISContext() const
90 if (mySelector && mySelector->viewer())
91 return mySelector->viewer()->getAISContext();
92 Handle(AIS_InteractiveContext) aNull;
96 //**********************************************
97 Handle(V3d_Viewer) SHAPERGUI_SalomeViewer::v3dViewer() const
100 return mySelector->viewer()->getViewer3d();
101 return Handle(V3d_Viewer)();
104 //**********************************************
105 Handle(AIS_Trihedron) SHAPERGUI_SalomeViewer::trihedron() const
107 return mySelector->viewer()->getTrihedron();
110 //**********************************************
111 Handle(V3d_View) SHAPERGUI_SalomeViewer::activeView() const
114 OCCViewer_Viewer* aViewer = mySelector->viewer();
115 SUIT_ViewManager* aMgr = aViewer->getViewManager();
116 OCCViewer_ViewWindow* aWnd = static_cast<OCCViewer_ViewWindow*>(aMgr->getActiveView());
117 return aWnd->getViewPort()->getView();
119 return Handle(V3d_View)();
122 //**********************************************
123 QWidget* SHAPERGUI_SalomeViewer::activeViewPort() const
125 QWidget* aViewPort = 0;
127 OCCViewer_Viewer* aViewer = mySelector->viewer();
128 SUIT_ViewManager* aMgr = aViewer->getViewManager();
129 OCCViewer_ViewWindow* aWnd = static_cast<OCCViewer_ViewWindow*>(aMgr->getActiveView());
130 aViewPort = aWnd->getViewPort();
135 //**********************************************
136 void SHAPERGUI_SalomeViewer::setSelector(SHAPERGUI_OCCSelector* theSel)
139 if (mySelector == theSel)
142 mySelector->viewer()->getViewManager()->disconnect(this);
143 OCCViewer_Viewer* aViewer = mySelector->viewer();
145 aViewer->disconnect(this);
151 OCCViewer_Viewer* aViewer = mySelector->viewer();
152 SUIT_ViewManager* aMgr = aViewer->getViewManager();
154 myView = new SHAPERGUI_SalomeView(mySelector->viewer());
156 // TODO: Provide ModuleBase_IViewWindow interface
157 connect(aMgr, SIGNAL(lastViewClosed(SUIT_ViewManager*)), this, SIGNAL(lastViewClosed()));
159 connect(aMgr, SIGNAL(tryCloseView(SUIT_ViewWindow*)),
160 this, SLOT(onTryCloseView(SUIT_ViewWindow*)));
161 connect(aMgr, SIGNAL(deleteView(SUIT_ViewWindow*)),
162 this, SLOT(onDeleteView(SUIT_ViewWindow*)));
163 connect(aMgr, SIGNAL(viewCreated(SUIT_ViewWindow*)),
164 this, SLOT(onViewCreated(SUIT_ViewWindow*)));
165 connect(aMgr, SIGNAL(activated(SUIT_ViewManager*)),
166 this, SLOT(onActivated(SUIT_ViewManager*)));
168 connect(aMgr, SIGNAL(mousePress(SUIT_ViewWindow*, QMouseEvent*)), this,
169 SLOT(onMousePress(SUIT_ViewWindow*, QMouseEvent*)));
170 connect(aMgr, SIGNAL(mouseRelease(SUIT_ViewWindow*, QMouseEvent*)), this,
171 SLOT(onMouseRelease(SUIT_ViewWindow*, QMouseEvent*)));
172 connect(aMgr, SIGNAL(mouseDoubleClick(SUIT_ViewWindow*, QMouseEvent*)), this,
173 SLOT(onMouseDoubleClick(SUIT_ViewWindow*, QMouseEvent*)));
174 connect(aMgr, SIGNAL(mouseMove(SUIT_ViewWindow*, QMouseEvent*)), this,
175 SLOT(onMouseMove(SUIT_ViewWindow*, QMouseEvent*)));
177 connect(aMgr, SIGNAL(keyPress(SUIT_ViewWindow*, QKeyEvent*)), this,
178 SLOT(onKeyPress(SUIT_ViewWindow*, QKeyEvent*)));
179 connect(aMgr, SIGNAL(keyRelease(SUIT_ViewWindow*, QKeyEvent*)), this,
180 SLOT(onKeyRelease(SUIT_ViewWindow*, QKeyEvent*)));
182 connect(aViewer, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
185 //**********************************************
186 void SHAPERGUI_SalomeViewer::onSelectionChanged()
188 // Selection event must be sent only after mouse release
189 myIsSelectionChanged = true;
192 //**********************************************
193 void SHAPERGUI_SalomeViewer::onMousePress(SUIT_ViewWindow* theView, QMouseEvent* theEvent)
195 myView->setCurrentView(theView);
196 emit mousePress(myView, theEvent);
199 //**********************************************
200 void SHAPERGUI_SalomeViewer::onMouseRelease(SUIT_ViewWindow* theView, QMouseEvent* theEvent)
202 myView->setCurrentView(theView);
203 emit mouseRelease(myView, theEvent);
204 if (myIsSelectionChanged) {
205 emit selectionChanged();
206 myIsSelectionChanged = false;
210 //**********************************************
211 void SHAPERGUI_SalomeViewer::onMouseDoubleClick(SUIT_ViewWindow* theView, QMouseEvent* theEvent)
213 myView->setCurrentView(theView);
214 emit mouseDoubleClick(myView, theEvent);
217 //**********************************************
218 void SHAPERGUI_SalomeViewer::onMouseMove(SUIT_ViewWindow* theView, QMouseEvent* theEvent)
220 myView->setCurrentView(theView);
221 emit mouseMove(myView, theEvent);
224 //**********************************************
225 bool SHAPERGUI_SalomeViewer::canDragByMouse() const
227 OCCViewer_Viewer* aViewer = mySelector->viewer();
228 SUIT_ViewWindow* aWnd = aViewer->getViewManager()->getActiveView();
229 OCCViewer_ViewWindow* aViewWnd = dynamic_cast<OCCViewer_ViewWindow*>(aWnd);
231 return (aViewWnd->interactionStyle() == 0);
237 //**********************************************
238 void SHAPERGUI_SalomeViewer::onKeyPress(SUIT_ViewWindow* theView, QKeyEvent* theEvent)
240 emit keyPress(myView, theEvent);
243 //**********************************************
244 void SHAPERGUI_SalomeViewer::onKeyRelease(SUIT_ViewWindow* theView, QKeyEvent* theEvent)
246 emit keyRelease(myView, theEvent);
249 //**********************************************
250 void SHAPERGUI_SalomeViewer::onTryCloseView(SUIT_ViewWindow*)
252 emit tryCloseView(myView);
255 //**********************************************
256 void SHAPERGUI_SalomeViewer::onDeleteView(SUIT_ViewWindow*)
258 if(myWindowScale.contains(myView->v3dView()))
259 myWindowScale.remove(myView->v3dView());
260 emit deleteView(myView);
263 //**********************************************
264 void SHAPERGUI_SalomeViewer::onViewCreated(SUIT_ViewWindow* theView)
266 myView->setCurrentView(theView);
268 OCCViewer_ViewFrame* aView = dynamic_cast<OCCViewer_ViewFrame*>(theView);
270 OCCViewer_ViewWindow* aWnd = aView->getView(OCCViewer_ViewFrame::MAIN_VIEW);
272 connect(aWnd, SIGNAL(vpTransformationFinished(OCCViewer_ViewWindow::OperationType)),
273 this, SLOT(onViewTransformed(OCCViewer_ViewWindow::OperationType)));
274 OCCViewer_ViewPort3d* aViewPort = aWnd->getViewPort();
276 connect(aViewPort, SIGNAL(vpMapped(OCCViewer_ViewPort3d*)), this, SLOT(onViewPortMapped()));
278 reconnectActions(aWnd, true);
280 myWindowScale.insert(aView->getViewPort()->getView(),
281 aView->getViewPort()->getView()->Camera()->Scale());
283 QTimer::singleShot(10, this, SLOT(onAfterViewCreated()));
285 emit viewCreated(myView);
288 //**********************************************
289 void SHAPERGUI_SalomeViewer::onAfterViewCreated()
291 // Update trihedron and dimension arrows
292 emit onViewTransformed(OCCViewer_ViewWindow::ZOOMVIEW);
296 //**********************************************
297 void SHAPERGUI_SalomeViewer::onActivated(SUIT_ViewManager* theMgr)
299 myView->setCurrentView(theMgr->getActiveView());
300 emit activated(myView);
303 //**********************************************
304 void SHAPERGUI_SalomeViewer::enableSelection(bool isEnabled)
307 if (mySelector->viewer()->isSelectionEnabled() != isEnabled)
308 mySelector->viewer()->enableSelection(isEnabled);
309 // The enableSelection() in SALOME 7.5 cause of forced Viewer update(we have blinking)
310 // After this is corrected, the first row should be recommented, the last - removed
311 //mySelector->viewer()->setInteractionStyle(isEnabled ? SUIT_ViewModel::STANDARD
312 // : SUIT_ViewModel::KEY_FREE);
315 //**********************************************
316 bool SHAPERGUI_SalomeViewer::isSelectionEnabled() const
319 return mySelector->viewer()->isSelectionEnabled();
323 //**********************************************
324 void SHAPERGUI_SalomeViewer::enableMultiselection(bool isEnable)
327 mySelector->viewer()->enableMultiselection(isEnable);
330 //**********************************************
331 bool SHAPERGUI_SalomeViewer::isMultiSelectionEnabled() const
334 return mySelector->viewer()->isMultiSelectionEnabled();
338 //**********************************************
339 bool SHAPERGUI_SalomeViewer::enableDrawMode(bool isEnabled)
341 // TODO: Has to be replaced when SALOME patch become available
343 return mySelector->viewer()->enableDrawMode(isEnabled);
347 //**********************************************
348 void SHAPERGUI_SalomeViewer::reconnectActions(SUIT_ViewWindow* theWindow,
349 const bool theUseSHAPERSlot)
351 OCCViewer_ViewWindow* aWindow = dynamic_cast<OCCViewer_ViewWindow*>(theWindow);
355 QAction* anAction = theWindow->toolMgr()->action(OCCViewer_ViewWindow::TrihedronShowId);
359 if (theUseSHAPERSlot) {
360 anAction->disconnect(anAction, SIGNAL(toggled(bool)),
361 theWindow, SLOT(onTrihedronShow(bool)));
362 anAction->connect(anAction, SIGNAL(toggled(bool)),
363 this, SIGNAL(trihedronVisibilityChanged(bool)));
366 anAction->connect(anAction, SIGNAL(toggled(bool)),
367 theWindow, SLOT(onTrihedronShow(bool)));
368 anAction->disconnect(anAction, SIGNAL(toggled(bool)),
369 this, SIGNAL(trihedronVisibilityChanged(bool)));
373 //**********************************************
374 void SHAPERGUI_SalomeViewer::fitAll()
377 SUIT_ViewManager* aMgr = mySelector->viewer()->getViewManager();
378 OCCViewer_ViewFrame* aVFrame = dynamic_cast<OCCViewer_ViewFrame*>(aMgr->getActiveView());
385 //**********************************************
386 void SHAPERGUI_SalomeViewer::eraseAll()
388 Handle(AIS_InteractiveContext) aContext = AISContext();
389 if (aContext.IsNull())
391 AIS_ListOfInteractive aList;
392 aContext->DisplayedObjects(aList);
393 AIS_ListIteratorOfListOfInteractive aLIt;
394 Handle(AIS_InteractiveObject) anAISIO;
395 for (aLIt.Initialize(aList); aLIt.More(); aLIt.Next()) {
396 anAISIO = aLIt.Value();
397 Handle(Standard_Type) aType = anAISIO->DynamicType();
398 if (anAISIO->IsKind(STANDARD_TYPE(SALOME_AISShape))) {
399 aContext->Erase(anAISIO, false);
404 //**********************************************
405 void SHAPERGUI_SalomeViewer::setViewProjection(double theX, double theY,
406 double theZ, double theTwist)
411 SUIT_ViewManager* aMgr = mySelector->viewer()->getViewManager();
412 OCCViewer_ViewFrame* aVFrame = dynamic_cast<OCCViewer_ViewFrame*>(aMgr->getActiveView());
414 Handle(V3d_View) aView3d = aVFrame->getViewPort()->getView();
415 if (!aView3d.IsNull()) {
416 aView3d->SetProj(theX, theY, theZ);
417 aView3d->SetTwist( theTwist );
418 aView3d->FitAll(0.01, false);
419 //aView3d->SetZSize(0.);
420 if (aView3d->Depth() < 0.1)
421 aView3d->DepthFitAll();
426 //***************************************
427 void SHAPERGUI_SalomeViewer::addSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
429 Handle(AIS_InteractiveContext) aContext = AISContext();
430 if (!aContext.IsNull()) {
431 aContext->AddFilter(theFilter);
435 //***************************************
436 void SHAPERGUI_SalomeViewer::removeSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
438 Handle(AIS_InteractiveContext) aContext = AISContext();
439 if (!aContext.IsNull()) {
440 aContext->RemoveFilter(theFilter);
444 //***************************************
445 bool SHAPERGUI_SalomeViewer::hasSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
447 bool aFoundFilter = false;
448 Handle(AIS_InteractiveContext) aContext = AISContext();
449 if (!aContext.IsNull()) {
450 const SelectMgr_ListOfFilter& aFilters = aContext->Filters();
451 SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
452 for (; aIt.More() && !aFoundFilter; aIt.Next()) {
453 aFoundFilter = theFilter.get() == aIt.Value().get();
459 //***************************************
460 void SHAPERGUI_SalomeViewer::clearSelectionFilters()
462 Handle(AIS_InteractiveContext) aContext = AISContext();
463 if (!aContext.IsNull()) {
464 aContext->RemoveFilters();
468 //***************************************
469 void SHAPERGUI_SalomeViewer::update()
471 Handle(AIS_InteractiveContext) aContext = AISContext();
472 if (!aContext.IsNull()) {
473 aContext->UpdateCurrentViewer();
477 //***************************************
478 void SHAPERGUI_SalomeViewer::onViewTransformed(OCCViewer_ViewWindow::OperationType theType)
480 emit viewTransformed((int) theType);
483 //***************************************
484 void SHAPERGUI_SalomeViewer::onViewPortMapped()
486 emit trihedronVisibilityChanged(true);
489 //***************************************
490 void SHAPERGUI_SalomeViewer::activateViewer(bool toActivate)
492 if (!mySelector || !mySelector->viewer())
494 SUIT_ViewManager* aMgr = mySelector->viewer()->getViewManager();
495 #ifdef SALOME_PATCH_FOR_CTRL_WHEEL
496 OCCViewer_Viewer* aViewer = dynamic_cast<OCCViewer_Viewer*>(aMgr->getViewModel());
498 aViewer->setUseLocalSelection(toActivate);
500 QVector<SUIT_ViewWindow*> aViews = aMgr->getViews();
502 foreach (SUIT_ViewWindow* aView, aViews) {
503 OCCViewer_ViewFrame* aOCCView = dynamic_cast<OCCViewer_ViewFrame*>(aView);
504 OCCViewer_ViewWindow* aWnd = aOCCView->getView(OCCViewer_ViewFrame::MAIN_VIEW);
505 connect(aWnd, SIGNAL(vpTransformationFinished(OCCViewer_ViewWindow::OperationType)),
506 this, SLOT(onViewTransformed(OCCViewer_ViewWindow::OperationType)));
507 reconnectActions(aWnd, true);
510 foreach (SUIT_ViewWindow* aView, aViews) {
511 OCCViewer_ViewFrame* aOCCView = dynamic_cast<OCCViewer_ViewFrame*>(aView);
512 OCCViewer_ViewWindow* aWnd = aOCCView->getView(OCCViewer_ViewFrame::MAIN_VIEW);
513 disconnect((OCCViewer_ViewWindow*)aWnd,
514 SIGNAL(vpTransformationFinished(OCCViewer_ViewWindow::OperationType)),
515 this, SLOT(onViewTransformed(OCCViewer_ViewWindow::OperationType)));
516 reconnectActions(aWnd, false);
521 bool SHAPERGUI_SalomeViewer::isColorScaleVisible() const
524 return mySelector->viewer()->isColorScaleVisible();
529 void SHAPERGUI_SalomeViewer::setColorScaleShown(bool on)
532 mySelector->viewer()->setColorScaleShown(on);
536 void SHAPERGUI_SalomeViewer::setColorScalePosition(double theX, double theY)
539 QWidget* aWindow = activeViewPort();
540 mySelector->viewer()->getColorScale()->SetPosition(aWindow->width() * theX,
541 aWindow->height() * theY);
545 void SHAPERGUI_SalomeViewer::setColorScaleSize(double theW, double theH)
548 QWidget* aWindow = activeViewPort();
549 mySelector->viewer()->getColorScale()->SetSize(aWindow->width() * theW,
550 aWindow->height() * theH);
554 void SHAPERGUI_SalomeViewer::setColorScaleRange(double theMin, double theMax)
557 mySelector->viewer()->getColorScale()->SetRange(theMin, theMax);
561 void SHAPERGUI_SalomeViewer::setColorScaleIntervals(int theNb)
564 mySelector->viewer()->getColorScale()->SetNumberOfIntervals(theNb);
568 void SHAPERGUI_SalomeViewer::setColorScaleTextHeigth(int theH)
571 mySelector->viewer()->getColorScale()->SetTextHeight(theH);
575 void SHAPERGUI_SalomeViewer::setColorScaleTextColor(const QColor& theColor)
578 Quantity_Color aColor(theColor.redF(), theColor.greenF(), theColor.blueF(), Quantity_TOC_RGB);
579 mySelector->viewer()->getColorScale()->SetColor(aColor);
583 void SHAPERGUI_SalomeViewer::setColorScaleTitle(const QString& theText)
586 mySelector->viewer()->getColorScale()->SetTitle(theText.toStdString().c_str());
590 void SHAPERGUI_SalomeViewer::setFitter(OCCViewer_Fitter* theFitter)
593 mySelector->viewer()->setFitter(theFitter);
596 OCCViewer_Fitter* SHAPERGUI_SalomeViewer::fitter() const
599 return mySelector->viewer()->fitter();
604 //void SHAPERGUI_SalomeViewer::Zfitall()
606 // if (!mySelector || !mySelector->viewer())
608 // SUIT_ViewManager* aMgr = mySelector->viewer()->getViewManager();
609 // /// WORKAROUND for issue #1798. SUIT_ViewManager::closeAllViews() should nullify myActiveView
610 // /// As a result, we need to check views count in manager
611 // if (aMgr->getViews().size() > 0) {
612 // OCCViewer_ViewFrame* aView = dynamic_cast<OCCViewer_ViewFrame*>(aMgr->getActiveView());
614 // OCCViewer_ViewWindow* aWnd = aView->getView(OCCViewer_ViewFrame::MAIN_VIEW);
615 // Handle(V3d_View) aView3d = aWnd->getViewPort()->getView();
616 // aView3d->ZFitAll();
617 // if (aView3d->Depth() < 0.1)
618 // aView3d->DepthFitAll();