From 3294b91379783cfb3530b0726a54d08395dbc3d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Fern=C3=A1ndez=20L=C3=B3pez?= Date: Sat, 19 May 2007 20:49:16 +0000 Subject: [PATCH] New widget: radio button svn path=/trunk/KDE/kdebase/workspace/plasma/lib/; revision=666420 --- CMakeLists.txt | 1 + widgets/radiobutton.cpp | 201 ++++++++++++++++++++++++++++++++++++++++ widgets/radiobutton.h | 92 ++++++++++++++++++ 3 files changed, 294 insertions(+) create mode 100644 widgets/radiobutton.cpp create mode 100644 widgets/radiobutton.h diff --git a/CMakeLists.txt b/CMakeLists.txt index bb55aabe5..c1baa349e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -16,6 +16,7 @@ set(plasma_LIB_SRCS widgets/lineedit.cpp widgets/pushbutton.cpp widgets/checkbox.cpp + widgets/radiobutton.cpp ) kde4_automoc(${plasma_LIB_SRCS}) diff --git a/widgets/radiobutton.cpp b/widgets/radiobutton.cpp new file mode 100644 index 000000000..be29fee50 --- /dev/null +++ b/widgets/radiobutton.cpp @@ -0,0 +1,201 @@ +/** + * Copyright (C) 2007 by Rafael Fernández López + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU Library General Public License version 2 as + * published by the Free Software Foundation + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details + * + * You should have received a copy of the GNU Library General Public + * License along with this program; if not, write to the + * Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +// Qt includes +#include +#include +#include +#include +#include + +// Radio button includes +#include "radiobutton.h" + +namespace Plasma +{ + + +/// Private section ============================== + +class RadioButton::Private +{ +public: + Private(); + ~Private(); + + // Attributes + bool checked; + bool mouseOver; + bool mouseDown; + QString text; +}; + +RadioButton::Private::Private() + : checked(false) + , mouseOver(false) + , mouseDown(false) + , text(QString()) +{ +} + +RadioButton::Private::~Private() +{ +} + +/// End Private section ========================== + + +RadioButton::RadioButton(QGraphicsItem *parent) + : QGraphicsItem(parent) + , d(new Private) +{ + setAcceptedMouseButtons(Qt::LeftButton); + setAcceptsHoverEvents(true); +} + +RadioButton::~RadioButton() +{ + delete d; +} + +void RadioButton::updated(const Plasma::DataSource::Data&) +{ +} + +QRectF RadioButton::boundingRect() const +{ + return QRectF(0, 0, 150, 30); +} + +void RadioButton::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) +{ + QStyle *style = widget ? widget->style() : QApplication::style(); + + QStyleOptionButton radioButtonOption; + radioButtonOption.rect = option->rect; + radioButtonOption.text = d->text; + radioButtonOption.state = option->state; + radioButtonOption.state |= d->checked ? QStyle::State_On : QStyle::State_Off; + radioButtonOption.state |= d->mouseOver && isEnabled() ? QStyle::State_MouseOver : QStyle::State_None; + radioButtonOption.state |= d->mouseDown && isEnabled() ? QStyle::State_Sunken : QStyle::State_Raised; + + style->drawControl(QStyle::CE_RadioButton, &radioButtonOption, painter, widget); +} + +bool RadioButton::isChecked() const +{ + return d->checked; +} + +const QString &RadioButton::text() const +{ + return d->text; +} + +void RadioButton::setChecked(bool checked) +{ + d->checked = checked; + + RadioButton *siblingRadioButton; + // If we have a parent item (some kind of grouping widget or whatever) + // check first there. + if (d->mouseOver && checked && parentItem()) + { + foreach(QGraphicsItem *sibling, parentItem()->children()) + { + siblingRadioButton = dynamic_cast(sibling); + + if (siblingRadioButton && siblingRadioButton->isChecked()) + { + siblingRadioButton->setChecked(false); + break; // Only an item is checked at same time as maximum + } + } + } + // If not, we should be on a scene, not flying anywhere + else if (checked && !parentItem() && scene()) + { + foreach(QGraphicsItem *sibling, scene()->items()) + { + siblingRadioButton = dynamic_cast(sibling); + + if (siblingRadioButton && siblingRadioButton->isChecked()) + { + siblingRadioButton->setChecked(false); + break; // Only an item is checked at same time as maximum + } + } + } + + update(); +} + +void RadioButton::setText(const QString &text) +{ + d->text = text; + update(); +} + +void RadioButton::mousePressEvent(QGraphicsSceneMouseEvent *event) +{ + event->accept(); + d->mouseDown = true; + update(); +} + +void RadioButton::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) +{ + event->accept(); + d->mouseDown = false; + setChecked(true); + update(); + + emit clicked(); +} + +void RadioButton::mouseMoveEvent(QGraphicsSceneMouseEvent *event) +{ + event->accept(); + d->mouseOver = true; + update(); +} + +void RadioButton::hoverMoveEvent(QGraphicsSceneHoverEvent *event) +{ + event->accept(); + d->mouseOver = true; + update(); +} + +void RadioButton::hoverLeaveEvent(QGraphicsSceneHoverEvent *event) +{ + event->accept(); + d->mouseOver = false; + update(); +} + +void RadioButton::hoverEnterEvent(QGraphicsSceneHoverEvent *event) +{ + event->accept(); + d->mouseOver = true; + update(); +} + +} // Plasma namespace + +#include "radiobutton.moc" diff --git a/widgets/radiobutton.h b/widgets/radiobutton.h new file mode 100644 index 000000000..492698cce --- /dev/null +++ b/widgets/radiobutton.h @@ -0,0 +1,92 @@ +/** + * Copyright (C) 2007 by Rafael Fernández López + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU Library General Public License version 2 as + * published by the Free Software Foundation + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details + * + * You should have received a copy of the GNU Library General Public + * License along with this program; if not, write to the + * Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#ifndef RADIOBUTTON_H +#define RADIOBUTTON_H + +// Qt includes +#include +#include + +// KDE includes +#include + +// Plasma includes +#include "datavisualization.h" + +/** + * This class emulates a QRadioButton. + * + * You will be able to add radio buttons as childrens of a QGraphicsItem, so + * only one radio button will be checked at a time. If you don't add them as + * siblings (adding all them as childrens of the same QGraphicsItem), then + * you can add them directly to a scene, where they will be siblings too. This + * way you can have groups of radio buttons. + * + * @note Please before working with radio buttons, add them to a scene or as + * childrens of another QGraphicsItem. + * + * @author Rafael Fernández López + */ + +namespace Plasma +{ + +class KDE_EXPORT RadioButton : public DataVisualization + , public QGraphicsItem +{ + Q_OBJECT + +public: + RadioButton(QGraphicsItem *parent = 0); + virtual ~RadioButton(); + + // DataVisualization overriden virtual methods + void updated(const Plasma::DataSource::Data&); + + // QGraphicsItem overriden virtual methods + QRectF boundingRect() const; + void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0); + + // Getters + bool isChecked() const; + const QString &text() const; + + // Setters + void setChecked(bool checked); + void setText(const QString &text); + +Q_SIGNALS: + void clicked(); + +protected: + virtual void mousePressEvent(QGraphicsSceneMouseEvent *event); + virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *event); + virtual void mouseMoveEvent(QGraphicsSceneMouseEvent *event); + virtual void hoverMoveEvent(QGraphicsSceneHoverEvent *event); + virtual void hoverLeaveEvent(QGraphicsSceneHoverEvent *event); + virtual void hoverEnterEvent(QGraphicsSceneHoverEvent *event); + +private: + class Private; + Private *const d; +}; + +} // Plasma namespace + +#endif // RADIOBUTTON_H