refactor: actionlabel clenup and improve size calculation
This commit is contained in:
@@ -10,35 +10,28 @@
|
||||
#include <QApplication>
|
||||
#include <QStyleOptionToolButton>
|
||||
|
||||
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user