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