Branch data Line data Source code
1 : : // SPDX-FileCopyrightText: 2011 Morgan Leborgne
2 : : // SPDX-FileCopyrightText: 2015 Anne Jan Brouwer
3 : : // SPDX-License-Identifier: MIT
4 : : /**
5 : : * @file
6 : : * @brief Implements QProgressIndicator: the animation timer and the painting.
7 : : */
8 : : #include "qprogressindicator.h"
9 : : #include <QPainter>
10 : :
11 : 30 : QProgressIndicator::QProgressIndicator(QWidget *parent) : QWidget(parent) {
12 : 30 : setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
13 : 30 : setFocusPolicy(Qt::NoFocus);
14 : 30 : }
15 : :
16 : 33 : auto QProgressIndicator::isAnimated() const -> bool { return m_timerId != -1; }
17 : :
18 : 5 : void QProgressIndicator::setDisplayedWhenStopped(bool state) {
19 : 5 : m_displayedWhenStopped = state;
20 : :
21 : 5 : update();
22 : 5 : }
23 : :
24 : 5 : auto QProgressIndicator::isDisplayedWhenStopped() const -> bool {
25 : 5 : return m_displayedWhenStopped;
26 : : }
27 : :
28 : 18 : void QProgressIndicator::startAnimation() {
29 : 18 : m_angle = 0;
30 : :
31 [ + + ]: 18 : if (m_timerId == -1) {
32 : 17 : m_timerId = startTimer(m_delay);
33 : : }
34 : 18 : }
35 : :
36 : 11 : void QProgressIndicator::stopAnimation() {
37 [ + + ]: 11 : if (m_timerId != -1) {
38 : 10 : killTimer(m_timerId);
39 : : }
40 : :
41 : 11 : m_timerId = -1;
42 : :
43 : 11 : update();
44 : 11 : }
45 : :
46 : 2 : void QProgressIndicator::setAnimationDelay(int delay) {
47 [ + + ]: 2 : if (m_timerId != -1) {
48 : 1 : killTimer(m_timerId);
49 : : }
50 : :
51 : 2 : m_delay = delay;
52 : :
53 [ + + ]: 2 : if (m_timerId != -1) {
54 : 1 : m_timerId = startTimer(m_delay);
55 : : }
56 : 2 : }
57 : :
58 : 3 : void QProgressIndicator::setColor(const QColor &color) {
59 : 3 : m_color = color;
60 : :
61 : 3 : update();
62 : 3 : }
63 : :
64 : 10 : auto QProgressIndicator::sizeHint() const -> QSize { return {20, 20}; }
65 : :
66 : 3 : auto QProgressIndicator::heightForWidth(int w) const -> int { return w; }
67 : :
68 : 6 : void QProgressIndicator::timerEvent(QTimerEvent * /*event*/) {
69 : 6 : m_angle = (m_angle + 30) % 360;
70 : :
71 : 6 : update();
72 : 6 : }
73 : :
74 : 12 : void QProgressIndicator::paintEvent(QPaintEvent * /*event*/) {
75 [ + + + + ]: 12 : if (!m_displayedWhenStopped && !isAnimated()) {
76 : 1 : return;
77 : : }
78 : :
79 : : int width = qMin(this->width(), this->height());
80 : :
81 : 11 : QPainter p(this);
82 : 11 : p.setRenderHint(QPainter::Antialiasing);
83 : :
84 : 11 : auto outerRadius = static_cast<int>((width - 1) * 0.5);
85 : 11 : auto innerRadius = static_cast<int>((width - 1) * 0.5 * 0.38);
86 : :
87 : 11 : int capsuleHeight = outerRadius - innerRadius;
88 [ + + ]: 11 : int capsuleWidth = (width > 32) ? static_cast<int>(capsuleHeight * 0.23)
89 : 1 : : static_cast<int>(capsuleHeight * 0.35);
90 : 11 : int capsuleRadius = capsuleWidth / 2;
91 : :
92 [ + + ]: 143 : for (int i = 0; i < 12; ++i) {
93 [ + + + ]: 132 : QColor color = m_color.isValid() ? m_color : palette().windowText().color();
94 : 132 : color.setAlphaF(1.0F - (static_cast<float>(i) / 12.0F));
95 : 132 : p.setPen(Qt::NoPen);
96 : 132 : p.setBrush(color);
97 : 132 : p.save();
98 : 132 : p.translate(rect().center());
99 : : // Whole degrees, as the int conversion of the float sum gave.
100 : 132 : p.rotate(m_angle - i * 30);
101 : 132 : p.drawRoundedRect(static_cast<int>(-capsuleWidth * 0.5),
102 : : -(innerRadius + capsuleHeight), capsuleWidth,
103 : : capsuleHeight, capsuleRadius, capsuleRadius);
104 : 132 : p.restore();
105 : : }
106 : 11 : }
|