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 <alfre@localhost.localdomain>
This commit is contained in:
Alfredo Monclus
2025-04-09 15:28:54 -03:00
committed by GitHub
parent f258a2639c
commit de124a72b5
2 changed files with 18 additions and 8 deletions

View File

@@ -25,6 +25,7 @@
#include <QLabel>
#include <QMouseEvent>
#include <QLineEdit>
class ExpressionLabel : public QLabel
{
@@ -39,6 +40,16 @@ public:
this->setToolTip(expressionEditorTooltipPrefix + text);
}
void show() {
if (auto parentLineEdit = qobject_cast<QLineEdit*>(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()))

View File

@@ -50,18 +50,17 @@ ExpressionSpinBox::ExpressionSpinBox(QAbstractSpinBox* sb)
: spinbox(sb)
{
lineedit = spinbox->findChild<QLineEdit*>();
// 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;