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