Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qquickstyleitemslider.cpp
Go to the documentation of this file.
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// Qt-Security score:significant reason:default
4
6
8
9QFont QQuickStyleItemSlider::styleFont(QQuickItem *control) const
10{
11 return style()->font(QStyle::CE_ProgressBarLabel, controlSize(control));
12}
13
14void QQuickStyleItemSlider::connectToControl() const
15{
16 QQuickStyleItem::connectToControl();
17 auto slider = control<QQuickSlider>();
18 connect(slider, &QQuickSlider::fromChanged, this, &QQuickStyleItem::markImageDirty);
19 connect(slider, &QQuickSlider::toChanged, this, &QQuickStyleItem::markImageDirty);
20 connect(slider, &QQuickSlider::positionChanged, this, &QQuickStyleItem::markImageDirty);
21 connect(slider, &QQuickSlider::valueChanged, this, &QQuickStyleItem::markImageDirty);
22 connect(slider, &QQuickSlider::stepSizeChanged, this, &QQuickStyleItem::markImageDirty);
23 connect(slider, &QQuickSlider::pressedChanged, this, &QQuickStyleItem::markImageDirty);
24 connect(slider, &QQuickSlider::orientationChanged, this, &QQuickStyleItem::markImageDirty);
25}
26
27StyleItemGeometry QQuickStyleItemSlider::calculateGeometry()
28{
29 QStyleOptionSlider styleOption;
30 initStyleOption(styleOption);
31
32 StyleItemGeometry geometry;
33 geometry.minimumSize = style()->sizeFromContents(QStyle::CT_Slider, &styleOption, QSize(0, 0));
34 geometry.implicitSize = geometry.minimumSize;
35 styleOption.rect = QRect(QPoint(0, 0), geometry.implicitSize);
36 geometry.layoutRect = style()->subElementRect(QStyle::SE_SliderLayoutItem, &styleOption);
37 geometry.ninePatchMargins = style()->ninePatchMargins(QStyle::CC_Slider, &styleOption, geometry.minimumSize);
38 geometry.focusFrameRadius = style()->pixelMetric(QStyle::PM_SliderFocusFrameRadius, &styleOption);
39
40 return geometry;
41}
42
43void QQuickStyleItemSlider::paintEvent(QPainter *painter) const
44{
45 QStyleOptionSlider styleOption;
46 initStyleOption(styleOption);
47 style()->drawComplexControl(QStyle::CC_Slider, &styleOption, painter);
48}
49
50void QQuickStyleItemSlider::initStyleOption(QStyleOptionSlider &styleOption) const
51{
52 initStyleOptionBase(styleOption);
53 auto slider = control<QQuickSlider>();
54
55 styleOption.subControls = QStyle::SC_None;
56 if (m_subControl & Groove)
57 styleOption.subControls |= QStyle::SC_SliderGroove;
58 if (m_subControl & Handle)
59 styleOption.subControls |= QStyle::SC_SliderHandle;
60 styleOption.activeSubControls = QStyle::SC_None;
61 styleOption.orientation = slider->orientation();
62
63 if (slider->isPressed())
64 styleOption.state |= QStyle::State_Sunken;
65
66 qreal min = 0;
67 qreal max = 1;
68 if (!qFuzzyIsNull(slider->stepSize())) {
69 min = slider->from();
70 max = slider->to();
71
72 // TODO: add proper API for tickmarks
73 const int index = slider->metaObject()->indexOfProperty("qqc2_style_tickPosition");
74 if (index != -1) {
75 const int tickPosition = slider->metaObject()->property(index).read(slider).toInt();
76 styleOption.tickPosition = QStyleOptionSlider::TickPosition(tickPosition);
77 if (styleOption.tickPosition != QStyleOptionSlider::NoTicks)
78 styleOption.subControls |= QStyle::SC_SliderTickmarks;
79 }
80 }
81
82 // Since the [from, to] interval in QQuickSlider is floating point, users can
83 // specify very small ranges and step sizes, (e.g. [0.., 0.25], step size 0.05).
84 // Since the style operates on ints, we cannot pass these values directly to the style,
85 // so we normalize all values to the range [0, 10000]
86 static const qreal Scale = 10000;
87 const qreal normalizeMultiplier = Scale/(max - min);
88 styleOption.tickInterval = int(slider->stepSize() * normalizeMultiplier);
89 styleOption.minimum = 0;
90 styleOption.maximum = int(Scale);
91 styleOption.sliderValue = int((slider->value() - min) * normalizeMultiplier);
92 styleOption.sliderPosition = int(slider->position() * styleOption.maximum);
93}
94
95QT_END_NAMESPACE
96
97#include "moc_qquickstyleitemslider.cpp"