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 "qquickstyleitemspinbox.h" |
5 | #include <QtQuickTemplates2/private/qquickindicatorbutton_p.h> |
6 | |
7 | QT_BEGIN_NAMESPACE |
8 | |
9 | QFont QQuickStyleItemSpinBox::styleFont(QQuickItem *control) const |
10 | { |
11 | return style()->font(element: QStyle::CE_ComboBoxLabel, state: controlSize(item: control)); |
12 | } |
13 | |
14 | void QQuickStyleItemSpinBox::connectToControl() const |
15 | { |
16 | QQuickStyleItem::connectToControl(); |
17 | auto spinbox = control<QQuickSpinBox>(); |
18 | connect(sender: spinbox->up(), signal: &QQuickIndicatorButton::pressedChanged, context: this, slot: &QQuickStyleItem::markImageDirty); |
19 | connect(sender: spinbox->down(), signal: &QQuickIndicatorButton::pressedChanged, context: this, slot: &QQuickStyleItem::markImageDirty); |
20 | } |
21 | |
22 | StyleItemGeometry QQuickStyleItemSpinBox::calculateGeometry() |
23 | { |
24 | QStyleOptionSpinBox styleOption; |
25 | initStyleOption(styleOption); |
26 | StyleItemGeometry geometry; |
27 | |
28 | geometry.minimumSize = style()->sizeFromContents(ct: QStyle::CT_SpinBox, opt: &styleOption, contentsSize: QSize(0, 0)); |
29 | |
30 | if (styleOption.subControls == QStyle::SC_SpinBoxFrame) { |
31 | geometry.implicitSize = style()->sizeFromContents(ct: QStyle::CT_SpinBox, opt: &styleOption, contentsSize: contentSize()); |
32 | styleOption.rect = QRect(QPoint(0, 0), geometry.implicitSize); |
33 | geometry.contentRect = style()->subControlRect(cc: QStyle::CC_SpinBox, opt: &styleOption, sc: QStyle::SC_SpinBoxEditField); |
34 | geometry.layoutRect = style()->subElementRect(subElement: QStyle::SE_SpinBoxLayoutItem, option: &styleOption); |
35 | geometry.ninePatchMargins = style()->ninePatchMargins(cc: QStyle::CC_SpinBox, opt: &styleOption, imageSize: geometry.minimumSize); |
36 | geometry.focusFrameRadius = style()->pixelMetric(metric: QStyle::PM_SpinBoxFocusFrameRadius, option: &styleOption); |
37 | } else { |
38 | geometry.implicitSize = geometry.minimumSize; |
39 | } |
40 | |
41 | return geometry; |
42 | } |
43 | |
44 | void QQuickStyleItemSpinBox::paintEvent(QPainter *painter) const |
45 | { |
46 | QStyleOptionSpinBox styleOption; |
47 | initStyleOption(styleOption); |
48 | style()->drawComplexControl(cc: QStyle::CC_SpinBox, opt: &styleOption, p: painter); |
49 | } |
50 | |
51 | void QQuickStyleItemSpinBox::initStyleOption(QStyleOptionSpinBox &styleOption) const |
52 | { |
53 | initStyleOptionBase(styleOption); |
54 | auto spinbox = control<QQuickSpinBox>(); |
55 | |
56 | switch (m_subControl) { |
57 | case Frame: |
58 | styleOption.subControls = QStyle::SC_SpinBoxFrame; |
59 | styleOption.frame = true; |
60 | break; |
61 | case Up: |
62 | styleOption.subControls = (QStyle::SC_SpinBoxUp | QStyle::SC_SpinBoxDown); |
63 | break; |
64 | case Down: |
65 | styleOption.subControls = QStyle::SC_SpinBoxDown; |
66 | break; |
67 | } |
68 | |
69 | if (spinbox->up()->isPressed()) { |
70 | styleOption.activeSubControls = QStyle::SC_SpinBoxUp; |
71 | styleOption.state |= QStyle::State_Sunken; |
72 | } else if (spinbox->down()->isPressed()) { |
73 | styleOption.activeSubControls = QStyle::SC_SpinBoxDown; |
74 | styleOption.state |= QStyle::State_Sunken; |
75 | } |
76 | |
77 | styleOption.buttonSymbols = QStyleOptionSpinBox::UpDownArrows; |
78 | styleOption.stepEnabled = QStyleOptionSpinBox::StepEnabled; |
79 | } |
80 | |
81 | QT_END_NAMESPACE |
82 | |
83 | #include "moc_qquickstyleitemspinbox.cpp" |
84 | |