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)
: q(i),
: ActionWidgetInterface<IconWidget>(i),
q(i),
iconSvg(0),
hoverAnimId(-1),
hoverAlpha(20 / 255),
@ -68,7 +69,6 @@ IconWidgetPrivate::IconWidgetPrivate(IconWidget *i)
states(IconWidgetPrivate::NoState),
orientation(Qt::Vertical),
numDisplayLines(2),
action(0),
activeMargins(0),
iconSvgElementChanged(false),
fadeIn(false),
@ -370,19 +370,7 @@ void IconWidget::removeIconAction(QAction *action)
void IconWidget::setAction(QAction *action)
{
if (d->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();
}
d->setAction(action);
}
QAction *IconWidget::action() const
@ -559,6 +547,15 @@ void IconWidget::setSvg(const QString &svgFilePath, const QString &elementId)
update();
}
QString IconWidget::svg() const
{
if (d->iconSvg) {
return d->iconSvg->imagePath();
}
return QString();
}
void IconWidgetPrivate::hoverEffect(bool show)
{
if (show) {
@ -1247,41 +1244,12 @@ void IconWidget::setUnpressed()
setPressed(false);
}
void IconWidgetPrivate::clearAction()
{
action = 0;
syncToAction();
emit q->changed();
}
void IconWidgetPrivate::svgChanged()
{
iconSvgElementChanged = true;
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)
{
d->orientation = orientation;

View File

@ -109,6 +109,11 @@ public:
*/
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.
*/

View File

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

View File

@ -35,15 +35,17 @@
#include "framesvg.h"
#include "animator.h"
#include "paintutils.h"
#include "private/actionwidgetinterface_p.h"
namespace Plasma
{
class PushButtonPrivate
class PushButtonPrivate : public ActionWidgetInterface<PushButton>
{
public:
PushButtonPrivate(PushButton *pushButton)
: q(pushButton),
: ActionWidgetInterface<PushButton>(pushButton),
q(pushButton),
background(0),
animId(-1),
fadeIn(false),
@ -215,6 +217,26 @@ QString PushButton::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
{
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 stylesheet READ styleSheet WRITE setStyleSheet)
Q_PROPERTY(KPushButton *nativeWidget READ nativeWidget)
Q_PROPERTY(QAction *action READ action WRITE setAction)
public:
explicit PushButton(QGraphicsWidget *parent = 0);
@ -86,6 +87,38 @@ public:
*/
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
*/

View File

@ -35,15 +35,17 @@
#include "framesvg.h"
#include "animator.h"
#include "paintutils.h"
#include "private/actionwidgetinterface_p.h"
namespace Plasma
{
class ToolButtonPrivate
class ToolButtonPrivate : public ActionWidgetInterface<ToolButton>
{
public:
ToolButtonPrivate(ToolButton *toolButton)
: q(toolButton),
: ActionWidgetInterface<ToolButton>(toolButton),
q(toolButton),
background(0),
animId(0),
fadeIn(false),
@ -161,6 +163,16 @@ ToolButton::~ToolButton()
delete d;
}
void ToolButton::setAction(QAction *action)
{
d->setAction(action);
}
QAction *ToolButton::action() const
{
return d->action;
}
void ToolButton::setAutoRaise(bool raise)
{
nativeWidget()->setAutoRaise(raise);
@ -312,7 +324,6 @@ void ToolButton::paint(QPainter *painter,
}
painter->setFont(Plasma::Theme::defaultTheme()->font(Plasma::Theme::DefaultFont));
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 stylesheet READ styleSheet WRITE setStyleSheet)
Q_PROPERTY(QToolButton *nativeWidget READ nativeWidget)
Q_PROPERTY(QAction *action READ action WRITE setAction)
public:
explicit ToolButton(QGraphicsWidget *parent = 0);
@ -100,9 +101,20 @@ public:
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
@ -116,6 +128,11 @@ public:
*/
QIcon icon() const;
/**
* @return the native widget wrapped by this ToolButton
*/
QToolButton *nativeWidget() const;
Q_SIGNALS:
void clicked();
@ -133,6 +150,8 @@ private:
friend class ToolButtonPrivate;
Q_PRIVATE_SLOT(d, void syncBorders())
Q_PRIVATE_SLOT(d, void animationUpdate(qreal progress))
Q_PRIVATE_SLOT(d, void syncToAction())
Q_PRIVATE_SLOT(d, void clearAction())
};
} // namespace Plasma