1
/****************************************************************************
3
** Copyright (C) 1992-2005 Trolltech AS. All rights reserved.
5
** This file is part of the Qt 3 compatibility classes of the Qt Toolkit.
7
** This file may be distributed under the terms of the Q Public License
8
** as defined by Trolltech AS of Norway and appearing in the file
9
** LICENSE.QPL included in the packaging of this file.
11
** This file may be distributed and/or modified under the terms of the
12
** GNU General Public License version 2 as published by the Free Software
13
** Foundation and appearing in the file LICENSE.GPL included in the
14
** packaging of this file.
16
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
17
** information about Qt Commercial License Agreements.
18
** See http://www.trolltech.com/qpl/ for QPL licensing information.
19
** See http://www.trolltech.com/gpl/ for GPL licensing information.
21
** Contact info@trolltech.com if any conditions of this licensing are
24
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
25
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
27
****************************************************************************/
29
#include "q3buttongroup.h"
30
#include "qabstractbutton.h"
32
#include "qapplication.h"
33
#include "qradiobutton.h"
39
\brief The Q3ButtonGroup widget organizes QAbstractButton widgets in a group.
43
A button group widget makes it easier to deal with groups of
44
buttons. Each button in a button group has a unique identifier.
45
The button group emits a clicked() signal with this identifier
46
when a button in the group is clicked. This makes a button group
47
particularly useful when you have several similar buttons and want
48
to connect all their clicked() signals to a single slot.
50
An \link setExclusive() exclusive\endlink button group switches
51
off all toggle buttons except the one that was clicked. A button
52
group is, by default, non-exclusive. Note that all radio buttons
53
that are inserted into a button group are mutually exclusive even
54
if the button group is non-exclusive. (See
55
setRadioButtonExclusive().)
57
There are two ways of using a button group:
59
\i The button group is the parent widget of a number of buttons,
60
i.e. the button group is the parent argument in the button
61
constructor. The buttons are assigned identifiers 0, 1, 2, etc.,
62
in the order they are created. A Q3ButtonGroup can display a frame
63
and a title because it inherits Q3GroupBox.
64
\i The button group is an invisible widget and the contained
65
buttons have some other parent widget. In this usage, each button
66
must be manually inserted, using insert(), into the button group
67
and given an identifier.
70
A button can be removed from the group with remove(). A pointer to
71
a button with a given id can be obtained using find(). The id of a
72
button is available using id(). A button can be set \e on with
73
setButton(). The number of buttons in the group is returned by
76
\sa QPushButton, QCheckBox, QRadioButton
80
\property Q3ButtonGroup::exclusive
81
\brief whether the button group is exclusive
83
If this property is true, then the buttons in the group are
84
toggled, and to untoggle a button you must click on another button
85
in the group. The default value is false.
89
\property Q3ButtonGroup::radioButtonExclusive
90
\brief whether the radio buttons in the group are exclusive
92
If this property is true (the default), the \link QRadioButton
93
radiobuttons\endlink in the group are treated exclusively.
98
Constructs a button group with no title.
100
The \a parent and \a name arguments are passed to the QWidget
104
Q3ButtonGroup::Q3ButtonGroup(QWidget *parent, const char *name)
105
: Q3GroupBox(parent, name)
111
Constructs a button group with the title \a title.
113
The \a parent and \a name arguments are passed to the QWidget
117
Q3ButtonGroup::Q3ButtonGroup(const QString &title, QWidget *parent,
119
: Q3GroupBox(title, parent, name)
125
Constructs a button group with no title. Child widgets will be
126
arranged in \a strips rows or columns (depending on \a
129
The \a parent and \a name arguments are passed to the QWidget
133
Q3ButtonGroup::Q3ButtonGroup(int strips, Qt::Orientation orientation,
134
QWidget *parent, const char *name)
135
: Q3GroupBox(strips, orientation, parent, name)
141
Constructs a button group with title \a title. Child widgets will
142
be arranged in \a strips rows or columns (depending on \a
145
The \a parent and \a name arguments are passed to the QWidget
149
Q3ButtonGroup::Q3ButtonGroup(int strips, Qt::Orientation orientation,
150
const QString &title, QWidget *parent,
152
: Q3GroupBox(strips, orientation, title, parent, name)
158
Initializes the button group.
161
void Q3ButtonGroup::init()
169
Q3ButtonGroup::~Q3ButtonGroup()
173
bool Q3ButtonGroup::isExclusive() const
178
void Q3ButtonGroup::setExclusive(bool enable)
185
Inserts the \a button with the identifier \a id into the button
186
group. Returns the button identifier.
188
Buttons are normally inserted into a button group automatically by
189
passing the button group as the parent when the button is
190
constructed. So it is not necessary to manually insert buttons
191
that have this button group as their parent widget. An exception
192
is when you want custom identifiers instead of the default 0, 1,
193
2, etc., or if you want the buttons to have some other parent.
195
The button is assigned the identifier \a id or an automatically
196
generated identifier. It works as follows: If \a id >= 0, this
197
identifier is assigned. If \a id == -1 (default), the identifier
198
is equal to the number of buttons in the group. If \a id is any
199
other negative integer, for instance -2, a unique identifier
200
(negative integer \<= -2) is generated. No button has an id of -1.
202
\sa find(), remove(), setExclusive()
205
int Q3ButtonGroup::insert(QAbstractButton *button, int id)
208
group.addButton(button);
209
static int seq_no = -2;
213
id = buttonIds.count();
214
buttonIds.insert(id, button);
215
connect(button, SIGNAL(pressed()) , SLOT(buttonPressed()));
216
connect(button, SIGNAL(released()), SLOT(buttonReleased()));
217
connect(button, SIGNAL(clicked()) , SLOT(buttonClicked()));
222
Returns the number of buttons in the group.
224
int Q3ButtonGroup::count() const
226
return group.buttons().count();
230
Removes the \a button from the button group.
235
void Q3ButtonGroup::remove(QAbstractButton *button)
237
QMap<int, QAbstractButton*>::Iterator it = buttonIds.begin();
238
while (it != buttonIds.end()) {
239
if (it.value() == button) {
241
button->disconnect(this);
242
group.removeButton(button);
251
Returns the button with the specified identifier \a id, or 0 if
252
the button was not found.
255
QAbstractButton *Q3ButtonGroup::find(int id) const
257
return buttonIds.value(id);
262
\fn void Q3ButtonGroup::pressed(int id)
264
This signal is emitted when a button in the group is \link
265
QAbstractButton::pressed() pressed\endlink. The \a id argument is the
272
\fn void Q3ButtonGroup::released(int id)
274
This signal is emitted when a button in the group is \link
275
QAbstractButton::released() released\endlink. The \a id argument is the
282
\fn void Q3ButtonGroup::clicked(int id)
284
This signal is emitted when a button in the group is \link
285
QAbstractButton::clicked() clicked\endlink. The \a id argument is the
294
This slot is activated when one of the buttons in the group emits the
295
QAbstractButton::pressed() signal.
298
void Q3ButtonGroup::buttonPressed()
300
QAbstractButton *senderButton = ::qobject_cast<QAbstractButton *>(sender());
301
Q_ASSERT(senderButton);
302
int senderId = id(senderButton);
304
emit pressed(senderId);
309
This slot is activated when one of the buttons in the group emits the
310
QAbstractButton::released() signal.
313
void Q3ButtonGroup::buttonReleased()
315
QAbstractButton *senderButton = ::qobject_cast<QAbstractButton *>(sender());
316
Q_ASSERT(senderButton);
317
int senderId = id(senderButton);
319
emit released(senderId);
324
This slot is activated when one of the buttons in the group emits the
325
QAbstractButton::clicked() signal.
328
void Q3ButtonGroup::buttonClicked()
330
QAbstractButton *senderButton = ::qobject_cast<QAbstractButton *>(sender());
331
Q_ASSERT(senderButton);
332
int senderId = id(senderButton);
334
emit clicked(senderId);
337
void Q3ButtonGroup::setButton(int id)
339
QAbstractButton *b = find(id);
344
void Q3ButtonGroup::setRadioButtonExclusive(bool on)
351
Returns the selected toggle button if exactly one is selected;
357
QAbstractButton *Q3ButtonGroup::selected() const
359
QAbstractButton *candidate = 0;
360
QMap<int, QAbstractButton*>::ConstIterator it = buttonIds.constBegin();
361
while (it != buttonIds.constEnd()) {
362
if (it.value()->isCheckable() && it.value()->isChecked()) {
365
candidate = it.value();
373
\property Q3ButtonGroup::selectedId
374
\brief The id of the selected toggle button.
376
If no toggle button is selected, id() returns -1.
378
If setButton() is called on an exclusive group, the button with
379
the given id will be set to on and all the others will be set to
385
int Q3ButtonGroup::selectedId() const
387
return id(selected());
392
Returns the id of \a button, or -1 if \a button is not a member of
398
int Q3ButtonGroup::id(QAbstractButton *button) const
400
QMap<int, QAbstractButton*>::ConstIterator it = buttonIds.constBegin();
401
while (it != buttonIds.constEnd()) {
402
if (it.value() == button)
413
bool Q3ButtonGroup::event(QEvent * e)
415
if (e->type() == QEvent::ChildInserted) {
416
QChildEvent * ce = (QChildEvent *) e;
417
if (QAbstractButton *button = qobject_cast<QRadioButton*>(ce->child())) {
418
button->setAutoExclusive(false);
419
if (excl_grp || (radio_excl && qobject_cast<QRadioButton*>(button)))
423
return Q3GroupBox::event(e);
427
\class Q3HButtonGroup
428
\brief The Q3HButtonGroup widget organizes button widgets in a
429
group with one horizontal row.
433
Q3HButtonGroup is a convenience class that offers a thin layer on
434
top of Q3ButtonGroup. From a layout point of view it is effectively
435
a Q3HBoxWidget that offers a frame with a title and is specifically
436
designed for buttons. From a functionality point of view it is a
443
\fn Q3HButtonGroup::Q3HButtonGroup(QWidget *parent, const char *name)
445
Constructs a horizontal button group with no title.
447
The \a parent and \a name arguments are passed to the QWidget
452
\fn Q3HButtonGroup::Q3HButtonGroup(const QString &title, QWidget *parent,
455
Constructs a horizontal button group with the title \a title.
457
The \a parent and \a name arguments are passed to the QWidget
462
\class Q3VButtonGroup
463
\brief The Q3VButtonGroup widget organizes button widgets in a
468
Q3VButtonGroup is a convenience class that offers a thin layer on top
469
of Q3ButtonGroup. Think of it as a QVBoxWidget that offers a frame with a
470
title and is specifically designed for buttons.
476
\fn Q3VButtonGroup::Q3VButtonGroup(QWidget *parent, const char *name)
478
Constructs a vertical button group with no title.
480
The \a parent and \a name arguments are passed on to the QWidget
485
\fn Q3VButtonGroup::Q3VButtonGroup(const QString &title, QWidget *parent,
488
Constructs a vertical button group with the title \a title.
490
The \a parent and \a name arguments are passed on to the QWidget