qcheckbox.cpp

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

Generated by Squish Coco Non-Commercial 4.3.0-BETA-master-30-08-2018-4cb69e9