From de124a72b56515a34f8ed627a768b54695698fc8 Mon Sep 17 00:00:00 2001 From: Alfredo Monclus Date: Wed, 9 Apr 2025 15:28:54 -0300 Subject: [PATCH] Gui: fix Quantity spinbox margin without an icon (#20695) * Gui: fix Quantity spinbox margin without an icon * refactor: apply review suggestions * refactor: reorder operations as suggested --------- Co-authored-by: alfre --- src/Gui/QuantitySpinBox_p.h | 11 +++++++++++ src/Gui/SpinBox.cpp | 15 +++++++-------- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/src/Gui/QuantitySpinBox_p.h b/src/Gui/QuantitySpinBox_p.h index 2c084b9d50..1abf8f6cf9 100644 --- a/src/Gui/QuantitySpinBox_p.h +++ b/src/Gui/QuantitySpinBox_p.h @@ -25,6 +25,7 @@ #include #include +#include class ExpressionLabel : public QLabel { @@ -39,6 +40,16 @@ public: this->setToolTip(expressionEditorTooltipPrefix + text); } + void show() { + if (auto parentLineEdit = qobject_cast(parent())) { + // horizontal margin, so text will not be behind the icon + QMargins margins = parentLineEdit->contentsMargins(); + margins.setRight(2 * margins.right() + sizeHint().width()); + parentLineEdit->setContentsMargins(margins); + } + QLabel::show(); + } + protected: void mouseReleaseEvent(QMouseEvent * event) override { if (rect().contains(event->pos())) diff --git a/src/Gui/SpinBox.cpp b/src/Gui/SpinBox.cpp index 931d8f2fdb..005fafdb9b 100644 --- a/src/Gui/SpinBox.cpp +++ b/src/Gui/SpinBox.cpp @@ -50,18 +50,17 @@ ExpressionSpinBox::ExpressionSpinBox(QAbstractSpinBox* sb) : spinbox(sb) { lineedit = spinbox->findChild(); + // Set Margins + // https://forum.freecad.org/viewtopic.php?f=8&t=50615 + // vertical margin, otherwise `,` is clipped to a `.` on some OSX versions + int margin = getMargin(); + lineedit->setTextMargins(margin, margin, margin, margin); + lineedit->setAlignment(Qt::AlignVCenter); + makeLabel(lineedit); QObject::connect(iconLabel, &ExpressionLabel::clicked, [this]() { this->openFormulaDialog(); }); - - // Set Margins - // vertical to avoid this: https://forum.freecad.org/viewtopic.php?f=8&t=50615 - // horizontal to avoid going under the icon - lineedit->setAlignment(Qt::AlignVCenter); - int iconWidth = iconLabel->sizeHint().width(); - int margin = getMargin(); - lineedit->setTextMargins(margin, margin, margin + iconWidth, margin); } ExpressionSpinBox::~ExpressionSpinBox() = default;