Skip to content

Commit

Permalink
Introduce contextMenuRequest() in Viewport
Browse files Browse the repository at this point in the history
  • Loading branch information
ncPUMA committed Jan 20, 2024
1 parent 6271cff commit cae6d36
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 66 deletions.
55 changes: 26 additions & 29 deletions Arrows/viewport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
}
4 changes: 3 additions & 1 deletion Arrows/viewport.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
46 changes: 44 additions & 2 deletions ExamplesBase/Viewport/viewport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include "viewport.h"

#include <QMenu>
#include <QMouseEvent>

#include <AIS_InteractiveContext.hxx>
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
}
Expand Down
10 changes: 9 additions & 1 deletion ExamplesBase/Viewport/viewport.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@

#include <Standard_Handle.hxx>

class QMenu;

class AIS_InteractiveContext;
class AIS_Shape;
class AIS_Trihedron;
class gp_XYZ;
class V3d_View;

namespace ExamplesBase {
Expand All @@ -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();

Expand All @@ -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;
Expand Down
36 changes: 5 additions & 31 deletions SurfaceEditor/viewport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

#include <AIS_InteractiveContext.hxx>
#include <BRepAlgoAPI_Cut.hxx>
#include <V3d_View.hxx>

#include <ExamplesBase/ObjectModels/objectstreemodel.h>
#include <ExamplesBase/ObjectModels/interactiveobjectitemmodelshape.h>
Expand All @@ -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,
Expand Down
7 changes: 5 additions & 2 deletions SurfaceEditor/viewport.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit cae6d36

Please sign in to comment.