source: trunk/src/qt3support/widgets/q3buttongroup.cpp@ 807

Last change on this file since 807 was 651, checked in by Dmitry A. Kuminov, 15 years ago

trunk: Merged in qt 4.6.2 sources.

File size: 15.6 KB
RevLine 
[2]1/****************************************************************************
2**
[651]3** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
[561]4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
[2]6**
7** This file is part of the Qt3Support module of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and Nokia.
15**
16** GNU Lesser General Public License Usage
17** Alternatively, this file may be used under the terms of the GNU Lesser
18** General Public License version 2.1 as published by the Free Software
19** Foundation and appearing in the file LICENSE.LGPL included in the
20** packaging of this file. Please review the following information to
21** ensure the GNU Lesser General Public License version 2.1 requirements
22** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23**
[561]24** In addition, as a special exception, Nokia gives you certain additional
25** rights. These rights are described in the Nokia Qt LGPL Exception
26** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
[2]27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
[561]36** If you have questions regarding the use of this file, please contact
37** Nokia at [email protected].
[2]38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include "q3buttongroup.h"
43#include "qabstractbutton.h"
44#include "qmap.h"
45#include "qapplication.h"
46#include "qradiobutton.h"
47#include "qevent.h"
48#include "qset.h"
49
50QT_BEGIN_NAMESPACE
51
52/*!
53 \class Q3ButtonGroup
54 \brief The Q3ButtonGroup widget organizes QAbstractButton widgets in a group.
55
56 \compat
57
58 A button group widget makes it easier to deal with groups of
59 buttons. Each button in a button group has a unique identifier.
60 The button group emits a clicked() signal with this identifier
61 when a button in the group is clicked. This makes a button group
62 particularly useful when you have several similar buttons and want
63 to connect all their clicked() signals to a single slot.
64
65 An \link setExclusive() exclusive\endlink button group switches
66 off all toggle buttons except the one that was clicked. A button
67 group is, by default, non-exclusive. Note that all radio buttons
68 that are inserted into a button group are mutually exclusive even
69 if the button group is non-exclusive. (See
70 setRadioButtonExclusive().)
71
72 There are two ways of using a button group:
73 \list
74 \i The button group is the parent widget of a number of buttons,
75 i.e. the button group is the parent argument in the button
76 constructor. The buttons are assigned identifiers 0, 1, 2, etc.,
77 in the order they are created. A Q3ButtonGroup can display a frame
78 and a title because it inherits Q3GroupBox.
79 \i The button group is an invisible widget and the contained
80 buttons have some other parent widget. In this usage, each button
81 must be manually inserted, using insert(), into the button group
82 and given an identifier.
83 \endlist
84
85 A button can be removed from the group with remove(). A pointer to
86 a button with a given id can be obtained using find(). The id of a
87 button is available using id(). A button can be set \e on with
88 setButton(). The number of buttons in the group is returned by
89 count().
90
91 \sa QPushButton, QCheckBox, QRadioButton
92*/
93
94/*!
95 \property Q3ButtonGroup::exclusive
96 \brief whether the button group is exclusive
97
98 If this property is true, then the buttons in the group are
99 toggled, and to untoggle a button you must click on another button
100 in the group. The default value is false.
101*/
102
103/*!
104 \property Q3ButtonGroup::radioButtonExclusive
105 \brief whether the radio buttons in the group are exclusive
106
107 If this property is true (the default), the \link QRadioButton
108 radio buttons\endlink in the group are treated exclusively.
109*/
110
111
112/*!
113 Constructs a button group with no title.
114
115 The \a parent and \a name arguments are passed to the QWidget
116 constructor.
117*/
118
119Q3ButtonGroup::Q3ButtonGroup(QWidget *parent, const char *name)
120 : Q3GroupBox(parent, name)
121{
122 init();
123}
124
125/*!
126 Constructs a button group with the title \a title.
127
128 The \a parent and \a name arguments are passed to the QWidget
129 constructor.
130*/
131
132Q3ButtonGroup::Q3ButtonGroup(const QString &title, QWidget *parent,
133 const char *name)
134 : Q3GroupBox(title, parent, name)
135{
136 init();
137}
138
139/*!
140 Constructs a button group with no title. Child widgets will be
141 arranged in \a strips rows or columns (depending on \a
142 orientation).
143
144 The \a parent and \a name arguments are passed to the QWidget
145 constructor.
146*/
147
148Q3ButtonGroup::Q3ButtonGroup(int strips, Qt::Orientation orientation,
149 QWidget *parent, const char *name)
150 : Q3GroupBox(strips, orientation, parent, name)
151{
152 init();
153}
154
155/*!
156 Constructs a button group with title \a title. Child widgets will
157 be arranged in \a strips rows or columns (depending on \a
158 orientation).
159
160 The \a parent and \a name arguments are passed to the QWidget
161 constructor.
162*/
163
164Q3ButtonGroup::Q3ButtonGroup(int strips, Qt::Orientation orientation,
165 const QString &title, QWidget *parent,
166 const char *name)
167 : Q3GroupBox(strips, orientation, title, parent, name)
168{
169 init();
170}
171
172/*!
173 Initializes the button group.
174*/
175
176void Q3ButtonGroup::init()
177{
178 group.setExclusive(false);
179 radio_excl = true;
180}
181
182/*! Destructor. */
183
184Q3ButtonGroup::~Q3ButtonGroup()
185{
186}
187
188bool Q3ButtonGroup::isExclusive() const
189{
190 return group.exclusive();
191}
192
193void Q3ButtonGroup::setExclusive(bool enable)
194{
195 group.setExclusive(enable);
196}
197
198
199/*!
200 Inserts the \a button with the identifier \a id into the button
201 group. Returns the button identifier.
202
203 Buttons are normally inserted into a button group automatically by
204 passing the button group as the parent when the button is
205 constructed. So it is not necessary to manually insert buttons
206 that have this button group as their parent widget. An exception
207 is when you want custom identifiers instead of the default 0, 1,
208 2, etc., or if you want the buttons to have some other parent.
209
210 The button is assigned the identifier \a id or an automatically
211 generated identifier. It works as follows: If \a id >= 0, this
212 identifier is assigned. If \a id == -1 (default), the identifier
213 is equal to the number of buttons in the group. If \a id is any
214 other negative integer, for instance -2, a unique identifier
215 (negative integer \<= -2) is generated. No button has an id of -1.
216
217 \sa find(), remove(), setExclusive()
218*/
219
220int Q3ButtonGroup::insert(QAbstractButton *button, int id)
221{
222 remove_helper(button);
223 return insert_helper(button, id);
224}
225
226int Q3ButtonGroup::insert_helper(QAbstractButton *button, int id)
227{
228 if (isExclusive() || !qobject_cast<QRadioButton*>(button))
229 group.addButton(button);
230
231 static int seq_no = -2;
232 if (id < -1)
233 id = seq_no--;
234 else if (id == -1)
235 id = buttonIds.count();
236 buttonIds.insert(id, button);
237 connect(button, SIGNAL(pressed()) , SLOT(buttonPressed()));
238 connect(button, SIGNAL(released()), SLOT(buttonReleased()));
239 connect(button, SIGNAL(clicked()) , SLOT(buttonClicked()));
240 connect(button, SIGNAL(destroyed()) , SLOT(buttonDestroyed()));
241 return id;
242}
243
244/*!
245 Returns the number of buttons in the group.
246*/
247int Q3ButtonGroup::count() const
248{
249 fixChildren();
250 return buttonIds.count();
251}
252
253/*!
254 Removes the \a button from the button group.
255
256 \sa insert()
257*/
258
259void Q3ButtonGroup::remove(QAbstractButton *button)
260{
261 fixChildren();
262 remove_helper(button);
263}
264
265void Q3ButtonGroup::remove_helper(QAbstractButton *button)
266{
267 QMap<int, QAbstractButton*>::Iterator it = buttonIds.begin();
268 while (it != buttonIds.end()) {