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 "qquickstyleitemdial.h"
5
6QT_BEGIN_NAMESPACE
7
8QFont QQuickStyleItemDial::styleFont(QQuickItem *control) const
9{
10 return style()->font(element: QStyle::CE_ProgressBarLabel, state: controlSize(item: control));
11}
12
13void QQuickStyleItemDial::connectToControl() const
14{
15 QQuickStyleItem::connectToControl();
16 auto dial = control<QQuickDial>();
17 connect(sender: dial, signal: &QQuickDial::fromChanged, context: this, slot: &QQuickStyleItem::markImageDirty);
18 connect(sender: dial, signal: &QQuickDial::toChanged, context: this, slot: &QQuickStyleItem::markImageDirty);
19 connect(sender: dial, signal: &QQuickDial::positionChanged, context: this, slot: &QQuickStyleItem::markImageDirty);
20 connect(sender: dial, signal: &QQuickDial::valueChanged, context: this, slot: &QQuickStyleItem::markImageDirty);
21 connect(sender: dial, signal: &QQuickDial::stepSizeChanged, context: this, slot: &QQuickStyleItem::markImageDirty);
22 connect(sender: dial, signal: &QQuickDial::startAngleChanged, context: this, slot: &QQuickStyleItem::markImageDirty);
23 connect(sender: dial, signal: &QQuickDial::endAngleChanged, context: this, slot: &QQuickStyleItem::markImageDirty);
24 connect(sender: dial, signal: &QQuickDial::pressedChanged, context: this, slot: &QQuickStyleItem::markImageDirty);
25}
26
27StyleItemGeometry QQuickStyleItemDial::calculateGeometry()
28{
29 QStyleOptionSlider styleOption;
30 initStyleOption(styleOption);
31
32 StyleItemGeometry geometry;
33 geometry.minimumSize = style()->sizeFromContents(ct: QStyle::CT_Dial, opt: &styleOption, contentsSize: QSize(0, 0));
34 geometry.implicitSize = geometry.minimumSize;
35 geometry.layoutRect = style()->subElementRect(subElement: QStyle::SE_SliderLayoutItem, option: &styleOption);
36 geometry.ninePatchMargins = style()->ninePatchMargins(cc: QStyle::CC_Dial, opt: &styleOption, imageSize: geometry.minimumSize);
37 geometry.focusFrameRadius = style()->pixelMetric(metric: QStyle::PM_DialFocusFrameRadius, option: &styleOption);
38
39 return geometry;
40}
41
42void QQuickStyleItemDial::paintEvent(QPainter *painter) const
43{
44 QStyleOptionSlider styleOption;
45 initStyleOption(styleOption);
46 style()->drawComplexControl(cc: QStyle::CC_Dial, opt: &styleOption, p: painter);
47}
48
49void QQuickStyleItemDial::initStyleOption(QStyleOptionSlider &styleOption) const
50{
51 initStyleOptionBase(styleOption);
52 auto dial = control<QQuickDial>();
53
54 styleOption.subControls = QStyle::SC_SliderGroove | QStyle::SC_SliderHandle;
55 styleOption.activeSubControls = QStyle::SC_None;
56 styleOption.tickInterval = dial->stepSize();
57 styleOption.dialWrapping = dial->wrap();
58 styleOption.upsideDown = true;
59 styleOption.startAngle = dial->startAngle();
60 styleOption.endAngle = dial->endAngle();
61
62 if (dial->isPressed())
63 styleOption.state |= QStyle::State_Sunken;
64
65 if (dial->stepSize() == 0) {
66 styleOption.minimum = 0;
67 styleOption.maximum = 10000;
68 styleOption.sliderPosition = dial->position() * styleOption.maximum;
69 } else {
70 styleOption.minimum = dial->from();
71 styleOption.maximum = dial->to();
72 styleOption.sliderPosition = dial->value();
73 }
74
75 // TODO: add proper API for tickmarks
76 const int index = dial->metaObject()->indexOfProperty(name: "qqc2_style_tickPosition");
77 if (index != -1) {
78 const int tickPosition = dial->metaObject()->property(index).read(obj: dial).toInt();
79 styleOption.tickPosition = QStyleOptionSlider::TickPosition(tickPosition);
80 if (styleOption.tickPosition != QStyleOptionSlider::NoTicks)
81 styleOption.subControls |= QStyle::SC_DialTickmarks;
82 }
83
84}
85
86QT_END_NAMESPACE
87
88#include "moc_qquickstyleitemdial.cpp"
89

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