diff --git a/Arrows/viewport.cpp b/Arrows/viewport.cpp index a806c92..012faa7 100644 --- a/Arrows/viewport.cpp +++ b/Arrows/viewport.cpp @@ -118,34 +118,31 @@ Viewport::~Viewport() delete d_ptr; } -bool Viewport::mouseReleased(QMouseEvent *event) +void Viewport::contextMenuRequest(const Handle(AIS_Shape) &object, + const gp_XYZ &pickedPoint, + QMenu &menu) { - if (event->button() == Qt::RightButton) { - QMenu topMenu; - topMenu.addAction(tr("Draw demo"), this, [this]() { - d_ptr->removeArrows(context()); - d_ptr->drawDemo(context()); - }); - topMenu.addSeparator(); - topMenu.addAction(tr("Draw 100K lines with arrows as one object"), this, [this]() { - d_ptr->removeArrows(context()); - d_ptr->drawWithArrows(context(), 100000); - d_ptr->tm.restart(); - QMetaObject::invokeMethod(this, [this](){ - qDebug() << "Render finish" << QString::number(d_ptr->tm.restart() / 1000., 'f') << "s."; - fitInView(); - }, Qt::QueuedConnection); - }); - topMenu.addAction(tr("Draw 10M lines without arrows as one object"), this, [this]() { - d_ptr->removeArrows(context()); - d_ptr->drawWithoutArrows(context(), 10000000); - d_ptr->tm.restart(); - QMetaObject::invokeMethod(this, [this](){ - qDebug() << "Render finish" << QString::number(d_ptr->tm.restart() / 1000., 'f') << "s."; - fitInView(); - }, Qt::QueuedConnection); - }); - return topMenu.exec(event->globalPos()) != nullptr; - } - return false; + menu.addAction(tr("Draw demo"), this, [this]() { + d_ptr->removeArrows(context()); + d_ptr->drawDemo(context()); + }); + menu.addSeparator(); + menu.addAction(tr("Draw 100K lines with arrows as one object"), this, [this]() { + d_ptr->removeArrows(context()); + d_ptr->drawWithArrows(context(), 100000); + d_ptr->tm.restart(); + QMetaObject::invokeMethod(this, [this](){ + qDebug() << "Render finish" << QString::number(d_ptr->tm.restart() / 1000., 'f') << "s."; + fitInView(); + }, Qt::QueuedConnection); + }); + menu.addAction(tr("Draw 10M lines without arrows as one object"), this, [this]() { + d_ptr->removeArrows(context()); + d_ptr->drawWithoutArrows(context(), 10000000); + d_ptr->tm.restart(); + QMetaObject::invokeMethod(this, [this](){ + qDebug() << "Render finish" << QString::number(d_ptr->tm.restart() / 1000., 'f') << "s."; + fitInView(); + }, Qt::QueuedConnection); + }); } diff --git a/Arrows/viewport.h b/Arrows/viewport.h index b5e3373..c333906 100644 --- a/Arrows/viewport.h +++ b/Arrows/viewport.h @@ -21,7 +21,9 @@ class Viewport : public ExamplesBase::Viewport void setObjectsView(QTreeView *objectsView); protected: - bool mouseReleased(QMouseEvent *event) final; + void contextMenuRequest(const Handle(AIS_Shape) &object, + const gp_XYZ &pickedPoint, + QMenu &menu) final; private: ViewportPrivate *const d_ptr; diff --git a/ExamplesBase/Viewport/viewport.cpp b/ExamplesBase/Viewport/viewport.cpp index 493a3df..d9491c1 100644 --- a/ExamplesBase/Viewport/viewport.cpp +++ b/ExamplesBase/Viewport/viewport.cpp @@ -6,6 +6,7 @@ #include "viewport.h" +#include #include #include @@ -125,7 +126,9 @@ Viewport::~Viewport() delete d_ptr; } -bool ExamplesBase::Viewport::loadModel(const QString &path, Handle(AIS_Shape) &shape, Handle(AIS_Trihedron) trihedron) const +bool ExamplesBase::Viewport::loadModel(const QString &path, + Handle(AIS_Shape) &shape, + Handle(AIS_Trihedron) trihedron) const { ExamplesBase::ModelLoaderFactoryMethod factory; auto &loader = factory.loaderByFName(path); @@ -189,6 +192,13 @@ bool ExamplesBase::Viewport::mouseMoved(QMouseEvent *) return false; } +void ExamplesBase::Viewport::contextMenuRequest(const Handle(AIS_Shape) &, + const gp_XYZ &, + QMenu &) +{ + +} + QPaintEngine *Viewport::paintEngine() const { return nullptr; @@ -265,7 +275,39 @@ void Viewport::mouseReleaseEvent(QMouseEvent *event) update(); } - if (mouseReleased(event)) { + bool needRedraw = mouseReleased(event); + if (event->button() == Qt::RightButton) { + const Graphic3d_Vec2i aPnt(event->pos().x(), event->pos().y()); + + Handle(AIS_Shape) object; + Graphic3d_Vec3d pickedPoint, projection; + view()->ConvertWithProj(aPnt.x(), aPnt.y(), + pickedPoint.x(), pickedPoint.y(), pickedPoint.z(), + projection.x(), projection.y(), projection.z()); + + auto ctx = context(); + ctx->MainSelector()->Pick(aPnt.x(), aPnt.y(), view()); + if (ctx->MainSelector()->NbPicked()) { + auto owner = ctx->MainSelector()->Picked(1); + if (owner) { + object = Handle(AIS_Shape)::DownCast(owner->Selectable()); + auto point = ctx->MainSelector()->PickedPoint(1); + if (object) { + point.Transform(ctx->Location(object).Transformation().Inverted()); + pickedPoint.SetValues(point.X(), point.Y(), point.Z()); + } + } + } + + gp_XYZ translation(pickedPoint.x(), pickedPoint.y(), pickedPoint.z()); + QMenu menu; + contextMenuRequest(object, translation, menu); + if (!menu.isEmpty() && menu.exec(event->globalPos()) != nullptr) { + needRedraw = true; + } + } + + if (needRedraw) { d_ptr->mView->Redraw(); } } diff --git a/ExamplesBase/Viewport/viewport.h b/ExamplesBase/Viewport/viewport.h index d3f02a0..edc5291 100644 --- a/ExamplesBase/Viewport/viewport.h +++ b/ExamplesBase/Viewport/viewport.h @@ -13,9 +13,12 @@ #include +class QMenu; + class AIS_InteractiveContext; class AIS_Shape; class AIS_Trihedron; +class gp_XYZ; class V3d_View; namespace ExamplesBase { @@ -28,7 +31,9 @@ class EXAMPLESBASE_EXPORT Viewport : public QWidget explicit Viewport(QWidget *parent = nullptr); ~Viewport(); - bool loadModel(const QString &path, Handle(AIS_Shape) &shape, Handle(AIS_Trihedron) trihedron = nullptr) const; + bool loadModel(const QString &path, + Handle(AIS_Shape) &shape, + Handle(AIS_Trihedron) trihedron = nullptr) const; void fitInView(); @@ -41,6 +46,9 @@ class EXAMPLESBASE_EXPORT Viewport : public QWidget virtual bool mousePressed(QMouseEvent *event); virtual bool mouseReleased(QMouseEvent *event); virtual bool mouseMoved(QMouseEvent *event); + virtual void contextMenuRequest(const Handle(AIS_Shape) &object, + const gp_XYZ &pickedPoint, + QMenu &menu); QPaintEngine* paintEngine() const final; void paintEvent(QPaintEvent *) final; diff --git a/SurfaceEditor/viewport.cpp b/SurfaceEditor/viewport.cpp index 446b680..eff6b7b 100644 --- a/SurfaceEditor/viewport.cpp +++ b/SurfaceEditor/viewport.cpp @@ -12,7 +12,6 @@ #include #include -#include #include #include @@ -37,37 +36,12 @@ void Viewport::objectsViewMenuRequest(const Handle(AIS_InteractiveObject) &obj, menuRequest(interactive, gp_XYZ(), menu); } -bool Viewport::mouseReleased(QMouseEvent *event) +void Viewport::contextMenuRequest(const Handle(AIS_Shape) &object, + const gp_XYZ &pickedPoint, + QMenu &menu) { - if (event->button() == Qt::RightButton) { - const Graphic3d_Vec2i aPnt(event->pos().x(), event->pos().y()); - - Handle(ExamplesBase::InteractiveObject) object; - Graphic3d_Vec3d pickedPoint, projection; - view()->ConvertWithProj(aPnt.x(), aPnt.y(), - pickedPoint.x(), pickedPoint.y(), pickedPoint.z(), - projection.x(), projection.y(), projection.z()); - - auto ctx = context(); - ctx->MainSelector()->Pick(aPnt.x(), aPnt.y(), view()); - if (ctx->MainSelector()->NbPicked()) { - auto owner = ctx->MainSelector()->Picked(1); - if (owner) { - object = Handle(ExamplesBase::InteractiveObject)::DownCast(owner->Selectable()); - auto point = ctx->MainSelector()->PickedPoint(1); - if (object) { - point.Transform(ctx->Location(object).Transformation().Inverted()); - pickedPoint.SetValues(point.X(), point.Y(), point.Z()); - } - } - } - - gp_XYZ translation(pickedPoint.x(), pickedPoint.y(), pickedPoint.z()); - QMenu menu; - menuRequest(object, translation, menu); - return menu.exec(event->globalPos()) != nullptr; - } - return false; + auto interactive = Handle(ExamplesBase::InteractiveObject)::DownCast(object); + menuRequest(interactive, pickedPoint, menu); } void Viewport::menuRequest(const Handle(ExamplesBase::InteractiveObject) &object, diff --git a/SurfaceEditor/viewport.h b/SurfaceEditor/viewport.h index 6cffbc9..a41231b 100644 --- a/SurfaceEditor/viewport.h +++ b/SurfaceEditor/viewport.h @@ -15,8 +15,11 @@ class Viewport : public ExamplesBase::InteractiveObjectsViewport Viewport(); protected: - void objectsViewMenuRequest(const Handle(AIS_InteractiveObject) &obj, QMenu &menu); - bool mouseReleased(QMouseEvent *event) final; + void objectsViewMenuRequest(const Handle(AIS_InteractiveObject) &obj, QMenu &menu) final; + + void contextMenuRequest(const Handle(AIS_Shape) &object, + const gp_XYZ &pickedPoint, + QMenu &menu) final; private: void menuRequest(const Handle(ExamplesBase::InteractiveObject) &object,