QtPass 1.4.0
Multi-platform GUI for pass, the standard unix password manager.
Loading...
Searching...
No Matches
qprogressindicator.cpp
Go to the documentation of this file.
1/*
2 * This code is based on https://github.com/mojocorp/QProgressIndicator
3 * and published under
4 *
5 * The MIT License (MIT)
6 *
7 * Copyright (c) 2011 Morgan Leborgne
8 *
9 * Permission is hereby granted, free of charge, to any person obtaining a copy
10 * of this software and associated documentation files (the "Software"), to deal
11 * in the Software without restriction, including without limitation the rights
12 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 * copies of the Software, and to permit persons to whom the Software is
14 * furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 * SOFTWARE.
26 */
27#include "qprogressindicator.h"
28#include <QPainter>
29
35 : QWidget(parent), m_angle(0), m_timerId(-1), m_delay(40),
36 m_displayedWhenStopped(false), m_color(Qt::black) {
37 setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
38 setFocusPolicy(Qt::NoFocus);
39}
40
41bool QProgressIndicator::isAnimated() const { return m_timerId != -1; }
42
44 m_displayedWhenStopped = state;
45
46 update();
47}
48
50 return m_displayedWhenStopped;
51}
52
54 m_angle = 0;
55
56 if (m_timerId == -1)
57 m_timerId = startTimer(m_delay);
58}
59
61 if (m_timerId != -1)
62 killTimer(m_timerId);
63
64 m_timerId = -1;
65
66 update();
67}
68
70 if (m_timerId != -1)
71 killTimer(m_timerId);
72
73 m_delay = delay;
74
75 if (m_timerId != -1)
76 m_timerId = startTimer(m_delay);
77}
78
79void QProgressIndicator::setColor(const QColor &color) {
80 m_color = color;
81
82 update();
83}
84
89QSize QProgressIndicator::sizeHint() const { return {20, 20}; }
90
96int QProgressIndicator::heightForWidth(int w) const { return w; }
97
101void QProgressIndicator::timerEvent(QTimerEvent * /*event*/) {
102 m_angle = (m_angle + 30) % 360;
103
104 update();
105}
106
110void QProgressIndicator::paintEvent(QPaintEvent * /*event*/) {
111 if (!m_displayedWhenStopped && !isAnimated())
112 return;
113
114 int width = qMin(this->width(), this->height());
115
116 QPainter p(this);
117 p.setRenderHint(QPainter::Antialiasing);
118
119 auto outerRadius = int((width - 1) * 0.5);
120 auto innerRadius = int((width - 1) * 0.5 * 0.38);
121
122 int capsuleHeight = outerRadius - innerRadius;
123 int capsuleWidth =
124 (width > 32) ? int(capsuleHeight * 0.23) : int(capsuleHeight * 0.35);
125 int capsuleRadius = capsuleWidth / 2;
126
127 for (int i = 0; i < 12; ++i) {
128 QColor color = m_color;
129 color.setAlphaF(int(1.0f - (i / 12.0f)));
130 p.setPen(Qt::NoPen);
131 p.setBrush(color);
132 p.save();
133 p.translate(rect().center());
134 p.rotate(int(m_angle - i * 30.0f));
135 p.drawRoundedRect(int(-capsuleWidth * 0.5), -(innerRadius + capsuleHeight),
136 capsuleWidth, capsuleHeight, capsuleRadius,
137 capsuleRadius);
138 p.restore();
139 }
140}
virtual void timerEvent(QTimerEvent *event)
QProgressIndicator::timerEvent do the actual animation.
void stopAnimation()
Stops the spin animation.
virtual void paintEvent(QPaintEvent *event)
QProgressIndicator::paintEvent draw the spinner.
bool isDisplayedWhenStopped() const
Returns a Boolean value indicating whether the receiver shows itself even when it is not animating.
void startAnimation()
Starts the spin animation.
void setColor(const QColor &color)
Sets the color of the components to the given color.
virtual QSize sizeHint() const
QProgressIndicator::sizeHint default minimum size.
bool isAnimated() const
Returns a Boolean value indicating whether the component is currently animated.
void setDisplayedWhenStopped(bool state)
Sets whether the component hides itself when it is not animating.
QProgressIndicator(QWidget *parent=0)
QProgressIndicator::QProgressIndicator constructor.
void setAnimationDelay(int delay)
Sets the delay between animation steps.
const QColor & color() const
Returns the color of the component.
int heightForWidth(int w) const
QProgressIndicator::heightForWidth square ratio.