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 <kadet1090@gmail.com>
This commit is contained in:
PaddleStroke
2025-07-20 19:47:21 +02:00
committed by GitHub
parent d3887d09a4
commit 64c8ec77f8
6 changed files with 51 additions and 16 deletions

View File

@@ -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;
}
}

View File

@@ -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

View File

@@ -38,6 +38,7 @@
#include <Mod/Sketcher/App/Constraint.h>
#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;

View File

@@ -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.

View File

@@ -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<float>::epsilon()) ? 1 : -1, 0);
double offsetVal = 0.0;
if (offset == OffsetConstraint) {
if (auto* view = qobject_cast<Gui::View3DInventor*>(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;

View File

@@ -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.