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

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