1 // Copyright (C) 2014-2023 CEA, EDF
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 <OCCViewer_ViewManager.h>
26 #include <SOCC_ViewModel.h>
27 #include <SUIT_ViewManager.h>
29 #include <QtxActionToolMgr.h>
30 #include <SALOME_AISShape.hxx>
32 #include <Standard_Version.hxx>
33 #include <SelectMgr_ListIteratorOfListOfFilter.hxx>
34 #include <AIS_TextLabel.hxx>
35 #include <Graphic3d_Vec2.hxx>
37 #include <QMouseEvent>
38 #include <QContextMenuEvent>
41 #if OCC_VERSION_HEX < 0x070400
42 #define SALOME_PATCH_FOR_CTRL_WHEEL
45 SHAPERGUI_SalomeView::SHAPERGUI_SalomeView(OCCViewer_Viewer* theViewer)
46 : ModuleBase_IViewWindow(), myCurrentView(0)
52 Handle(V3d_View) SHAPERGUI_SalomeView::v3dView() const
54 Handle(V3d_View) aView;
56 OCCViewer_ViewWindow* aWnd = static_cast<OCCViewer_ViewWindow*>(myCurrentView);
57 aView = aWnd->getViewPort()->getView();
62 QWidget* SHAPERGUI_SalomeView::viewPort() const
64 QWidget* aViewPort = 0;
66 OCCViewer_ViewWindow* aWnd = static_cast<OCCViewer_ViewWindow*>(myCurrentView);
67 aViewPort = aWnd->getViewPort();
72 //**********************************************
73 //**********************************************
74 //**********************************************
78 SHAPERGUI_SalomeViewer::SHAPERGUI_SalomeViewer(QObject* theParent)
79 : ModuleBase_IViewer(theParent),
80 mySelector(0), myView(0), myIsSelectionChanged(false)
84 SHAPERGUI_SalomeViewer::~SHAPERGUI_SalomeViewer()
91 //**********************************************
92 Handle(AIS_InteractiveContext) SHAPERGUI_SalomeViewer::AISContext() const
94 if (mySelector && mySelector->viewer())
95 return mySelector->viewer()->getAISContext();
96 Handle(AIS_InteractiveContext) aNull;
100 //**********************************************
101 Handle(V3d_Viewer) SHAPERGUI_SalomeViewer::v3dViewer() const
104 return mySelector->viewer()->getViewer3d();
105 return Handle(V3d_Viewer)();
108 //**********************************************
109 Handle(AIS_Trihedron) SHAPERGUI_SalomeViewer::trihedron() const
111 return mySelector->viewer()->getTrihedron();
114 //**********************************************
115 Handle(V3d_View) SHAPERGUI_SalomeViewer::activeView() const
118 OCCViewer_Viewer* aViewer = mySelector->viewer();
119 SUIT_ViewManager* aMgr = aViewer->getViewManager();
120 OCCViewer_ViewWindow* aWnd = static_cast<OCCViewer_ViewWindow*>(aMgr->getActiveView());
121 return aWnd->getViewPort()->getView();
123 return Handle(V3d_View)();
126 //**********************************************
127 QWidget* SHAPERGUI_SalomeViewer::activeViewPort() const
129 QWidget* aViewPort = 0;
131 OCCViewer_Viewer* aViewer = mySelector->viewer();
132 SUIT_ViewManager* aMgr = aViewer->getViewManager();
133 OCCViewer_ViewWindow* aWnd = static_cast<OCCViewer_ViewWindow*>(aMgr->getActiveView());
134 aViewPort = aWnd->getViewPort();
139 //**********************************************
140 void SHAPERGUI_SalomeViewer::setSelector(SHAPERGUI_OCCSelector* theSel)
143 if (mySelector == theSel)
146 mySelector->viewer()->getViewManager()->disconnect(this);
147 OCCViewer_Viewer* aViewer = mySelector->viewer();
149 aViewer->disconnect(this);
155 OCCViewer_Viewer* aViewer = mySelector->viewer();
156 SUIT_ViewManager* aMgr = aViewer->getViewManager();
158 myView = new SHAPERGUI_SalomeView(mySelector->viewer());
160 // TODO: Provide ModuleBase_IViewWindow interface
161 connect(aMgr, SIGNAL(lastViewClosed(SUIT_ViewManager*)), this, SIGNAL(lastViewClosed()));
163 connect(aMgr, SIGNAL(tryCloseView(SUIT_ViewWindow*)),
164 this, SLOT(onTryCloseView(SUIT_ViewWindow*)));
165 connect(aMgr, SIGNAL(deleteView(SUIT_ViewWindow*)),
166 this, SLOT(onDeleteView(SUIT_ViewWindow*)));
167 connect(aMgr, SIGNAL(viewCreated(SUIT_ViewWindow*)),
168 this, SLOT(onViewCreated(SUIT_ViewWindow*)));
169 connect(aMgr, SIGNAL(activated(SUIT_ViewManager*)),
170 this, SLOT(onActivated(SUIT_ViewManager*)));
172 connect(aMgr, SIGNAL(mousePress(SUIT_ViewWindow*, QMouseEvent*)), this,
173 SLOT(onMousePress(SUIT_ViewWindow*, QMouseEvent*)));
174 connect(aMgr, SIGNAL(mouseRelease(SUIT_ViewWindow*, QMouseEvent*)), this,
175 SLOT(onMouseRelease(SUIT_ViewWindow*, QMouseEvent*)));
176 connect(aMgr, SIGNAL(mouseDoubleClick(SUIT_ViewWindow*, QMouseEvent*)), this,
177 SLOT(onMouseDoubleClick(SUIT_ViewWindow*, QMouseEvent*)));
178 connect(aMgr, SIGNAL(mouseMove(SUIT_ViewWindow*, QMouseEvent*)), this,
179 SLOT(onMouseMove(SUIT_ViewWindow*, QMouseEvent*)));
181 connect(aMgr, SIGNAL(keyPress(SUIT_ViewWindow*, QKeyEvent*)), this,
182 SLOT(onKeyPress(SUIT_ViewWindow*, QKeyEvent*)));
183 connect(aMgr, SIGNAL(keyRelease(SUIT_ViewWindow*, QKeyEvent*)), this,
184 SLOT(onKeyRelease(SUIT_ViewWindow*, QKeyEvent*)));
186 connect(aViewer, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
189 //**********************************************
190 void SHAPERGUI_SalomeViewer::onSelectionChanged()
192 // Selection event must be sent only after mouse release
193 myIsSelectionChanged = true;
196 //**********************************************
197 void SHAPERGUI_SalomeViewer::onMousePress(SUIT_ViewWindow* theView, QMouseEvent* theEvent)
199 myView->setCurrentView(theView);
200 emit mousePress(myView, theEvent);
203 //**********************************************
204 void SHAPERGUI_SalomeViewer::onMouseRelease(SUIT_ViewWindow* theView, QMouseEvent* theEvent)
206 myView->setCurrentView(theView);
207 emit mouseRelease(myView, theEvent);
208 if (myIsSelectionChanged) {
209 emit selectionChanged();
210 myIsSelectionChanged = false;
214 //**********************************************
215 void SHAPERGUI_SalomeViewer::onMouseDoubleClick(SUIT_ViewWindow* theView, QMouseEvent* theEvent)
217 myView->setCurrentView(theView);
218 emit mouseDoubleClick(myView, theEvent);
221 //**********************************************
222 void SHAPERGUI_SalomeViewer::onMouseMove(SUIT_ViewWindow* theView, QMouseEvent* theEvent)
224 myView->setCurrentView(theView);
225 emit mouseMove(myView, theEvent);
228 //**********************************************
229 bool SHAPERGUI_SalomeViewer::canDragByMouse() const
231 OCCViewer_Viewer* aViewer = mySelector->viewer();
232 SUIT_ViewWindow* aWnd = aViewer->getViewManager()->getActiveView();
233 OCCViewer_ViewWindow* aViewWnd = dynamic_cast<OCCViewer_ViewWindow*>(aWnd);
235 return (aViewWnd->interactionStyle() == 0);
241 //**********************************************
242 void SHAPERGUI_SalomeViewer::onKeyPress(SUIT_ViewWindow* /*theView*/, QKeyEvent* theEvent)
244 emit keyPress(myView, theEvent);
247 //**********************************************
248 void SHAPERGUI_SalomeViewer::onKeyRelease(SUIT_ViewWindow* /*theView*/, QKeyEvent* theEvent)
250 emit keyRelease(myView, theEvent);
253 //**********************************************
254 void SHAPERGUI_SalomeViewer::onTryCloseView(SUIT_ViewWindow*)
256 emit tryCloseView(myView);
259 //**********************************************
260 void SHAPERGUI_SalomeViewer::onDeleteView(SUIT_ViewWindow*)
262 if(myWindowScale.contains(myView->v3dView()))
263 myWindowScale.remove(myView->v3dView());
264 emit deleteView(myView);
267 //**********************************************
268 void SHAPERGUI_SalomeViewer::onViewCreated(SUIT_ViewWindow* theView)
270 myView->setCurrentView(theView);
272 OCCViewer_ViewFrame* aView = dynamic_cast<OCCViewer_ViewFrame*>(theView);
274 OCCViewer_ViewWindow* aWnd = aView->getView(OCCViewer_ViewFrame::MAIN_VIEW);
276 connect(aWnd, SIGNAL(vpTransformationFinished(OCCViewer_ViewWindow::OperationType)),
277 this, SLOT(onViewTransformed(OCCViewer_ViewWindow::OperationType)));
278 OCCViewer_ViewPort3d* aViewPort = aWnd->getViewPort();
280 connect(aViewPort, SIGNAL(vpMapped(OCCViewer_ViewPort3d*)), this, SLOT(onViewPortMapped()));
282 OCCViewer_ViewManager* aMgr = dynamic_cast<OCCViewer_ViewManager*>(aWnd->getViewManager());
283 if (aMgr) aWnd->enableAutoRotation(aMgr->isAutoRotation());
285 reconnectActions(aWnd, true);
287 myWindowScale.insert(aView->getViewPort()->getView(),
288 aView->getViewPort()->getView()->Camera()->Scale());
290 QTimer::singleShot(10, this, SLOT(onAfterViewCreated()));
292 emit viewCreated(myView);
295 //**********************************************
296 void SHAPERGUI_SalomeViewer::onAfterViewCreated()
298 // Update trihedron and dimension arrows
299 emit onViewTransformed(OCCViewer_ViewWindow::ZOOMVIEW);
303 //**********************************************
304 void SHAPERGUI_SalomeViewer::onActivated(SUIT_ViewManager* theMgr)
306 myView->setCurrentView(theMgr->getActiveView());
307 emit activated(myView);
310 //**********************************************
311 void SHAPERGUI_SalomeViewer::enableSelection(bool isEnabled)
314 if (mySelector->viewer()->isSelectionEnabled() != isEnabled)
315 mySelector->viewer()->enableSelection(isEnabled);
316 // The enableSelection() in SALOME 7.5 cause of forced Viewer update(we have blinking)
317 // After this is corrected, the first row should be recommented, the last - removed
318 //mySelector->viewer()->setInteractionStyle(isEnabled ? SUIT_ViewModel::STANDARD
319 // : SUIT_ViewModel::KEY_FREE);
322 //**********************************************
323 bool SHAPERGUI_SalomeViewer::isSelectionEnabled() const
326 return mySelector->viewer()->isSelectionEnabled();
330 //**********************************************
331 void SHAPERGUI_SalomeViewer::enableMultiselection(bool isEnable)
334 mySelector->viewer()->enableMultiselection(isEnable);
337 //**********************************************
338 bool SHAPERGUI_SalomeViewer::isMultiSelectionEnabled() const
341 return mySelector->viewer()->isMultiSelectionEnabled();
345 //**********************************************
346 bool SHAPERGUI_SalomeViewer::enableDrawMode(bool isEnabled)
348 // TODO: Has to be replaced when SALOME patch become available
350 return mySelector->viewer()->enableDrawMode(isEnabled);
354 //**********************************************
355 void SHAPERGUI_SalomeViewer::reconnectActions(SUIT_ViewWindow* theWindow,
356 const bool theUseSHAPERSlot)
358 OCCViewer_ViewWindow* aWindow = dynamic_cast<OCCViewer_ViewWindow*>(theWindow);
362 QAction* anAction = theWindow->toolMgr()->action(OCCViewer_ViewWindow::TrihedronShowId);
366 if (theUseSHAPERSlot) {
367 anAction->disconnect(anAction, SIGNAL(toggled(bool)),
368 theWindow, SLOT(onTrihedronShow(bool)));
369 anAction->connect(anAction, SIGNAL(toggled(bool)),
370 this, SIGNAL(trihedronVisibilityChanged(bool)));
373 anAction->connect(anAction, SIGNAL(toggled(bool)),
374 theWindow, SLOT(onTrihedronShow(bool)));
375 anAction->disconnect(anAction, SIGNAL(toggled(bool)),
376 this, SIGNAL(trihedronVisibilityChanged(bool)));
380 //**********************************************
381 void SHAPERGUI_SalomeViewer::fitAll()
384 SUIT_ViewManager* aMgr = mySelector->viewer()->getViewManager();
385 OCCViewer_ViewFrame* aVFrame = dynamic_cast<OCCViewer_ViewFrame*>(aMgr->getActiveView());
392 //**********************************************
393 void SHAPERGUI_SalomeViewer::eraseAll()
395 Handle(AIS_InteractiveContext) aContext = AISContext();
396 if (aContext.IsNull())
398 AIS_ListOfInteractive aList;
399 aContext->DisplayedObjects(aList);
400 AIS_ListIteratorOfListOfInteractive aLIt;
401 Handle(AIS_InteractiveObject) anAISIO;
402 for (aLIt.Initialize(aList); aLIt.More(); aLIt.Next()) {
403 anAISIO = aLIt.Value();
404 Handle(Standard_Type) aType = anAISIO->DynamicType();
405 if (anAISIO->IsKind(STANDARD_TYPE(SALOME_AISShape))) {
406 aContext->Erase(anAISIO, false);
411 //**********************************************
412 void SHAPERGUI_SalomeViewer::setViewProjection(double theX, double theY,
413 double theZ, double theTwist)
418 SUIT_ViewManager* aMgr = mySelector->viewer()->getViewManager();
419 OCCViewer_ViewFrame* aVFrame = dynamic_cast<OCCViewer_ViewFrame*>(aMgr->getActiveView());
421 Handle(V3d_View) aView3d = aVFrame->getViewPort()->getView();
422 if (!aView3d.IsNull()) {
423 aView3d->SetProj(theX, theY, theZ);
424 aView3d->SetTwist( theTwist );
425 aView3d->FitAll(0.01, false);
426 //aView3d->SetZSize(0.);
427 if (aView3d->Depth() < 0.1)
428 aView3d->DepthFitAll();
433 //***************************************
434 void SHAPERGUI_SalomeViewer::addSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
436 Handle(AIS_InteractiveContext) aContext = AISContext();
437 if (!aContext.IsNull()) {
438 aContext->AddFilter(theFilter);
442 //***************************************
443 void SHAPERGUI_SalomeViewer::removeSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
445 Handle(AIS_InteractiveContext) aContext = AISContext();
446 if (!aContext.IsNull()) {
447 aContext->RemoveFilter(theFilter);
451 //***************************************
452 bool SHAPERGUI_SalomeViewer::hasSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
454 bool aFoundFilter = false;
455 Handle(AIS_InteractiveContext) aContext = AISContext();
456 if (!aContext.IsNull()) {
457 const SelectMgr_ListOfFilter& aFilters = aContext->Filters();
458 SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
459 for (; aIt.More() && !aFoundFilter; aIt.Next()) {
460 aFoundFilter = theFilter.get() == aIt.Value().get();
466 //***************************************
467 void SHAPERGUI_SalomeViewer::clearSelectionFilters()
469 Handle(AIS_InteractiveContext) aContext = AISContext();
470 if (!aContext.IsNull()) {
471 aContext->RemoveFilters();
475 //***************************************
476 void SHAPERGUI_SalomeViewer::update()
478 Handle(AIS_InteractiveContext) aContext = AISContext();
479 if (!aContext.IsNull()) {
480 aContext->UpdateCurrentViewer();
484 //***************************************
485 void SHAPERGUI_SalomeViewer::onViewTransformed(OCCViewer_ViewWindow::OperationType theType)
487 emit viewTransformed((int) theType);
490 //***************************************
491 void SHAPERGUI_SalomeViewer::onViewPortMapped()
493 emit trihedronVisibilityChanged(true);
496 //***************************************
497 void SHAPERGUI_SalomeViewer::activateViewer(bool toActivate)
499 if (!mySelector || !mySelector->viewer())
501 SUIT_ViewManager* aMgr = mySelector->viewer()->getViewManager();
502 #ifdef SALOME_PATCH_FOR_CTRL_WHEEL
503 OCCViewer_Viewer* aViewer = dynamic_cast<OCCViewer_Viewer*>(aMgr->getViewModel());
505 aViewer->setUseLocalSelection(toActivate);
507 QVector<SUIT_ViewWindow*> aViews = aMgr->getViews();
509 foreach (SUIT_ViewWindow* aView, aViews) {
510 OCCViewer_ViewFrame* aOCCView = dynamic_cast<OCCViewer_ViewFrame*>(aView);
511 OCCViewer_ViewWindow* aWnd = aOCCView->getView(OCCViewer_ViewFrame::MAIN_VIEW);
512 connect(aWnd, SIGNAL(vpTransformationFinished(OCCViewer_ViewWindow::OperationType)),
513 this, SLOT(onViewTransformed(OCCViewer_ViewWindow::OperationType)));
514 reconnectActions(aWnd, true);
515 OCCViewer_ViewManager* aOCCMgr = dynamic_cast<OCCViewer_ViewManager*>(aMgr);
516 if (aOCCMgr) aWnd->enableAutoRotation(aOCCMgr->isAutoRotation());
519 foreach (SUIT_ViewWindow* aView, aViews) {
520 OCCViewer_ViewFrame* aOCCView = dynamic_cast<OCCViewer_ViewFrame*>(aView);
521 OCCViewer_ViewWindow* aWnd = aOCCView->getView(OCCViewer_ViewFrame::MAIN_VIEW);
522 disconnect((OCCViewer_ViewWindow*)aWnd,
523 SIGNAL(vpTransformationFinished(OCCViewer_ViewWindow::OperationType)),
524 this, SLOT(onViewTransformed(OCCViewer_ViewWindow::OperationType)));
525 reconnectActions(aWnd, false);
526 aWnd->enableAutoRotation(false);
531 bool SHAPERGUI_SalomeViewer::isColorScaleVisible() const
534 return mySelector->viewer()->isColorScaleVisible();
539 void SHAPERGUI_SalomeViewer::setColorScaleShown(bool on)
542 mySelector->viewer()->setColorScaleShown(on);
546 void SHAPERGUI_SalomeViewer::setColorScalePosition(double theX, double theY)
549 QWidget* aWindow = activeViewPort();
550 mySelector->viewer()->getColorScale()->SetPosition(aWindow->width() * theX,
551 aWindow->height() * theY);
555 void SHAPERGUI_SalomeViewer::setColorScaleSize(double theW, double theH)
558 QWidget* aWindow = activeViewPort();
559 mySelector->viewer()->getColorScale()->SetSize(aWindow->width() * theW,
560 aWindow->height() * theH);
564 void SHAPERGUI_SalomeViewer::setColorScaleRange(double theMin, double theMax)
567 mySelector->viewer()->getColorScale()->SetRange(theMin, theMax);
571 void SHAPERGUI_SalomeViewer::setColorScaleIntervals(int theNb)
574 mySelector->viewer()->getColorScale()->SetNumberOfIntervals(theNb);
578 void SHAPERGUI_SalomeViewer::setColorScaleTextHeigth(int theH)
581 mySelector->viewer()->getColorScale()->SetTextHeight(theH);
585 void SHAPERGUI_SalomeViewer::setColorScaleTextColor(const QColor& theColor)
588 Quantity_Color aColor(theColor.redF(), theColor.greenF(), theColor.blueF(), Quantity_TOC_RGB);
589 mySelector->viewer()->getColorScale()->SetColor(aColor);
593 void SHAPERGUI_SalomeViewer::setColorScaleTitle(const QString& theText)
596 mySelector->viewer()->getColorScale()->SetTitle(theText.toStdString().c_str());
600 void SHAPERGUI_SalomeViewer::setText(
601 const ModuleBase_IViewer::TextColor& theText, const int theSize)
603 Handle(AIS_InteractiveContext) aContext = AISContext();
604 if (aContext.IsNull())
606 NCollection_List<Handle(AIS_TextLabel)>::Iterator aPrsIter(myText);
607 if (!theText.empty())
609 Quantity_Color aTextColor;
610 std::string aTextFont;
612 double anOffset = -theSize - 1; // initial offset from the toolbar of the viewer
613 ModuleBase_IViewer::TextColor::const_iterator aLine = theText.cbegin();
614 for (; aLine != theText.cend(); aLine++)
616 Quantity_Color aColor(aLine->second.at(0) / 255.,
617 aLine->second.at(1) / 255., aLine->second.at(2) / 255., Quantity_TOC_RGB);
618 if (aLine == theText.cbegin())
619 { // the first is the font name and text color
621 aTextFont = std::string(aLine->first.begin(), aLine->first.end());
625 Handle(AIS_TextLabel) aPrs;
626 if (!aPrsIter.More())
627 { // new presentation is added to the list forever
628 aPrs = new AIS_TextLabel;
629 aPrs->SetZLayer(Graphic3d_ZLayerId_TopOSD);
630 aPrs->SetHJustification(Graphic3d_HTA_RIGHT);
631 aPrs->SetVJustification(Graphic3d_VTA_TOPFIRSTLINE);
632 aPrs->SetDisplayType(Aspect_TODT_SUBTITLE);
633 aPrs->SetTransparency(0.2);
634 static const Graphic3d_Vec2i aZOffset(0);
635 aPrs->SetTransformPersistence(new Graphic3d_TransformPers(
636 Graphic3d_TMF_2d, Aspect_TOTP_RIGHT_UPPER, aZOffset));
639 { // update the existing presentation
640 aPrs = aPrsIter.ChangeValue();
641 if (!aContext->IsDisplayed(aPrs))
642 aContext->Display(aPrs, Standard_False);
644 // common part of parameters for new and existing prs
645 aPrs->SetText(aLine->first.c_str());
646 aPrs->SetFont(aTextFont.c_str());
647 aPrs->SetHeight(theSize);
648 aPrs->SetPosition(gp_Pnt(0, anOffset, 0));
649 anOffset -= int(double(theSize) * 1.2 + 1); // to have a proportional gap between lines
650 aPrs->SetColor(aTextColor);
651 aPrs->SetColorSubTitle(aColor);
652 if (!aPrsIter.More())
653 { // for the new presentation
654 aContext->Display(aPrs, Standard_False);
658 { // update existing presentation
659 aContext->Redisplay(aPrs, Standard_False, Standard_False);
664 // hide the left presentations, created previously, but unused for now
665 for (; aPrsIter.More(); aPrsIter.Next())
666 aContext->Erase(aPrsIter.ChangeValue(), Standard_False);
669 void SHAPERGUI_SalomeViewer::setFitter(OCCViewer_Fitter* theFitter)
672 mySelector->viewer()->setFitter(theFitter);
675 OCCViewer_Fitter* SHAPERGUI_SalomeViewer::fitter() const
678 return mySelector->viewer()->fitter();