1// Copyright (C) 2020 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include "qquickstyleitemcheckbox.h"
5
6QT_BEGIN_NAMESPACE
7
8QFont QQuickStyleItemCheckBox::styleFont(QQuickItem *control) const
9{
10 return style()->font(element: QStyle::CE_RadioButtonLabel, state: controlSize(item: control));
11}
12
13void QQuickStyleItemCheckBox::connectToControl() const
14{
15 QQuickStyleItem::connectToControl();
16 auto checkbox = control<QQuickCheckBox>();
17 connect(sender: checkbox, signal: &QQuickCheckBox::downChanged, context: this, slot: &QQuickStyleItem::markImageDirty);
18 connect(sender: checkbox, signal: &QQuickCheckBox::checkStateChanged, context: this, slot: &QQuickStyleItem::markImageDirty);
19}
20
21StyleItemGeometry QQuickStyleItemCheckBox::calculateGeometry()
22{
23 QStyleOptionButton styleOption;
24 initStyleOption(styleOption);
25 StyleItemGeometry geometry;
26
27 geometry.minimumSize = style()->sizeFromContents(ct: QStyle::CT_CheckBox, opt: &styleOption, contentsSize: QSize(0, 0));
28 geometry.implicitSize = geometry.minimumSize;
29 styleOption.rect = QRect(QPoint(0, 0), geometry.implicitSize);
30 geometry.contentRect = style()->subElementRect(subElement: QStyle::SE_CheckBoxContents, option: &styleOption);
31 geometry.layoutRect = style()->subElementRect(subElement: QStyle::SE_CheckBoxLayoutItem, option: &styleOption);
32 geometry.ninePatchMargins = style()->ninePatchMargins(ce: QStyle::CE_CheckBox, opt: &styleOption, imageSize: geometry.minimumSize);
33 geometry.focusFrameRadius = style()->pixelMetric(metric: QStyle::PM_CheckBoxFocusFrameRadius, option: &styleOption);
34
35 // Spacing seems to already be baked into SE_CheckBoxContents, so ignore until needed
36 //const int space = style()->pixelMetric(QStyle::PM_CheckBoxLabelSpacing, &styleOption);
37
38 return geometry;
39}
40
41void QQuickStyleItemCheckBox::paintEvent(QPainter *painter) const
42{
43 QStyleOptionButton styleOption;
44 initStyleOption(styleOption);
45 style()->drawControl(element: QStyle::CE_CheckBox, opt: &styleOption, p: painter);
46}
47
48void QQuickStyleItemCheckBox::initStyleOption(QStyleOptionButton &styleOption) const
49{
50 initStyleOptionBase(styleOption);
51 auto checkbox = control<QQuickCheckBox>();
52
53 styleOption.state |= checkbox->isDown() ? QStyle::State_Sunken : QStyle::State_Raised;
54 if (checkbox->isTristate() && checkbox->checkState() == Qt::PartiallyChecked)
55 styleOption.state |= QStyle::State_NoChange;
56 else
57 styleOption.state |= checkbox->isChecked() ? QStyle::State_On : QStyle::State_Off;
58}
59
60QT_END_NAMESPACE
61
62#include "moc_qquickstyleitemcheckbox.cpp"
63

source code of qtdeclarative/src/quicknativestyle/items/qquickstyleitemcheckbox.cpp