From 64c8ec77f891ea673c83224e789a2037c754d7af Mon Sep 17 00:00:00 2001 From: PaddleStroke Date: Sun, 20 Jul 2025 19:47:21 +0200 Subject: [PATCH] Sketcher: Dimension: Offset the label when moving mouse (#22387) * Sketcher: Dimension: Offset the label when moving mouse * Sketcher: Create dedicated enum for offset boolean This improves readability because the intent must be stated explicitly. --------- Co-authored-by: Kacper Donat --- src/Mod/Sketcher/Gui/CommandConstraints.cpp | 2 +- src/Mod/Sketcher/Gui/DrawSketchHandler.cpp | 12 ++++++---- src/Mod/Sketcher/Gui/DrawSketchHandler.h | 10 ++++---- src/Mod/Sketcher/Gui/Utils.h | 9 +++++++ src/Mod/Sketcher/Gui/ViewProviderSketch.cpp | 26 +++++++++++++++++---- src/Mod/Sketcher/Gui/ViewProviderSketch.h | 8 +++++-- 6 files changed, 51 insertions(+), 16 deletions(-) diff --git a/src/Mod/Sketcher/Gui/CommandConstraints.cpp b/src/Mod/Sketcher/Gui/CommandConstraints.cpp index 833d9a0d0d..c031eefdec 100644 --- a/src/Mod/Sketcher/Gui/CommandConstraints.cpp +++ b/src/Mod/Sketcher/Gui/CommandConstraints.cpp @@ -1700,7 +1700,7 @@ public: else pointWhereToMove.x = Obj->getPoint(selPoints[0].GeoId, selPoints[0].PosId).x; } - moveConstraint(index, pointWhereToMove); + moveConstraint(index, pointWhereToMove, OffsetConstraint); oneMoved = true; } } diff --git a/src/Mod/Sketcher/Gui/DrawSketchHandler.cpp b/src/Mod/Sketcher/Gui/DrawSketchHandler.cpp index 640dbf5b26..26b60ecd6d 100644 --- a/src/Mod/Sketcher/Gui/DrawSketchHandler.cpp +++ b/src/Mod/Sketcher/Gui/DrawSketchHandler.cpp @@ -150,9 +150,10 @@ ViewProviderSketchDrawSketchHandlerAttorney::setAngleSnapping(ViewProviderSketch inline void ViewProviderSketchDrawSketchHandlerAttorney::moveConstraint(ViewProviderSketch& vp, int constNum, - const Base::Vector2d& toPos) + const Base::Vector2d& toPos, + OffsetMode offset) { - vp.moveConstraint(constNum, toPos); + vp.moveConstraint(constNum, toPos, offset); } inline void @@ -1092,9 +1093,12 @@ void DrawSketchHandler::setAngleSnapping(bool enable, Base::Vector2d referencePo referencePoint); } -void DrawSketchHandler::moveConstraint(int constNum, const Base::Vector2d& toPos) +void DrawSketchHandler::moveConstraint(int constNum, const Base::Vector2d& toPos, OffsetMode offset) { - ViewProviderSketchDrawSketchHandlerAttorney::moveConstraint(*sketchgui, constNum, toPos); + ViewProviderSketchDrawSketchHandlerAttorney::moveConstraint(*sketchgui, + constNum, + toPos, + offset); } void DrawSketchHandler::signalToolChanged() const diff --git a/src/Mod/Sketcher/Gui/DrawSketchHandler.h b/src/Mod/Sketcher/Gui/DrawSketchHandler.h index 7a434e4460..1df188fc7a 100644 --- a/src/Mod/Sketcher/Gui/DrawSketchHandler.h +++ b/src/Mod/Sketcher/Gui/DrawSketchHandler.h @@ -38,6 +38,7 @@ #include #include "AutoConstraint.h" +#include "Utils.h" class QWidget; @@ -84,7 +85,6 @@ private: int curvedEdgeCountSegments; }; - /** * In order to enforce a certain degree of encapsulation and promote a not * too tight coupling, while still allowing well defined collaboration, @@ -117,8 +117,10 @@ private: static inline int getPreselectCurve(const ViewProviderSketch& vp); static inline int getPreselectCross(const ViewProviderSketch& vp); - static inline void - moveConstraint(ViewProviderSketch& vp, int constNum, const Base::Vector2d& toPos); + static inline void moveConstraint(ViewProviderSketch& vp, + int constNum, + const Base::Vector2d& toPos, + OffsetMode offset = NoOffset); static inline void signalToolChanged(const ViewProviderSketch& vp, const std::string& toolname); @@ -277,7 +279,7 @@ protected: void setAngleSnapping(bool enable, Base::Vector2d referencePoint = Base::Vector2d(0., 0.)); - void moveConstraint(int constNum, const Base::Vector2d& toPos); + void moveConstraint(int constNum, const Base::Vector2d& toPos, OffsetMode offset = NoOffset); void signalToolChanged() const; diff --git a/src/Mod/Sketcher/Gui/Utils.h b/src/Mod/Sketcher/Gui/Utils.h index bbe864018c..4e39d199d9 100644 --- a/src/Mod/Sketcher/Gui/Utils.h +++ b/src/Mod/Sketcher/Gui/Utils.h @@ -75,6 +75,15 @@ namespace SketcherGui class DrawSketchHandler; class ViewProviderSketch; +enum OffsetMode : bool +{ + NoOffset = false, + OffsetConstraint = true +}; + +// to improve readability, expose the enum cases directly in the namespace +using enum OffsetMode; + /// This function tries to auto-recompute the active document if the option /// is set in the user parameter. If the option is not set nothing will be done /// @return true if a recompute was undertaken, false if not. diff --git a/src/Mod/Sketcher/Gui/ViewProviderSketch.cpp b/src/Mod/Sketcher/Gui/ViewProviderSketch.cpp index db44327774..154d4f644f 100644 --- a/src/Mod/Sketcher/Gui/ViewProviderSketch.cpp +++ b/src/Mod/Sketcher/Gui/ViewProviderSketch.cpp @@ -1753,14 +1753,14 @@ void ViewProviderSketch::commitDragMove(double x, double y) resetPositionText(); } -void ViewProviderSketch::moveConstraint(int constNum, const Base::Vector2d& toPos) +void ViewProviderSketch::moveConstraint(int constNum, const Base::Vector2d& toPos, OffsetMode offset) { if (auto constr = getConstraint(constNum)) { - moveConstraint(constr, constNum, toPos); + moveConstraint(constr, constNum, toPos, offset); } } -void ViewProviderSketch::moveConstraint(Sketcher::Constraint* Constr, int constNum, const Base::Vector2d& toPos) +void ViewProviderSketch::moveConstraint(Sketcher::Constraint* Constr, int constNum, const Base::Vector2d& toPos, OffsetMode offset) { // are we in edit? if (!isInEditMode()) @@ -1942,13 +1942,29 @@ void ViewProviderSketch::moveConstraint(Sketcher::Constraint* Constr, int constN else if (Constr->Type == DistanceY) dir = Base::Vector3d(0, (p2.y - p1.y >= std::numeric_limits::epsilon()) ? 1 : -1, 0); + double offsetVal = 0.0; + if (offset == OffsetConstraint) { + if (auto* view = qobject_cast(this->getActiveView())) { + Gui::View3DInventorViewer* viewer = view->getViewer(); + float fHeight = -1.0; + float fWidth = -1.0; + viewer->getDimensions(fHeight, fWidth); + offsetVal = (fHeight + fWidth) * 0.01; + } + } + if (Constr->Type == Radius || Constr->Type == Diameter || Constr->Type == Weight) { - Constr->LabelDistance = vec.x * dir.x + vec.y * dir.y; + double distance = vec.x * dir.x + vec.y * dir.y; + if (distance > offsetVal) { + distance -= offsetVal; + } + Constr->LabelDistance = distance; Constr->LabelPosition = atan2(dir.y, dir.x); } else { Base::Vector3d normal(-dir.y, dir.x, 0); - Constr->LabelDistance = vec.x * normal.x + vec.y * normal.y; + double distance = vec.x * normal.x + vec.y * normal.y - offsetVal; + Constr->LabelDistance = distance; if (Constr->Type == Distance || Constr->Type == DistanceX || Constr->Type == DistanceY) { vec = Base::Vector3d(toPos.x, toPos.y, 0) - (p2 + p1) / 2; diff --git a/src/Mod/Sketcher/Gui/ViewProviderSketch.h b/src/Mod/Sketcher/Gui/ViewProviderSketch.h index 9a7932bddf..dceb381af3 100644 --- a/src/Mod/Sketcher/Gui/ViewProviderSketch.h +++ b/src/Mod/Sketcher/Gui/ViewProviderSketch.h @@ -43,6 +43,7 @@ #include "PropertyVisualLayerList.h" #include "ShortcutListener.h" +#include "Utils.h" class TopoDS_Shape; @@ -808,8 +809,11 @@ private: /** @name miscelanea utilities */ //@{ /// moves a selected constraint - void moveConstraint(int constNum, const Base::Vector2d& toPos); - void moveConstraint(Sketcher::Constraint*, int constNum, const Base::Vector2d& toPos); + void moveConstraint(int constNum, const Base::Vector2d& toPos, OffsetMode offset = NoOffset); + void moveConstraint(Sketcher::Constraint*, + int constNum, + const Base::Vector2d& toPos, + OffsetMode offset = NoOffset); void moveAngleConstraint(Sketcher::Constraint*, int constNum, const Base::Vector2d& toPos); /// returns whether the sketch is in edit mode.