widgets/qcheckbox.cpp

Source codeSwitch to Preprocessed file
LineSource CodeCoverage
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 "qcheckbox.h" -
43#include "qapplication.h" -
44#include "qbitmap.h" -
45#include "qicon.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 -
53QT_BEGIN_NAMESPACE -
54 -
55class QCheckBoxPrivate : public QAbstractButtonPrivate -
56{ -
57 Q_DECLARE_PUBLIC(QCheckBox) -
58public: -
59 QCheckBoxPrivate() -
60 : QAbstractButtonPrivate(QSizePolicy::CheckBox), tristate(false), noChange(false), -
61 hovering(true), publishedState(Qt::Unchecked) {}
executed: }
Execution Count:31
31
62 -
63 uint tristate : 1; -
64 uint noChange : 1; -
65 uint hovering : 1; -
66 uint publishedState : 2; -
67 -
68 void init(); -
69}; -
70 -
71/*! -
72 \class QCheckBox -
73 \brief The QCheckBox widget provides a checkbox with a text label. -
74 -
75 \ingroup basicwidgets -
76 \inmodule QtWidgets -
77 -
78 A QCheckBox is an option button that can be switched on (checked) or off -
79 (unchecked). Checkboxes are typically used to represent features in an -
80 application that can be enabled or disabled without affecting others. -
81 Different types of behavior can be implemented. For example, a -
82 QButtonGroup can be used to group check buttons logically, allowing -
83 exclusive checkboxes. However, QButtonGroup does not provide any visual -
84 representation. -
85 -
86 The image below further illustrates the differences between exclusive and -
87 non-exclusive checkboxes. -
88 -
89 \table -
90 \row \li \inlineimage checkboxes-exclusive.png -
91 \li \inlineimage checkboxes-non-exclusive.png -
92 \endtable -
93 -
94 Whenever a checkbox is checked or cleared, it emits the signal -
95 stateChanged(). Connect to this signal if you want to trigger an action -
96 each time the checkbox changes state. You can use isChecked() to query -
97 whether or not a checkbox is checked. -
98 -
99 In addition to the usual checked and unchecked states, QCheckBox optionally -
100 provides a third state to indicate "no change". This is useful whenever you -
101 need to give the user the option of neither checking nor unchecking a -
102 checkbox. If you need this third state, enable it with setTristate(), and -
103 use checkState() to query the current toggle state. -
104 -
105 Just like QPushButton, a checkbox displays text, and optionally a small -
106 icon. The icon is set with setIcon(). The text can be set in the -
107 constructor or with setText(). A shortcut key can be specified by preceding -
108 the preferred character with an ampersand. For example: -
109 -
110 \snippet code/src_gui_widgets_qcheckbox.cpp 0 -
111 -
112 In this example, the shortcut is \e{Alt+A}. See the \l{QShortcut#mnemonic} -
113 {QShortcut} documentation for details. To display an actual ampersand, -
114 use '&&'. -
115 -
116 Important inherited functions: text(), setText(), text(), pixmap(), -
117 setPixmap(), accel(), setAccel(), isToggleButton(), setDown(), isDown(), -
118 isOn(), checkState(), autoRepeat(), isExclusiveToggle(), group(), -
119 setAutoRepeat(), toggle(), pressed(), released(), clicked(), toggled(), -
120 checkState(), and stateChanged(). -
121 -
122 \table 100% -
123 \row -
124 \li \inlineimage macintosh-checkbox.png Screenshot of a Macintosh style checkbox -
125 \li A checkbox shown in the \l{Macintosh Style Widget Gallery}{Macintosh widget style}. -
126 \row -
127 \li \inlineimage windowsvista-checkbox.png Screenshot of a Windows Vista style checkbox -
128 \li A checkbox shown in the \l{Windows Vista Style Widget Gallery}{Windows Vista widget style}. -
129 \row -
130 \li \inlineimage fusion-checkbox.png Screenshot of a Fusion style checkbox -
131 \li A checkbox shown in the \l{Fusion Style Widget Gallery}{Fusion widget style}. -
132 \endtable -
133 -
134 \sa QAbstractButton, QRadioButton, {fowler}{GUI Design Handbook: Check Box} -
135*/ -
136 -
137/*! -
138 \fn void QCheckBox::stateChanged(int state) -
139 -
140 This signal is emitted whenever the checkbox's state changes, i.e., -
141 whenever the user checks or unchecks it. -
142 -
143 \a state contains the checkbox's new Qt::CheckState. -
144*/ -
145 -
146/*! -
147 \property QCheckBox::tristate -
148 \brief whether the checkbox is a tri-state checkbox -
149 -
150 The default is false, i.e., the checkbox has only two states. -
151*/ -
152 -
153void QCheckBoxPrivate::init() -
154{ -
155 Q_Q(QCheckBox);
executed (the execution status of this line is deduced): QCheckBox * const q = q_func();
-
156 q->setCheckable(true);
executed (the execution status of this line is deduced): q->setCheckable(true);
-
157 q->setMouseTracking(true);
executed (the execution status of this line is deduced): q->setMouseTracking(true);
-
158 q->setForegroundRole(QPalette::WindowText);
executed (the execution status of this line is deduced): q->setForegroundRole(QPalette::WindowText);
-
159 setLayoutItemMargins(QStyle::SE_CheckBoxLayoutItem);
executed (the execution status of this line is deduced): setLayoutItemMargins(QStyle::SE_CheckBoxLayoutItem);
-
160}
executed: }
Execution Count:31
31
161 -
162/*! -
163 Initializes \a option with the values from this QCheckBox. This method is -
164 useful for subclasses that require a QStyleOptionButton, but do not want -
165 to fill in all the information themselves. -
166 -
167 \sa QStyleOption::initFrom() -
168*/ -
169void QCheckBox::initStyleOption(QStyleOptionButton *option) const -
170{ -
171 if (!option)
partially evaluated: !option
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:30
0-30
172 return;
never executed: return;
0
173 Q_D(const QCheckBox);
executed (the execution status of this line is deduced): const QCheckBoxPrivate * const d = d_func();
-
174 option->initFrom(this);
executed (the execution status of this line is deduced): option->initFrom(this);
-
175 if (d->down)
evaluated: d->down
TRUEFALSE
yes
Evaluation Count:2
yes
Evaluation Count:28
2-28
176 option->state |= QStyle::State_Sunken;
executed: option->state |= QStyle::State_Sunken;
Execution Count:2
2
177 if (d->tristate && d->noChange)
evaluated: d->tristate
TRUEFALSE
yes
Evaluation Count:2
yes
Evaluation Count:28
partially evaluated: d->noChange
TRUEFALSE
yes
Evaluation Count:2
no
Evaluation Count:0
0-28
178 option->state |= QStyle::State_NoChange;
executed: option->state |= QStyle::State_NoChange;
Execution Count:2
2
179 else -
180 option->state |= d->checked ? QStyle::State_On : QStyle::State_Off;
executed: option->state |= d->checked ? QStyle::State_On : QStyle::State_Off;
Execution Count:28
evaluated: d->checked
TRUEFALSE
yes
Evaluation Count:4
yes
Evaluation Count:24
4-28
181 if (testAttribute(Qt::WA_Hover) && underMouse()) {
partially evaluated: testAttribute(Qt::WA_Hover)
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:30
never evaluated: underMouse()
0-30
182 if (d->hovering)
never evaluated: d->hovering
0
183 option->state |= QStyle::State_MouseOver;
never executed: option->state |= QStyle::State_MouseOver;
0
184 else -
185 option->state &= ~QStyle::State_MouseOver;
never executed: option->state &= ~QStyle::State_MouseOver;
0
186 } -
187 option->text = d->text;
executed (the execution status of this line is deduced): option->text = d->text;
-
188 option->icon = d->icon;
executed (the execution status of this line is deduced): option->icon = d->icon;
-
189 option->iconSize = iconSize();
executed (the execution status of this line is deduced): option->iconSize = iconSize();
-
190}
executed: }
Execution Count:30
30
191 -
192/*! -
193 Constructs a checkbox with the given \a parent, but with no text. -
194 -
195 \a parent is passed on to the QAbstractButton constructor. -
196*/ -
197 -
198QCheckBox::QCheckBox(QWidget *parent) -
199 : QAbstractButton (*new QCheckBoxPrivate, parent) -
200{ -
201 Q_D(QCheckBox);
executed (the execution status of this line is deduced): QCheckBoxPrivate * const d = d_func();
-
202 d->init();
executed (the execution status of this line is deduced): d->init();
-
203}
executed: }
Execution Count:18
18
204 -
205/*! -
206 Constructs a checkbox with the given \a parent and \a text. -
207 -
208 \a parent is passed on to the QAbstractButton constructor. -
209*/ -
210 -
211QCheckBox::QCheckBox(const QString &text, QWidget *parent) -
212 : QAbstractButton (*new QCheckBoxPrivate, parent) -
213{ -
214 Q_D(QCheckBox);
executed (the execution status of this line is deduced): QCheckBoxPrivate * const d = d_func();
-
215 d->init();
executed (the execution status of this line is deduced): d->init();
-
216 setText(text);
executed (the execution status of this line is deduced): setText(text);
-
217}
executed: }
Execution Count:13
13
218 -
219/*! -
220 Destructor. -
221*/ -
222QCheckBox::~QCheckBox() -
223{ -
224} -
225 -
226void QCheckBox::setTristate(bool y) -
227{ -
228 Q_D(QCheckBox);
executed (the execution status of this line is deduced): QCheckBoxPrivate * const d = d_func();
-
229 d->tristate = y;
executed (the execution status of this line is deduced): d->tristate = y;
-
230}
executed: }
Execution Count:19
19
231 -
232bool QCheckBox::isTristate() const -
233{ -
234 Q_D(const QCheckBox);
executed (the execution status of this line is deduced): const QCheckBoxPrivate * const d = d_func();
-
235 return d->tristate;
executed: return d->tristate;
Execution Count:2
2
236} -
237 -
238 -
239/*! -
240 Returns the checkbox's check state. If you do not need tristate support, -
241 you can also use \l QAbstractButton::isChecked(), which returns a boolean. -
242 -
243 \sa setCheckState(), Qt::CheckState -
244*/ -
245Qt::CheckState QCheckBox::checkState() const -
246{ -
247 Q_D(const QCheckBox);
executed (the execution status of this line is deduced): const QCheckBoxPrivate * const d = d_func();
-
248 if (d->tristate && d->noChange)
evaluated: d->tristate
TRUEFALSE
yes
Evaluation Count:12
yes
Evaluation Count:56
evaluated: d->noChange
TRUEFALSE
yes
Evaluation Count:3
yes
Evaluation Count:9
3-56
249 return Qt::PartiallyChecked;
executed: return Qt::PartiallyChecked;
Execution Count:3
3
250 return d->checked ? Qt::Checked : Qt::Unchecked;
executed: return d->checked ? Qt::Checked : Qt::Unchecked;
Execution Count:65
65
251} -
252 -
253/*! -
254 Sets the checkbox's check state to \a state. If you do not need tristate -
255 support, you can also use \l QAbstractButton::setChecked(), which takes a -
256 boolean. -
257 -
258 \sa checkState(), Qt::CheckState -
259*/ -
260void QCheckBox::setCheckState(Qt::CheckState state) -
261{ -
262 Q_D(QCheckBox);
executed (the execution status of this line is deduced): QCheckBoxPrivate * const d = d_func();
-
263 if (state == Qt::PartiallyChecked) {
partially evaluated: state == Qt::PartiallyChecked
TRUEFALSE
yes
Evaluation Count:5
no
Evaluation Count:0
0-5
264 d->tristate = true;
executed (the execution status of this line is deduced): d->tristate = true;
-
265 d->noChange = true;
executed (the execution status of this line is deduced): d->noChange = true;
-
266 } else {
executed: }
Execution Count:5
5
267 d->noChange = false;
never executed (the execution status of this line is deduced): d->noChange = false;
-
268 }
never executed: }
0
269 d->blockRefresh = true;
executed (the execution status of this line is deduced): d->blockRefresh = true;
-
270 setChecked(state != Qt::Unchecked);
executed (the execution status of this line is deduced): setChecked(state != Qt::Unchecked);
-
271 d->blockRefresh = false;
executed (the execution status of this line is deduced): d->blockRefresh = false;
-
272 d->refresh();
executed (the execution status of this line is deduced): d->refresh();
-
273 if ((uint)state != d->publishedState) {
evaluated: (uint)state != d->publishedState
TRUEFALSE
yes
Evaluation Count:3
yes
Evaluation Count:2
2-3
274 d->publishedState = state;
executed (the execution status of this line is deduced): d->publishedState = state;
-
275 emit stateChanged(state);
executed (the execution status of this line is deduced): stateChanged(state);
-
276 }
executed: }
Execution Count:3
3
277}
executed: }
Execution Count:5
5
278 -
279 -
280/*! -
281 \reimp -
282*/ -
283QSize QCheckBox::sizeHint() const -
284{ -
285 Q_D(const QCheckBox);
executed (the execution status of this line is deduced): const QCheckBoxPrivate * const d = d_func();
-
286 if (d->sizeHint.isValid())
evaluated: d->sizeHint.isValid()
TRUEFALSE
yes
Evaluation Count:22
yes
Evaluation Count:18
18-22
287 return d->sizeHint;
executed: return d->sizeHint;
Execution Count:22
22
288 ensurePolished();
executed (the execution status of this line is deduced): ensurePolished();
-
289 QFontMetrics fm = fontMetrics();
executed (the execution status of this line is deduced): QFontMetrics fm = fontMetrics();
-
290 QStyleOptionButton opt;
executed (the execution status of this line is deduced): QStyleOptionButton opt;
-
291 initStyleOption(&opt);
executed (the execution status of this line is deduced): initStyleOption(&opt);
-
292 QSize sz = style()->itemTextRect(fm, QRect(), Qt::TextShowMnemonic, false,
executed (the execution status of this line is deduced): QSize sz = style()->itemTextRect(fm, QRect(), Qt::TextShowMnemonic, false,
-
293 text()).size();
executed (the execution status of this line is deduced): text()).size();
-
294 if (!opt.icon.isNull())
partially evaluated: !opt.icon.isNull()
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:18
0-18
295 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
296 d->sizeHint = (style()->sizeFromContents(QStyle::CT_CheckBox, &opt, sz, this)
executed (the execution status of this line is deduced): d->sizeHint = (style()->sizeFromContents(QStyle::CT_CheckBox, &opt, sz, this)
-
297 .expandedTo(QApplication::globalStrut()));
executed (the execution status of this line is deduced): .expandedTo(QApplication::globalStrut()));
-
298 return d->sizeHint;
executed: return d->sizeHint;
Execution Count:18
18
299} -
300 -
301 -
302/*! -
303 \reimp -
304*/ -
305QSize QCheckBox::minimumSizeHint() const -
306{ -
307 return sizeHint();
executed: return sizeHint();
Execution Count:20
20
308} -
309 -
310/*! -
311 \reimp -
312*/ -
313void QCheckBox::paintEvent(QPaintEvent *) -
314{ -
315 QStylePainter p(this);
executed (the execution status of this line is deduced): QStylePainter p(this);
-
316 QStyleOptionButton opt;
executed (the execution status of this line is deduced): QStyleOptionButton opt;
-
317 initStyleOption(&opt);
executed (the execution status of this line is deduced): initStyleOption(&opt);
-
318 p.drawControl(QStyle::CE_CheckBox, opt);
executed (the execution status of this line is deduced): p.drawControl(QStyle::CE_CheckBox, opt);
-
319}
executed: }
Execution Count:12
12
320 -
321/*! -
322 \reimp -
323*/ -
324void QCheckBox::mouseMoveEvent(QMouseEvent *e) -
325{ -
326 Q_D(QCheckBox);
never executed (the execution status of this line is deduced): QCheckBoxPrivate * const d = d_func();
-
327 if (testAttribute(Qt::WA_Hover)) {
never evaluated: testAttribute(Qt::WA_Hover)
0
328 bool hit = false;
never executed (the execution status of this line is deduced): bool hit = false;
-
329 if (underMouse())
never evaluated: underMouse()
0
330 hit = hitButton(e->pos());
never executed: hit = hitButton(e->pos());
0
331 -
332 if (hit != d->hovering) {
never evaluated: hit != d->hovering
0
333 update(rect());
never executed (the execution status of this line is deduced): update(rect());
-
334 d->hovering = hit;
never executed (the execution status of this line is deduced): d->hovering = hit;
-
335 }
never executed: }
0
336 }
never executed: }
0
337 -
338 QAbstractButton::mouseMoveEvent(e);
never executed (the execution status of this line is deduced): QAbstractButton::mouseMoveEvent(e);
-
339}
never executed: }
0
340 -
341 -
342/*! -
343 \reimp -
344*/ -
345bool QCheckBox::hitButton(const QPoint &pos) const -
346{ -
347 QStyleOptionButton opt;
never executed (the execution status of this line is deduced): QStyleOptionButton opt;
-
348 initStyleOption(&opt);
never executed (the execution status of this line is deduced): initStyleOption(&opt);
-
349 return style()->subElementRect(QStyle::SE_CheckBoxClickRect, &opt, this).contains(pos);
never executed: return style()->subElementRect(QStyle::SE_CheckBoxClickRect, &opt, this).contains(pos);
0
350} -
351 -
352/*! -
353 \reimp -
354*/ -
355void QCheckBox::checkStateSet() -
356{ -
357 Q_D(QCheckBox);
executed (the execution status of this line is deduced): QCheckBoxPrivate * const d = d_func();
-
358 d->noChange = false;
executed (the execution status of this line is deduced): d->noChange = false;
-
359 Qt::CheckState state = checkState();
executed (the execution status of this line is deduced): Qt::CheckState state = checkState();
-
360 if ((uint)state != d->publishedState) {
evaluated: (uint)state != d->publishedState
TRUEFALSE
yes
Evaluation Count:29
yes
Evaluation Count:28
28-29
361 d->publishedState = state;
executed (the execution status of this line is deduced): d->publishedState = state;
-
362 emit stateChanged(state);
executed (the execution status of this line is deduced): stateChanged(state);
-
363 }
executed: }
Execution Count:29
29
364}
executed: }
Execution Count:57
57
365 -
366/*! -
367 \reimp -
368*/ -
369void QCheckBox::nextCheckState() -
370{ -
371 Q_D(QCheckBox);
executed (the execution status of this line is deduced): QCheckBoxPrivate * const d = d_func();
-
372 if (d->tristate)
partially evaluated: d->tristate
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:3
0-3
373 setCheckState((Qt::CheckState)((checkState() + 1) % 3));
never executed: setCheckState((Qt::CheckState)((checkState() + 1) % 3));
0
374 else { -
375 QAbstractButton::nextCheckState();
executed (the execution status of this line is deduced): QAbstractButton::nextCheckState();
-
376 QCheckBox::checkStateSet();
executed (the execution status of this line is deduced): QCheckBox::checkStateSet();
-
377 }
executed: }
Execution Count:3
3
378} -
379 -
380/*! -
381 \reimp -
382*/ -
383bool QCheckBox::event(QEvent *e) -
384{ -
385 Q_D(QCheckBox);
executed (the execution status of this line is deduced): QCheckBoxPrivate * const d = d_func();
-
386 if (e->type() == QEvent::StyleChange
partially evaluated: e->type() == QEvent::StyleChange
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:268
0-268
387#ifdef Q_OS_MAC -
388 || e->type() == QEvent::MacSizeChange -
389#endif -
390 ) -
391 d->setLayoutItemMargins(QStyle::SE_CheckBoxLayoutItem);
never executed: d->setLayoutItemMargins(QStyle::SE_CheckBoxLayoutItem);
0
392 return QAbstractButton::event(e);
executed: return QAbstractButton::event(e);
Execution Count:268
268
393} -
394 -
395 -
396 -
397QT_END_NAMESPACE -
398 -
Source codeSwitch to Preprocessed file

Generated by Squish Coco Non-Commercial