~oif-team/ubuntu/natty/qt4-x11/xi2.1

« back to all changes in this revision

Viewing changes to src/qt3support/widgets/q3buttongroup.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-08-24 04:09:09 UTC
  • Revision ID: james.westby@ubuntu.com-20050824040909-xmxe9jfr4a0w5671
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
**
 
3
** Copyright (C) 1992-2005 Trolltech AS. All rights reserved.
 
4
**
 
5
** This file is part of the Qt 3 compatibility classes of the Qt Toolkit.
 
6
**
 
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.
 
10
**
 
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.
 
15
**
 
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.
 
20
**
 
21
** Contact info@trolltech.com if any conditions of this licensing are
 
22
** not clear to you.
 
23
**
 
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.
 
26
**
 
27
****************************************************************************/
 
28
 
 
29
#include "q3buttongroup.h"
 
30
#include "qabstractbutton.h"
 
31
#include "qmap.h"
 
32
#include "qapplication.h"
 
33
#include "qradiobutton.h"
 
34
#include "qevent.h"
 
35
 
 
36
 
 
37
/*!
 
38
    \class Q3ButtonGroup
 
39
    \brief The Q3ButtonGroup widget organizes QAbstractButton widgets in a group.
 
40
 
 
41
    \compat
 
42
 
 
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.
 
49
 
 
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().)
 
56
 
 
57
    There are two ways of using a button group:
 
58
    \list
 
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.
 
68
    \endlist
 
69
 
 
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
 
74
    count().
 
75
 
 
76
    \sa QPushButton, QCheckBox, QRadioButton
 
77
*/
 
78
 
 
79
/*!
 
80
    \property Q3ButtonGroup::exclusive
 
81
    \brief whether the button group is exclusive
 
82
 
 
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.
 
86
*/
 
87
 
 
88
/*!
 
89
    \property Q3ButtonGroup::radioButtonExclusive
 
90
    \brief whether the radio buttons in the group are exclusive
 
91
 
 
92
    If this property is true (the default), the \link QRadioButton
 
93
    radiobuttons\endlink in the group are treated exclusively.
 
94
*/
 
95
 
 
96
 
 
97
/*!
 
98
    Constructs a button group with no title.
 
99
 
 
100
    The \a parent and \a name arguments are passed to the QWidget
 
101
    constructor.
 
102
*/
 
103
 
 
104
Q3ButtonGroup::Q3ButtonGroup(QWidget *parent, const char *name)
 
105
    : Q3GroupBox(parent, name)
 
106
{
 
107
    init();
 
108
}
 
109
 
 
110
/*!
 
111
    Constructs a button group with the title \a title.
 
112
 
 
113
    The \a parent and \a name arguments are passed to the QWidget
 
114
    constructor.
 
115
*/
 
116
 
 
117
Q3ButtonGroup::Q3ButtonGroup(const QString &title, QWidget *parent,
 
118
                            const char *name)
 
119
    : Q3GroupBox(title, parent, name)
 
120
{
 
121
    init();
 
122
}
 
123
 
 
124
/*!
 
125
    Constructs a button group with no title. Child widgets will be
 
126
    arranged in \a strips rows or columns (depending on \a
 
127
    orientation).
 
128
 
 
129
    The \a parent and \a name arguments are passed to the QWidget
 
130
    constructor.
 
131
*/
 
132
 
 
133
Q3ButtonGroup::Q3ButtonGroup(int strips, Qt::Orientation orientation,
 
134
                            QWidget *parent, const char *name)
 
135
    : Q3GroupBox(strips, orientation, parent, name)
 
136
{
 
137
    init();
 
138
}
 
139
 
 
140
/*!
 
141
    Constructs a button group with title \a title. Child widgets will
 
142
    be arranged in \a strips rows or columns (depending on \a
 
143
    orientation).
 
144
 
 
145
    The \a parent and \a name arguments are passed to the QWidget
 
146
    constructor.
 
147
*/
 
148
 
 
149
Q3ButtonGroup::Q3ButtonGroup(int strips, Qt::Orientation orientation,
 
150
                            const QString &title, QWidget *parent,
 
151
                            const char *name)
 
152
    : Q3GroupBox(strips, orientation, title, parent, name)
 
153
{
 
154
    init();
 
155
}
 
156
 
 
157
/*!
 
158
    Initializes the button group.
 
159
*/
 
160
 
 
161
void Q3ButtonGroup::init()
 
162
{
 
163
    excl_grp = false;
 
164
    radio_excl = true;
 
165
}
 
166
 
 
167
/*! Destructor. */
 
168
 
 
169
Q3ButtonGroup::~Q3ButtonGroup()
 
170
{
 
171
}
 
172
 
 
173
bool Q3ButtonGroup::isExclusive() const
 
174
{
 
175
    return excl_grp;
 
176
}
 
177
 
 
178
void Q3ButtonGroup::setExclusive(bool enable)
 
179
{
 
180
    excl_grp = enable;
 
181
}
 
182
 
 
183
 
 
184
/*!
 
185
    Inserts the \a button with the identifier \a id into the button
 
186
    group. Returns the button identifier.
 
187
 
 
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.
 
194
 
 
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.
 
201
 
 
202
    \sa find(), remove(), setExclusive()
 
203
*/
 
204
 
 
205
int Q3ButtonGroup::insert(QAbstractButton *button, int id)
 
206
{
 
207
    remove(button);
 
208
    group.addButton(button);
 
209
    static int seq_no = -2;
 
210
    if (id < -1)
 
211
        id = seq_no--;
 
212
    else if (id == -1)
 
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()));
 
218
    return id;
 
219
}
 
220
 
 
221
/*!
 
222
    Returns the number of buttons in the group.
 
223
*/
 
224
int Q3ButtonGroup::count() const
 
225
{
 
226
    return group.buttons().count();
 
227
}
 
228
 
 
229
/*!
 
230
    Removes the \a button from the button group.
 
231
 
 
232
    \sa insert()
 
233
*/
 
234
 
 
235
void Q3ButtonGroup::remove(QAbstractButton *button)
 
236
{
 
237
    QMap<int, QAbstractButton*>::Iterator it = buttonIds.begin();
 
238
    while (it != buttonIds.end()) {
 
239
        if (it.value() == button) {
 
240
            buttonIds.erase(it);
 
241
            button->disconnect(this);
 
242
            group.removeButton(button);
 
243
            break;
 
244
        }
 
245
        ++it;
 
246
    }
 
247
}
 
248
 
 
249
 
 
250
/*!
 
251
    Returns the button with the specified identifier \a id, or 0 if
 
252
    the button was not found.
 
253
*/
 
254
 
 
255
QAbstractButton *Q3ButtonGroup::find(int id) const
 
256
{
 
257
    return buttonIds.value(id);
 
258
}
 
259
 
 
260
 
 
261
/*!
 
262
    \fn void Q3ButtonGroup::pressed(int id)
 
263
 
 
264
    This signal is emitted when a button in the group is \link
 
265
    QAbstractButton::pressed() pressed\endlink. The \a id argument is the
 
266
    button's identifier.
 
267
 
 
268
    \sa insert()
 
269
*/
 
270
 
 
271
/*!
 
272
    \fn void Q3ButtonGroup::released(int id)
 
273
 
 
274
    This signal is emitted when a button in the group is \link
 
275
    QAbstractButton::released() released\endlink. The \a id argument is the
 
276
    button's identifier.
 
277
 
 
278
    \sa insert()
 
279
*/
 
280
 
 
281
/*!
 
282
    \fn void Q3ButtonGroup::clicked(int id)
 
283
 
 
284
    This signal is emitted when a button in the group is \link
 
285
    QAbstractButton::clicked() clicked\endlink. The \a id argument is the
 
286
    button's identifier.
 
287
 
 
288
    \sa insert()
 
289
*/
 
290
 
 
291
 
 
292
/*!
 
293
  \internal
 
294
  This slot is activated when one of the buttons in the group emits the
 
295
  QAbstractButton::pressed() signal.
 
296
*/
 
297
 
 
298
void Q3ButtonGroup::buttonPressed()
 
299
{
 
300
    QAbstractButton *senderButton = ::qobject_cast<QAbstractButton *>(sender());
 
301
    Q_ASSERT(senderButton);
 
302
    int senderId = id(senderButton);
 
303
    if (senderId != -1)
 
304
        emit pressed(senderId);
 
305
}
 
306
 
 
307
/*!
 
308
  \internal
 
309
  This slot is activated when one of the buttons in the group emits the
 
310
  QAbstractButton::released() signal.
 
311
*/
 
312
 
 
313
void Q3ButtonGroup::buttonReleased()
 
314
{
 
315
    QAbstractButton *senderButton = ::qobject_cast<QAbstractButton *>(sender());
 
316
    Q_ASSERT(senderButton);
 
317
    int senderId = id(senderButton);
 
318
    if (senderId != -1)
 
319
        emit released(senderId);
 
320
}
 
321
 
 
322
/*!
 
323
  \internal
 
324
  This slot is activated when one of the buttons in the group emits the
 
325
  QAbstractButton::clicked() signal.
 
326
*/
 
327
 
 
328
void Q3ButtonGroup::buttonClicked()
 
329
{
 
330
    QAbstractButton *senderButton = ::qobject_cast<QAbstractButton *>(sender());
 
331
    Q_ASSERT(senderButton);
 
332
    int senderId = id(senderButton);
 
333
    if (senderId != -1)
 
334
        emit clicked(senderId);
 
335
}
 
336
 
 
337
void Q3ButtonGroup::setButton(int id)
 
338
{
 
339
    QAbstractButton *b = find(id);
 
340
    if (b)
 
341
        b->setOn(true);
 
342
}
 
343
 
 
344
void Q3ButtonGroup::setRadioButtonExclusive(bool on)
 
345
{
 
346
    radio_excl = on;
 
347
}
 
348
 
 
349
 
 
350
/*!
 
351
    Returns the selected toggle button if exactly one is selected;
 
352
    otherwise returns 0.
 
353
 
 
354
    \sa selectedId()
 
355
*/
 
356
 
 
357
QAbstractButton *Q3ButtonGroup::selected() const
 
358
{
 
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()) {
 
363
            if (candidate)
 
364
                return 0;
 
365
            candidate = it.value();
 
366
        }
 
367
        ++it;
 
368
    }
 
369
    return candidate;
 
370
}
 
371
 
 
372
/*!
 
373
    \property Q3ButtonGroup::selectedId
 
374
    \brief The id of the selected toggle button.
 
375
 
 
376
    If no toggle button is selected, id() returns -1.
 
377
 
 
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
 
380
    off.
 
381
 
 
382
    \sa selected()
 
383
*/
 
384
 
 
385
int Q3ButtonGroup::selectedId() const
 
386
{
 
387
    return id(selected());
 
388
}
 
389
 
 
390
 
 
391
/*!
 
392
    Returns the id of \a button, or -1 if \a button is not a member of
 
393
    this group.
 
394
 
 
395
    \sa selectedId()
 
396
*/
 
397
 
 
398
int Q3ButtonGroup::id(QAbstractButton *button) const
 
399
{
 
400
    QMap<int, QAbstractButton*>::ConstIterator it = buttonIds.constBegin();
 
401
    while (it != buttonIds.constEnd()) {
 
402
        if (it.value() == button)
 
403
            return it.key();
 
404
        ++it;
 
405
    }
 
406
    return -1;
 
407
}
 
408
 
 
409
 
 
410
/*!
 
411
    \reimp
 
412
*/
 
413
bool Q3ButtonGroup::event(QEvent * e)
 
414
{
 
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)))
 
420
                insert(button);
 
421
        }
 
422
    }
 
423
    return Q3GroupBox::event(e);
 
424
}
 
425
 
 
426
/*!
 
427
    \class Q3HButtonGroup
 
428
    \brief The Q3HButtonGroup widget organizes button widgets in a
 
429
    group with one horizontal row.
 
430
 
 
431
    \compat
 
432
 
 
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
 
437
    Q3ButtonGroup.
 
438
 
 
439
    \sa Q3VButtonGroup
 
440
*/
 
441
 
 
442
/*!
 
443
    \fn Q3HButtonGroup::Q3HButtonGroup(QWidget *parent, const char *name)
 
444
 
 
445
    Constructs a horizontal button group with no title.
 
446
 
 
447
    The \a parent and \a name arguments are passed to the QWidget
 
448
    constructor.
 
449
*/
 
450
 
 
451
/*!
 
452
    \fn Q3HButtonGroup::Q3HButtonGroup(const QString &title, QWidget *parent,
 
453
                                       const char *name)
 
454
 
 
455
    Constructs a horizontal button group with the title \a title.
 
456
 
 
457
    The \a parent and \a name arguments are passed to the QWidget
 
458
    constructor.
 
459
*/
 
460
 
 
461
/*!
 
462
    \class Q3VButtonGroup
 
463
    \brief The Q3VButtonGroup widget organizes button widgets in a
 
464
    vertical column.
 
465
 
 
466
    \compat
 
467
 
 
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.
 
471
 
 
472
    \sa Q3HButtonGroup
 
473
*/
 
474
 
 
475
/*!
 
476
    \fn Q3VButtonGroup::Q3VButtonGroup(QWidget *parent, const char *name)
 
477
 
 
478
    Constructs a vertical button group with no title.
 
479
 
 
480
    The \a parent and \a name arguments are passed on to the QWidget
 
481
    constructor.
 
482
*/
 
483
 
 
484
/*!
 
485
    \fn Q3VButtonGroup::Q3VButtonGroup(const QString &title, QWidget *parent,
 
486
                                       const char *name)
 
487
 
 
488
    Constructs a vertical button group with the title \a title.
 
489
 
 
490
    The \a parent and \a name arguments are passed on to the QWidget
 
491
    constructor.
 
492
*/