From 291b11f8a2875654a23aed2e56450adfc6e93e9a Mon Sep 17 00:00:00 2001 From: Marco Martin Date: Thu, 7 Jan 2010 12:44:20 +0000 Subject: [PATCH] -update geometry when the text changes -return a good size hint based on svg margins svn path=/trunk/KDE/kdelibs/; revision=1071052 --- widgets/toolbutton.cpp | 24 ++++++++++++++++++++---- widgets/toolbutton.h | 1 + 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/widgets/toolbutton.cpp b/widgets/toolbutton.cpp index 86e2ec3d8..1e3ba12fa 100644 --- a/widgets/toolbutton.cpp +++ b/widgets/toolbutton.cpp @@ -170,6 +170,11 @@ ToolButton::ToolButton(QGraphicsWidget *parent) : QGraphicsProxyWidget(parent), d(new ToolButtonPrivate(this)) { + d->background = new FrameSvg(this); + d->background->setImagePath("widgets/button"); + d->background->setCacheAllRenderedFrames(true); + d->background->setElementPrefix("normal"); + QToolButton *native = new QToolButton; connect(native, SIGNAL(clicked()), this, SIGNAL(clicked())); connect(native, SIGNAL(pressed()), this, SIGNAL(pressed())); @@ -178,10 +183,6 @@ ToolButton::ToolButton(QGraphicsWidget *parent) native->setAttribute(Qt::WA_NoSystemBackground); native->setAutoRaise(true); - d->background = new FrameSvg(this); - d->background->setImagePath("widgets/button"); - d->background->setCacheAllRenderedFrames(true); - d->background->setElementPrefix("normal"); d->syncBorders(); setAcceptHoverEvents(true); connect(Plasma::Theme::defaultTheme(), SIGNAL(themeChanged()), SLOT(syncBorders())); @@ -215,6 +216,7 @@ bool ToolButton::autoRaise() const void ToolButton::setText(const QString &text) { static_cast(widget())->setText(text); + updateGeometry(); } QString ToolButton::text() const @@ -430,6 +432,20 @@ void ToolButton::changeEvent(QEvent *event) QGraphicsProxyWidget::changeEvent(event); } +QSizeF ToolButton::sizeHint(Qt::SizeHint which, const QSizeF & constraint) const +{ + QSizeF hint = QGraphicsProxyWidget::sizeHint(which, constraint); + + //replace the native margin with the Svg one + QStyleOption option; + option.initFrom(nativeWidget()); + int nativeMargin = nativeWidget()->style()->pixelMetric(QStyle::PM_ButtonMargin, &option, nativeWidget()); + qreal left, top, right, bottom; + d->background->getMargins(left, top, right, bottom); + hint = hint - QSize(nativeMargin, nativeMargin) + QSize(left+right, top+bottom); + return hint; +} + } // namespace Plasma #include diff --git a/widgets/toolbutton.h b/widgets/toolbutton.h index 9a6a5d81d..0e1222589 100644 --- a/widgets/toolbutton.h +++ b/widgets/toolbutton.h @@ -172,6 +172,7 @@ protected: void hoverEnterEvent(QGraphicsSceneHoverEvent *event); void hoverLeaveEvent(QGraphicsSceneHoverEvent *event); void changeEvent(QEvent *event); + QSizeF sizeHint(Qt::SizeHint which, const QSizeF & constraint) const; private: Q_PRIVATE_SLOT(d, void syncBorders())