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 #define SALOME_PATCH_FOR_CTRL_WHEEL
38 SHAPERGUI_SalomeView::SHAPERGUI_SalomeView(OCCViewer_Viewer* theViewer)
39 : ModuleBase_IViewWindow(), myCurrentView(0)
45 Handle(V3d_View) SHAPERGUI_SalomeView::v3dView() const
47 Handle(V3d_View) aView;
49 OCCViewer_ViewWindow* aWnd = static_cast<OCCViewer_ViewWindow*>(myCurrentView);
50 aView = aWnd->getViewPort()->getView();
55 QWidget* SHAPERGUI_SalomeView::viewPort() const
57 QWidget* aViewPort = 0;
59 OCCViewer_ViewWindow* aWnd = static_cast<OCCViewer_ViewWindow*>(myCurrentView);
60 aViewPort = aWnd->getViewPort();
65 //**********************************************
66 //**********************************************
67 //**********************************************
71 SHAPERGUI_SalomeViewer::SHAPERGUI_SalomeViewer(QObject* theParent)
72 : ModuleBase_IViewer(theParent),
73 mySelector(0), myView(0), myIsSelectionChanged(false)
77 SHAPERGUI_SalomeViewer::~SHAPERGUI_SalomeViewer()
84 //**********************************************
85 Handle(AIS_InteractiveContext) SHAPERGUI_SalomeViewer::AISContext() const
87 if (mySelector && mySelector->viewer())
88 return mySelector->viewer()->getAISContext();
89 Handle(AIS_InteractiveContext) aNull;
93 //**********************************************
94 Handle(V3d_Viewer) SHAPERGUI_SalomeViewer::v3dViewer() const
97 return mySelector->viewer()->getViewer3d();
98 return Handle(V3d_Viewer)();
101 //**********************************************
102 Handle(AIS_Trihedron) SHAPERGUI_SalomeViewer::trihedron() const
104 return mySelector->viewer()->getTrihedron();
107 //**********************************************
108 Handle(V3d_View) SHAPERGUI_SalomeViewer::activeView() const
111 OCCViewer_Viewer* aViewer = mySelector->viewer();
112 SUIT_ViewManager* aMgr = aViewer->getViewManager();
113 OCCViewer_ViewWindow* aWnd = static_cast<OCCViewer_ViewWindow*>(aMgr->getActiveView());
114 return aWnd->getViewPort()->getView();
116 return Handle(V3d_View)();
119 //**********************************************
120 QWidget* SHAPERGUI_SalomeViewer::activeViewPort() const
122 QWidget* aViewPort = 0;
124 OCCViewer_Viewer* aViewer = mySelector->viewer();
125 SUIT_ViewManager* aMgr = aViewer->getViewManager();
126 OCCViewer_ViewWindow* aWnd = static_cast<OCCViewer_ViewWindow*>(aMgr->getActiveView());
127 aViewPort = aWnd->getViewPort();
132 //**********************************************
133 void SHAPERGUI_SalomeViewer::setSelector(SHAPERGUI_OCCSelector* theSel)
136 if (mySelector == theSel)
139 mySelector->viewer()->getViewManager()->disconnect(this);
140 OCCViewer_Viewer* aViewer = mySelector->viewer();
142 aViewer->disconnect(this);
148 OCCViewer_Viewer* aViewer = mySelector->viewer();
149 SUIT_ViewManager* aMgr = aViewer->getViewManager();
151 myView = new SHAPERGUI_SalomeView(mySelector->viewer());
153 // TODO: Provide ModuleBase_IViewWindow interface
154 connect(aMgr, SIGNAL(lastViewClosed(SUIT_ViewManager*)), this, SIGNAL(lastViewClosed()));
156 connect(aMgr, SIGNAL(tryCloseView(SUIT_ViewWindow*)),
157 this, SLOT(onTryCloseView(SUIT_ViewWindow*)));
158 connect(aMgr, SIGNAL(deleteView(SUIT_ViewWindow*)),
159 this, SLOT(onDeleteView(SUIT_ViewWindow*)));
160 connect(aMgr, SIGNAL(viewCreated(SUIT_ViewWindow*)),
161 this, SLOT(onViewCreated(SUIT_ViewWindow*)));
162 connect(aMgr, SIGNAL(activated(SUIT_ViewManager*)),
163 this, SLOT(onActivated(SUIT_ViewManager*)));
165 connect(aMgr, SIGNAL(mousePress(SUIT_ViewWindow*, QMouseEvent*)), this,
166 SLOT(onMousePress(SUIT_ViewWindow*, QMouseEvent*)));
167 connect(aMgr, SIGNAL(mouseRelease(SUIT_ViewWindow*, QMouseEvent*)), this,
168 SLOT(onMouseRelease(SUIT_ViewWindow*, QMouseEvent*)));
169 connect(aMgr, SIGNAL(mouseDoubleClick(SUIT_ViewWindow*, QMouseEvent*)), this,
170 SLOT(onMouseDoubleClick(SUIT_ViewWindow*, QMouseEvent*)));
171 connect(aMgr, SIGNAL(mouseMove(SUIT_ViewWindow*, QMouseEvent*)), this,
172 SLOT(onMouseMove(SUIT_ViewWindow*, QMouseEvent*)));
174 connect(aMgr, SIGNAL(keyPress(SUIT_ViewWindow*, QKeyEvent*)), this,
175 SLOT(onKeyPress(SUIT_ViewWindow*, QKeyEvent*)));
176 connect(aMgr, SIGNAL(keyRelease(SUIT_ViewWindow*, QKeyEvent*)), this,
177 SLOT(onKeyRelease(SUIT_ViewWindow*, QKeyEvent*)));
179 connect(aViewer, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
182 //**********************************************
183 void SHAPERGUI_SalomeViewer::onSelectionChanged()
185 // Selection event must be sent only after mouse release
186 myIsSelectionChanged = true;
189 //**********************************************
190 void SHAPERGUI_SalomeViewer::onMousePress(SUIT_ViewWindow* theView, QMouseEvent* theEvent)
192 myView->setCurrentView(theView);
193 emit mousePress(myView, theEvent);
196 //**********************************************
197 void SHAPERGUI_SalomeViewer::onMouseRelease(SUIT_ViewWindow* theView, QMouseEvent* theEvent)
199 myView->setCurrentView(theView);
200 emit mouseRelease(myView, theEvent);
201 if (myIsSelectionChanged) {
202 emit selectionChanged();
203 myIsSelectionChanged = false;
207 //**********************************************
208 void SHAPERGUI_SalomeViewer::onMouseDoubleClick(SUIT_ViewWindow* theView, QMouseEvent* theEvent)
210 myView->setCurrentView(theView);
211 emit mouseDoubleClick(myView, theEvent);
214 //**********************************************
215 void SHAPERGUI_SalomeViewer::onMouseMove(SUIT_ViewWindow* theView, QMouseEvent* theEvent)
217 myView->setCurrentView(theView);
218 emit mouseMove(myView, theEvent);
221 //**********************************************
222 bool SHAPERGUI_SalomeViewer::canDragByMouse() const
224 OCCViewer_Viewer* aViewer = mySelector->viewer();
225 SUIT_ViewWindow* aWnd = aViewer->getViewManager()->getActiveView();
226 OCCViewer_ViewWindow* aViewWnd = dynamic_cast<OCCViewer_ViewWindow*>(aWnd);
228 return (aViewWnd->interactionStyle() == 0);
234 //**********************************************
235 void SHAPERGUI_SalomeViewer::onKeyPress(SUIT_ViewWindow* theView, QKeyEvent* theEvent)
237 emit keyPress(myView, theEvent);
240 //**********************************************
241 void SHAPERGUI_SalomeViewer::onKeyRelease(SUIT_ViewWindow* theView, QKeyEvent* theEvent)
243 emit keyRelease(myView, theEvent);
246 //**********************************************
247 void SHAPERGUI_SalomeViewer::onTryCloseView(SUIT_ViewWindow*)
249 emit tryCloseView(myView);
252 //**********************************************
253 void SHAPERGUI_SalomeViewer::onDeleteView(SUIT_ViewWindow*)
255 if(myWindowScale.contains(myView->v3dView()))
256 myWindowScale.remove(myView->v3dView());
257 emit deleteView(myView);
260 //**********************************************
261 void SHAPERGUI_SalomeViewer::onViewCreated(SUIT_ViewWindow* theView)
263 myView->setCurrentView(theView);
265 OCCViewer_ViewFrame* aView = dynamic_cast<OCCViewer_ViewFrame*>(theView);
267 OCCViewer_ViewWindow* aWnd = aView->getView(OCCViewer_ViewFrame::MAIN_VIEW);
269 connect(aWnd, SIGNAL(vpTransformationFinished(OCCViewer_ViewWindow::OperationType)),
270 this, SLOT(onViewTransformed(OCCViewer_ViewWindow::OperationType)));
271 OCCViewer_ViewPort3d* aViewPort = aWnd->getViewPort();
273 connect(aViewPort, SIGNAL(vpMapped(OCCViewer_ViewPort3d*)), this, SLOT(onViewPortMapped()));
275 reconnectActions(aWnd, true);
277 myWindowScale.insert(aView->getViewPort()->getView(),
278 aView->getViewPort()->getView()->Camera()->Scale());
280 emit viewCreated(myView);
285 //**********************************************
286 void SHAPERGUI_SalomeViewer::onActivated(SUIT_ViewManager* theMgr)
288 myView->setCurrentView(theMgr->getActiveView());
289 emit activated(myView);
292 //**********************************************
293 void SHAPERGUI_SalomeViewer::enableSelection(bool isEnabled)
296 if (mySelector->viewer()->isSelectionEnabled() != isEnabled)
297 mySelector->viewer()->enableSelection(isEnabled);
298 // The enableSelection() in SALOME 7.5 cause of forced Viewer update(we have blinking)
299 // After this is corrected, the first row should be recommented, the last - removed
300 //mySelector->viewer()->setInteractionStyle(isEnabled ? SUIT_ViewModel::STANDARD
301 // : SUIT_ViewModel::KEY_FREE);
304 //**********************************************
305 bool SHAPERGUI_SalomeViewer::isSelectionEnabled() const
308 return mySelector->viewer()->isSelectionEnabled();
312 //**********************************************
313 void SHAPERGUI_SalomeViewer::enableMultiselection(bool isEnable)
316 mySelector->viewer()->enableMultiselection(isEnable);
319 //**********************************************
320 bool SHAPERGUI_SalomeViewer::isMultiSelectionEnabled() const
323 return mySelector->viewer()->isMultiSelectionEnabled();
327 //**********************************************
328 bool SHAPERGUI_SalomeViewer::enableDrawMode(bool isEnabled)
330 // TODO: Has to be replaced when SALOME patch become available
332 return mySelector->viewer()->enableDrawMode(isEnabled);
336 //**********************************************
337 void SHAPERGUI_SalomeViewer::reconnectActions(SUIT_ViewWindow* theWindow,
338 const bool theUseSHAPERSlot)
340 OCCViewer_ViewWindow* aWindow = dynamic_cast<OCCViewer_ViewWindow*>(theWindow);
344 QAction* anAction = theWindow->toolMgr()->action(OCCViewer_ViewWindow::TrihedronShowId);
348 if (theUseSHAPERSlot) {
349 anAction->disconnect(anAction, SIGNAL(toggled(bool)),
350 theWindow, SLOT(onTrihedronShow(bool)));
351 anAction->connect(anAction, SIGNAL(toggled(bool)),
352 this, SIGNAL(trihedronVisibilityChanged(bool)));
355 anAction->connect(anAction, SIGNAL(toggled(bool)),
356 theWindow, SLOT(onTrihedronShow(bool)));
357 anAction->disconnect(anAction, SIGNAL(toggled(bool)),
358 this, SIGNAL(trihedronVisibilityChanged(bool)));
362 //**********************************************
363 void SHAPERGUI_SalomeViewer::fitAll()
366 SUIT_ViewManager* aMgr = mySelector->viewer()->getViewManager();
367 OCCViewer_ViewFrame* aVFrame = dynamic_cast<OCCViewer_ViewFrame*>(aMgr->getActiveView());
374 //**********************************************
375 void SHAPERGUI_SalomeViewer::eraseAll()
377 Handle(AIS_InteractiveContext) aContext = AISContext();
378 if (aContext.IsNull())
380 AIS_ListOfInteractive aList;
381 aContext->DisplayedObjects(aList);
382 AIS_ListIteratorOfListOfInteractive aLIt;
383 Handle(AIS_InteractiveObject) anAISIO;
384 for (aLIt.Initialize(aList); aLIt.More(); aLIt.Next()) {
385 anAISIO = aLIt.Value();
386 Handle(Standard_Type) aType = anAISIO->DynamicType();
387 if (anAISIO->IsKind(STANDARD_TYPE(SALOME_AISShape))) {
388 aContext->Erase(anAISIO, false);
393 //**********************************************
394 void SHAPERGUI_SalomeViewer::setViewProjection(double theX, double theY,
395 double theZ, double theTwist)
400 SUIT_ViewManager* aMgr = mySelector->viewer()->getViewManager();
401 OCCViewer_ViewFrame* aVFrame = dynamic_cast<OCCViewer_ViewFrame*>(aMgr->getActiveView());
403 Handle(V3d_View) aView3d = aVFrame->getViewPort()->getView();
404 if (!aView3d.IsNull()) {
405 aView3d->SetProj(theX, theY, theZ);
406 aView3d->SetTwist( theTwist );
407 aView3d->FitAll(0.01, false);
408 aView3d->SetZSize(0.);
409 if (aView3d->Depth() < 0.1)
410 aView3d->DepthFitAll();
415 //***************************************
416 void SHAPERGUI_SalomeViewer::addSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
418 Handle(AIS_InteractiveContext) aContext = AISContext();
419 if (!aContext.IsNull()) {
420 aContext->AddFilter(theFilter);
424 //***************************************
425 void SHAPERGUI_SalomeViewer::removeSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
427 Handle(AIS_InteractiveContext) aContext = AISContext();
428 if (!aContext.IsNull()) {
429 aContext->RemoveFilter(theFilter);
433 //***************************************
434 bool SHAPERGUI_SalomeViewer::hasSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
436 bool aFoundFilter = false;
437 Handle(AIS_InteractiveContext) aContext = AISContext();
438 if (!aContext.IsNull()) {
439 const SelectMgr_ListOfFilter& aFilters = aContext->Filters();
440 SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
441 for (; aIt.More() && !aFoundFilter; aIt.Next()) {
442 aFoundFilter = theFilter.get() == aIt.Value().get();
448 //***************************************
449 void SHAPERGUI_SalomeViewer::clearSelectionFilters()
451 Handle(AIS_InteractiveContext) aContext = AISContext();
452 if (!aContext.IsNull()) {
453 aContext->RemoveFilters();
457 //***************************************
458 void SHAPERGUI_SalomeViewer::update()
460 Handle(AIS_InteractiveContext) aContext = AISContext();
461 if (!aContext.IsNull()) {
462 aContext->UpdateCurrentViewer();
466 //***************************************
467 void SHAPERGUI_SalomeViewer::onViewTransformed(OCCViewer_ViewWindow::OperationType theType)
469 emit viewTransformed((int) theType);
472 //***************************************
473 void SHAPERGUI_SalomeViewer::onViewPortMapped()
475 emit trihedronVisibilityChanged(true);
478 //***************************************
479 void SHAPERGUI_SalomeViewer::activateViewer(bool toActivate)
481 if (!mySelector || !mySelector->viewer())
483 SUIT_ViewManager* aMgr = mySelector->viewer()->getViewManager();
484 #ifdef SALOME_PATCH_FOR_CTRL_WHEEL
485 OCCViewer_Viewer* aViewer = dynamic_cast<OCCViewer_Viewer*>(aMgr->getViewModel());
487 aViewer->setUseLocalSelection(toActivate);
489 QVector<SUIT_ViewWindow*> aViews = aMgr->getViews();
491 foreach (SUIT_ViewWindow* aView, aViews) {
492 OCCViewer_ViewFrame* aOCCView = dynamic_cast<OCCViewer_ViewFrame*>(aView);
493 OCCViewer_ViewWindow* aWnd = aOCCView->getView(OCCViewer_ViewFrame::MAIN_VIEW);
494 connect(aWnd, SIGNAL(vpTransformationFinished(OCCViewer_ViewWindow::OperationType)),
495 this, SLOT(onViewTransformed(OCCViewer_ViewWindow::OperationType)));
496 reconnectActions(aWnd, true);
499 foreach (SUIT_ViewWindow* aView, aViews) {
500 OCCViewer_ViewFrame* aOCCView = dynamic_cast<OCCViewer_ViewFrame*>(aView);
501 OCCViewer_ViewWindow* aWnd = aOCCView->getView(OCCViewer_ViewFrame::MAIN_VIEW);
502 disconnect((OCCViewer_ViewWindow*)aWnd,
503 SIGNAL(vpTransformationFinished(OCCViewer_ViewWindow::OperationType)),
504 this, SLOT(onViewTransformed(OCCViewer_ViewWindow::OperationType)));
505 reconnectActions(aWnd, false);
510 bool SHAPERGUI_SalomeViewer::isColorScaleVisible() const
513 return mySelector->viewer()->isColorScaleVisible();
518 void SHAPERGUI_SalomeViewer::setColorScaleShown(bool on)
521 mySelector->viewer()->setColorScaleShown(on);
525 void SHAPERGUI_SalomeViewer::setColorScalePosition(double theX, double theY)
528 QWidget* aWindow = activeViewPort();
529 mySelector->viewer()->getColorScale()->SetPosition(aWindow->width() * theX,
530 aWindow->height() * theY);
534 void SHAPERGUI_SalomeViewer::setColorScaleSize(double theW, double theH)
537 QWidget* aWindow = activeViewPort();
538 mySelector->viewer()->getColorScale()->SetSize(aWindow->width() * theW,
539 aWindow->height() * theH);
543 void SHAPERGUI_SalomeViewer::setColorScaleRange(double theMin, double theMax)
546 mySelector->viewer()->getColorScale()->SetRange(theMin, theMax);
550 void SHAPERGUI_SalomeViewer::setColorScaleIntervals(int theNb)
553 mySelector->viewer()->getColorScale()->SetNumberOfIntervals(theNb);
557 void SHAPERGUI_SalomeViewer::setColorScaleTextHeigth(int theH)
560 mySelector->viewer()->getColorScale()->SetTextHeight(theH);
564 void SHAPERGUI_SalomeViewer::setColorScaleTextColor(const QColor& theColor)
567 Quantity_Color aColor(theColor.redF(), theColor.greenF(), theColor.blueF(), Quantity_TOC_RGB);
568 mySelector->viewer()->getColorScale()->SetColor(aColor);
572 void SHAPERGUI_SalomeViewer::setColorScaleTitle(const QString& theText)
575 mySelector->viewer()->getColorScale()->SetTitle(theText.toStdString().c_str());
581 //void SHAPERGUI_SalomeViewer::Zfitall()
583 // if (!mySelector || !mySelector->viewer())
585 // SUIT_ViewManager* aMgr = mySelector->viewer()->getViewManager();
586 // /// WORKAROUND for issue #1798. SUIT_ViewManager::closeAllViews() should nullify myActiveView
587 // /// As a result, we need to check views count in manager
588 // if (aMgr->getViews().size() > 0) {
589 // OCCViewer_ViewFrame* aView = dynamic_cast<OCCViewer_ViewFrame*>(aMgr->getActiveView());
591 // OCCViewer_ViewWindow* aWnd = aView->getView(OCCViewer_ViewFrame::MAIN_VIEW);
592 // Handle(V3d_View) aView3d = aWnd->getViewPort()->getView();
593 // aView3d->ZFitAll();
594 // if (aView3d->Depth() < 0.1)
595 // aView3d->DepthFitAll();