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