allow setting a QAction on push and tool buttons

svn path=/trunk/KDE/kdelibs/; revision=958514
This commit is contained in:
Aaron J. Seigo 2009-04-24 07:40:52 +00:00
parent dcf0a75754
commit 9ddc33a663
7 changed files with 117 additions and 59 deletions

View File

@ -60,7 +60,8 @@ namespace Plasma
{ {
IconWidgetPrivate::IconWidgetPrivate(IconWidget *i) IconWidgetPrivate::IconWidgetPrivate(IconWidget *i)
: q(i), : ActionWidgetInterface<IconWidget>(i),
q(i),
iconSvg(0), iconSvg(0),
hoverAnimId(-1), hoverAnimId(-1),
hoverAlpha(20 / 255), hoverAlpha(20 / 255),
@ -68,7 +69,6 @@ IconWidgetPrivate::IconWidgetPrivate(IconWidget *i)
states(IconWidgetPrivate::NoState), states(IconWidgetPrivate::NoState),
orientation(Qt::Vertical), orientation(Qt::Vertical),
numDisplayLines(2), numDisplayLines(2),
action(0),
activeMargins(0), activeMargins(0),
iconSvgElementChanged(false), iconSvgElementChanged(false),
fadeIn(false), fadeIn(false),
@ -370,19 +370,7 @@ void IconWidget::removeIconAction(QAction *action)
void IconWidget::setAction(QAction *action) void IconWidget::setAction(QAction *action)
{ {
if (d->action) { d->setAction(action);
disconnect(d->action, 0, this, 0);
disconnect(this, 0, d->action, 0);
}
d->action = action;
if (action) {
connect(action, SIGNAL(changed()), this, SLOT(syncToAction()));
connect(action, SIGNAL(destroyed(QObject*)), this, SLOT(clearAction()));
connect(this, SIGNAL(clicked()), action, SLOT(trigger()));
d->syncToAction();
}
} }
QAction *IconWidget::action() const QAction *IconWidget::action() const
@ -559,6 +547,15 @@ void IconWidget::setSvg(const QString &svgFilePath, const QString &elementId)
update(); update();
} }
QString IconWidget::svg() const
{
if (d->iconSvg) {
return d->iconSvg->imagePath();
}
return QString();
}
void IconWidgetPrivate::hoverEffect(bool show) void IconWidgetPrivate::hoverEffect(bool show)
{ {
if (show) { if (show) {
@ -1247,41 +1244,12 @@ void IconWidget::setUnpressed()
setPressed(false); setPressed(false);
} }
void IconWidgetPrivate::clearAction()
{
action = 0;
syncToAction();
emit q->changed();
}
void IconWidgetPrivate::svgChanged() void IconWidgetPrivate::svgChanged()
{ {
iconSvgElementChanged = true; iconSvgElementChanged = true;
q->update(); q->update();
} }
void IconWidgetPrivate::syncToAction()
{
if (!action) {
q->setIcon(QIcon());
q->setText(QString());
q->setEnabled(false);
return;
}
//we don't get told *what* changed, just that something changed
//so we update everything we care about
q->setIcon(action->icon());
q->setText(action->iconText());
q->setEnabled(action->isEnabled());
q->setVisible(action->isVisible());
if (!q->toolTip().isEmpty()) {
q->setToolTip(action->text());
}
emit q->changed();
}
void IconWidget::setOrientation(Qt::Orientation orientation) void IconWidget::setOrientation(Qt::Orientation orientation)
{ {
d->orientation = orientation; d->orientation = orientation;

View File

@ -109,6 +109,11 @@ public:
*/ */
void setSvg(const QString &svgFilePath, const QString &svgIconElement = QString()); void setSvg(const QString &svgFilePath, const QString &svgIconElement = QString());
/**
* @return the path to the svg file set, if any
*/
QString svg() const;
/** /**
* Returns the meta text associated with this icon. * Returns the meta text associated with this icon.
*/ */

View File

@ -35,6 +35,7 @@
#include "iconwidget.h" #include "iconwidget.h"
#include "animator.h" #include "animator.h"
#include "private/actionwidgetinterface_p.h"
class QAction; class QAction;
class QPainter; class QPainter;
@ -88,7 +89,7 @@ struct Margin
qreal left, right, top, bottom; qreal left, right, top, bottom;
}; };
class IconWidgetPrivate class IconWidgetPrivate : public ActionWidgetInterface<IconWidget>
{ {
public: public:
enum MarginType { enum MarginType {
@ -109,6 +110,11 @@ public:
IconWidgetPrivate(IconWidget *i); IconWidgetPrivate(IconWidget *i);
~IconWidgetPrivate(); ~IconWidgetPrivate();
void changed()
{
emit q->changed();
}
void drawBackground(QPainter *painter, IconWidgetState state); void drawBackground(QPainter *painter, IconWidgetState state);
void drawText(QPainter *painter); void drawText(QPainter *painter);
void drawTextItems(QPainter *painter, const QStyleOptionGraphicsItem *option, void drawTextItems(QPainter *painter, const QStyleOptionGraphicsItem *option,
@ -172,14 +178,9 @@ public:
inline QSizeF subtractMargin(const QSizeF &size, MarginType type) const; inline QSizeF subtractMargin(const QSizeF &size, MarginType type) const;
inline QRectF actionRect(ActionPosition position) const; inline QRectF actionRect(ActionPosition position) const;
/** void actionDestroyed(QObject *obj);
* update the icon's text, icon, etc. to reflect the properties of its associated action.
*/
void syncToAction();
void clearAction();
void svgChanged(); void svgChanged();
void actionDestroyed(QObject *obj);
void readColors(); void readColors();
void colorConfigChanged(); void colorConfigChanged();
void iconConfigChanged(); void iconConfigChanged();
@ -209,7 +210,6 @@ public:
QPointF clickStartPos; QPointF clickStartPos;
QList<IconAction*> cornerActions; QList<IconAction*> cornerActions;
QAction *action;
Margin verticalMargin[NMargins]; Margin verticalMargin[NMargins];
Margin horizontalMargin[NMargins]; Margin horizontalMargin[NMargins];

View File

@ -35,15 +35,17 @@
#include "framesvg.h" #include "framesvg.h"
#include "animator.h" #include "animator.h"
#include "paintutils.h" #include "paintutils.h"
#include "private/actionwidgetinterface_p.h"
namespace Plasma namespace Plasma
{ {
class PushButtonPrivate class PushButtonPrivate : public ActionWidgetInterface<PushButton>
{ {
public: public:
PushButtonPrivate(PushButton *pushButton) PushButtonPrivate(PushButton *pushButton)
: q(pushButton), : ActionWidgetInterface<PushButton>(pushButton),
q(pushButton),
background(0), background(0),
animId(-1), animId(-1),
fadeIn(false), fadeIn(false),
@ -215,6 +217,26 @@ QString PushButton::styleSheet()
return widget()->styleSheet(); return widget()->styleSheet();
} }
void PushButton::setAction(QAction *action)
{
d->setAction(action);
}
QAction *PushButton::action() const
{
return d->action;
}
void PushButton::setIcon(const QIcon &icon)
{
nativeWidget()->setIcon(icon);
}
QIcon PushButton::icon() const
{
return nativeWidget()->icon();
}
KPushButton *PushButton::nativeWidget() const KPushButton *PushButton::nativeWidget() const
{ {
return static_cast<KPushButton*>(widget()); return static_cast<KPushButton*>(widget());

View File

@ -45,6 +45,7 @@ class PLASMA_EXPORT PushButton : public QGraphicsProxyWidget
Q_PROPERTY(QString image READ image WRITE setImage) Q_PROPERTY(QString image READ image WRITE setImage)
Q_PROPERTY(QString stylesheet READ styleSheet WRITE setStyleSheet) Q_PROPERTY(QString stylesheet READ styleSheet WRITE setStyleSheet)
Q_PROPERTY(KPushButton *nativeWidget READ nativeWidget) Q_PROPERTY(KPushButton *nativeWidget READ nativeWidget)
Q_PROPERTY(QAction *action READ action WRITE setAction)
public: public:
explicit PushButton(QGraphicsWidget *parent = 0); explicit PushButton(QGraphicsWidget *parent = 0);
@ -86,6 +87,38 @@ public:
*/ */
QString styleSheet(); QString styleSheet();
/**
* Associate an action with this IconWidget
* this makes the button follow the state of the action, using its icon, text, etc.
* when the button is clicked, it will also trigger the action.
*
* @since 4.3
*/
void setAction(QAction *action);
/**
* @return the currently associated action, if any.
*
* @since 4.3
*/
QAction *action() const;
/**
* sets the icon for this toolbutton
*
* @arg icon the icon we want to use
*
* @since 4.3
*/
void setIcon(const QIcon &icon);
/**
* @return the icon of this button
*
* @since 4.3
*/
QIcon icon() const;
/** /**
* @return the native widget wrapped by this PushButton * @return the native widget wrapped by this PushButton
*/ */

View File

@ -35,15 +35,17 @@
#include "framesvg.h" #include "framesvg.h"
#include "animator.h" #include "animator.h"
#include "paintutils.h" #include "paintutils.h"
#include "private/actionwidgetinterface_p.h"
namespace Plasma namespace Plasma
{ {
class ToolButtonPrivate class ToolButtonPrivate : public ActionWidgetInterface<ToolButton>
{ {
public: public:
ToolButtonPrivate(ToolButton *toolButton) ToolButtonPrivate(ToolButton *toolButton)
: q(toolButton), : ActionWidgetInterface<ToolButton>(toolButton),
q(toolButton),
background(0), background(0),
animId(0), animId(0),
fadeIn(false), fadeIn(false),
@ -161,6 +163,16 @@ ToolButton::~ToolButton()
delete d; delete d;
} }
void ToolButton::setAction(QAction *action)
{
d->setAction(action);
}
QAction *ToolButton::action() const
{
return d->action;
}
void ToolButton::setAutoRaise(bool raise) void ToolButton::setAutoRaise(bool raise)
{ {
nativeWidget()->setAutoRaise(raise); nativeWidget()->setAutoRaise(raise);
@ -312,7 +324,6 @@ void ToolButton::paint(QPainter *painter,
} }
painter->setFont(Plasma::Theme::defaultTheme()->font(Plasma::Theme::DefaultFont)); painter->setFont(Plasma::Theme::defaultTheme()->font(Plasma::Theme::DefaultFont));
button->style()->drawControl(QStyle::CE_ToolButtonLabel, &buttonOpt, painter, button); button->style()->drawControl(QStyle::CE_ToolButtonLabel, &buttonOpt, painter, button);
} }

View File

@ -46,6 +46,7 @@ class PLASMA_EXPORT ToolButton : public QGraphicsProxyWidget
Q_PROPERTY(QString image READ image WRITE setImage) Q_PROPERTY(QString image READ image WRITE setImage)
Q_PROPERTY(QString stylesheet READ styleSheet WRITE setStyleSheet) Q_PROPERTY(QString stylesheet READ styleSheet WRITE setStyleSheet)
Q_PROPERTY(QToolButton *nativeWidget READ nativeWidget) Q_PROPERTY(QToolButton *nativeWidget READ nativeWidget)
Q_PROPERTY(QAction *action READ action WRITE setAction)
public: public:
explicit ToolButton(QGraphicsWidget *parent = 0); explicit ToolButton(QGraphicsWidget *parent = 0);
@ -100,9 +101,20 @@ public:
QString styleSheet(); QString styleSheet();
/** /**
* @return the native widget wrapped by this ToolButton * Associate an action with this IconWidget
* this makes the button follow the state of the action, using its icon, text, etc.
* when the button is clicked, it will also trigger the action.
*
* @since 4.3
*/ */
QToolButton *nativeWidget() const; void setAction(QAction *action);
/**
* @return the currently associated action, if any.
*
* @since 4.3
*/
QAction *action() const;
/** /**
* sets the icon for this toolbutton * sets the icon for this toolbutton
@ -116,6 +128,11 @@ public:
*/ */
QIcon icon() const; QIcon icon() const;
/**
* @return the native widget wrapped by this ToolButton
*/
QToolButton *nativeWidget() const;
Q_SIGNALS: Q_SIGNALS:
void clicked(); void clicked();
@ -133,6 +150,8 @@ private:
friend class ToolButtonPrivate; friend class ToolButtonPrivate;
Q_PRIVATE_SLOT(d, void syncBorders()) Q_PRIVATE_SLOT(d, void syncBorders())
Q_PRIVATE_SLOT(d, void animationUpdate(qreal progress)) Q_PRIVATE_SLOT(d, void animationUpdate(qreal progress))
Q_PRIVATE_SLOT(d, void syncToAction())
Q_PRIVATE_SLOT(d, void clearAction())
}; };
} // namespace Plasma } // namespace Plasma