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
qquickstyleitemprogressbar.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 QQuickStyleItemProgressBar::styleFont(QQuickItem *control) const
10{
11 return style()->font(QStyle::CE_ProgressBarLabel, controlSize(control));
12}
13
14void QQuickStyleItemProgressBar::connectToControl() const
15{
16 QQuickStyleItem::connectToControl();
17 auto progressBar = control<QQuickProgressBar>();
18 connect(progressBar, &QQuickProgressBar::fromChanged, this, &QQuickStyleItem::markImageDirty);
19 connect(progressBar, &QQuickProgressBar::toChanged, this, &QQuickStyleItem::markImageDirty);
20 connect(progressBar, &QQuickProgressBar::positionChanged, this, &QQuickStyleItem::markImageDirty);
21}
22
23StyleItemGeometry QQuickStyleItemProgressBar::calculateGeometry()
24{
25 QStyleOptionProgressBar styleOption;
26 initStyleOption(styleOption);
27
28 StyleItemGeometry geometry;
29 geometry.minimumSize = style()->sizeFromContents(QStyle::CT_ProgressBar, &styleOption, QSize(0, 0));
30
31 // From qprogressbar.cpp in qtbase:
32 const int cw = style()->pixelMetric(QStyle::PM_ProgressBarChunkWidth, &styleOption);
33 QFontMetrics fm(control<QQuickProgressBar>()->font());
34 QSize size = QSize(qMax(9, cw) * 7 + fm.horizontalAdvance(QLatin1Char('0')) * 4, fm.height() + 8);
35 if (!(styleOption.state & QStyle::State_Horizontal))
36 size = size.transposed();
37
38 geometry.implicitSize = style()->sizeFromContents(QStyle::CT_ProgressBar, &styleOption, size);
39 styleOption.rect = QRect(QPoint(0, 0), geometry.implicitSize);
40 geometry.contentRect = style()->subElementRect(QStyle::SE_ProgressBarContents, &styleOption);
41 geometry.layoutRect = style()->subElementRect(QStyle::SE_ProgressBarLayoutItem, &styleOption);
42 geometry.ninePatchMargins = style()->ninePatchMargins(QStyle::CE_ProgressBar, &styleOption, geometry.minimumSize);
43
44 return geometry;
45}
46
47void QQuickStyleItemProgressBar::paintEvent(QPainter *painter) const
48{
49 QStyleOptionProgressBar styleOption;
50 initStyleOption(styleOption);
51#ifndef Q_OS_MACOS
52 const QRect r = styleOption.rect;
53#endif
54 // Note: on macOS, the groove will paint both the background and the contents
55 styleOption.rect = style()->subElementRect(QStyle::SE_ProgressBarGroove, &styleOption);
56 style()->drawControl(QStyle::CE_ProgressBarGroove, &styleOption, painter);
57#ifndef Q_OS_MACOS
58 styleOption.rect = r;
59 styleOption.rect = style()->subElementRect(QStyle::SE_ProgressBarContents, &styleOption);
60 style()->drawControl(QStyle::CE_ProgressBarContents, &styleOption, painter);
61#endif
62}
63
64void QQuickStyleItemProgressBar::initStyleOption(QStyleOptionProgressBar &styleOption) const
65{
66 initStyleOptionBase(styleOption);
67 auto progressBar = control<QQuickProgressBar>();
68
69 styleOption.state = QStyle::State_Horizontal;
70
71 if (progressBar->isIndeterminate()) {
72 styleOption.minimum = 0;
73 styleOption.maximum = 0;
74 } else if (progressBar->to() - progressBar->from() < 100) {
75 // Add some range to support float numbers
76 styleOption.minimum = 0;
77 styleOption.maximum = (progressBar->to() - progressBar->from()) * 100;
78 styleOption.progress = (progressBar->value() - progressBar->from()) * 100;
79 } else {
80 styleOption.minimum = progressBar->from();
81 styleOption.maximum = progressBar->to();
82 styleOption.progress = progressBar->value();
83 }
84}
85
86QT_END_NAMESPACE
87
88#include "moc_qquickstyleitemprogressbar.cpp"