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