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 <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>
33 #include <AIS_TextLabel.hxx>
34 #include <Graphic3d_Vec2.hxx>
36 #include <QMouseEvent>
37 #include <QContextMenuEvent>
40 #if OCC_VERSION_HEX < 0x070400
41 #define SALOME_PATCH_FOR_CTRL_WHEEL
44 SHAPERGUI_SalomeView::SHAPERGUI_SalomeView(OCCViewer_Viewer* theViewer)
45 : ModuleBase_IViewWindow(), myCurrentView(0)
51 Handle(V3d_View) SHAPERGUI_SalomeView::v3dView() const
53 Handle(V3d_View) aView;
55 OCCViewer_ViewWindow* aWnd = static_cast<OCCViewer_ViewWindow*>(myCurrentView);
56 aView = aWnd->getViewPort()->getView();
61 QWidget* SHAPERGUI_SalomeView::viewPort() const
63 QWidget* aViewPort = 0;
65 OCCViewer_ViewWindow* aWnd = static_cast<OCCViewer_ViewWindow*>(myCurrentView);
66 aViewPort = aWnd->getViewPort();
71 //**********************************************
72 //**********************************************
73 //**********************************************
77 SHAPERGUI_SalomeViewer::SHAPERGUI_SalomeViewer(QObject* theParent)
78 : ModuleBase_IViewer(theParent),
79 mySelector(0), myView(0), myIsSelectionChanged(false)
83 SHAPERGUI_SalomeViewer::~SHAPERGUI_SalomeViewer()
90 //**********************************************
91 Handle(AIS_InteractiveContext) SHAPERGUI_SalomeViewer::AISContext() const
93 if (mySelector && mySelector->viewer())
94 return mySelector->viewer()->getAISContext();
95 Handle(AIS_InteractiveContext) aNull;
99 //**********************************************
100 Handle(V3d_Viewer) SHAPERGUI_SalomeViewer::v3dViewer() const
103 return mySelector->viewer()->getViewer3d();
104 return Handle(V3d_Viewer)();
107 //**********************************************
108 Handle(AIS_Trihedron) SHAPERGUI_SalomeViewer::trihedron() const
110 return mySelector->viewer()->getTrihedron();
113 //**********************************************
114 Handle(V3d_View) SHAPERGUI_SalomeViewer::activeView() const
117 OCCViewer_Viewer* aViewer = mySelector->viewer();
118 SUIT_ViewManager* aMgr = aViewer->getViewManager();
119 OCCViewer_ViewWindow* aWnd = static_cast<OCCViewer_ViewWindow*>(aMgr->getActiveView());
120 return aWnd->getViewPort()->getView();
122 return Handle(V3d_View)();
125 //**********************************************
126 QWidget* SHAPERGUI_SalomeViewer::activeViewPort() const
128 QWidget* aViewPort = 0;
130 OCCViewer_Viewer* aViewer = mySelector->viewer();
131 SUIT_ViewManager* aMgr = aViewer->getViewManager();
132 OCCViewer_ViewWindow* aWnd = static_cast<OCCViewer_ViewWindow*>(aMgr->getActiveView());
133 aViewPort = aWnd->getViewPort();
138 //**********************************************
139 void SHAPERGUI_SalomeViewer::setSelector(SHAPERGUI_OCCSelector* theSel)
142 if (mySelector == theSel)
145 mySelector->viewer()->getViewManager()->disconnect(this);
146 OCCViewer_Viewer* aViewer = mySelector->viewer();
148 aViewer->disconnect(this);
154 OCCViewer_Viewer* aViewer = mySelector->viewer();
155 SUIT_ViewManager* aMgr = aViewer->getViewManager();
157 myView = new SHAPERGUI_SalomeView(mySelector->viewer());
159 // TODO: Provide ModuleBase_IViewWindow interface
160 connect(aMgr, SIGNAL(lastViewClosed(SUIT_ViewManager*)), this, SIGNAL(lastViewClosed()));
162 connect(aMgr, SIGNAL(tryCloseView(SUIT_ViewWindow*)),
163 this, SLOT(onTryCloseView(SUIT_ViewWindow*)));
164 connect(aMgr, SIGNAL(deleteView(SUIT_ViewWindow*)),
165 this, SLOT(onDeleteView(SUIT_ViewWindow*)));
166 connect(aMgr, SIGNAL(viewCreated(SUIT_ViewWindow*)),
167 this, SLOT(onViewCreated(SUIT_ViewWindow*)));
168 connect(aMgr, SIGNAL(activated(SUIT_ViewManager*)),
169 this, SLOT(onActivated(SUIT_ViewManager*)));
171 connect(aMgr, SIGNAL(mousePress(SUIT_ViewWindow*, QMouseEvent*)), this,
172 SLOT(onMousePress(SUIT_ViewWindow*, QMouseEvent*)));
173 connect(aMgr, SIGNAL(mouseRelease(SUIT_ViewWindow*, QMouseEvent*)), this,
174 SLOT(onMouseRelease(SUIT_ViewWindow*, QMouseEvent*)));
175 connect(aMgr, SIGNAL(mouseDoubleClick(SUIT_ViewWindow*, QMouseEvent*)), this,
176 SLOT(onMouseDoubleClick(SUIT_ViewWindow*, QMouseEvent*)));
177 connect(aMgr, SIGNAL(mouseMove(SUIT_ViewWindow*, QMouseEvent*)), this,
178 SLOT(onMouseMove(SUIT_ViewWindow*, QMouseEvent*)));
180 connect(aMgr, SIGNAL(keyPress(SUIT_ViewWindow*, QKeyEvent*)), this,
181 SLOT(onKeyPress(SUIT_ViewWindow*, QKeyEvent*)));
182 connect(aMgr, SIGNAL(keyRelease(SUIT_ViewWindow*, QKeyEvent*)), this,
183 SLOT(onKeyRelease(SUIT_ViewWindow*, QKeyEvent*)));
185 connect(aViewer, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
188 //**********************************************
189 void SHAPERGUI_SalomeViewer::onSelectionChanged()
191 // Selection event must be sent only after mouse release
192 myIsSelectionChanged = true;
195 //**********************************************
196 void SHAPERGUI_SalomeViewer::onMousePress(SUIT_ViewWindow* theView, QMouseEvent* theEvent)
198 myView->setCurrentView(theView);
199 emit mousePress(myView, theEvent);
202 //**********************************************
203 void SHAPERGUI_SalomeViewer::onMouseRelease(SUIT_ViewWindow* theView, QMouseEvent* theEvent)
205 myView->setCurrentView(theView);
206 emit mouseRelease(myView, theEvent);
207 if (myIsSelectionChanged) {
208 emit selectionChanged();
209 myIsSelectionChanged = false;
213 //**********************************************
214 void SHAPERGUI_SalomeViewer::onMouseDoubleClick(SUIT_ViewWindow* theView, QMouseEvent* theEvent)
216 myView->setCurrentView(theView);
217 emit mouseDoubleClick(myView, theEvent);
220 //**********************************************
221 void SHAPERGUI_SalomeViewer::onMouseMove(SUIT_ViewWindow* theView, QMouseEvent* theEvent)
223 myView->setCurrentView(theView);
224 emit mouseMove(myView, theEvent);
227 //**********************************************
228 bool SHAPERGUI_SalomeViewer::canDragByMouse() const
230 OCCViewer_Viewer* aViewer = mySelector->viewer();
231 SUIT_ViewWindow* aWnd = aViewer->getViewManager()->getActiveView();
232 OCCViewer_ViewWindow* aViewWnd = dynamic_cast<OCCViewer_ViewWindow*>(aWnd);
234 return (aViewWnd->interactionStyle() == 0);
240 //**********************************************
241 void SHAPERGUI_SalomeViewer::onKeyPress(SUIT_ViewWindow* /*theView*/, QKeyEvent* theEvent)
243 emit keyPress(myView, theEvent);
246 //**********************************************
247 void SHAPERGUI_SalomeViewer::onKeyRelease(SUIT_ViewWindow* /*theView*/, QKeyEvent* theEvent)
249 emit keyRelease(myView, theEvent);
252 //**********************************************
253 void SHAPERGUI_SalomeViewer::onTryCloseView(SUIT_ViewWindow*)
255 emit tryCloseView(myView);
258 //**********************************************
259 void SHAPERGUI_SalomeViewer::onDeleteView(SUIT_ViewWindow*)
261 if(myWindowScale.contains(myView->v3dView()))
262 myWindowScale.remove(myView->v3dView());
263 emit deleteView(myView);
266 //**********************************************
267 void SHAPERGUI_SalomeViewer::onViewCreated(SUIT_ViewWindow* theView)
269 myView->setCurrentView(theView);
271 OCCViewer_ViewFrame* aView = dynamic_cast<OCCViewer_ViewFrame*>(theView);
273 OCCViewer_ViewWindow* aWnd = aView->getView(OCCViewer_ViewFrame::MAIN_VIEW);
275 connect(aWnd, SIGNAL(vpTransformationFinished(OCCViewer_ViewWindow::OperationType)),
276 this, SLOT(onViewTransformed(OCCViewer_ViewWindow::OperationType)));
277 OCCViewer_ViewPort3d* aViewPort = aWnd->getViewPort();
279 connect(aViewPort, SIGNAL(vpMapped(OCCViewer_ViewPort3d*)), this, SLOT(onViewPortMapped()));
281 reconnectActions(aWnd, true);
283 myWindowScale.insert(aView->getViewPort()->getView(),
284 aView->getViewPort()->getView()->Camera()->Scale());
286 QTimer::singleShot(10, this, SLOT(onAfterViewCreated()));
288 emit viewCreated(myView);
291 //**********************************************
292 void SHAPERGUI_SalomeViewer::onAfterViewCreated()
294 // Update trihedron and dimension arrows
295 emit onViewTransformed(OCCViewer_ViewWindow::ZOOMVIEW);
299 //**********************************************
300 void SHAPERGUI_SalomeViewer::onActivated(SUIT_ViewManager* theMgr)
302 myView->setCurrentView(theMgr->getActiveView());
303 emit activated(myView);
306 //**********************************************
307 void SHAPERGUI_SalomeViewer::enableSelection(bool isEnabled)
310 if (mySelector->viewer()->isSelectionEnabled() != isEnabled)
311 mySelector->viewer()->enableSelection(isEnabled);
312 // The enableSelection() in SALOME 7.5 cause of forced Viewer update(we have blinking)
313 // After this is corrected, the first row should be recommented, the last - removed
314 //mySelector->viewer()->setInteractionStyle(isEnabled ? SUIT_ViewModel::STANDARD
315 // : SUIT_ViewModel::KEY_FREE);
318 //**********************************************
319 bool SHAPERGUI_SalomeViewer::isSelectionEnabled() const
322 return mySelector->viewer()->isSelectionEnabled();
326 //**********************************************
327 void SHAPERGUI_SalomeViewer::enableMultiselection(bool isEnable)
330 mySelector->viewer()->enableMultiselection(isEnable);
333 //**********************************************
334 bool SHAPERGUI_SalomeViewer::isMultiSelectionEnabled() const
337 return mySelector->viewer()->isMultiSelectionEnabled();
341 //**********************************************
342 bool SHAPERGUI_SalomeViewer::enableDrawMode(bool isEnabled)
344 // TODO: Has to be replaced when SALOME patch become available
346 return mySelector->viewer()->enableDrawMode(isEnabled);
350 //**********************************************
351 void SHAPERGUI_SalomeViewer::reconnectActions(SUIT_ViewWindow* theWindow,
352 const bool theUseSHAPERSlot)
354 OCCViewer_ViewWindow* aWindow = dynamic_cast<OCCViewer_ViewWindow*>(theWindow);
358 QAction* anAction = theWindow->toolMgr()->action(OCCViewer_ViewWindow::TrihedronShowId);
362 if (theUseSHAPERSlot) {
363 anAction->disconnect(anAction, SIGNAL(toggled(bool)),
364 theWindow, SLOT(onTrihedronShow(bool)));
365 anAction->connect(anAction, SIGNAL(toggled(bool)),
366 this, SIGNAL(trihedronVisibilityChanged(bool)));
369 anAction->connect(anAction, SIGNAL(toggled(bool)),
370 theWindow, SLOT(onTrihedronShow(bool)));
371 anAction->disconnect(anAction, SIGNAL(toggled(bool)),
372 this, SIGNAL(trihedronVisibilityChanged(bool)));
376 //**********************************************
377 void SHAPERGUI_SalomeViewer::fitAll()
380 SUIT_ViewManager* aMgr = mySelector->viewer()->getViewManager();
381 OCCViewer_ViewFrame* aVFrame = dynamic_cast<OCCViewer_ViewFrame*>(aMgr->getActiveView());
388 //**********************************************
389 void SHAPERGUI_SalomeViewer::eraseAll()
391 Handle(AIS_InteractiveContext) aContext = AISContext();
392 if (aContext.IsNull())
394 AIS_ListOfInteractive aList;
395 aContext->DisplayedObjects(aList);
396 AIS_ListIteratorOfListOfInteractive aLIt;
397 Handle(AIS_InteractiveObject) anAISIO;
398 for (aLIt.Initialize(aList); aLIt.More(); aLIt.Next()) {
399 anAISIO = aLIt.Value();
400 Handle(Standard_Type) aType = anAISIO->DynamicType();
401 if (anAISIO->IsKind(STANDARD_TYPE(SALOME_AISShape))) {
402 aContext->Erase(anAISIO, false);
407 //**********************************************
408 void SHAPERGUI_SalomeViewer::setViewProjection(double theX, double theY,
409 double theZ, double theTwist)
414 SUIT_ViewManager* aMgr = mySelector->viewer()->getViewManager();
415 OCCViewer_ViewFrame* aVFrame = dynamic_cast<OCCViewer_ViewFrame*>(aMgr->getActiveView());
417 Handle(V3d_View) aView3d = aVFrame->getViewPort()->getView();
418 if (!aView3d.IsNull()) {
419 aView3d->SetProj(theX, theY, theZ);
420 aView3d->SetTwist( theTwist );
421 aView3d->FitAll(0.01, false);
422 //aView3d->SetZSize(0.);
423 if (aView3d->Depth() < 0.1)
424 aView3d->DepthFitAll();
429 //***************************************
430 void SHAPERGUI_SalomeViewer::addSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
432 Handle(AIS_InteractiveContext) aContext = AISContext();
433 if (!aContext.IsNull()) {
434 aContext->AddFilter(theFilter);
438 //***************************************
439 void SHAPERGUI_SalomeViewer::removeSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
441 Handle(AIS_InteractiveContext) aContext = AISContext();
442 if (!aContext.IsNull()) {
443 aContext->RemoveFilter(theFilter);
447 //***************************************
448 bool SHAPERGUI_SalomeViewer::hasSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
450 bool aFoundFilter = false;
451 Handle(AIS_InteractiveContext) aContext = AISContext();
452 if (!aContext.IsNull()) {
453 const SelectMgr_ListOfFilter& aFilters = aContext->Filters();
454 SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
455 for (; aIt.More() && !aFoundFilter; aIt.Next()) {
456 aFoundFilter = theFilter.get() == aIt.Value().get();
462 //***************************************
463 void SHAPERGUI_SalomeViewer::clearSelectionFilters()
465 Handle(AIS_InteractiveContext) aContext = AISContext();
466 if (!aContext.IsNull()) {
467 aContext->RemoveFilters();
471 //***************************************
472 void SHAPERGUI_SalomeViewer::update()
474 Handle(AIS_InteractiveContext) aContext = AISContext();
475 if (!aContext.IsNull()) {
476 aContext->UpdateCurrentViewer();
480 //***************************************
481 void SHAPERGUI_SalomeViewer::onViewTransformed(OCCViewer_ViewWindow::OperationType theType)
483 emit viewTransformed((int) theType);
486 //***************************************
487 void SHAPERGUI_SalomeViewer::onViewPortMapped()
489 emit trihedronVisibilityChanged(true);
492 //***************************************
493 void SHAPERGUI_SalomeViewer::activateViewer(bool toActivate)
495 if (!mySelector || !mySelector->viewer())
497 SUIT_ViewManager* aMgr = mySelector->viewer()->getViewManager();
498 #ifdef SALOME_PATCH_FOR_CTRL_WHEEL
499 OCCViewer_Viewer* aViewer = dynamic_cast<OCCViewer_Viewer*>(aMgr->getViewModel());
501 aViewer->setUseLocalSelection(toActivate);
503 QVector<SUIT_ViewWindow*> aViews = aMgr->getViews();
505 foreach (SUIT_ViewWindow* aView, aViews) {
506 OCCViewer_ViewFrame* aOCCView = dynamic_cast<OCCViewer_ViewFrame*>(aView);
507 OCCViewer_ViewWindow* aWnd = aOCCView->getView(OCCViewer_ViewFrame::MAIN_VIEW);
508 connect(aWnd, SIGNAL(vpTransformationFinished(OCCViewer_ViewWindow::OperationType)),
509 this, SLOT(onViewTransformed(OCCViewer_ViewWindow::OperationType)));
510 reconnectActions(aWnd, true);
513 foreach (SUIT_ViewWindow* aView, aViews) {
514 OCCViewer_ViewFrame* aOCCView = dynamic_cast<OCCViewer_ViewFrame*>(aView);
515 OCCViewer_ViewWindow* aWnd = aOCCView->getView(OCCViewer_ViewFrame::MAIN_VIEW);
516 disconnect((OCCViewer_ViewWindow*)aWnd,
517 SIGNAL(vpTransformationFinished(OCCViewer_ViewWindow::OperationType)),
518 this, SLOT(onViewTransformed(OCCViewer_ViewWindow::OperationType)));
519 reconnectActions(aWnd, false);
524 bool SHAPERGUI_SalomeViewer::isColorScaleVisible() const
527 return mySelector->viewer()->isColorScaleVisible();
532 void SHAPERGUI_SalomeViewer::setColorScaleShown(bool on)
535 mySelector->viewer()->setColorScaleShown(on);
539 void SHAPERGUI_SalomeViewer::setColorScalePosition(double theX, double theY)
542 QWidget* aWindow = activeViewPort();
543 mySelector->viewer()->getColorScale()->SetPosition(aWindow->width() * theX,
544 aWindow->height() * theY);
548 void SHAPERGUI_SalomeViewer::setColorScaleSize(double theW, double theH)
551 QWidget* aWindow = activeViewPort();
552 mySelector->viewer()->getColorScale()->SetSize(aWindow->width() * theW,
553 aWindow->height() * theH);
557 void SHAPERGUI_SalomeViewer::setColorScaleRange(double theMin, double theMax)
560 mySelector->viewer()->getColorScale()->SetRange(theMin, theMax);
564 void SHAPERGUI_SalomeViewer::setColorScaleIntervals(int theNb)
567 mySelector->viewer()->getColorScale()->SetNumberOfIntervals(theNb);
571 void SHAPERGUI_SalomeViewer::setColorScaleTextHeigth(int theH)
574 mySelector->viewer()->getColorScale()->SetTextHeight(theH);
578 void SHAPERGUI_SalomeViewer::setColorScaleTextColor(const QColor& theColor)
581 Quantity_Color aColor(theColor.redF(), theColor.greenF(), theColor.blueF(), Quantity_TOC_RGB);
582 mySelector->viewer()->getColorScale()->SetColor(aColor);
586 void SHAPERGUI_SalomeViewer::setColorScaleTitle(const QString& theText)
589 mySelector->viewer()->getColorScale()->SetTitle(theText.toStdString().c_str());
593 void SHAPERGUI_SalomeViewer::setText(
594 const ModuleBase_IViewer::TextColor& theText, const int theSize)
596 Handle(AIS_InteractiveContext) aContext = AISContext();
597 if (aContext.IsNull())
599 NCollection_List<Handle(AIS_TextLabel)>::Iterator aPrsIter(myText);
600 if (!theText.empty())
602 Quantity_Color aTextColor;
603 std::string aTextFont;
605 double anOffset = -theSize - 1; // initial offset from the toolbar of the viewer
606 ModuleBase_IViewer::TextColor::const_iterator aLine = theText.cbegin();
607 for (; aLine != theText.cend(); aLine++)
609 Quantity_Color aColor(aLine->second.at(0) / 255.,
610 aLine->second.at(1) / 255., aLine->second.at(2) / 255., Quantity_TOC_RGB);
611 if (aLine == theText.cbegin())
612 { // the first is the font name and text color
614 aTextFont = std::string(aLine->first.begin(), aLine->first.end());
618 Handle(AIS_TextLabel) aPrs;
619 if (!aPrsIter.More())
620 { // new presentation is added to the list forever
621 aPrs = new AIS_TextLabel;
622 aPrs->SetZLayer(Graphic3d_ZLayerId_TopOSD);
623 aPrs->SetHJustification(Graphic3d_HTA_RIGHT);
624 aPrs->SetVJustification(Graphic3d_VTA_TOPFIRSTLINE);
625 aPrs->SetDisplayType(Aspect_TODT_SUBTITLE);
626 aPrs->SetTransparency(0.2);
627 static const Graphic3d_Vec2i aZOffset(0);
628 aPrs->SetTransformPersistence(new Graphic3d_TransformPers(
629 Graphic3d_TMF_2d, Aspect_TOTP_RIGHT_UPPER, aZOffset));
632 { // update the existing presentation
633 aPrs = aPrsIter.ChangeValue();
634 if (!aContext->IsDisplayed(aPrs))
635 aContext->Display(aPrs, Standard_False);
637 // common part of parameters for new and existing prs
638 aPrs->SetText(aLine->first.c_str());
639 aPrs->SetFont(aTextFont.c_str());
640 aPrs->SetHeight(theSize);
641 aPrs->SetPosition(gp_Pnt(0, anOffset, 0));
642 anOffset -= int(double(theSize) * 1.2 + 1); // to have a proportional gap between lines
643 aPrs->SetColor(aTextColor);
644 aPrs->SetColorSubTitle(aColor);
645 if (!aPrsIter.More())
646 { // for the new presentation
647 aContext->Display(aPrs, Standard_False);
651 { // update existing presentation
652 aContext->Redisplay(aPrs, Standard_False, Standard_False);
657 // hide the left presentations, created previously, but unused for now
658 for (; aPrsIter.More(); aPrsIter.Next())
659 aContext->Erase(aPrsIter.ChangeValue(), Standard_False);
662 void SHAPERGUI_SalomeViewer::setFitter(OCCViewer_Fitter* theFitter)
665 mySelector->viewer()->setFitter(theFitter);
668 OCCViewer_Fitter* SHAPERGUI_SalomeViewer::fitter() const
671 return mySelector->viewer()->fitter();