Adds toggle support to Plasma::PushButton.

svn path=/trunk/KDE/kdebase/workspace/libs/plasma/; revision=721138
This commit is contained in:
Petri Damstén 2007-10-04 17:36:21 +00:00
parent 2fe2f1bca9
commit 940f7e00fb
2 changed files with 67 additions and 5 deletions

View File

@ -54,6 +54,8 @@ public:
public:
Private()
: flat(false),
checkable(false),
checked(false),
state(None)
{}
@ -65,6 +67,8 @@ public:
KIcon icon;
QSizeF iconSize;
bool flat;
bool checkable;
bool checked;
ButtonState state;
};
@ -102,7 +106,7 @@ void PushButton::Private::initStyleOption(QStyleOptionButton *option, const Push
if (flat) {
option->features |= QStyleOptionButton::Flat;
}
if (!flat && !(state == Private::Pressed)) {
if (!flat && !(checked || state == Private::Pressed)) {
option->state |= QStyle::State_Raised;
} else {
option->state |= QStyle::State_Sunken;
@ -216,6 +220,29 @@ void PushButton::setFlat(bool flat)
update();
}
bool PushButton::isChecked() const
{
return d->checked;
}
void PushButton::setChecked(bool checked)
{
if (isCheckable()) {
d->checked = checked;
update();
}
}
bool PushButton::isCheckable() const
{
return d->checkable;
}
void PushButton::setCheckable(bool checkable)
{
d->checkable = checkable;
}
void PushButton::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
event->accept();
@ -226,9 +253,15 @@ void PushButton::mousePressEvent(QGraphicsSceneMouseEvent *event)
void PushButton::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
event->accept();
if (d->state == Private::Pressed)
if (d->state == Private::Pressed) {
d->state = Private::Released;
emit clicked();
d->state = Private::Released;
if (d->checkable) {
d->checked = ! d->checked;
emit toggled(d->checked);
}
}
update();
}
@ -261,7 +294,7 @@ QSizeF PushButton::sizeHint() const
QSize textSize = option.fontMetrics.size(Qt::TextShowMnemonic, display);
width += textSize.width();
height = qMax(height, textSize.height());
return QSizeF((QApplication::style()->sizeFromContents(QStyle::CT_PushButton, &option, QSize(width, height), 0).
expandedTo(QApplication::globalStrut())));
}

View File

@ -39,7 +39,7 @@ namespace Plasma
/**
* Class that emulates a QPushButton inside Plasma
*
* @author Siraj Razick
* @author Siraj Razick
* @author Matias Valdenegro
* @author Matt Broadstone
*
@ -52,6 +52,8 @@ class PLASMA_EXPORT PushButton : public Plasma::Widget
Q_PROPERTY( QSizeF iconSize READ iconSize WRITE setIconSize )
Q_PROPERTY( KIcon icon READ icon WRITE setIcon )
Q_PROPERTY( bool flat READ isFlat WRITE setFlat )
Q_PROPERTY( bool checkable READ isCheckable WRITE setCheckable )
Q_PROPERTY( bool checked READ isChecked WRITE setChecked)
public:
/**
* Creates a new Plasma::PushButton.
@ -136,6 +138,28 @@ public:
*/
void setFlat(bool flat);
/**
* @return whether this button is checkable.
*/
bool isCheckable() const;
/**
* Sets whether the button is checkable.
* @param checkable whether button is checkable or not.
*/
void setCheckable(bool checkable);
/**
* @return whether this button is checked.
*/
bool isChecked() const;
/**
* Sets whether the button is checked.
* @param checked whether button is checked or not.
*/
void setChecked(bool checked);
// NOTE: bogus
QSizeF minimumSize() const;
Qt::Orientations expandingDirections() const;
@ -147,6 +171,11 @@ Q_SIGNALS:
*/
void clicked();
/**
* Triggered when the checkable button has been toggled.
*/
void toggled(bool checked);
protected:
void mousePressEvent(QGraphicsSceneMouseEvent *event);
void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);