From e3927a01058914bc693031dd68f9e8f32bc82f41 Mon Sep 17 00:00:00 2001 From: Alfredo Monclus Date: Tue, 4 Feb 2025 06:49:07 -0300 Subject: [PATCH] refactor: actionlabel clenup and improve size calculation --- src/Gui/QSint/actionpanel/actionlabel.cpp | 53 ++++++++--------------- 1 file changed, 17 insertions(+), 36 deletions(-) diff --git a/src/Gui/QSint/actionpanel/actionlabel.cpp b/src/Gui/QSint/actionpanel/actionlabel.cpp index fbbae82c7f..1a2f18229e 100644 --- a/src/Gui/QSint/actionpanel/actionlabel.cpp +++ b/src/Gui/QSint/actionpanel/actionlabel.cpp @@ -10,35 +10,28 @@ #include #include - namespace QSint { - -ActionLabel::ActionLabel(QWidget *parent) : - QToolButton(parent) +ActionLabel::ActionLabel(QWidget *parent) + : QToolButton(parent) { init(); } -ActionLabel::ActionLabel(QAction *action, QWidget *parent) : - QToolButton(parent) +ActionLabel::ActionLabel(QAction *action, QWidget *parent) + : QToolButton(parent) { init(); - setDefaultAction(action); } void ActionLabel::init() { setProperty("class", "action"); - setCursor(Qt::PointingHandCursor); - setToolButtonStyle(Qt::ToolButtonTextBesideIcon); - setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred); - setFocusPolicy(Qt::StrongFocus); } @@ -46,37 +39,27 @@ QSize ActionLabel::sizeHint() const { ensurePolished(); - int w = 0, h = 0; - QStyleOptionToolButton opt; initStyleOption(&opt); - QString s(text()); - bool empty = s.isEmpty(); - if (empty) - s = QStringLiteral("XXXX"); - QFontMetrics fm = fontMetrics(); - QSize sz = fm.size(Qt::TextShowMnemonic, s); - w += sz.width(); - h = qMax(h, sz.height()); - opt.rect.setSize(QSize(w, h)); // PM_MenuButtonIndicator depends on the height + QString s = text().isEmpty() ? QString::fromLatin1("XXXX") : text(); + QSize textSize = fontMetrics().size(Qt::TextShowMnemonic, s); + const int padding = 10; + int width = textSize.width(); if (!icon().isNull()) { - int ih = opt.iconSize.height(); - int iw = opt.iconSize.width() + 4; - w += iw; - h = qMax(h, ih); + width += opt.iconSize.width() + padding; } - if (menu()) - w += style()->pixelMetric(QStyle::PM_MenuButtonIndicator, &opt, this); + if (menu()) { + width += style()->pixelMetric(QStyle::PM_MenuButtonIndicator, &opt, this); + } - h += 4; - w += 8; + int height = qMax(textSize.height(), opt.iconSize.height()); - QSize sizeHint = style()->sizeFromContents(QStyle::CT_PushButton, &opt, QSize(w, h), this); - - return sizeHint; + return style()->sizeFromContents( + QStyle::CT_PushButton, &opt, QSize(width + 2 * padding, height + padding), this + ); } QSize ActionLabel::minimumSizeHint() const @@ -84,6 +67,4 @@ QSize ActionLabel::minimumSizeHint() const return sizeHint(); } - -} // namespace - +} // namespace QSint