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