~ubuntu-branches/ubuntu/gutsy/kdebase-workspace/gutsy-backports

« back to all changes in this revision

Viewing changes to libs/plasma/widgets/radiobutton.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Riddell
  • Date: 2007-09-05 20:45:14 UTC
  • Revision ID: james.westby@ubuntu.com-20070905204514-632hhspl0nvrc84i
Tags: upstream-3.93.0
Import upstream version 3.93.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *   Copyright 2007 by Rafael Fernández López <ereslibre@gmail.com>
 
3
 *
 
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
 
7
 *
 
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
 
12
 *
 
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.
 
17
 */
 
18
 
 
19
// Header Includes
 
20
#include "radiobutton.h"
 
21
 
 
22
// Qt includes
 
23
#include <QtGui/QWidget>
 
24
#include <QtGui/QApplication>
 
25
#include <QtGui/QGraphicsScene>
 
26
#include <QtGui/QStyleOptionButton>
 
27
#include <QtGui/QGraphicsSceneMouseEvent>
 
28
 
 
29
namespace Plasma
 
30
{
 
31
 
 
32
 
 
33
/// Private section ==============================
 
34
 
 
35
class RadioButton::Private
 
36
{
 
37
public:
 
38
    Private();
 
39
    ~Private();
 
40
 
 
41
    // Attributes
 
42
    bool checked;
 
43
    bool mouseOver;
 
44
    bool mouseDown;
 
45
    QString text;
 
46
};
 
47
 
 
48
RadioButton::Private::Private()
 
49
    : checked(false)
 
50
    , mouseOver(false)
 
51
    , mouseDown(false)
 
52
    , text(QString())
 
53
{
 
54
}
 
55
 
 
56
RadioButton::Private::~Private()
 
57
{
 
58
}
 
59
 
 
60
/// End Private section ==========================
 
61
 
 
62
 
 
63
RadioButton::RadioButton(QGraphicsItem *parent)
 
64
    : Plasma::Widget(parent),
 
65
      d(new Private)
 
66
{
 
67
    setAcceptedMouseButtons(Qt::LeftButton);
 
68
    setAcceptsHoverEvents(true);
 
69
}
 
70
 
 
71
RadioButton::~RadioButton()
 
72
{
 
73
    delete d;
 
74
}
 
75
 
 
76
QRectF RadioButton::boundingRect() const
 
77
{
 
78
    return QRectF(0, 0, 150, 30); // FIXME: this is obviously wrong
 
79
}
 
80
 
 
81
void RadioButton::paintWidget(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
 
82
{
 
83
    QStyle *style = widget ? widget->style() : QApplication::style();
 
84
 
 
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;
 
92
 
 
93
    style->drawControl(QStyle::CE_RadioButton, &radioButtonOption, painter, widget);
 
94
}
 
95
 
 
96
bool RadioButton::isChecked() const
 
97
{
 
98
    return d->checked;
 
99
}
 
100
 
 
101
const QString &RadioButton::text() const
 
102
{
 
103
    return d->text;
 
104
}
 
105
 
 
106
void RadioButton::setChecked(bool checked)
 
107
{
 
108
    RadioButton *siblingRadioButton;
 
109
    // If we have a parent item (some kind of grouping widget or whatever)
 
110
    // check first there
 
111
    if (d->mouseOver && checked && parentItem())
 
112
    {
 
113
        foreach(QGraphicsItem *sibling, parentItem()->children())
 
114
        {
 
115
            siblingRadioButton = dynamic_cast<RadioButton*>(sibling);
 
116
 
 
117
            if (siblingRadioButton && siblingRadioButton->isChecked())
 
118
            {
 
119
                siblingRadioButton->setChecked(false);
 
120
                break; // Only an item is checked at same time as maximum
 
121
            }
 
122
        }
 
123
    }
 
124
    // If not, we should be on a scene, not flying anywhere
 
125
    else if (checked && !parentItem() && scene())
 
126
    {
 
127
        foreach(QGraphicsItem *sibling, scene()->items())
 
128
        {
 
129
            siblingRadioButton = dynamic_cast<RadioButton*>(sibling);
 
130
 
 
131
            if (siblingRadioButton && siblingRadioButton->isChecked() && !siblingRadioButton->parentItem())
 
132
            {
 
133
                siblingRadioButton->setChecked(false);
 
134
                break; // Only an item is checked at same time as maximum
 
135
            }
 
136
        }
 
137
    }
 
138
 
 
139
    d->checked = checked;
 
140
 
 
141
    update();
 
142
}
 
143
 
 
144
void RadioButton::setText(const QString &text)
 
145
{
 
146
    d->text = text;
 
147
    update();
 
148
}
 
149
 
 
150
void RadioButton::updated(const QString&, const Plasma::DataEngine::Data &data)
 
151
{
 
152
    Q_UNUSED(data);
 
153
}
 
154
 
 
155
void RadioButton::mousePressEvent(QGraphicsSceneMouseEvent *event)
 
156
{
 
157
    event->accept();
 
158
    d->mouseDown = true;
 
159
    update();
 
160
}
 
161
 
 
162
void RadioButton::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
 
163
{
 
164
    event->accept();
 
165
    d->mouseDown = false;
 
166
    setChecked(true);
 
167
    update();
 
168
 
 
169
    emit clicked();
 
170
}
 
171
 
 
172
void RadioButton::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
 
173
{
 
174
    event->accept();
 
175
    d->mouseOver = true;
 
176
    update();
 
177
}
 
178
 
 
179
void RadioButton::hoverMoveEvent(QGraphicsSceneHoverEvent *event)
 
180
{
 
181
    event->accept();
 
182
    d->mouseOver = true;
 
183
    update();
 
184
}
 
185
 
 
186
void RadioButton::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
 
187
{
 
188
    event->accept();
 
189
    d->mouseOver = false;
 
190
    update();
 
191
}
 
192
 
 
193
void RadioButton::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
 
194
{
 
195
    event->accept();
 
196
    d->mouseOver = true;
 
197
    update();
 
198
}
 
199
 
 
200
 
 
201
} // Plasma namespace
 
202
 
 
203
#include "radiobutton.moc"