QtPass
2.0.0
Developer documentation
Toggle main menu visibility
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
8
#include "
qprogressindicator.h
"
9
#include <QPainter>
10
11
QProgressIndicator::QProgressIndicator
(QWidget *parent) : QWidget(parent) {
12
setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
13
setFocusPolicy(Qt::NoFocus);
14
}
15
16
auto
QProgressIndicator::isAnimated
() const ->
bool
{
return
m_timerId
!= -1; }
17
18
void
QProgressIndicator::setDisplayedWhenStopped
(
bool
state) {
19
m_displayedWhenStopped
= state;
20
21
update();
22
}
23
24
auto
QProgressIndicator::isDisplayedWhenStopped
() const ->
bool
{
25
return
m_displayedWhenStopped
;
26
}
27
28
void
QProgressIndicator::startAnimation
() {
29
m_angle
= 0;
30
31
if
(
m_timerId
== -1) {
32
m_timerId
= startTimer(
m_delay
);
33
}
34
}
35
36
void
QProgressIndicator::stopAnimation
() {
37
if
(
m_timerId
!= -1) {
38
killTimer(
m_timerId
);
39
}
40
41
m_timerId
= -1;
42
43
update();
44
}
45
46
void
QProgressIndicator::setAnimationDelay
(
int
delay) {
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
58
void
QProgressIndicator::setColor
(
const
QColor &
color
) {
59
m_color
=
color
;
60
61
update();
62
}
63
64
auto
QProgressIndicator::sizeHint
() const -> QSize {
return
{20, 20}; }
65
66
auto
QProgressIndicator::heightForWidth
(
int
w)
const
->
int
{
return
w; }
67
68
void
QProgressIndicator::timerEvent
(QTimerEvent *
/*event*/
) {
69
m_angle
= (
m_angle
+ 30) % 360;
70
71
update();
72
}
73
74
void
QProgressIndicator::paintEvent
(QPaintEvent *
/*event*/
) {
75
if
(!
m_displayedWhenStopped
&& !
isAnimated
()) {
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
}
QProgressIndicator::paintEvent
void paintEvent(QPaintEvent *event) override
Draw the twelve capsules, the newest opaque and the rest fading.
Definition
qprogressindicator.cpp:74
QProgressIndicator::m_color
QColor m_color
Colour from setColor(); invalid means the palette colour.
Definition
qprogressindicator.h:141
QProgressIndicator::m_angle
int m_angle
Rotation of the first capsule, in degrees.
Definition
qprogressindicator.h:133
QProgressIndicator::stopAnimation
void stopAnimation()
Stop the spin animation.
Definition
qprogressindicator.cpp:36
QProgressIndicator::QProgressIndicator
QProgressIndicator(QWidget *parent=nullptr)
Create a stopped indicator with a fixed size policy that never takes keyboard focus.
Definition
qprogressindicator.cpp:11
QProgressIndicator::isAnimated
auto isAnimated() const -> bool
Whether the indicator is animating.
Definition
qprogressindicator.cpp:16
QProgressIndicator::startAnimation
void startAnimation()
Start the spin animation from its first position.
Definition
qprogressindicator.cpp:28
QProgressIndicator::isDisplayedWhenStopped
auto isDisplayedWhenStopped() const -> bool
Whether the indicator is drawn while it does not animate.
Definition
qprogressindicator.cpp:24
QProgressIndicator::setColor
void setColor(const QColor &color)
Set the colour the indicator is drawn in.
Definition
qprogressindicator.cpp:58
QProgressIndicator::timerEvent
void timerEvent(QTimerEvent *event) override
Turn the indicator one step (30 degrees) and repaint.
Definition
qprogressindicator.cpp:68
QProgressIndicator::m_displayedWhenStopped
bool m_displayedWhenStopped
Whether the indicator is drawn while it does not animate.
Definition
qprogressindicator.h:139
QProgressIndicator::setDisplayedWhenStopped
void setDisplayedWhenStopped(bool state)
Set whether the indicator is drawn while it does not animate.
Definition
qprogressindicator.cpp:18
QProgressIndicator::m_delay
int m_delay
Milliseconds between two animation steps.
Definition
qprogressindicator.h:137
QProgressIndicator::setAnimationDelay
void setAnimationDelay(int delay)
Set the delay between animation steps.
Definition
qprogressindicator.cpp:46
QProgressIndicator::m_timerId
int m_timerId
Id of the animation timer, or -1 while stopped.
Definition
qprogressindicator.h:135
QProgressIndicator::heightForWidth
auto heightForWidth(int w) const -> int override
Keep the indicator square.
Definition
qprogressindicator.cpp:66
QProgressIndicator::sizeHint
auto sizeHint() const -> QSize override
Preferred size of the indicator.
Definition
qprogressindicator.cpp:64
QProgressIndicator::color
auto color() const -> const QColor &
Colour set with setColor().
Definition
qprogressindicator.h:64
qprogressindicator.h
Spinner widget that shows the application is busy.
src
qprogressindicator.cpp
Generated by
1.18.0