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
qquickstyleitemscrollviewcorner.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
7#include <QtGui/qpainter.h>
8#include <QtGui/qpainterpath.h>
9
10StyleItemGeometry QQuickStyleItemScrollViewCorner::calculateGeometry()
11{
12 QStyleOptionSlider styleOption;
13 initStyleOption(styleOption);
14
15 StyleItemGeometry geometry;
16
17 // The size of the corner should be the width of the vertical
18 // scrollbar and the height of the horizontal scrollbar.
19 styleOption.orientation = Qt::Vertical;
20 const auto vScrollBarWidth = style()->sizeFromContents(QStyle::CT_ScrollBar, &styleOption, QSize(0, 0)).width();
21 styleOption.orientation = Qt::Horizontal;
22 const auto hScrollBarHeight = style()->sizeFromContents(QStyle::CT_ScrollBar, &styleOption, QSize(0, 0)).height();
23
24 geometry.minimumSize = QSize(vScrollBarWidth, hScrollBarHeight);
25 geometry.implicitSize = geometry.minimumSize;
26
27 return geometry;
28}
29
30void QQuickStyleItemScrollViewCorner::paintEvent(QPainter *painter) const
31{
32 QStyleOptionSlider styleOption;
33 initStyleOption(styleOption);
34
35 // Grab a center piece of a vertical scrollbar groove onto a QImage, and use this
36 // image to draw a corner. We draw the corner by first drawing the piece as it is, and
37 // then rotate it 90 degrees, clip it into a triangle, and draw it once more on top.
38 // The result is that we end up with one vertical and one horizontal triangle that
39 // together form a filled corner rectangle.
40
41 styleOption.orientation = Qt::Vertical;
42
43 const qreal scale = window()->effectiveDevicePixelRatio();
44 const int grooveWidth = minimumSize().width();
45 const int grooveHeight = minimumSize().height();
46 const QSize scrollBarMinSize = style()->sizeFromContents(QStyle::CT_ScrollBar, &styleOption, QSize(0, 0));
47 const QSize scrollBarSize = scrollBarMinSize + QSize(0, grooveHeight);
48 const int hStart = scrollBarMinSize.height() / 2;
49 const QRect targetImageRect(0, hStart * scale, grooveWidth * scale, grooveHeight * scale);
50
51 QImage scrollBarImage(scrollBarSize * scale, QImage::Format_ARGB32_Premultiplied);
52 scrollBarImage.setDevicePixelRatio(scale);
53 scrollBarImage.fill(Qt::transparent);
54 QPainter scrollBarPainter(&scrollBarImage);
55 styleOption.rect = QRect(QPoint(0, 0), scrollBarSize);
56 style()->drawComplexControl(QStyle::CC_ScrollBar, &styleOption, &scrollBarPainter);
57
58 // Draw vertical groove
59 painter->drawImage(QPoint(0, 0), scrollBarImage, targetImageRect);
60
61 QPainterPath path;
62 path.moveTo(0, 0);
63 path.lineTo(0, grooveHeight);
64 path.lineTo(grooveWidth, grooveHeight);
65 path.closeSubpath();
66
67 QTransform transform;
68 transform.translate(grooveWidth, 0);
69 transform.rotate(90);
70
71 painter->save();
72 painter->setCompositionMode(QPainter::CompositionMode_Source);
73 painter->setClipPath(path);
74 painter->setTransform(transform);
75 // Draw horizontal groove, clipped to a triangle
76 painter->drawImage(QPoint(0, 0), scrollBarImage, targetImageRect);
77 painter->restore();
78}
79
80void QQuickStyleItemScrollViewCorner::initStyleOption(QStyleOptionSlider &styleOption) const
81{
82 initStyleOptionBase(styleOption);
83
84 styleOption.subControls = QStyle::SC_ScrollBarGroove;
85 styleOption.activeSubControls = QStyle::SC_None;
86 styleOption.pageStep = 1000;
87 styleOption.minimum = 0;
88 styleOption.maximum = 1;
89 styleOption.sliderValue = 0;
90}