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

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