From 6509cc72e730e69278068d13078606828eade62a Mon Sep 17 00:00:00 2001 From: wmayer Date: Sat, 8 Apr 2023 12:37:15 +0200 Subject: [PATCH] Gui: add method to convert between QPoint and SbVec2s considering device pixel ratio --- src/Gui/View3DInventorViewer.cpp | 28 ++++++++++++++++++++++++++++ src/Gui/View3DInventorViewer.h | 21 +++++++++++++++++++++ src/Gui/View3DPy.cpp | 6 ++++-- 3 files changed, 53 insertions(+), 2 deletions(-) diff --git a/src/Gui/View3DInventorViewer.cpp b/src/Gui/View3DInventorViewer.cpp index 82f915810e..441136456f 100644 --- a/src/Gui/View3DInventorViewer.cpp +++ b/src/Gui/View3DInventorViewer.cpp @@ -2424,6 +2424,34 @@ SbVec2s View3DInventorViewer::getPointOnScreen(const SbVec3f& pnt) const return SbVec2s(x, y); } +QPoint View3DInventorViewer::toQPoint(const SbVec2s& pnt) const +{ + const SbViewportRegion& vp = this->getSoRenderManager()->getViewportRegion(); + const SbVec2s& vps = vp.getViewportSizePixels(); + int xpos = pnt[0]; + int ypos = vps[1] - pnt[0] - 1; + + qreal dev_pix_ratio = devicePixelRatio(); + xpos = int(std::roundf(xpos / dev_pix_ratio)); + ypos = int(std::roundf(ypos / dev_pix_ratio)); + + return QPoint(xpos, ypos); +} + +SbVec2s View3DInventorViewer::fromQPoint(const QPoint& pnt) const +{ + const SbViewportRegion& vp = this->getSoRenderManager()->getViewportRegion(); + const SbVec2s& vps = vp.getViewportSizePixels(); + int xpos = pnt.x(); + int ypos = pnt.y(); + + qreal dev_pix_ratio = devicePixelRatio(); + xpos = int(std::roundf(xpos * dev_pix_ratio)); + ypos = int(std::roundf(ypos * dev_pix_ratio)); + + return SbVec2s(short(xpos), vps[1] - short(ypos) - 1); +} + void View3DInventorViewer::getNearPlane(SbVec3f& rcPt, SbVec3f& rcNormal) const { SoCamera* pCam = getSoRenderManager()->getCamera(); diff --git a/src/Gui/View3DInventorViewer.h b/src/Gui/View3DInventorViewer.h index c7331bf279..4db02acc94 100644 --- a/src/Gui/View3DInventorViewer.h +++ b/src/Gui/View3DInventorViewer.h @@ -305,27 +305,48 @@ public: void setViewDirection(SbVec3f); /** Returns the up direction */ SbVec3f getUpDirection() const; + /** Returns the orientation of the camera. */ SbRotation getCameraOrientation() const; + /** Returns the 3d point on the focal plane to the given 2d point. */ SbVec3f getPointOnFocalPlane(const SbVec2s&) const; + /** Returns the 2d coordinates on the screen to the given 3d point. */ SbVec2s getPointOnScreen(const SbVec3f&) const; + + /** Converts Inventor coordinates into Qt coordinates. + * The conversion takes the device pixel ratio into account. + */ + QPoint toQPoint(const SbVec2s&) const; + + /** Converts Qt coordinates into Inventor coordinates. + * The conversion takes the device pixel ratio into account. + */ + SbVec2s fromQPoint(const QPoint&) const; + /** Returns the near plane represented by its normal and base point. */ void getNearPlane(SbVec3f& rcPt, SbVec3f& rcNormal) const; + /** Returns the far plane represented by its normal and base point. */ void getFarPlane(SbVec3f& rcPt, SbVec3f& rcNormal) const; + /** Adds or remove a manipulator to/from the scenegraph. */ void toggleClippingPlane(int toggle=-1, bool beforeEditing=false, bool noManip=false, const Base::Placement &pla = Base::Placement()); + /** Checks whether a clipping plane is set or not. */ bool hasClippingPlane() const; + /** Project the given normalized 2d point onto the near plane */ SbVec3f projectOnNearPlane(const SbVec2f&) const; + /** Project the given normalized 2d point onto the far plane */ SbVec3f projectOnFarPlane(const SbVec2f&) const; + /** Project the given 2d point to a line */ void projectPointToLine(const SbVec2s&, SbVec3f& pt1, SbVec3f& pt2) const; + /** Get the normalized position of the 2d point. */ SbVec2f getNormalizedPosition(const SbVec2s&) const; //@} diff --git a/src/Gui/View3DPy.cpp b/src/Gui/View3DPy.cpp index 2f5608be3e..beb194ccfe 100644 --- a/src/Gui/View3DPy.cpp +++ b/src/Gui/View3DPy.cpp @@ -1312,9 +1312,11 @@ Py::Object View3DInventorPy::getCursorPos(const Py::Tuple& args) throw Py::Exception(); try { QPoint pos = getView3DIventorPtr()->mapFromGlobal(QCursor::pos()); + auto viewer = getView3DIventorPtr()->getViewer(); + SbVec2s vec = viewer->fromQPoint(pos); Py::Tuple tuple(2); - tuple.setItem(0, Py::Int(pos.x())); - tuple.setItem(1, Py::Int(getView3DIventorPtr()->height()-pos.y()-1)); + tuple.setItem(0, Py::Int(vec[0])); + tuple.setItem(1, Py::Int(vec[1])); return tuple; } catch (const Py::Exception&) {