Debian Codegraph @

src/contrib/qtwaitingspinner/QtWaitingSpinner.cpp

raw symbols SCIP
1 /* Original Work Copyright (c) 2012-2014 Alexander Turkin
2 Modified 2014 by William Hallatt
3
4 Permission is hereby granted, free of charge, to any person obtaining a copy of
5 this software and associated documentation files (the "Software"), to deal in
6 the Software without restriction, including without limitation the rights to
7 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
8 the Software, and to permit persons to whom the Software is furnished to do so,
9 subject to the following conditions:
10
11 The above copyright notice and this permission notice shall be included in all
12 copies or substantial portions of the Software.
13
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
16 FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
17 COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
18 IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 */
21 #include <algorithm>
22 #include <cmath>
23
24 #include <QPainter>
25 #include <QTimer>
26
27 #include "QtWaitingSpinner.h"
28
29 /*----------------------------------------------------------------------------*/
30
31 // Defaults
32 const QColor c_color(Qt::black);
33 const qreal c_roundness(70.0);
34 const qreal c_minTrailOpacity(15.0);
35 const qreal c_trailFadePercentage(70.0);
36 const int c_lines(12);
37 const int c_lineLength(10);
38 const int c_lineWidth(5);
39 const int c_innerRadius(10);
40 const int c_revPerSec(1);
41
42 /*----------------------------------------------------------------------------*/
43
44 QtWaitingSpinner::QtWaitingSpinner(QWidget *parent)
45 : QWidget(parent),
46
47 // Configurable settings.
48 m_color(c_color),
49 m_roundness(c_roundness),
50 m_minTrailOpacity(c_minTrailOpacity),
51 m_trailFadePercentage(c_trailFadePercentage),
52 m_revPerSec(c_revPerSec),
53 m_numberOfLines(c_lines),
54 m_lineLength(c_lineLength + c_lineWidth),
55 m_lineWidth(c_lineWidth),
56 m_innerRadius(c_innerRadius),
57
58 // Other
59 m_timer(NULL),
60 m_parent(parent),
61 m_centreOnParent(false),
62 m_currentCounter(0),
63 m_isSpinning(false) {
64 initialise();
65 }
66
67 /*----------------------------------------------------------------------------*/
68
69 QtWaitingSpinner::QtWaitingSpinner(Qt::WindowModality modality, QWidget *parent,
70 bool centreOnParent)
71 : QWidget(parent, Qt::Dialog | Qt::FramelessWindowHint),
72
73 // Configurable settings.
74 m_color(c_color),
75 m_roundness(c_roundness),
76 m_minTrailOpacity(c_minTrailOpacity),
77 m_trailFadePercentage(c_trailFadePercentage),
78 m_revPerSec(c_revPerSec),
79 m_numberOfLines(c_lines),
80 m_lineLength(c_lineLength + c_lineWidth),
81 m_lineWidth(c_lineWidth),
82 m_innerRadius(c_innerRadius),
83
84 // Other
85 m_timer(NULL),
86 m_parent(parent),
87 m_centreOnParent(centreOnParent),
88 m_currentCounter(0) {
89 initialise();
90
91 // We need to set the window modality AFTER we've hidden the
92 // widget for the first time since changing this property while
93 // the widget is visible has no effect.
94 this->setWindowModality(modality);
95 this->setAttribute(Qt::WA_TranslucentBackground);
96 }
97
98 /*----------------------------------------------------------------------------*/
99
100 void QtWaitingSpinner::initialise() {
101 m_timer = new QTimer(this);
102 connect(m_timer, &QTimer::timeout, this, &QtWaitingSpinner::rotate);
103 updateSize();
104 updateTimer();
105 this->hide();
106 }
107
108 /*----------------------------------------------------------------------------*/
109
110 void QtWaitingSpinner::paintEvent(QPaintEvent * /*ev*/) {
111 QPainter painter(this);
112 painter.fillRect(this->rect(), Qt::transparent);
113 painter.setRenderHint(QPainter::Antialiasing, true);
114
115 if (m_currentCounter >= m_numberOfLines) {
116 m_currentCounter = 0;
117 }
118 painter.setPen(Qt::NoPen);
119 for (int i = 0; i < m_numberOfLines; ++i) {
120 painter.save();
121 painter.translate(m_innerRadius + m_lineLength,
122 m_innerRadius + m_lineLength);
123 qreal rotateAngle =
124 static_cast<qreal>(360 * i) / static_cast<qreal>(m_numberOfLines);
125 painter.rotate(rotateAngle);
126 painter.translate(m_innerRadius, 0);
127 int distance =
128 lineCountDistanceFromPrimary(i, m_currentCounter, m_numberOfLines);
129 QColor color =
130 currentLineColor(distance, m_numberOfLines, m_trailFadePercentage,
131 m_minTrailOpacity, m_color);
132 painter.setBrush(color);
133 // TODO improve the way rounded rect is painted
134 painter.drawRoundedRect(
135 QRect(0, -m_lineWidth / 2, m_lineLength, m_lineWidth), m_roundness,
136 m_roundness, Qt::RelativeSize);
137 painter.restore();
138 }
139 }
140
141 /*----------------------------------------------------------------------------*/
142
143 void QtWaitingSpinner::start() {
144 updatePosition();
145 m_isSpinning = true;
146 this->show();
147 if (!m_timer->isActive()) {
148 m_timer->start();
149 m_currentCounter = 0;
150 }
151 }
152
153 /*----------------------------------------------------------------------------*/
154
155 void QtWaitingSpinner::stop() {
156 m_isSpinning = false;
157 this->hide();
158 if (m_timer->isActive()) {
159 m_timer->stop();
160 m_currentCounter = 0;
161 }
162 }
163
164 /*----------------------------------------------------------------------------*/
165
166 void QtWaitingSpinner::setNumberOfLines(int lines) {
167 m_numberOfLines = lines;
168 m_currentCounter = 0;
169 updateTimer();
170 }
171
172 /*----------------------------------------------------------------------------*/
173
174 void QtWaitingSpinner::setLineLength(int length) {
175 m_lineLength = length;
176 updateSize();
177 }
178
179 /*----------------------------------------------------------------------------*/
180
181 void QtWaitingSpinner::setLineWidth(int width) {
182 m_lineWidth = width;
183 updateSize();
184 }
185
186 /*----------------------------------------------------------------------------*/
187
188 void QtWaitingSpinner::setInnerRadius(int radius) {
189 m_innerRadius = radius;
190 updateSize();
191 }
192
193 /*----------------------------------------------------------------------------*/
194
195 bool QtWaitingSpinner::isSpinning() const { return m_isSpinning; }
196
197 /*----------------------------------------------------------------------------*/
198
199 void QtWaitingSpinner::setRoundness(qreal roundness) {
200 m_roundness = std::max(0.0, std::min(100.0, roundness));
201 }
202
203 /*----------------------------------------------------------------------------*/
204
205 void QtWaitingSpinner::setColor(QColor color) { m_color = color; }
206
207 /*----------------------------------------------------------------------------*/
208
209 void QtWaitingSpinner::setRevolutionsPerSecond(int rps) {
210 m_revPerSec = rps;
211 updateTimer();
212 }
213
214 /*----------------------------------------------------------------------------*/
215
216 void QtWaitingSpinner::setTrailFadePercentage(qreal trail) {
217 m_trailFadePercentage = trail;
218 }
219
220 /*----------------------------------------------------------------------------*/
221
222 void QtWaitingSpinner::setMinimumTrailOpacity(qreal minOpacity) {
223 m_minTrailOpacity = minOpacity;
224 }
225
226 /*----------------------------------------------------------------------------*/
227
228 void QtWaitingSpinner::rotate() {
229 ++m_currentCounter;
230 if (m_currentCounter >= m_numberOfLines) {
231 m_currentCounter = 0;
232 }
233 update();
234 }
235
236 /*----------------------------------------------------------------------------*/
237
238 void QtWaitingSpinner::updateSize() {
239 int size = (m_innerRadius + m_lineLength) * 2;
240 setFixedSize(size, size);
241 }
242
243 /*----------------------------------------------------------------------------*/
244
245 void QtWaitingSpinner::updateTimer() {
246 m_timer->setInterval(calculateTimerInterval(m_numberOfLines, m_revPerSec));
247 }
248
249 /*----------------------------------------------------------------------------*/
250
251 void QtWaitingSpinner::updatePosition() {
252 if (m_parent && m_centreOnParent) {
253 this->move(m_parent->frameGeometry().topLeft() +
254 m_parent->rect().center() - this->rect().center());
255 }
256 }
257
258 /*----------------------------------------------------------------------------*/
259
260 int QtWaitingSpinner::calculateTimerInterval(int lines, int speed) {
261 return 1000 / (lines * speed);
262 }
263
264 /*----------------------------------------------------------------------------*/
265
266 int QtWaitingSpinner::lineCountDistanceFromPrimary(int current, int primary,
267 int totalNrOfLines) {
268 int distance = primary - current;
269 if (distance < 0) {
270 distance += totalNrOfLines;
271 }
272 return distance;
273 }
274
275 /*----------------------------------------------------------------------------*/
276
277 QColor QtWaitingSpinner::currentLineColor(int countDistance, int totalNrOfLines,
278 qreal trailFadePerc, qreal minOpacity,
279 QColor color) {
280 if (countDistance == 0) {
281 return color;
282 }
283 const qreal minAlphaF = minOpacity / 100.0;
284 int distanceThreshold =
285 static_cast<int>(ceil((totalNrOfLines - 1) * trailFadePerc / 100.0));
286 if (countDistance > distanceThreshold) {
287 color.setAlphaF(minAlphaF);
288 } else {
289 qreal alphaDiff = color.alphaF() - minAlphaF;
290 qreal gradient = alphaDiff / static_cast<qreal>(distanceThreshold + 1);
291 qreal resultAlpha = color.alphaF() - gradient * countDistance;
292
293 // If alpha is out of bounds, clip it.
294 resultAlpha = std::min(1.0, std::max(0.0, resultAlpha));
295 color.setAlphaF(resultAlpha);
296 }
297 return color;
298 }
299
300 /*----------------------------------------------------------------------------*/