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 "qquickstyleitembutton.h"
5
6QT_BEGIN_NAMESPACE
7
8QFont QQuickStyleItemButton::styleFont(QQuickItem *control) const
9{
10 return style()->font(element: QStyle::CE_PushButtonLabel, state: controlSize(item: control));
11}
12
13void QQuickStyleItemButton::connectToControl() const
14{
15 QQuickStyleItem::connectToControl();
16 auto button = control<QQuickButton>();
17 connect(sender: button, signal: &QQuickButton::downChanged, context: this, slot: &QQuickStyleItem::markImageDirty);
18 connect(sender: button, signal: &QQuickButton::checkedChanged, context: this, slot: &QQuickStyleItem::markImageDirty);
19}
20
21StyleItemGeometry QQuickStyleItemButton::calculateGeometry()
22{
23 QStyleOptionButton styleOption;
24 initStyleOption(styleOption);
25 StyleItemGeometry geometry;
26
27 geometry.minimumSize = style()->sizeFromContents(ct: QStyle::CT_PushButton, opt: &styleOption, contentsSize: QSize(0, 0));
28 geometry.implicitSize = style()->sizeFromContents(ct: QStyle::CT_PushButton, opt: &styleOption, contentsSize: contentSize());
29 styleOption.rect = QRect(QPoint(0, 0), geometry.implicitSize);
30 geometry.contentRect = style()->subElementRect(subElement: QStyle::SE_PushButtonContents, option: &styleOption);
31 geometry.layoutRect = style()->subElementRect(subElement: QStyle::SE_PushButtonLayoutItem, option: &styleOption);
32 geometry.ninePatchMargins = style()->ninePatchMargins(ce: QStyle::CE_PushButtonBevel, opt: &styleOption, imageSize: geometry.minimumSize);
33 geometry.focusFrameRadius = style()->pixelMetric(metric: QStyle::PM_PushButtonFocusFrameRadius, option: &styleOption);
34
35 return geometry;
36}
37
38void QQuickStyleItemButton::paintEvent(QPainter *painter) const
39{
40 QStyleOptionButton styleOption;
41 initStyleOption(styleOption);
42 style()->drawControl(element: QStyle::CE_PushButtonBevel, opt: &styleOption, p: painter);
43}
44
45void QQuickStyleItemButton::initStyleOption(QStyleOptionButton &styleOption) const
46{
47 initStyleOptionBase(styleOption);
48 auto button = control<QQuickButton>();
49
50 if (button->isDown())
51 styleOption.state |= QStyle::State_Sunken;
52 if (!button->isFlat() && !button->isDown())
53 styleOption.state |= QStyle::State_Raised;
54 if (button->isHighlighted() || button->isChecked())
55 styleOption.state |= QStyle::State_On;
56 if (button->isFlat())
57 styleOption.features |= QStyleOptionButton::Flat;
58}
59
60QT_END_NAMESPACE
61
62#include "moc_qquickstyleitembutton.cpp"
63

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