2
* Copyright 2007 by Rafael Fernández López <ereslibre@gmail.com>
4
* This program is free software; you can redistribute it and/or modify
5
* it under the terms of the GNU Library General Public License version 2 as
6
* published by the Free Software Foundation
8
* This program is distributed in the hope that it will be useful,
9
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
* GNU General Public License for more details
13
* You should have received a copy of the GNU Library General Public
14
* License along with this program; if not, write to the
15
* Free Software Foundation, Inc.,
16
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20
#include "radiobutton.h"
23
#include <QtGui/QWidget>
24
#include <QtGui/QApplication>
25
#include <QtGui/QGraphicsScene>
26
#include <QtGui/QStyleOptionButton>
27
#include <QtGui/QGraphicsSceneMouseEvent>
33
/// Private section ==============================
35
class RadioButton::Private
48
RadioButton::Private::Private()
56
RadioButton::Private::~Private()
60
/// End Private section ==========================
63
RadioButton::RadioButton(QGraphicsItem *parent)
64
: Plasma::Widget(parent),
67
setAcceptedMouseButtons(Qt::LeftButton);
68
setAcceptsHoverEvents(true);
71
RadioButton::~RadioButton()
76
QRectF RadioButton::boundingRect() const
78
return QRectF(0, 0, 150, 30); // FIXME: this is obviously wrong
81
void RadioButton::paintWidget(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
83
QStyle *style = widget ? widget->style() : QApplication::style();
85
QStyleOptionButton radioButtonOption;
86
radioButtonOption.rect = option->rect;
87
radioButtonOption.text = d->text;
88
radioButtonOption.state = option->state;
89
radioButtonOption.state |= d->checked ? QStyle::State_On : QStyle::State_Off;
90
radioButtonOption.state |= d->mouseOver ? QStyle::State_MouseOver : QStyle::State_None;
91
radioButtonOption.state |= d->mouseDown ? QStyle::State_Sunken : QStyle::State_Raised;
93
style->drawControl(QStyle::CE_RadioButton, &radioButtonOption, painter, widget);
96
bool RadioButton::isChecked() const
101
const QString &RadioButton::text() const
106
void RadioButton::setChecked(bool checked)
108
RadioButton *siblingRadioButton;
109
// If we have a parent item (some kind of grouping widget or whatever)
111
if (d->mouseOver && checked && parentItem())
113
foreach(QGraphicsItem *sibling, parentItem()->children())
115
siblingRadioButton = dynamic_cast<RadioButton*>(sibling);
117
if (siblingRadioButton && siblingRadioButton->isChecked())
119
siblingRadioButton->setChecked(false);
120
break; // Only an item is checked at same time as maximum
124
// If not, we should be on a scene, not flying anywhere
125
else if (checked && !parentItem() && scene())
127
foreach(QGraphicsItem *sibling, scene()->items())
129
siblingRadioButton = dynamic_cast<RadioButton*>(sibling);
131
if (siblingRadioButton && siblingRadioButton->isChecked() && !siblingRadioButton->parentItem())
133
siblingRadioButton->setChecked(false);
134
break; // Only an item is checked at same time as maximum
139
d->checked = checked;
144
void RadioButton::setText(const QString &text)
150
void RadioButton::updated(const QString&, const Plasma::DataEngine::Data &data)
155
void RadioButton::mousePressEvent(QGraphicsSceneMouseEvent *event)
162
void RadioButton::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
165
d->mouseDown = false;
172
void RadioButton::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
179
void RadioButton::hoverMoveEvent(QGraphicsSceneHoverEvent *event)
186
void RadioButton::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
189
d->mouseOver = false;
193
void RadioButton::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
201
} // Plasma namespace
203
#include "radiobutton.moc"