Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/widgets/widgets/qbuttongroup.cpp |
Source code | Switch to Preprocessed file |
Line | Source | Count | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1 | /**************************************************************************** | - | ||||||||||||
2 | ** | - | ||||||||||||
3 | ** Copyright (C) 2015 The Qt Company Ltd. | - | ||||||||||||
4 | ** Contact: http://www.qt.io/licensing/ | - | ||||||||||||
5 | ** | - | ||||||||||||
6 | ** This file is part of the QtWidgets module of the Qt Toolkit. | - | ||||||||||||
7 | ** | - | ||||||||||||
8 | ** $QT_BEGIN_LICENSE:LGPL21$ | - | ||||||||||||
9 | ** Commercial License Usage | - | ||||||||||||
10 | ** Licensees holding valid commercial Qt licenses may use this file in | - | ||||||||||||
11 | ** accordance with the commercial license agreement provided with the | - | ||||||||||||
12 | ** Software or, alternatively, in accordance with the terms contained in | - | ||||||||||||
13 | ** a written agreement between you and The Qt Company. For licensing terms | - | ||||||||||||
14 | ** and conditions see http://www.qt.io/terms-conditions. For further | - | ||||||||||||
15 | ** information use the contact form at http://www.qt.io/contact-us. | - | ||||||||||||
16 | ** | - | ||||||||||||
17 | ** GNU Lesser General Public License Usage | - | ||||||||||||
18 | ** Alternatively, this file may be used under the terms of the GNU Lesser | - | ||||||||||||
19 | ** General Public License version 2.1 or version 3 as published by the Free | - | ||||||||||||
20 | ** Software Foundation and appearing in the file LICENSE.LGPLv21 and | - | ||||||||||||
21 | ** LICENSE.LGPLv3 included in the packaging of this file. Please review the | - | ||||||||||||
22 | ** following information to ensure the GNU Lesser General Public License | - | ||||||||||||
23 | ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and | - | ||||||||||||
24 | ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. | - | ||||||||||||
25 | ** | - | ||||||||||||
26 | ** As a special exception, The Qt Company gives you certain additional | - | ||||||||||||
27 | ** rights. These rights are described in The Qt Company LGPL Exception | - | ||||||||||||
28 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. | - | ||||||||||||
29 | ** | - | ||||||||||||
30 | ** $QT_END_LICENSE$ | - | ||||||||||||
31 | ** | - | ||||||||||||
32 | ****************************************************************************/ | - | ||||||||||||
33 | - | |||||||||||||
34 | #include "private/qbuttongroup_p.h" | - | ||||||||||||
35 | - | |||||||||||||
36 | #ifndef QT_NO_BUTTONGROUP | - | ||||||||||||
37 | - | |||||||||||||
38 | #include "private/qabstractbutton_p.h" | - | ||||||||||||
39 | - | |||||||||||||
40 | QT_BEGIN_NAMESPACE | - | ||||||||||||
41 | - | |||||||||||||
42 | // detect a checked button other than the current one | - | ||||||||||||
43 | void QButtonGroupPrivate::detectCheckedButton() | - | ||||||||||||
44 | { | - | ||||||||||||
45 | QAbstractButton *previous = checkedButton; | - | ||||||||||||
46 | checkedButton = 0; | - | ||||||||||||
47 | if (exclusive)
| 0 | ||||||||||||
48 | return; never executed: return; | 0 | ||||||||||||
49 | for (int i = 0; i < buttonList.count(); i++) {
| 0 | ||||||||||||
50 | if (buttonList.at(i) != previous && buttonList.at(i)->isChecked()) {
| 0 | ||||||||||||
51 | checkedButton = buttonList.at(i); | - | ||||||||||||
52 | return; never executed: return; | 0 | ||||||||||||
53 | } | - | ||||||||||||
54 | } never executed: end of block | 0 | ||||||||||||
55 | } never executed: end of block | 0 | ||||||||||||
56 | - | |||||||||||||
57 | /*! | - | ||||||||||||
58 | \class QButtonGroup | - | ||||||||||||
59 | \brief The QButtonGroup class provides a container to organize groups of | - | ||||||||||||
60 | button widgets. | - | ||||||||||||
61 | - | |||||||||||||
62 | \ingroup organizers | - | ||||||||||||
63 | \ingroup geomanagement | - | ||||||||||||
64 | \inmodule QtWidgets | - | ||||||||||||
65 | - | |||||||||||||
66 | QButtonGroup provides an abstract container into which button widgets can | - | ||||||||||||
67 | be placed. It does not provide a visual representation of this container | - | ||||||||||||
68 | (see QGroupBox for a container widget), but instead manages the states of | - | ||||||||||||
69 | each of the buttons in the group. | - | ||||||||||||
70 | - | |||||||||||||
71 | An \l {QButtonGroup::exclusive} {exclusive} button group switches | - | ||||||||||||
72 | off all checkable (toggle) buttons except the one that has been | - | ||||||||||||
73 | clicked. By default, a button group is exclusive. The buttons in a | - | ||||||||||||
74 | button group are usually checkable \l{QPushButton}s, \l{QCheckBox}es | - | ||||||||||||
75 | (normally for non-exclusive button groups), or \l{QRadioButton}s. | - | ||||||||||||
76 | If you create an exclusive button group, you should ensure that | - | ||||||||||||
77 | one of the buttons in the group is initially checked; otherwise, | - | ||||||||||||
78 | the group will initially be in a state where no buttons are | - | ||||||||||||
79 | checked. | - | ||||||||||||
80 | - | |||||||||||||
81 | A button can be added to the group with addButton() and removed | - | ||||||||||||
82 | with removeButton(). If the group is exclusive, the | - | ||||||||||||
83 | currently checked button is available with checkedButton(). If a | - | ||||||||||||
84 | button is clicked, the buttonClicked() signal is emitted; for a | - | ||||||||||||
85 | checkable button in an exclusive group this means that the button | - | ||||||||||||
86 | has been checked. The list of buttons in the group is returned by | - | ||||||||||||
87 | buttons(). | - | ||||||||||||
88 | - | |||||||||||||
89 | In addition, QButtonGroup can map between integers and buttons. | - | ||||||||||||
90 | You can assign an integer id to a button with setId(), and | - | ||||||||||||
91 | retrieve it with id(). The id of the currently checked button is | - | ||||||||||||
92 | available with checkedId(), and there is an overloaded signal | - | ||||||||||||
93 | buttonClicked() which emits the id of the button. The id \c {-1} | - | ||||||||||||
94 | is reserved by QButtonGroup to mean "no such button". The purpose | - | ||||||||||||
95 | of the mapping mechanism is to simplify the representation of enum | - | ||||||||||||
96 | values in a user interface. | - | ||||||||||||
97 | - | |||||||||||||
98 | \sa QGroupBox, QPushButton, QCheckBox, QRadioButton | - | ||||||||||||
99 | */ | - | ||||||||||||
100 | - | |||||||||||||
101 | /*! | - | ||||||||||||
102 | Constructs a new, empty button group with the given \a parent. | - | ||||||||||||
103 | - | |||||||||||||
104 | \sa addButton(), setExclusive() | - | ||||||||||||
105 | */ | - | ||||||||||||
106 | QButtonGroup::QButtonGroup(QObject *parent) | - | ||||||||||||
107 | : QObject(*new QButtonGroupPrivate, parent) | - | ||||||||||||
108 | { | - | ||||||||||||
109 | } never executed: end of block | 0 | ||||||||||||
110 | - | |||||||||||||
111 | /*! | - | ||||||||||||
112 | Destroys the button group. | - | ||||||||||||
113 | */ | - | ||||||||||||
114 | QButtonGroup::~QButtonGroup() | - | ||||||||||||
115 | { | - | ||||||||||||
116 | Q_D(QButtonGroup); | - | ||||||||||||
117 | for (int i = 0; i < d->buttonList.count(); ++i)
| 0 | ||||||||||||
118 | d->buttonList.at(i)->d_func()->group = 0; never executed: d->buttonList.at(i)->d_func()->group = 0; | 0 | ||||||||||||
119 | } never executed: end of block | 0 | ||||||||||||
120 | - | |||||||||||||
121 | /*! | - | ||||||||||||
122 | \property QButtonGroup::exclusive | - | ||||||||||||
123 | \brief whether the button group is exclusive | - | ||||||||||||
124 | - | |||||||||||||
125 | If this property is \c true, then only one button in the group can be checked | - | ||||||||||||
126 | at any given time. The user can click on any button to check it, and that | - | ||||||||||||
127 | button will replace the existing one as the checked button in the group. | - | ||||||||||||
128 | - | |||||||||||||
129 | In an exclusive group, the user cannot uncheck the currently checked button | - | ||||||||||||
130 | by clicking on it; instead, another button in the group must be clicked | - | ||||||||||||
131 | to set the new checked button for that group. | - | ||||||||||||
132 | - | |||||||||||||
133 | By default, this property is \c true. | - | ||||||||||||
134 | */ | - | ||||||||||||
135 | bool QButtonGroup::exclusive() const | - | ||||||||||||
136 | { | - | ||||||||||||
137 | Q_D(const QButtonGroup); | - | ||||||||||||
138 | return d->exclusive; never executed: return d->exclusive; | 0 | ||||||||||||
139 | } | - | ||||||||||||
140 | - | |||||||||||||
141 | void QButtonGroup::setExclusive(bool exclusive) | - | ||||||||||||
142 | { | - | ||||||||||||
143 | Q_D(QButtonGroup); | - | ||||||||||||
144 | d->exclusive = exclusive; | - | ||||||||||||
145 | } never executed: end of block | 0 | ||||||||||||
146 | - | |||||||||||||
147 | - | |||||||||||||
148 | - | |||||||||||||
149 | /*! | - | ||||||||||||
150 | \fn void QButtonGroup::buttonClicked(QAbstractButton *button) | - | ||||||||||||
151 | - | |||||||||||||
152 | This signal is emitted when the given \a button is clicked. A | - | ||||||||||||
153 | button is clicked when it is first pressed and then released, when | - | ||||||||||||
154 | its shortcut key is typed, or when QAbstractButton::click() | - | ||||||||||||
155 | or QAbstractButton::animateClick() is programmatically called. | - | ||||||||||||
156 | - | |||||||||||||
157 | - | |||||||||||||
158 | \sa checkedButton(), QAbstractButton::clicked() | - | ||||||||||||
159 | */ | - | ||||||||||||
160 | - | |||||||||||||
161 | /*! | - | ||||||||||||
162 | \fn void QButtonGroup::buttonClicked(int id) | - | ||||||||||||
163 | - | |||||||||||||
164 | This signal is emitted when a button with the given \a id is | - | ||||||||||||
165 | clicked. | - | ||||||||||||
166 | - | |||||||||||||
167 | \sa checkedButton(), QAbstractButton::clicked() | - | ||||||||||||
168 | */ | - | ||||||||||||
169 | - | |||||||||||||
170 | /*! | - | ||||||||||||
171 | \fn void QButtonGroup::buttonPressed(QAbstractButton *button) | - | ||||||||||||
172 | \since 4.2 | - | ||||||||||||
173 | - | |||||||||||||
174 | This signal is emitted when the given \a button is pressed down. | - | ||||||||||||
175 | - | |||||||||||||
176 | \sa QAbstractButton::pressed() | - | ||||||||||||
177 | */ | - | ||||||||||||
178 | - | |||||||||||||
179 | /*! | - | ||||||||||||
180 | \fn void QButtonGroup::buttonPressed(int id) | - | ||||||||||||
181 | \since 4.2 | - | ||||||||||||
182 | - | |||||||||||||
183 | This signal is emitted when a button with the given \a id is | - | ||||||||||||
184 | pressed down. | - | ||||||||||||
185 | - | |||||||||||||
186 | \sa QAbstractButton::pressed() | - | ||||||||||||
187 | */ | - | ||||||||||||
188 | - | |||||||||||||
189 | /*! | - | ||||||||||||
190 | \fn void QButtonGroup::buttonReleased(QAbstractButton *button) | - | ||||||||||||
191 | \since 4.2 | - | ||||||||||||
192 | - | |||||||||||||
193 | This signal is emitted when the given \a button is released. | - | ||||||||||||
194 | - | |||||||||||||
195 | \sa QAbstractButton::released() | - | ||||||||||||
196 | */ | - | ||||||||||||
197 | - | |||||||||||||
198 | /*! | - | ||||||||||||
199 | \fn void QButtonGroup::buttonReleased(int id) | - | ||||||||||||
200 | \since 4.2 | - | ||||||||||||
201 | - | |||||||||||||
202 | This signal is emitted when a button with the given \a id is | - | ||||||||||||
203 | released. | - | ||||||||||||
204 | - | |||||||||||||
205 | \sa QAbstractButton::released() | - | ||||||||||||
206 | */ | - | ||||||||||||
207 | - | |||||||||||||
208 | /*! | - | ||||||||||||
209 | \fn void QButtonGroup::buttonToggled(QAbstractButton *button, bool checked) | - | ||||||||||||
210 | \since 5.2 | - | ||||||||||||
211 | - | |||||||||||||
212 | This signal is emitted when the given \a button is toggled. | - | ||||||||||||
213 | \a checked is true if the button is checked, or false if the button is unchecked. | - | ||||||||||||
214 | - | |||||||||||||
215 | \sa QAbstractButton::toggled() | - | ||||||||||||
216 | */ | - | ||||||||||||
217 | - | |||||||||||||
218 | /*! | - | ||||||||||||
219 | \fn void QButtonGroup::buttonToggled(int id, bool checked) | - | ||||||||||||
220 | \since 5.2 | - | ||||||||||||
221 | - | |||||||||||||
222 | This signal is emitted when a button with the given \a id is toggled. | - | ||||||||||||
223 | \a checked is true if the button is checked, or false if the button is unchecked. | - | ||||||||||||
224 | - | |||||||||||||
225 | \sa QAbstractButton::toggled() | - | ||||||||||||
226 | */ | - | ||||||||||||
227 | - | |||||||||||||
228 | - | |||||||||||||
229 | /*! | - | ||||||||||||
230 | Adds the given \a button to the button group. If \a id is -1, | - | ||||||||||||
231 | an id will be assigned to the button. | - | ||||||||||||
232 | Automatically assigned ids are guaranteed to be negative, | - | ||||||||||||
233 | starting with -2. If you are assigning your own ids, use | - | ||||||||||||
234 | positive values to avoid conflicts. | - | ||||||||||||
235 | - | |||||||||||||
236 | \sa removeButton(), buttons() | - | ||||||||||||
237 | */ | - | ||||||||||||
238 | void QButtonGroup::addButton(QAbstractButton *button, int id) | - | ||||||||||||
239 | { | - | ||||||||||||
240 | Q_D(QButtonGroup); | - | ||||||||||||
241 | if (QButtonGroup *previous = button->d_func()->group)
| 0 | ||||||||||||
242 | previous->removeButton(button); never executed: previous->removeButton(button); | 0 | ||||||||||||
243 | button->d_func()->group = this; | - | ||||||||||||
244 | d->buttonList.append(button); | - | ||||||||||||
245 | if (id == -1) {
| 0 | ||||||||||||
246 | const QHash<QAbstractButton*, int>::const_iterator it | - | ||||||||||||
247 | = std::min_element(d->mapping.cbegin(), d->mapping.cend()); | - | ||||||||||||
248 | if (it == d->mapping.cend())
| 0 | ||||||||||||
249 | d->mapping[button] = -2; never executed: d->mapping[button] = -2; | 0 | ||||||||||||
250 | else | - | ||||||||||||
251 | d->mapping[button] = *it - 1; never executed: d->mapping[button] = *it - 1; | 0 | ||||||||||||
252 | } else { | - | ||||||||||||
253 | d->mapping[button] = id; | - | ||||||||||||
254 | } never executed: end of block | 0 | ||||||||||||
255 | - | |||||||||||||
256 | if (d->exclusive && button->isChecked())
| 0 | ||||||||||||
257 | button->d_func()->notifyChecked(); never executed: button->d_func()->notifyChecked(); | 0 | ||||||||||||
258 | } never executed: end of block | 0 | ||||||||||||
259 | - | |||||||||||||
260 | /*! | - | ||||||||||||
261 | Removes the given \a button from the button group. | - | ||||||||||||
262 | - | |||||||||||||
263 | \sa addButton(), buttons() | - | ||||||||||||
264 | */ | - | ||||||||||||
265 | void QButtonGroup::removeButton(QAbstractButton *button) | - | ||||||||||||
266 | { | - | ||||||||||||
267 | Q_D(QButtonGroup); | - | ||||||||||||
268 | if (d->checkedButton == button) {
| 0 | ||||||||||||
269 | d->detectCheckedButton(); | - | ||||||||||||
270 | } never executed: end of block | 0 | ||||||||||||
271 | if (button->d_func()->group == this) {
| 0 | ||||||||||||
272 | button->d_func()->group = 0; | - | ||||||||||||
273 | d->buttonList.removeAll(button); | - | ||||||||||||
274 | d->mapping.remove(button); | - | ||||||||||||
275 | } never executed: end of block | 0 | ||||||||||||
276 | } never executed: end of block | 0 | ||||||||||||
277 | - | |||||||||||||
278 | /*! | - | ||||||||||||
279 | Returns the button group's list of buttons. This may be empty. | - | ||||||||||||
280 | - | |||||||||||||
281 | \sa addButton(), removeButton() | - | ||||||||||||
282 | */ | - | ||||||||||||
283 | QList<QAbstractButton*> QButtonGroup::buttons() const | - | ||||||||||||
284 | { | - | ||||||||||||
285 | Q_D(const QButtonGroup); | - | ||||||||||||
286 | return d->buttonList; never executed: return d->buttonList; | 0 | ||||||||||||
287 | } | - | ||||||||||||
288 | - | |||||||||||||
289 | /*! | - | ||||||||||||
290 | Returns the button group's checked button, or 0 if no buttons are | - | ||||||||||||
291 | checked. | - | ||||||||||||
292 | - | |||||||||||||
293 | \sa buttonClicked() | - | ||||||||||||
294 | */ | - | ||||||||||||
295 | QAbstractButton *QButtonGroup::checkedButton() const | - | ||||||||||||
296 | { | - | ||||||||||||
297 | Q_D(const QButtonGroup); | - | ||||||||||||
298 | return d->checkedButton; never executed: return d->checkedButton; | 0 | ||||||||||||
299 | } | - | ||||||||||||
300 | - | |||||||||||||
301 | /*! | - | ||||||||||||
302 | \since 4.1 | - | ||||||||||||
303 | - | |||||||||||||
304 | Returns the button with the specified \a id, or 0 if no such button | - | ||||||||||||
305 | exists. | - | ||||||||||||
306 | */ | - | ||||||||||||
307 | QAbstractButton *QButtonGroup::button(int id) const | - | ||||||||||||
308 | { | - | ||||||||||||
309 | Q_D(const QButtonGroup); | - | ||||||||||||
310 | return d->mapping.key(id); never executed: return d->mapping.key(id); | 0 | ||||||||||||
311 | } | - | ||||||||||||
312 | - | |||||||||||||
313 | /*! | - | ||||||||||||
314 | \since 4.1 | - | ||||||||||||
315 | - | |||||||||||||
316 | Sets the \a id for the specified \a button. Note that \a id cannot | - | ||||||||||||
317 | be -1. | - | ||||||||||||
318 | - | |||||||||||||
319 | \sa id() | - | ||||||||||||
320 | */ | - | ||||||||||||
321 | void QButtonGroup::setId(QAbstractButton *button, int id) | - | ||||||||||||
322 | { | - | ||||||||||||
323 | Q_D(QButtonGroup); | - | ||||||||||||
324 | if (button && id != -1)
| 0 | ||||||||||||
325 | d->mapping[button] = id; never executed: d->mapping[button] = id; | 0 | ||||||||||||
326 | } never executed: end of block | 0 | ||||||||||||
327 | - | |||||||||||||
328 | /*! | - | ||||||||||||
329 | \since 4.1 | - | ||||||||||||
330 | - | |||||||||||||
331 | Returns the id for the specified \a button, or -1 if no such button | - | ||||||||||||
332 | exists. | - | ||||||||||||
333 | - | |||||||||||||
334 | - | |||||||||||||
335 | \sa setId() | - | ||||||||||||
336 | */ | - | ||||||||||||
337 | int QButtonGroup::id(QAbstractButton *button) const | - | ||||||||||||
338 | { | - | ||||||||||||
339 | Q_D(const QButtonGroup); | - | ||||||||||||
340 | return d->mapping.value(button, -1); never executed: return d->mapping.value(button, -1); | 0 | ||||||||||||
341 | } | - | ||||||||||||
342 | - | |||||||||||||
343 | /*! | - | ||||||||||||
344 | \since 4.1 | - | ||||||||||||
345 | - | |||||||||||||
346 | Returns the id of the checkedButton(), or -1 if no button is checked. | - | ||||||||||||
347 | - | |||||||||||||
348 | \sa setId() | - | ||||||||||||
349 | */ | - | ||||||||||||
350 | int QButtonGroup::checkedId() const | - | ||||||||||||
351 | { | - | ||||||||||||
352 | Q_D(const QButtonGroup); | - | ||||||||||||
353 | return d->mapping.value(d->checkedButton, -1); never executed: return d->mapping.value(d->checkedButton, -1); | 0 | ||||||||||||
354 | } | - | ||||||||||||
355 | - | |||||||||||||
356 | QT_END_NAMESPACE | - | ||||||||||||
357 | - | |||||||||||||
358 | #include "moc_qbuttongroup.cpp" | - | ||||||||||||
359 | - | |||||||||||||
360 | #endif // QT_NO_BUTTONGROUP | - | ||||||||||||
Source code | Switch to Preprocessed file |