1 // Copyright (C) 2014-2022 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 <Standard_Version.hxx>
32 #include <SelectMgr_ListIteratorOfListOfFilter.hxx>
34 #include <QMouseEvent>
35 #include <QContextMenuEvent>
38 #if OCC_VERSION_HEX < 0x070400
39 #define SALOME_PATCH_FOR_CTRL_WHEEL
42 SHAPERGUI_SalomeView::SHAPERGUI_SalomeView(OCCViewer_Viewer* theViewer)
43 : ModuleBase_IViewWindow(), myCurrentView(0)
49 Handle(V3d_View) SHAPERGUI_SalomeView::v3dView() const
51 Handle(V3d_View) aView;
53 OCCViewer_ViewWindow* aWnd = static_cast<OCCViewer_ViewWindow*>(myCurrentView);
54 aView = aWnd->getViewPort()->getView();
59 QWidget* SHAPERGUI_SalomeView::viewPort() const
61 QWidget* aViewPort = 0;
63 OCCViewer_ViewWindow* aWnd = static_cast<OCCViewer_ViewWindow*>(myCurrentView);
64 aViewPort = aWnd->getViewPort();
69 //**********************************************
70 //**********************************************
71 //**********************************************
75 SHAPERGUI_SalomeViewer::SHAPERGUI_SalomeViewer(QObject* theParent)
76 : ModuleBase_IViewer(theParent),
77 mySelector(0), myView(0), myIsSelectionChanged(false)
81 SHAPERGUI_SalomeViewer::~SHAPERGUI_SalomeViewer()
88 //**********************************************
89 Handle(AIS_InteractiveContext) SHAPERGUI_SalomeViewer::AISContext() const
91 if (mySelector && mySelector->viewer())
92 return mySelector->viewer()->getAISContext();
93 Handle(AIS_InteractiveContext) aNull;
97 //**********************************************
98 Handle(V3d_Viewer) SHAPERGUI_SalomeViewer::v3dViewer() const
101 return mySelector->viewer()->getViewer3d();
102 return Handle(V3d_Viewer)();
105 //**********************************************
106 Handle(AIS_Trihedron) SHAPERGUI_SalomeViewer::trihedron() const
108 return mySelector->viewer()->getTrihedron();
111 //**********************************************
112 Handle(V3d_View) SHAPERGUI_SalomeViewer::activeView() const
115 OCCViewer_Viewer* aViewer = mySelector->viewer();
116 SUIT_ViewManager* aMgr = aViewer->getViewManager();
117 OCCViewer_ViewWindow* aWnd = static_cast<OCCViewer_ViewWindow*>(aMgr->getActiveView());
118 return aWnd->getViewPort()->getView();
120 return Handle(V3d_View)();
123 //**********************************************
124 QWidget* SHAPERGUI_SalomeViewer::activeViewPort() const
126 QWidget* aViewPort = 0;
128 OCCViewer_Viewer* aViewer = mySelector->viewer();
129 SUIT_ViewManager* aMgr = aViewer->getViewManager();
130 OCCViewer_ViewWindow* aWnd = static_cast<OCCViewer_ViewWindow*>(aMgr->getActiveView());
131 aViewPort = aWnd->getViewPort();
136 //**********************************************
137 void SHAPERGUI_SalomeViewer::setSelector(SHAPERGUI_OCCSelector* theSel)
140 if (mySelector == theSel)
143 mySelector->viewer()->getViewManager()->disconnect(this);
144 OCCViewer_Viewer* aViewer = mySelector->viewer();
146 aViewer->disconnect(this);
152 OCCViewer_Viewer* aViewer = mySelector->viewer();
153 SUIT_ViewManager* aMgr = aViewer->getViewManager();
155 myView = new SHAPERGUI_SalomeView(mySelector->viewer());
157 // TODO: Provide ModuleBase_IViewWindow interface
158 connect(aMgr, SIGNAL(lastViewClosed(SUIT_ViewManager*)), this, SIGNAL(lastViewClosed()));
160 connect(aMgr, SIGNAL(tryCloseView(SUIT_ViewWindow*)),
161 this, SLOT(onTryCloseView(SUIT_ViewWindow*)));
162 connect(aMgr, SIGNAL(deleteView(SUIT_ViewWindow*)),
163 this, SLOT(onDeleteView(SUIT_ViewWindow*)));
164 connect(aMgr, SIGNAL(viewCreated(SUIT_ViewWindow*)),
165 this, SLOT(onViewCreated(SUIT_ViewWindow*)));
166 connect(aMgr, SIGNAL(activated(SUIT_ViewManager*)),
167 this, SLOT(onActivated(SUIT_ViewManager*)));
169 connect(aMgr, SIGNAL(mousePress(SUIT_ViewWindow*, QMouseEvent*)), this,
170 SLOT(onMousePress(SUIT_ViewWindow*, QMouseEvent*)));
171 connect(aMgr, SIGNAL(mouseRelease(SUIT_ViewWindow*, QMouseEvent*)), this,
172 SLOT(onMouseRelease(SUIT_ViewWindow*, QMouseEvent*)));
173 connect(aMgr, SIGNAL(mouseDoubleClick(SUIT_ViewWindow*, QMouseEvent*)), this,
174 SLOT(onMouseDoubleClick(SUIT_ViewWindow*, QMouseEvent*)));
175 connect(aMgr, SIGNAL(mouseMove(SUIT_ViewWindow*, QMouseEvent*)), this,
176 SLOT(onMouseMove(SUIT_ViewWindow*, QMouseEvent*)));
178 connect(aMgr, SIGNAL(keyPress(SUIT_ViewWindow*, QKeyEvent*)), this,
179 SLOT(onKeyPress(SUIT_ViewWindow*, QKeyEvent*)));
180 connect(aMgr, SIGNAL(keyRelease(SUIT_ViewWindow*, QKeyEvent*)), this,
181 SLOT(onKeyRelease(SUIT_ViewWindow*, QKeyEvent*)));
183 connect(aViewer, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
186 //**********************************************
187 void SHAPERGUI_SalomeViewer::onSelectionChanged()
189 // Selection event must be sent only after mouse release
190 myIsSelectionChanged = true;
193 //**********************************************
194 void SHAPERGUI_SalomeViewer::onMousePress(SUIT_ViewWindow* theView, QMouseEvent* theEvent)
196 myView->setCurrentView(theView);
197 emit mousePress(myView, theEvent);
200 //**********************************************
201 void SHAPERGUI_SalomeViewer::onMouseRelease(SUIT_ViewWindow* theView, QMouseEvent* theEvent)
203 myView->setCurrentView(theView);
204 emit mouseRelease(myView, theEvent);
205 if (myIsSelectionChanged) {
206 emit selectionChanged();
207 myIsSelectionChanged = false;
211 //**********************************************
212 void SHAPERGUI_SalomeViewer::onMouseDoubleClick(SUIT_ViewWindow* theView, QMouseEvent* theEvent)
214 myView->setCurrentView(theView);
215 emit mouseDoubleClick(myView, theEvent);
218 //**********************************************
219 void SHAPERGUI_SalomeViewer::onMouseMove(SUIT_ViewWindow* theView, QMouseEvent* theEvent)
221 myView->setCurrentView(theView);
222 emit mouseMove(myView, theEvent);
225 //**********************************************
226 bool SHAPERGUI_SalomeViewer::canDragByMouse() const
228 OCCViewer_Viewer* aViewer = mySelector->viewer();
229 SUIT_ViewWindow* aWnd = aViewer->getViewManager()->getActiveView();
230 OCCViewer_ViewWindow* aViewWnd = dynamic_cast<OCCViewer_ViewWindow*>(aWnd);
232 return (aViewWnd->interactionStyle() == 0);
238 //**********************************************
239 void SHAPERGUI_SalomeViewer::onKeyPress(SUIT_ViewWindow* theView, QKeyEvent* theEvent)
241 emit keyPress(myView, theEvent);
244 //**********************************************
245 void SHAPERGUI_SalomeViewer::onKeyRelease(SUIT_ViewWindow* theView, QKeyEvent* theEvent)
247 emit keyRelease(myView, theEvent);
250 //**********************************************
251 void SHAPERGUI_SalomeViewer::onTryCloseView(SUIT_ViewWindow*)
253 emit tryCloseView(myView);
256 //**********************************************
257 void SHAPERGUI_SalomeViewer::onDeleteView(SUIT_ViewWindow*)
259 if(myWindowScale.contains(myView->v3dView()))
260 myWindowScale.remove(myView->v3dView());
261 emit deleteView(myView);
264 //**********************************************
265 void SHAPERGUI_SalomeViewer::onViewCreated(SUIT_ViewWindow* theView)
267 myView->setCurrentView(theView);
269 OCCViewer_ViewFrame* aView = dynamic_cast<OCCViewer_ViewFrame*>(theView);
271 OCCViewer_ViewWindow* aWnd = aView->getView(OCCViewer_ViewFrame::MAIN_VIEW);
273 connect(aWnd, SIGNAL(vpTransformationFinished(OCCViewer_ViewWindow::OperationType)),
274 this, SLOT(onViewTransformed(OCCViewer_ViewWindow::OperationType)));
275 OCCViewer_ViewPort3d* aViewPort = aWnd->getViewPort();
277 connect(aViewPort, SIGNAL(vpMapped(OCCViewer_ViewPort3d*)), this, SLOT(onViewPortMapped()));
279 reconnectActions(aWnd, true);
281 myWindowScale.insert(aView->getViewPort()->getView(),
282 aView->getViewPort()->getView()->Camera()->Scale());
284 QTimer::singleShot(10, this, SLOT(onAfterViewCreated()));
286 emit viewCreated(myView);
289 //**********************************************
290 void SHAPERGUI_SalomeViewer::onAfterViewCreated()
292 // Update trihedron and dimension arrows
293 emit onViewTransformed(OCCViewer_ViewWindow::ZOOMVIEW);
297 //**********************************************
298 void SHAPERGUI_SalomeViewer::onActivated(SUIT_ViewManager* theMgr)
300 myView->setCurrentView(theMgr->getActiveView());
301 emit activated(myView);
304 //**********************************************
305 void SHAPERGUI_SalomeViewer::enableSelection(bool isEnabled)
308 if (mySelector->viewer()->isSelectionEnabled() != isEnabled)
309 mySelector->viewer()->enableSelection(isEnabled);
310 // The enableSelection() in SALOME 7.5 cause of forced Viewer update(we have blinking)
311 // After this is corrected, the first row should be recommented, the last - removed
312 //mySelector->viewer()->setInteractionStyle(isEnabled ? SUIT_ViewModel::STANDARD
313 // : SUIT_ViewModel::KEY_FREE);
316 //**********************************************
317 bool SHAPERGUI_SalomeViewer::isSelectionEnabled() const
320 return mySelector->viewer()->isSelectionEnabled();
324 //**********************************************
325 void SHAPERGUI_SalomeViewer::enableMultiselection(bool isEnable)
328 mySelector->viewer()->enableMultiselection(isEnable);
331 //**********************************************
332 bool SHAPERGUI_SalomeViewer::isMultiSelectionEnabled() const
335 return mySelector->viewer()->isMultiSelectionEnabled();
339 //**********************************************
340 bool SHAPERGUI_SalomeViewer::enableDrawMode(bool isEnabled)
342 // TODO: Has to be replaced when SALOME patch become available
344 return mySelector->viewer()->enableDrawMode(isEnabled);
348 //**********************************************
349 void SHAPERGUI_SalomeViewer::reconnectActions(SUIT_ViewWindow* theWindow,
350 const bool theUseSHAPERSlot)
352 OCCViewer_ViewWindow* aWindow = dynamic_cast<OCCViewer_ViewWindow*>(theWindow);
356 QAction* anAction = theWindow->toolMgr()->action(OCCViewer_ViewWindow::TrihedronShowId);
360 if (theUseSHAPERSlot) {
361 anAction->disconnect(anAction, SIGNAL(toggled(bool)),
362 theWindow, SLOT(onTrihedronShow(bool)));
363 anAction->connect(anAction, SIGNAL(toggled(bool)),
364 this, SIGNAL(trihedronVisibilityChanged(bool)));
367 anAction->connect(anAction, SIGNAL(toggled(bool)),
368 theWindow, SLOT(onTrihedronShow(bool)));
369 anAction->disconnect(anAction, SIGNAL(toggled(bool)),
370 this, SIGNAL(trihedronVisibilityChanged(bool)));
374 //**********************************************
375 void SHAPERGUI_SalomeViewer::fitAll()
378 SUIT_ViewManager* aMgr = mySelector->viewer()->getViewManager();
379 OCCViewer_ViewFrame* aVFrame = dynamic_cast<OCCViewer_ViewFrame*>(aMgr->getActiveView());
386 //**********************************************
387 void SHAPERGUI_SalomeViewer::eraseAll()
389 Handle(AIS_InteractiveContext) aContext = AISContext();
390 if (aContext.IsNull())
392 AIS_ListOfInteractive aList;
393 aContext->DisplayedObjects(aList);
394 AIS_ListIteratorOfListOfInteractive aLIt;
395 Handle(AIS_InteractiveObject) anAISIO;
396 for (aLIt.Initialize(aList); aLIt.More(); aLIt.Next()) {
397 anAISIO = aLIt.Value();
398 Handle(Standard_Type) aType = anAISIO->DynamicType();
399 if (anAISIO->IsKind(STANDARD_TYPE(SALOME_AISShape))) {
400 aContext->Erase(anAISIO, false);
405 //**********************************************
406 void SHAPERGUI_SalomeViewer::setViewProjection(double theX, double theY,
407 double theZ, double theTwist)
412 SUIT_ViewManager* aMgr = mySelector->viewer()->getViewManager();
413 OCCViewer_ViewFrame* aVFrame = dynamic_cast<OCCViewer_ViewFrame*>(aMgr->getActiveView());
415 Handle(V3d_View) aView3d = aVFrame->getViewPort()->getView();
416 if (!aView3d.IsNull()) {
417 aView3d->SetProj(theX, theY, theZ);
418 aView3d->SetTwist( theTwist );
419 aView3d->FitAll(0.01, false);
420 //aView3d->SetZSize(0.);
421 if (aView3d->Depth() < 0.1)
422 aView3d->DepthFitAll();
427 //***************************************
428 void SHAPERGUI_SalomeViewer::addSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
430 Handle(AIS_InteractiveContext) aContext = AISContext();
431 if (!aContext.IsNull()) {
432 aContext->AddFilter(theFilter);
436 //***************************************
437 void SHAPERGUI_SalomeViewer::removeSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
439 Handle(AIS_InteractiveContext) aContext = AISContext();
440 if (!aContext.IsNull()) {
441 aContext->RemoveFilter(theFilter);
445 //***************************************
446 bool SHAPERGUI_SalomeViewer::hasSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
448 bool aFoundFilter = false;
449 Handle(AIS_InteractiveContext) aContext = AISContext();
450 if (!aContext.IsNull()) {
451 const SelectMgr_ListOfFilter& aFilters = aContext->Filters();
452 SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
453 for (; aIt.More() && !aFoundFilter; aIt.Next()) {
454 aFoundFilter = theFilter.get() == aIt.Value().get();
460 //***************************************
461 void SHAPERGUI_SalomeViewer::clearSelectionFilters()
463 Handle(AIS_InteractiveContext) aContext = AISContext();
464 if (!aContext.IsNull()) {
465 aContext->RemoveFilters();
469 //***************************************
470 void SHAPERGUI_SalomeViewer::update()
472 Handle(AIS_InteractiveContext) aContext = AISContext();
473 if (!aContext.IsNull()) {
474 aContext->UpdateCurrentViewer();
478 //***************************************
479 void SHAPERGUI_SalomeViewer::onViewTransformed(OCCViewer_ViewWindow::OperationType theType)
481 emit viewTransformed((int) theType);
484 //***************************************
485 void SHAPERGUI_SalomeViewer::onViewPortMapped()
487 emit trihedronVisibilityChanged(true);
490 //***************************************
491 void SHAPERGUI_SalomeViewer::activateViewer(bool toActivate)
493 if (!mySelector || !mySelector->viewer())
495 SUIT_ViewManager* aMgr = mySelector->viewer()->getViewManager();
496 #ifdef SALOME_PATCH_FOR_CTRL_WHEEL
497 OCCViewer_Viewer* aViewer = dynamic_cast<OCCViewer_Viewer*>(aMgr->getViewModel());
499 aViewer->setUseLocalSelection(toActivate);
501 QVector<SUIT_ViewWindow*> aViews = aMgr->getViews();
503 foreach (SUIT_ViewWindow* aView, aViews) {
504 OCCViewer_ViewFrame* aOCCView = dynamic_cast<OCCViewer_ViewFrame*>(aView);
505 OCCViewer_ViewWindow* aWnd = aOCCView->getView(OCCViewer_ViewFrame::MAIN_VIEW);
506 connect(aWnd, SIGNAL(vpTransformationFinished(OCCViewer_ViewWindow::OperationType)),
507 this, SLOT(onViewTransformed(OCCViewer_ViewWindow::OperationType)));
508 reconnectActions(aWnd, true);
511 foreach (SUIT_ViewWindow* aView, aViews) {
512 OCCViewer_ViewFrame* aOCCView = dynamic_cast<OCCViewer_ViewFrame*>(aView);
513 OCCViewer_ViewWindow* aWnd = aOCCView->getView(OCCViewer_ViewFrame::MAIN_VIEW);
514 disconnect((OCCViewer_ViewWindow*)aWnd,
515 SIGNAL(vpTransformationFinished(OCCViewer_ViewWindow::OperationType)),
516 this, SLOT(onViewTransformed(OCCViewer_ViewWindow::OperationType)));
517 reconnectActions(aWnd, false);
522 bool SHAPERGUI_SalomeViewer::isColorScaleVisible() const
525 return mySelector->viewer()->isColorScaleVisible();
530 void SHAPERGUI_SalomeViewer::setColorScaleShown(bool on)
533 mySelector->viewer()->setColorScaleShown(on);
537 void SHAPERGUI_SalomeViewer::setColorScalePosition(double theX, double theY)
540 QWidget* aWindow = activeViewPort();
541 mySelector->viewer()->getColorScale()->SetPosition(aWindow->width() * theX,
542 aWindow->height() * theY);
546 void SHAPERGUI_SalomeViewer::setColorScaleSize(double theW, double theH)
549 QWidget* aWindow = activeViewPort();
550 mySelector->viewer()->getColorScale()->SetSize(aWindow->width() * theW,
551 aWindow->height() * theH);
555 void SHAPERGUI_SalomeViewer::setColorScaleRange(double theMin, double theMax)
558 mySelector->viewer()->getColorScale()->SetRange(theMin, theMax);
562 void SHAPERGUI_SalomeViewer::setColorScaleIntervals(int theNb)
565 mySelector->viewer()->getColorScale()->SetNumberOfIntervals(theNb);
569 void SHAPERGUI_SalomeViewer::setColorScaleTextHeigth(int theH)
572 mySelector->viewer()->getColorScale()->SetTextHeight(theH);
576 void SHAPERGUI_SalomeViewer::setColorScaleTextColor(const QColor& theColor)
579 Quantity_Color aColor(theColor.redF(), theColor.greenF(), theColor.blueF(), Quantity_TOC_RGB);
580 mySelector->viewer()->getColorScale()->SetColor(aColor);
584 void SHAPERGUI_SalomeViewer::setColorScaleTitle(const QString& theText)
587 mySelector->viewer()->getColorScale()->SetTitle(theText.toStdString().c_str());
591 void SHAPERGUI_SalomeViewer::setFitter(OCCViewer_Fitter* theFitter)
594 mySelector->viewer()->setFitter(theFitter);
597 OCCViewer_Fitter* SHAPERGUI_SalomeViewer::fitter() const
600 return mySelector->viewer()->fitter();
605 //void SHAPERGUI_SalomeViewer::Zfitall()
607 // if (!mySelector || !mySelector->viewer())
609 // SUIT_ViewManager* aMgr = mySelector->viewer()->getViewManager();
610 // /// WORKAROUND for issue #1798. SUIT_ViewManager::closeAllViews() should nullify myActiveView
611 // /// As a result, we need to check views count in manager
612 // if (aMgr->getViews().size() > 0) {
613 // OCCViewer_ViewFrame* aView = dynamic_cast<OCCViewer_ViewFrame*>(aMgr->getActiveView());
615 // OCCViewer_ViewWindow* aWnd = aView->getView(OCCViewer_ViewFrame::MAIN_VIEW);
616 // Handle(V3d_View) aView3d = aWnd->getViewPort()->getView();
617 // aView3d->ZFitAll();
618 // if (aView3d->Depth() < 0.1)
619 // aView3d->DepthFitAll();