QtPass 2.0.0
Developer documentation
Loading...
Searching...
No Matches
qprogressindicator.cpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2011 Morgan Leborgne
2// SPDX-FileCopyrightText: 2015 Anne Jan Brouwer
3// SPDX-License-Identifier: MIT
9#include <QPainter>
10
11QProgressIndicator::QProgressIndicator(QWidget *parent) : QWidget(parent) {
12 setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
13 setFocusPolicy(Qt::NoFocus);
14}
15
16auto QProgressIndicator::isAnimated() const -> bool { return m_timerId != -1; }
17
20
21 update();
22}
23
27
29 m_angle = 0;
30
31 if (m_timerId == -1) {
32 m_timerId = startTimer(m_delay);
33 }
34}
35
37 if (m_timerId != -1) {
38 killTimer(m_timerId);
39 }
40
41 m_timerId = -1;
42
43 update();
44}
45
47 if (m_timerId != -1) {
48 killTimer(m_timerId);
49 }
50
51 m_delay = delay;
52
53 if (m_timerId != -1) {
54 m_timerId = startTimer(m_delay);
55 }
56}
57
59 m_color = color;
60
61 update();
62}
63
64auto QProgressIndicator::sizeHint() const -> QSize { return {20, 20}; }
65
66auto QProgressIndicator::heightForWidth(int w) const -> int { return w; }
67
68void QProgressIndicator::timerEvent(QTimerEvent * /*event*/) {
69 m_angle = (m_angle + 30) % 360;
70
71 update();
72}
73
74void QProgressIndicator::paintEvent(QPaintEvent * /*event*/) {
76 return;
77 }
78
79 int width = qMin(this->width(), this->height());
80
81 QPainter p(this);
82 p.setRenderHint(QPainter::Antialiasing);
83
84 auto outerRadius = static_cast<int>((width - 1) * 0.5);
85 auto innerRadius = static_cast<int>((width - 1) * 0.5 * 0.38);
86
87 int capsuleHeight = outerRadius - innerRadius;
88 int capsuleWidth = (width > 32) ? static_cast<int>(capsuleHeight * 0.23)
89 : static_cast<int>(capsuleHeight * 0.35);
90 int capsuleRadius = capsuleWidth / 2;
91
92 for (int i = 0; i < 12; ++i) {
93 QColor color = m_color.isValid() ? m_color : palette().windowText().color();
94 color.setAlphaF(1.0F - (static_cast<float>(i) / 12.0F));
95 p.setPen(Qt::NoPen);
96 p.setBrush(color);
97 p.save();
98 p.translate(rect().center());
99 // Whole degrees, as the int conversion of the float sum gave.
100 p.rotate(m_angle - i * 30);
101 p.drawRoundedRect(static_cast<int>(-capsuleWidth * 0.5),
102 -(innerRadius + capsuleHeight), capsuleWidth,
103 capsuleHeight, capsuleRadius, capsuleRadius);
104 p.restore();
105 }
106}
void paintEvent(QPaintEvent *event) override
Draw the twelve capsules, the newest opaque and the rest fading.
QColor m_color
Colour from setColor(); invalid means the palette colour.
int m_angle
Rotation of the first capsule, in degrees.
void stopAnimation()
Stop the spin animation.
QProgressIndicator(QWidget *parent=nullptr)
Create a stopped indicator with a fixed size policy that never takes keyboard focus.
auto isAnimated() const -> bool
Whether the indicator is animating.
void startAnimation()
Start the spin animation from its first position.
auto isDisplayedWhenStopped() const -> bool
Whether the indicator is drawn while it does not animate.
void setColor(const QColor &color)
Set the colour the indicator is drawn in.
void timerEvent(QTimerEvent *event) override
Turn the indicator one step (30 degrees) and repaint.
bool m_displayedWhenStopped
Whether the indicator is drawn while it does not animate.
void setDisplayedWhenStopped(bool state)
Set whether the indicator is drawn while it does not animate.
int m_delay
Milliseconds between two animation steps.
void setAnimationDelay(int delay)
Set the delay between animation steps.
int m_timerId
Id of the animation timer, or -1 while stopped.
auto heightForWidth(int w) const -> int override
Keep the indicator square.
auto sizeHint() const -> QSize override
Preferred size of the indicator.
auto color() const -> const QColor &
Colour set with setColor().
Spinner widget that shows the application is busy.