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
qquickstyleitemscrollbar.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
9QQuickStyleItemScrollBar::QQuickStyleItemScrollBar(QQuickItem *parent)
10 : QQuickStyleItem(parent)
11{
12#ifdef QT_DEBUG
13 setObjectName("styleItemScrollBar");
14#endif
15}
16
17QFont QQuickStyleItemScrollBar::styleFont(QQuickItem *control) const
18{
19 return style()->font(QStyle::CE_ProgressBarLabel, controlSize(control));
20}
21
22void QQuickStyleItemScrollBar::connectToControl() const
23{
24 QQuickStyleItem::connectToControl();
25 auto scrollBar = control<QQuickScrollBar>();
26 connect(scrollBar, &QQuickScrollBar::orientationChanged, this, &QQuickStyleItem::markImageDirty);
27 connect(scrollBar, &QQuickScrollBar::pressedChanged, this, &QQuickStyleItem::markImageDirty);
28}
29
30StyleItemGeometry QQuickStyleItemScrollBar::calculateGeometry()
31{
32 QStyleOptionSlider styleOption;
33 initStyleOption(styleOption);
34
35 StyleItemGeometry geometry;
36 geometry.minimumSize = style()->sizeFromContents(QStyle::CT_ScrollBar, &styleOption, QSize(0, 0));
37 if (m_subControl == SubLine || m_subControl == AddLine) {
38 // So far, we know that only the windows style uses these subcontrols,
39 // so we can use hardcoded sizes...
40 QSize sz(16, 17);
41 if (styleOption.orientation == Qt::Vertical)
42 sz.transpose();
43 geometry.minimumSize = sz;
44 }
45 geometry.implicitSize = geometry.minimumSize;
46 styleOption.rect = QRect(QPoint(0, 0), geometry.implicitSize);
47 geometry.layoutRect = style()->subElementRect(QStyle::SE_ScrollBarLayoutItem, &styleOption);
48 geometry.ninePatchMargins = style()->ninePatchMargins(QStyle::CC_ScrollBar, &styleOption, geometry.minimumSize);
49
50 return geometry;
51}
52
53void QQuickStyleItemScrollBar::paintEvent(QPainter *painter) const
54{
55 QStyleOptionSlider styleOption;
56 initStyleOption(styleOption);
57 if (m_subControl == SubLine || m_subControl == AddLine) {
58 QStyle::SubControl sc = m_subControl == SubLine ? QStyle::SC_ScrollBarSubLine : QStyle::SC_ScrollBarAddLine;
59 QStyleOptionSlider opt = styleOption;
60 opt.subControls = QStyle::SC_ScrollBarAddLine
61 | QStyle::SC_ScrollBarSubLine
62 | QStyle::SC_ScrollBarGroove;
63
64 const qreal scale = window()->effectiveDevicePixelRatio();
65 const QSize scrollBarMinSize = style()->sizeFromContents(QStyle::CT_ScrollBar, &opt, QSize(0, 0));
66 const QSize sz = scrollBarMinSize * scale;
67 QImage scrollBarImage(sz, QImage::Format_ARGB32_Premultiplied);
68 scrollBarImage.setDevicePixelRatio(scale);
69 QPainter p(&scrollBarImage);
70 opt.rect = QRect(QPoint(0, 0), scrollBarMinSize);
71 style()->drawComplexControl(QStyle::CC_ScrollBar, &opt, &p);
72 QRect sourceImageRect = style()->subControlRect(QStyle::CC_ScrollBar, &opt, sc);
73 sourceImageRect = QRect(sourceImageRect.topLeft() * scale, sourceImageRect.size() * scale);
74 painter->drawImage(QPoint(0, 0), scrollBarImage, sourceImageRect);
75 } else {
76 style()->drawComplexControl(QStyle::CC_ScrollBar, &styleOption, painter);
77 }
78}
79
80void QQuickStyleItemScrollBar::initStyleOption(QStyleOptionSlider &styleOption) const
81{
82 initStyleOptionBase(styleOption);
83 auto scrollBar = control<QQuickScrollBar>();
84
85 switch (m_subControl) {
86 case Groove:
87 styleOption.subControls = QStyle::SC_ScrollBarGroove | QStyle::SC_ScrollBarAddLine | QStyle::SC_ScrollBarSubLine;
88 break;
89 case Handle:
90 styleOption.subControls = QStyle::SC_ScrollBarSlider;
91 break;
92 case AddLine:
93 styleOption.subControls = QStyle::SC_ScrollBarAddLine;
94 break;
95 case SubLine:
96 styleOption.subControls = QStyle::SC_ScrollBarSubLine;
97 break;
98 }
99
100 styleOption.activeSubControls = QStyle::SC_None;
101 styleOption.orientation = scrollBar->orientation();
102 if (styleOption.orientation == Qt::Horizontal)
103 styleOption.state |= QStyle::State_Horizontal;
104
105 if (scrollBar->isPressed())
106 styleOption.state |= QStyle::State_Sunken;
107
108 if (m_overrideState != None) {
109 // In ScrollBar.qml we fade between two versions of
110 // the handle, depending on if it's hovered or not
111
112 if (m_overrideState == AlwaysHovered) {
113 styleOption.state &= ~QStyle::State_Sunken;
114 styleOption.activeSubControls = (styleOption.subControls & (QStyle::SC_ScrollBarSlider | QStyle::SC_ScrollBarGroove | QStyle::SC_ScrollBarAddLine | QStyle::SC_ScrollBarSubLine));
115 } else if (m_overrideState == NeverHovered) {
116 styleOption.state &= ~QStyle::State_Sunken;
117 styleOption.activeSubControls &= ~(styleOption.subControls & (QStyle::SC_ScrollBarSlider | QStyle::SC_ScrollBarGroove | QStyle::SC_ScrollBarAddLine | QStyle::SC_ScrollBarSubLine));
118 } else if (m_overrideState == AlwaysSunken) {
119 styleOption.state |= QStyle::State_Sunken;
120 styleOption.activeSubControls = (styleOption.subControls & (QStyle::SC_ScrollBarSlider | QStyle::SC_ScrollBarGroove | QStyle::SC_ScrollBarAddLine | QStyle::SC_ScrollBarSubLine));
121 }
122 }
123
124 // The following values will let the handle fill 100% of the
125 // groove / imageSize. But when the handle is resized by
126 // QQuickScrollBar, it will end up with the correct size visually.
127 styleOption.pageStep = 1000;
128 styleOption.minimum = 0;
129 styleOption.maximum = 1;
130 styleOption.sliderValue = 0;
131}
132
133QT_END_NAMESPACE
134
135#include "moc_qquickstyleitemscrollbar.cpp"