2007-05-21 16:28:03 +02:00
|
|
|
/*
|
2007-10-03 02:44:18 +02:00
|
|
|
* Copyright 2007 by Rafael Fernández López <ereslibre@kde.org>
|
2007-05-19 22:49:16 +02:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
2007-09-14 22:17:11 +02:00
|
|
|
* it under the terms of the GNU Library General Public License as
|
2007-09-14 21:06:18 +02:00
|
|
|
* published by the Free Software Foundation; either version 2, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
2007-05-19 22:49:16 +02:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2007-05-21 16:28:03 +02:00
|
|
|
// Header Includes
|
|
|
|
#include "radiobutton.h"
|
|
|
|
|
2007-05-19 22:49:16 +02:00
|
|
|
// Qt includes
|
|
|
|
#include <QtGui/QWidget>
|
|
|
|
#include <QtGui/QApplication>
|
|
|
|
#include <QtGui/QGraphicsScene>
|
|
|
|
#include <QtGui/QStyleOptionButton>
|
|
|
|
#include <QtGui/QGraphicsSceneMouseEvent>
|
|
|
|
|
|
|
|
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)
|
2007-08-03 18:00:10 +02:00
|
|
|
: Plasma::Widget(parent),
|
2007-05-25 04:27:33 +02:00
|
|
|
d(new Private)
|
2007-05-19 22:49:16 +02:00
|
|
|
{
|
|
|
|
setAcceptedMouseButtons(Qt::LeftButton);
|
|
|
|
setAcceptsHoverEvents(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
RadioButton::~RadioButton()
|
|
|
|
{
|
|
|
|
delete d;
|
|
|
|
}
|
|
|
|
|
|
|
|
QRectF RadioButton::boundingRect() const
|
|
|
|
{
|
2007-05-19 23:12:40 +02:00
|
|
|
return QRectF(0, 0, 150, 30); // FIXME: this is obviously wrong
|
2007-05-19 22:49:16 +02:00
|
|
|
}
|
|
|
|
|
2007-08-03 18:00:10 +02:00
|
|
|
void RadioButton::paintWidget(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
|
2007-05-19 22:49:16 +02:00
|
|
|
{
|
|
|
|
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;
|
2007-05-20 15:29:06 +02:00
|
|
|
radioButtonOption.state |= d->mouseOver ? QStyle::State_MouseOver : QStyle::State_None;
|
|
|
|
radioButtonOption.state |= d->mouseDown ? QStyle::State_Sunken : QStyle::State_Raised;
|
2007-05-19 22:49:16 +02:00
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
RadioButton *siblingRadioButton;
|
|
|
|
// If we have a parent item (some kind of grouping widget or whatever)
|
2007-05-19 23:00:16 +02:00
|
|
|
// check first there
|
2007-05-19 22:49:16 +02:00
|
|
|
if (d->mouseOver && checked && parentItem())
|
|
|
|
{
|
|
|
|
foreach(QGraphicsItem *sibling, parentItem()->children())
|
|
|
|
{
|
|
|
|
siblingRadioButton = dynamic_cast<RadioButton*>(sibling);
|
|
|
|
|
|
|
|
if (siblingRadioButton && siblingRadioButton->isChecked())
|
|
|
|
{
|
|
|
|
siblingRadioButton->setChecked(false);
|
|
|
|
break; // Only an item is checked at same time as maximum
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (checked && !parentItem() && scene())
|
|
|
|
{
|
2007-11-11 00:58:17 +01:00
|
|
|
// we should be on a scene, not flying anywhere
|
2007-05-19 22:49:16 +02:00
|
|
|
foreach(QGraphicsItem *sibling, scene()->items())
|
|
|
|
{
|
|
|
|
siblingRadioButton = dynamic_cast<RadioButton*>(sibling);
|
|
|
|
|
2007-05-20 02:58:40 +02:00
|
|
|
if (siblingRadioButton && siblingRadioButton->isChecked() && !siblingRadioButton->parentItem())
|
2007-05-19 22:49:16 +02:00
|
|
|
{
|
|
|
|
siblingRadioButton->setChecked(false);
|
|
|
|
break; // Only an item is checked at same time as maximum
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-05-19 22:54:26 +02:00
|
|
|
d->checked = checked;
|
|
|
|
|
2007-05-19 22:49:16 +02:00
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
void RadioButton::setText(const QString &text)
|
|
|
|
{
|
|
|
|
d->text = text;
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
2007-11-06 08:20:08 +01:00
|
|
|
void RadioButton::dataUpdated(const QString&, const Plasma::DataEngine::Data &data)
|
2007-05-19 23:03:21 +02:00
|
|
|
{
|
2007-11-06 08:20:08 +01:00
|
|
|
DataEngine::DataIterator it(data);
|
|
|
|
|
|
|
|
while (it.hasNext()) {
|
|
|
|
it.next();
|
|
|
|
if (it.value().canConvert(QVariant::Bool)) {
|
|
|
|
setChecked(it.value().toBool());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2007-05-19 23:03:21 +02:00
|
|
|
}
|
|
|
|
|
2007-05-19 22:49:16 +02:00
|
|
|
void RadioButton::mousePressEvent(QGraphicsSceneMouseEvent *event)
|
|
|
|
{
|
|
|
|
event->accept();
|
|
|
|
d->mouseDown = true;
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
void RadioButton::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
|
|
|
|
{
|
|
|
|
event->accept();
|
|
|
|
d->mouseDown = false;
|
|
|
|
|
2007-11-11 00:58:17 +01:00
|
|
|
if (sceneBoundingRect().contains(event->scenePos())) {
|
|
|
|
setChecked(true);
|
|
|
|
emit clicked();
|
|
|
|
}
|
|
|
|
|
|
|
|
update();
|
2007-05-19 22:49:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2007-05-19 23:00:16 +02:00
|
|
|
|
2007-05-19 22:49:16 +02:00
|
|
|
} // Plasma namespace
|
|
|
|
|
|
|
|
#include "radiobutton.moc"
|