| Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/widgets/widgets/qradiobutton.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 "qradiobutton.h" | - | ||||||||||||
| 41 | #include "qapplication.h" | - | ||||||||||||
| 42 | #include "qbitmap.h" | - | ||||||||||||
| 43 | #include "qbuttongroup.h" | - | ||||||||||||
| 44 | #include "qstylepainter.h" | - | ||||||||||||
| 45 | #include "qstyle.h" | - | ||||||||||||
| 46 | #include "qstyleoption.h" | - | ||||||||||||
| 47 | #include "qevent.h" | - | ||||||||||||
| 48 | - | |||||||||||||
| 49 | #include "private/qabstractbutton_p.h" | - | ||||||||||||
| 50 | - | |||||||||||||
| 51 | QT_BEGIN_NAMESPACE | - | ||||||||||||
| 52 | - | |||||||||||||
| 53 | class QRadioButtonPrivate : public QAbstractButtonPrivate | - | ||||||||||||
| 54 | { | - | ||||||||||||
| 55 | Q_DECLARE_PUBLIC(QRadioButton) | - | ||||||||||||
| 56 | - | |||||||||||||
| 57 | public: | - | ||||||||||||
| 58 | QRadioButtonPrivate() : QAbstractButtonPrivate(QSizePolicy::RadioButton), hovering(true) {} never executed: end of block | 0 | ||||||||||||
| 59 | void init(); | - | ||||||||||||
| 60 | uint hovering : 1; | - | ||||||||||||
| 61 | }; | - | ||||||||||||
| 62 | - | |||||||||||||
| 63 | /* | - | ||||||||||||
| 64 | Initializes the radio button. | - | ||||||||||||
| 65 | */ | - | ||||||||||||
| 66 | void QRadioButtonPrivate::init() | - | ||||||||||||
| 67 | { | - | ||||||||||||
| 68 | Q_Q(QRadioButton); | - | ||||||||||||
| 69 | q->setCheckable(true); | - | ||||||||||||
| 70 | q->setAutoExclusive(true); | - | ||||||||||||
| 71 | q->setMouseTracking(true); | - | ||||||||||||
| 72 | q->setForegroundRole(QPalette::WindowText); | - | ||||||||||||
| 73 | setLayoutItemMargins(QStyle::SE_RadioButtonLayoutItem); | - | ||||||||||||
| 74 | } never executed: end of block | 0 | ||||||||||||
| 75 | - | |||||||||||||
| 76 | /*! | - | ||||||||||||
| 77 | \class QRadioButton | - | ||||||||||||
| 78 | \brief The QRadioButton widget provides a radio button with a text label. | - | ||||||||||||
| 79 | - | |||||||||||||
| 80 | \ingroup basicwidgets | - | ||||||||||||
| 81 | \inmodule QtWidgets | - | ||||||||||||
| 82 | - | |||||||||||||
| 83 | A QRadioButton is an option button that can be switched on (checked) or | - | ||||||||||||
| 84 | off (unchecked). Radio buttons typically present the user with a "one | - | ||||||||||||
| 85 | of many" choice. In a group of radio buttons, only one radio button at | - | ||||||||||||
| 86 | a time can be checked; if the user selects another button, the | - | ||||||||||||
| 87 | previously selected button is switched off. | - | ||||||||||||
| 88 | - | |||||||||||||
| 89 | Radio buttons are autoExclusive by default. If auto-exclusive is | - | ||||||||||||
| 90 | enabled, radio buttons that belong to the same parent widget | - | ||||||||||||
| 91 | behave as if they were part of the same exclusive button group. If | - | ||||||||||||
| 92 | you need multiple exclusive button groups for radio buttons that | - | ||||||||||||
| 93 | belong to the same parent widget, put them into a QButtonGroup. | - | ||||||||||||
| 94 | - | |||||||||||||
| 95 | Whenever a button is switched on or off, it emits the toggled() signal. | - | ||||||||||||
| 96 | Connect to this signal if you want to trigger an action each time the | - | ||||||||||||
| 97 | button changes state. Use isChecked() to see if a particular button is | - | ||||||||||||
| 98 | selected. | - | ||||||||||||
| 99 | - | |||||||||||||
| 100 | Just like QPushButton, a radio button displays text, and | - | ||||||||||||
| 101 | optionally a small icon. The icon is set with setIcon(). The text | - | ||||||||||||
| 102 | can be set in the constructor or with setText(). A shortcut key | - | ||||||||||||
| 103 | can be specified by preceding the preferred character with an | - | ||||||||||||
| 104 | ampersand in the text. For example: | - | ||||||||||||
| 105 | - | |||||||||||||
| 106 | \snippet code/src_gui_widgets_qradiobutton.cpp 0 | - | ||||||||||||
| 107 | - | |||||||||||||
| 108 | In this example the shortcut is \e{Alt+c}. See the \l | - | ||||||||||||
| 109 | {QShortcut#mnemonic}{QShortcut} documentation for details. To | - | ||||||||||||
| 110 | display an actual ampersand, use '&&'. | - | ||||||||||||
| 111 | - | |||||||||||||
| 112 | Important inherited members: text(), setText(), text(), | - | ||||||||||||
| 113 | setDown(), isDown(), autoRepeat(), group(), setAutoRepeat(), | - | ||||||||||||
| 114 | toggle(), pressed(), released(), clicked(), and toggled(). | - | ||||||||||||
| 115 | - | |||||||||||||
| 116 | \table 100% | - | ||||||||||||
| 117 | \row \li \inlineimage fusion-radiobutton.png Screenshot of a Fusion radio button | - | ||||||||||||
| 118 | \li A radio button shown in the \l{Fusion Style Widget Gallery}{Fusion widget style}. | - | ||||||||||||
| 119 | \row \li \inlineimage windowsvista-radiobutton.png Screenshot of a Windows Vista radio button | - | ||||||||||||
| 120 | \li A radio button shown in the \l{Windows Vista Style Widget Gallery}{Windows Vista widget style}. | - | ||||||||||||
| 121 | \row \li \inlineimage macintosh-radiobutton.png Screenshot of a Macintosh radio button | - | ||||||||||||
| 122 | \li A radio button shown in the \l{Macintosh Style Widget Gallery}{Macintosh widget style}. | - | ||||||||||||
| 123 | \endtable | - | ||||||||||||
| 124 | - | |||||||||||||
| 125 | \sa QPushButton, QToolButton, QCheckBox, {fowler}{GUI Design Handbook: Radio Button}, | - | ||||||||||||
| 126 | {Group Box Example} | - | ||||||||||||
| 127 | */ | - | ||||||||||||
| 128 | - | |||||||||||||
| 129 | - | |||||||||||||
| 130 | /*! | - | ||||||||||||
| 131 | Constructs a radio button with the given \a parent, but with no text or | - | ||||||||||||
| 132 | pixmap. | - | ||||||||||||
| 133 | - | |||||||||||||
| 134 | The \a parent argument is passed on to the QAbstractButton constructor. | - | ||||||||||||
| 135 | */ | - | ||||||||||||
| 136 | - | |||||||||||||
| 137 | QRadioButton::QRadioButton(QWidget *parent) | - | ||||||||||||
| 138 | : QAbstractButton(*new QRadioButtonPrivate, parent) | - | ||||||||||||
| 139 | { | - | ||||||||||||
| 140 | Q_D(QRadioButton); | - | ||||||||||||
| 141 | d->init(); | - | ||||||||||||
| 142 | } never executed: end of block | 0 | ||||||||||||
| 143 | - | |||||||||||||
| 144 | /*! | - | ||||||||||||
| 145 | Destructor. | - | ||||||||||||
| 146 | */ | - | ||||||||||||
| 147 | QRadioButton::~QRadioButton() | - | ||||||||||||
| 148 | { | - | ||||||||||||
| 149 | } | - | ||||||||||||
| 150 | - | |||||||||||||
| 151 | /*! | - | ||||||||||||
| 152 | Constructs a radio button with the given \a parent and \a text string. | - | ||||||||||||
| 153 | - | |||||||||||||
| 154 | The \a parent argument is passed on to the QAbstractButton constructor. | - | ||||||||||||
| 155 | */ | - | ||||||||||||
| 156 | - | |||||||||||||
| 157 | QRadioButton::QRadioButton(const QString &text, QWidget *parent) | - | ||||||||||||
| 158 | : QAbstractButton(*new QRadioButtonPrivate, parent) | - | ||||||||||||
| 159 | { | - | ||||||||||||
| 160 | Q_D(QRadioButton); | - | ||||||||||||
| 161 | d->init(); | - | ||||||||||||
| 162 | setText(text); | - | ||||||||||||
| 163 | } never executed: end of block | 0 | ||||||||||||
| 164 | - | |||||||||||||
| 165 | /*! | - | ||||||||||||
| 166 | Initialize \a option with the values from this QRadioButton. This method is useful | - | ||||||||||||
| 167 | for subclasses when they need a QStyleOptionButton, but don't want to fill | - | ||||||||||||
| 168 | in all the information themselves. | - | ||||||||||||
| 169 | - | |||||||||||||
| 170 | \sa QStyleOption::initFrom() | - | ||||||||||||
| 171 | */ | - | ||||||||||||
| 172 | void QRadioButton::initStyleOption(QStyleOptionButton *option) const | - | ||||||||||||
| 173 | { | - | ||||||||||||
| 174 | if (!option)
| 0 | ||||||||||||
| 175 | return; never executed: return; | 0 | ||||||||||||
| 176 | Q_D(const QRadioButton); | - | ||||||||||||
| 177 | option->initFrom(this); | - | ||||||||||||
| 178 | option->text = d->text; | - | ||||||||||||
| 179 | option->icon = d->icon; | - | ||||||||||||
| 180 | option->iconSize = iconSize(); | - | ||||||||||||
| 181 | if (d->down)
| 0 | ||||||||||||
| 182 | option->state |= QStyle::State_Sunken; never executed: option->state |= QStyle::State_Sunken; | 0 | ||||||||||||
| 183 | option->state |= (d->checked) ? QStyle::State_On : QStyle::State_Off;
| 0 | ||||||||||||
| 184 | if (testAttribute(Qt::WA_Hover) && underMouse()) {
| 0 | ||||||||||||
| 185 | option->state.setFlag(QStyle::State_MouseOver, d->hovering); | - | ||||||||||||
| 186 | } never executed: end of block | 0 | ||||||||||||
| 187 | } never executed: end of block | 0 | ||||||||||||
| 188 | - | |||||||||||||
| 189 | /*! | - | ||||||||||||
| 190 | \reimp | - | ||||||||||||
| 191 | */ | - | ||||||||||||
| 192 | QSize QRadioButton::sizeHint() const | - | ||||||||||||
| 193 | { | - | ||||||||||||
| 194 | Q_D(const QRadioButton); | - | ||||||||||||
| 195 | if (d->sizeHint.isValid())
| 0 | ||||||||||||
| 196 | return d->sizeHint; never executed: return d->sizeHint; | 0 | ||||||||||||
| 197 | ensurePolished(); | - | ||||||||||||
| 198 | QStyleOptionButton opt; | - | ||||||||||||
| 199 | initStyleOption(&opt); | - | ||||||||||||
| 200 | QSize sz = style()->itemTextRect(fontMetrics(), QRect(), Qt::TextShowMnemonic, | - | ||||||||||||
| 201 | false, text()).size(); | - | ||||||||||||
| 202 | if (!opt.icon.isNull())
| 0 | ||||||||||||
| 203 | sz = QSize(sz.width() + opt.iconSize.width() + 4, qMax(sz.height(), opt.iconSize.height())); never executed: sz = QSize(sz.width() + opt.iconSize.width() + 4, qMax(sz.height(), opt.iconSize.height())); | 0 | ||||||||||||
| 204 | d->sizeHint = (style()->sizeFromContents(QStyle::CT_RadioButton, &opt, sz, this). | - | ||||||||||||
| 205 | expandedTo(QApplication::globalStrut())); | - | ||||||||||||
| 206 | return d->sizeHint; never executed: return d->sizeHint; | 0 | ||||||||||||
| 207 | } | - | ||||||||||||
| 208 | - | |||||||||||||
| 209 | /*! | - | ||||||||||||
| 210 | \reimp | - | ||||||||||||
| 211 | */ | - | ||||||||||||
| 212 | QSize QRadioButton::minimumSizeHint() const | - | ||||||||||||
| 213 | { | - | ||||||||||||
| 214 | return sizeHint(); never executed: return sizeHint(); | 0 | ||||||||||||
| 215 | } | - | ||||||||||||
| 216 | - | |||||||||||||
| 217 | /*! | - | ||||||||||||
| 218 | \reimp | - | ||||||||||||
| 219 | */ | - | ||||||||||||
| 220 | bool QRadioButton::hitButton(const QPoint &pos) const | - | ||||||||||||
| 221 | { | - | ||||||||||||
| 222 | QStyleOptionButton opt; | - | ||||||||||||
| 223 | initStyleOption(&opt); | - | ||||||||||||
| 224 | return style()->subElementRect(QStyle::SE_RadioButtonClickRect, &opt, this).contains(pos); never executed: return style()->subElementRect(QStyle::SE_RadioButtonClickRect, &opt, this).contains(pos); | 0 | ||||||||||||
| 225 | } | - | ||||||||||||
| 226 | - | |||||||||||||
| 227 | /*! | - | ||||||||||||
| 228 | \reimp | - | ||||||||||||
| 229 | */ | - | ||||||||||||
| 230 | void QRadioButton::mouseMoveEvent(QMouseEvent *e) | - | ||||||||||||
| 231 | { | - | ||||||||||||
| 232 | Q_D(QRadioButton); | - | ||||||||||||
| 233 | if (testAttribute(Qt::WA_Hover)) {
| 0 | ||||||||||||
| 234 | bool hit = false; | - | ||||||||||||
| 235 | if (underMouse())
| 0 | ||||||||||||
| 236 | hit = hitButton(e->pos()); never executed: hit = hitButton(e->pos()); | 0 | ||||||||||||
| 237 | - | |||||||||||||
| 238 | if (hit != d->hovering) {
| 0 | ||||||||||||
| 239 | update(); | - | ||||||||||||
| 240 | d->hovering = hit; | - | ||||||||||||
| 241 | } never executed: end of block | 0 | ||||||||||||
| 242 | } never executed: end of block | 0 | ||||||||||||
| 243 | - | |||||||||||||
| 244 | QAbstractButton::mouseMoveEvent(e); | - | ||||||||||||
| 245 | } never executed: end of block | 0 | ||||||||||||
| 246 | - | |||||||||||||
| 247 | /*!\reimp | - | ||||||||||||
| 248 | */ | - | ||||||||||||
| 249 | void QRadioButton::paintEvent(QPaintEvent *) | - | ||||||||||||
| 250 | { | - | ||||||||||||
| 251 | QStylePainter p(this); | - | ||||||||||||
| 252 | QStyleOptionButton opt; | - | ||||||||||||
| 253 | initStyleOption(&opt); | - | ||||||||||||
| 254 | p.drawControl(QStyle::CE_RadioButton, opt); | - | ||||||||||||
| 255 | } never executed: end of block | 0 | ||||||||||||
| 256 | - | |||||||||||||
| 257 | /*! \reimp */ | - | ||||||||||||
| 258 | bool QRadioButton::event(QEvent *e) | - | ||||||||||||
| 259 | { | - | ||||||||||||
| 260 | Q_D(QRadioButton); | - | ||||||||||||
| 261 | if (e->type() == QEvent::StyleChange
| 0 | ||||||||||||
| 262 | #ifdef Q_OS_MAC | - | ||||||||||||
| 263 | || e->type() == QEvent::MacSizeChange | - | ||||||||||||
| 264 | #endif | - | ||||||||||||
| 265 | ) | - | ||||||||||||
| 266 | d->setLayoutItemMargins(QStyle::SE_RadioButtonLayoutItem); never executed: d->setLayoutItemMargins(QStyle::SE_RadioButtonLayoutItem); | 0 | ||||||||||||
| 267 | return QAbstractButton::event(e); never executed: return QAbstractButton::event(e); | 0 | ||||||||||||
| 268 | } | - | ||||||||||||
| 269 | - | |||||||||||||
| 270 | - | |||||||||||||
| 271 | QT_END_NAMESPACE | - | ||||||||||||
| 272 | - | |||||||||||||
| 273 | #include "moc_qradiobutton.cpp" | - | ||||||||||||
| Source code | Switch to Preprocessed file |