1 // Copyright (C) 2014-2019 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>
36 #if OCC_VERSION_HEX < 0x070400
37 #define SALOME_PATCH_FOR_CTRL_WHEEL
40 SHAPERGUI_SalomeView::SHAPERGUI_SalomeView(OCCViewer_Viewer* theViewer)
41 : ModuleBase_IViewWindow(), myCurrentView(0)
47 Handle(V3d_View) SHAPERGUI_SalomeView::v3dView() const
49 Handle(V3d_View) aView;
51 OCCViewer_ViewWindow* aWnd = static_cast<OCCViewer_ViewWindow*>(myCurrentView);
52 aView = aWnd->getViewPort()->getView();
57 QWidget* SHAPERGUI_SalomeView::viewPort() const
59 QWidget* aViewPort = 0;
61 OCCViewer_ViewWindow* aWnd = static_cast<OCCViewer_ViewWindow*>(myCurrentView);
62 aViewPort = aWnd->getViewPort();
67 //**********************************************
68 //**********************************************
69 //**********************************************
73 SHAPERGUI_SalomeViewer::SHAPERGUI_SalomeViewer(QObject* theParent)
74 : ModuleBase_IViewer(theParent),
75 mySelector(0), myView(0), myIsSelectionChanged(false)
79 SHAPERGUI_SalomeViewer::~SHAPERGUI_SalomeViewer()
86 //**********************************************
87 Handle(AIS_InteractiveContext) SHAPERGUI_SalomeViewer::AISContext() const
89 if (mySelector && mySelector->viewer())
90 return mySelector->viewer()->getAISContext();
91 Handle(AIS_InteractiveContext) aNull;
95 //**********************************************
96 Handle(V3d_Viewer) SHAPERGUI_SalomeViewer::v3dViewer() const
99 return mySelector->viewer()->getViewer3d();
100 return Handle(V3d_Viewer)();
103 //**********************************************
104 Handle(AIS_Trihedron) SHAPERGUI_SalomeViewer::trihedron() const
106 return mySelector->viewer()->getTrihedron();
109 //**********************************************
110 Handle(V3d_View) SHAPERGUI_SalomeViewer::activeView() const
113 OCCViewer_Viewer* aViewer = mySelector->viewer();
114 SUIT_ViewManager* aMgr = aViewer->getViewManager();
115 OCCViewer_ViewWindow* aWnd = static_cast<OCCViewer_ViewWindow*>(aMgr->getActiveView());
116 return aWnd->getViewPort()->getView();
118 return Handle(V3d_View)();
121 //**********************************************
122 QWidget* SHAPERGUI_SalomeViewer::activeViewPort() const
124 QWidget* aViewPort = 0;
126 OCCViewer_Viewer* aViewer = mySelector->viewer();
127 SUIT_ViewManager* aMgr = aViewer->getViewManager();
128 OCCViewer_ViewWindow* aWnd = static_cast<OCCViewer_ViewWindow*>(aMgr->getActiveView());
129 aViewPort = aWnd->getViewPort();
134 //**********************************************
135 void SHAPERGUI_SalomeViewer::setSelector(SHAPERGUI_OCCSelector* theSel)
138 if (mySelector == theSel)
141 mySelector->viewer()->getViewManager()->disconnect(this);
142 OCCViewer_Viewer* aViewer = mySelector->viewer();
144 aViewer->disconnect(this);
150 OCCViewer_Viewer* aViewer = mySelector->viewer();
151 SUIT_ViewManager* aMgr = aViewer->getViewManager();
153 myView = new SHAPERGUI_SalomeView(mySelector->viewer());
155 // TODO: Provide ModuleBase_IViewWindow interface
156 connect(aMgr, SIGNAL(lastViewClosed(SUIT_ViewManager*)), this, SIGNAL(lastViewClosed()));
158 connect(aMgr, SIGNAL(tryCloseView(SUIT_ViewWindow*)),
159 this, SLOT(onTryCloseView(SUIT_ViewWindow*)));
160 connect(aMgr, SIGNAL(deleteView(SUIT_ViewWindow*)),
161 this, SLOT(onDeleteView(SUIT_ViewWindow*)));
162 connect(aMgr, SIGNAL(viewCreated(SUIT_ViewWindow*)),
163 this, SLOT(onViewCreated(SUIT_ViewWindow*)));
164 connect(aMgr, SIGNAL(activated(SUIT_ViewManager*)),
165 this, SLOT(onActivated(SUIT_ViewManager*)));
167 connect(aMgr, SIGNAL(mousePress(SUIT_ViewWindow*, QMouseEvent*)), this,
168 SLOT(onMousePress(SUIT_ViewWindow*, QMouseEvent*)));
169 connect(aMgr, SIGNAL(mouseRelease(SUIT_ViewWindow*, QMouseEvent*)), this,
170 SLOT(onMouseRelease(SUIT_ViewWindow*, QMouseEvent*)));
171 connect(aMgr, SIGNAL(mouseDoubleClick(SUIT_ViewWindow*, QMouseEvent*)), this,
172 SLOT(onMouseDoubleClick(SUIT_ViewWindow*, QMouseEvent*)));
173 connect(aMgr, SIGNAL(mouseMove(SUIT_ViewWindow*, QMouseEvent*)), this,
174 SLOT(onMouseMove(SUIT_ViewWindow*, QMouseEvent*)));
176 connect(aMgr, SIGNAL(keyPress(SUIT_ViewWindow*, QKeyEvent*)), this,
177 SLOT(onKeyPress(SUIT_ViewWindow*, QKeyEvent*)));
178 connect(aMgr, SIGNAL(keyRelease(SUIT_ViewWindow*, QKeyEvent*)), this,
179 SLOT(onKeyRelease(SUIT_ViewWindow*, QKeyEvent*)));
181 connect(aViewer, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
184 //**********************************************
185 void SHAPERGUI_SalomeViewer::onSelectionChanged()
187 // Selection event must be sent only after mouse release
188 myIsSelectionChanged = true;
191 //**********************************************
192 void SHAPERGUI_SalomeViewer::onMousePress(SUIT_ViewWindow* theView, QMouseEvent* theEvent)
194 myView->setCurrentView(theView);
195 emit mousePress(myView, theEvent);
198 //**********************************************
199 void SHAPERGUI_SalomeViewer::onMouseRelease(SUIT_ViewWindow* theView, QMouseEvent* theEvent)
201 myView->setCurrentView(theView);
202 emit mouseRelease(myView, theEvent);
203 if (myIsSelectionChanged) {
204 emit selectionChanged();
205 myIsSelectionChanged = false;
209 //**********************************************
210 void SHAPERGUI_SalomeViewer::onMouseDoubleClick(SUIT_ViewWindow* theView, QMouseEvent* theEvent)
212 myView->setCurrentView(theView);
213 emit mouseDoubleClick(myView, theEvent);
216 //**********************************************
217 void SHAPERGUI_SalomeViewer::onMouseMove(SUIT_ViewWindow* theView, QMouseEvent* theEvent)
219 myView->setCurrentView(theView);
220 emit mouseMove(myView, theEvent);
223 //**********************************************
224 bool SHAPERGUI_SalomeViewer::canDragByMouse() const
226 OCCViewer_Viewer* aViewer = mySelector->viewer();
227 SUIT_ViewWindow* aWnd = aViewer->getViewManager()->getActiveView();
228 OCCViewer_ViewWindow* aViewWnd = dynamic_cast<OCCViewer_ViewWindow*>(aWnd);
230 return (aViewWnd->interactionStyle() == 0);
236 //**********************************************
237 void SHAPERGUI_SalomeViewer::onKeyPress(SUIT_ViewWindow* theView, QKeyEvent* theEvent)
239 emit keyPress(myView, theEvent);
242 //**********************************************
243 void SHAPERGUI_SalomeViewer::onKeyRelease(SUIT_ViewWindow* theView, QKeyEvent* theEvent)
245 emit keyRelease(myView, theEvent);
248 //**********************************************
249 void SHAPERGUI_SalomeViewer::onTryCloseView(SUIT_ViewWindow*)
251 emit tryCloseView(myView);
254 //**********************************************
255 void SHAPERGUI_SalomeViewer::onDeleteView(SUIT_ViewWindow*)
257 if(myWindowScale.contains(myView->v3dView()))
258 myWindowScale.remove(myView->v3dView());
259 emit deleteView(myView);
262 //**********************************************
263 void SHAPERGUI_SalomeViewer::onViewCreated(SUIT_ViewWindow* theView)
265 myView->setCurrentView(theView);
267 OCCViewer_ViewFrame* aView = dynamic_cast<OCCViewer_ViewFrame*>(theView);
269 OCCViewer_ViewWindow* aWnd = aView->getView(OCCViewer_ViewFrame::MAIN_VIEW);
271 connect(aWnd, SIGNAL(vpTransformationFinished(OCCViewer_ViewWindow::OperationType)),
272 this, SLOT(onViewTransformed(OCCViewer_ViewWindow::OperationType)));
273 OCCViewer_ViewPort3d* aViewPort = aWnd->getViewPort();
275 connect(aViewPort, SIGNAL(vpMapped(OCCViewer_ViewPort3d*)), this, SLOT(onViewPortMapped()));
277 reconnectActions(aWnd, true);
279 myWindowScale.insert(aView->getViewPort()->getView(),
280 aView->getViewPort()->getView()->Camera()->Scale());
282 emit viewCreated(myView);
287 //**********************************************
288 void SHAPERGUI_SalomeViewer::onActivated(SUIT_ViewManager* theMgr)
290 myView->setCurrentView(theMgr->getActiveView());
291 emit activated(myView);
294 //**********************************************
295 void SHAPERGUI_SalomeViewer::enableSelection(bool isEnabled)
298 if (mySelector->viewer()->isSelectionEnabled() != isEnabled)
299 mySelector->viewer()->enableSelection(isEnabled);
300 // The enableSelection() in SALOME 7.5 cause of forced Viewer update(we have blinking)
301 // After this is corrected, the first row should be recommented, the last - removed
302 //mySelector->viewer()->setInteractionStyle(isEnabled ? SUIT_ViewModel::STANDARD
303 // : SUIT_ViewModel::KEY_FREE);
306 //**********************************************
307 bool SHAPERGUI_SalomeViewer::isSelectionEnabled() const
310 return mySelector->viewer()->isSelectionEnabled();
314 //**********************************************
315 void SHAPERGUI_SalomeViewer::enableMultiselection(bool isEnable)
318 mySelector->viewer()->enableMultiselection(isEnable);
321 //**********************************************
322 bool SHAPERGUI_SalomeViewer::isMultiSelectionEnabled() const
325 return mySelector->viewer()->isMultiSelectionEnabled();
329 //**********************************************
330 bool SHAPERGUI_SalomeViewer::enableDrawMode(bool isEnabled)
332 // TODO: Has to be replaced when SALOME patch become available
334 return mySelector->viewer()->enableDrawMode(isEnabled);
338 //**********************************************
339 void SHAPERGUI_SalomeViewer::reconnectActions(SUIT_ViewWindow* theWindow,
340 const bool theUseSHAPERSlot)
342 OCCViewer_ViewWindow* aWindow = dynamic_cast<OCCViewer_ViewWindow*>(theWindow);
346 QAction* anAction = theWindow->toolMgr()->action(OCCViewer_ViewWindow::TrihedronShowId);
350 if (theUseSHAPERSlot) {
351 anAction->disconnect(anAction, SIGNAL(toggled(bool)),
352 theWindow, SLOT(onTrihedronShow(bool)));
353 anAction->connect(anAction, SIGNAL(toggled(bool)),
354 this, SIGNAL(trihedronVisibilityChanged(bool)));
357 anAction->connect(anAction, SIGNAL(toggled(bool)),
358 theWindow, SLOT(onTrihedronShow(bool)));
359 anAction->disconnect(anAction, SIGNAL(toggled(bool)),
360 this, SIGNAL(trihedronVisibilityChanged(bool)));
364 //**********************************************
365 void SHAPERGUI_SalomeViewer::fitAll()
368 SUIT_ViewManager* aMgr = mySelector->viewer()->getViewManager();
369 OCCViewer_ViewFrame* aVFrame = dynamic_cast<OCCViewer_ViewFrame*>(aMgr->getActiveView());
376 //**********************************************
377 void SHAPERGUI_SalomeViewer::eraseAll()
379 Handle(AIS_InteractiveContext) aContext = AISContext();
380 if (aContext.IsNull())
382 AIS_ListOfInteractive aList;
383 aContext->DisplayedObjects(aList);
384 AIS_ListIteratorOfListOfInteractive aLIt;
385 Handle(AIS_InteractiveObject) anAISIO;
386 for (aLIt.Initialize(aList); aLIt.More(); aLIt.Next()) {
387 anAISIO = aLIt.Value();
388 Handle(Standard_Type) aType = anAISIO->DynamicType();
389 if (anAISIO->IsKind(STANDARD_TYPE(SALOME_AISShape))) {
390 aContext->Erase(anAISIO, false);
395 //**********************************************
396 void SHAPERGUI_SalomeViewer::setViewProjection(double theX, double theY,
397 double theZ, double theTwist)
402 SUIT_ViewManager* aMgr = mySelector->viewer()->getViewManager();
403 OCCViewer_ViewFrame* aVFrame = dynamic_cast<OCCViewer_ViewFrame*>(aMgr->getActiveView());
405 Handle(V3d_View) aView3d = aVFrame->getViewPort()->getView();
406 if (!aView3d.IsNull()) {
407 aView3d->SetProj(theX, theY, theZ);
408 aView3d->SetTwist( theTwist );
409 aView3d->FitAll(0.01, false);
410 //aView3d->SetZSize(0.);
411 if (aView3d->Depth() < 0.1)
412 aView3d->DepthFitAll();
417 //***************************************
418 void SHAPERGUI_SalomeViewer::addSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
420 Handle(AIS_InteractiveContext) aContext = AISContext();
421 if (!aContext.IsNull()) {
422 aContext->AddFilter(theFilter);
426 //***************************************
427 void SHAPERGUI_SalomeViewer::removeSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
429 Handle(AIS_InteractiveContext) aContext = AISContext();
430 if (!aContext.IsNull()) {
431 aContext->RemoveFilter(theFilter);
435 //***************************************
436 bool SHAPERGUI_SalomeViewer::hasSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
438 bool aFoundFilter = false;
439 Handle(AIS_InteractiveContext) aContext = AISContext();
440 if (!aContext.IsNull()) {
441 const SelectMgr_ListOfFilter& aFilters = aContext->Filters();
442 SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
443 for (; aIt.More() && !aFoundFilter; aIt.Next()) {
444 aFoundFilter = theFilter.get() == aIt.Value().get();
450 //***************************************
451 void SHAPERGUI_SalomeViewer::clearSelectionFilters()
453 Handle(AIS_InteractiveContext) aContext = AISContext();
454 if (!aContext.IsNull()) {
455 aContext->RemoveFilters();
459 //***************************************
460 void SHAPERGUI_SalomeViewer::update()
462 Handle(AIS_InteractiveContext) aContext = AISContext();
463 if (!aContext.IsNull()) {
464 aContext->UpdateCurrentViewer();
468 //***************************************
469 void SHAPERGUI_SalomeViewer::onViewTransformed(OCCViewer_ViewWindow::OperationType theType)
471 emit viewTransformed((int) theType);
474 //***************************************
475 void SHAPERGUI_SalomeViewer::onViewPortMapped()
477 emit trihedronVisibilityChanged(true);
480 //***************************************
481 void SHAPERGUI_SalomeViewer::activateViewer(bool toActivate)
483 if (!mySelector || !mySelector->viewer())
485 SUIT_ViewManager* aMgr = mySelector->viewer()->getViewManager();
486 #ifdef SALOME_PATCH_FOR_CTRL_WHEEL
487 OCCViewer_Viewer* aViewer = dynamic_cast<OCCViewer_Viewer*>(aMgr->getViewModel());
489 aViewer->setUseLocalSelection(toActivate);
491 QVector<SUIT_ViewWindow*> aViews = aMgr->getViews();
493 foreach (SUIT_ViewWindow* aView, aViews) {
494 OCCViewer_ViewFrame* aOCCView = dynamic_cast<OCCViewer_ViewFrame*>(aView);
495 OCCViewer_ViewWindow* aWnd = aOCCView->getView(OCCViewer_ViewFrame::MAIN_VIEW);
496 connect(aWnd, SIGNAL(vpTransformationFinished(OCCViewer_ViewWindow::OperationType)),
497 this, SLOT(onViewTransformed(OCCViewer_ViewWindow::OperationType)));
498 reconnectActions(aWnd, true);
501 foreach (SUIT_ViewWindow* aView, aViews) {
502 OCCViewer_ViewFrame* aOCCView = dynamic_cast<OCCViewer_ViewFrame*>(aView);
503 OCCViewer_ViewWindow* aWnd = aOCCView->getView(OCCViewer_ViewFrame::MAIN_VIEW);
504 disconnect((OCCViewer_ViewWindow*)aWnd,
505 SIGNAL(vpTransformationFinished(OCCViewer_ViewWindow::OperationType)),
506 this, SLOT(onViewTransformed(OCCViewer_ViewWindow::OperationType)));
507 reconnectActions(aWnd, false);
512 bool SHAPERGUI_SalomeViewer::isColorScaleVisible() const
515 return mySelector->viewer()->isColorScaleVisible();
520 void SHAPERGUI_SalomeViewer::setColorScaleShown(bool on)
523 mySelector->viewer()->setColorScaleShown(on);
527 void SHAPERGUI_SalomeViewer::setColorScalePosition(double theX, double theY)
530 QWidget* aWindow = activeViewPort();
531 mySelector->viewer()->getColorScale()->SetPosition(aWindow->width() * theX,
532 aWindow->height() * theY);
536 void SHAPERGUI_SalomeViewer::setColorScaleSize(double theW, double theH)
539 QWidget* aWindow = activeViewPort();
540 mySelector->viewer()->getColorScale()->SetSize(aWindow->width() * theW,
541 aWindow->height() * theH);
545 void SHAPERGUI_SalomeViewer::setColorScaleRange(double theMin, double theMax)
548 mySelector->viewer()->getColorScale()->SetRange(theMin, theMax);
552 void SHAPERGUI_SalomeViewer::setColorScaleIntervals(int theNb)
555 mySelector->viewer()->getColorScale()->SetNumberOfIntervals(theNb);
559 void SHAPERGUI_SalomeViewer::setColorScaleTextHeigth(int theH)
562 mySelector->viewer()->getColorScale()->SetTextHeight(theH);
566 void SHAPERGUI_SalomeViewer::setColorScaleTextColor(const QColor& theColor)
569 Quantity_Color aColor(theColor.redF(), theColor.greenF(), theColor.blueF(), Quantity_TOC_RGB);
570 mySelector->viewer()->getColorScale()->SetColor(aColor);
574 void SHAPERGUI_SalomeViewer::setColorScaleTitle(const QString& theText)
577 mySelector->viewer()->getColorScale()->SetTitle(theText.toStdString().c_str());
581 void SHAPERGUI_SalomeViewer::setFitter(OCCViewer_Fitter* theFitter)
584 mySelector->viewer()->setFitter(theFitter);
587 OCCViewer_Fitter* SHAPERGUI_SalomeViewer::fitter() const
590 return mySelector->viewer()->fitter();
595 //void SHAPERGUI_SalomeViewer::Zfitall()
597 // if (!mySelector || !mySelector->viewer())
599 // SUIT_ViewManager* aMgr = mySelector->viewer()->getViewManager();
600 // /// WORKAROUND for issue #1798. SUIT_ViewManager::closeAllViews() should nullify myActiveView
601 // /// As a result, we need to check views count in manager
602 // if (aMgr->getViews().size() > 0) {
603 // OCCViewer_ViewFrame* aView = dynamic_cast<OCCViewer_ViewFrame*>(aMgr->getActiveView());
605 // OCCViewer_ViewWindow* aWnd = aView->getView(OCCViewer_ViewFrame::MAIN_VIEW);
606 // Handle(V3d_View) aView3d = aWnd->getViewPort()->getView();
607 // aView3d->ZFitAll();
608 // if (aView3d->Depth() < 0.1)
609 // aView3d->DepthFitAll();