2
* busywidget.cpp - cool animating widget
3
* Copyright (C) 2001, 2002 Justin Karneges
6
* This program is free software; you can redistribute it and/or
7
* modify it under the terms of the GNU General Public License
8
* as published by the Free Software Foundation; either version 2
9
* of the License, or (at your option) any later version.
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this library; if not, write to the Free Software
18
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22
#include "busywidget.h"
29
/////////////////////////////////////////////////////////////////////////////
32
#define FPS 20 // frequency of update
34
#define SPINRATE 24 // 1024ths of a revolution per frame
35
#define SPINOFFSET 4 // frames each panel is offset by
37
#define COLORSTOPPED 0xFFFFFF // color when stopped
38
#define COLORSPINNING 0xFFFFFF // color when spinning
39
#define COLORSHADOW 0x000000 // color of shadow
41
/////////////////////////////////////////////////////////////////////////////
44
#define MSECSPERFRAME (1000 / FPS)
46
/////////////////////////////////////////////////////////////////////////////
49
extern char psigraphic[];
60
CPanel(int height = 1);
65
void SetAngle(int _angle) { angle = _angle % 1024; }
66
void SetHeight(int _height) { height = _height; }
67
int GetAngle() { return angle % 1024; }
68
int GetHeight() { return height; }
69
int GetModOffset() { return (height - GetModHeight()) / 2; }
78
CColor(int _r, int _g, int _b)
86
inline void SetColor(int _r, int _g, int _b)
88
SetColor((_r << 16) + (_g << 8) + _b);
90
inline void SetColor(int _clr)
100
return (m_clr >> 8) & 255;
106
CColor Alpha(CColor clr, int alpha = 256);
109
class BusyWidget::Private : public QObject
116
Private(BusyWidget *b)
120
stopInProgress = false;
142
pix = QPixmap(busy->width(), busy->height());
143
pix.fill(QColor("#406080"));
150
for(i = 0; i < 5; i++)
152
int o = panel[i].GetModOffset();
154
CColor c1(COLORSPINNING), c2(COLORSTOPPED), c3(COLORSHADOW);
155
CColor b = c1.Alpha(c2, panel[i].alpha * 8);
157
CColor a = b.Alpha(c3, panel[i].GetShade());
159
l = panel[i].GetModHeight();
161
double radangle = (double) 3.1415926f * (double) panel[i].GetAngle() / (double) 512;
162
int step = (int)((double)1024 / cos(radangle));
163
step = step < 0 ? -step : step;
165
int n = (int)((double)1024 * cos(radangle) * 17 / 2);
168
row = 8192 - step * n / 1024;
170
QColor clr(a.GetR(), a.GetG(), a.GetB());
172
for(j = 0; j < l; j++)
174
int m = row / 1024 + 1;
175
for(k = 0; k < 16; k++)
177
p.setPen(psigraphic[i * 304 + m * 16 + k] ? Qt::black : clr);
178
p.drawPoint(i * 16 + k + 1, o + j + 1);
185
p.drawRect(0, 0, busy->width(), busy->height());
191
if ( stopInProgress ) {
192
stopInProgress = false;
196
ocountdown = SPINOFFSET * 4 + 1;
201
/////////////////////////////////////////////////////////////////////////////
204
BusyWidget::BusyWidget(QWidget *parent)
207
d = new Private(this);
216
setFixedSize(sizeHint());
217
setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum);
220
for(i = 0; i < 5; i++)
221
d->panel[i].SetHeight(17);
226
BusyWidget::~BusyWidget()
231
QSize BusyWidget::minimumSizeHint() const
233
return QSize( 82, 19 );
236
QSize BusyWidget::sizeHint() const
238
return minimumSizeHint();
241
bool BusyWidget::isActive() const
243
return (d->isActive && !d->stopInProgress);
246
void BusyWidget::setActive(bool a)
254
void BusyWidget::start()
256
d->stopInProgress = false;
264
d->pcountdown = SPINOFFSET * 4 + 1;
268
d->t = new QTimer(this);
269
connect(d->t, SIGNAL(timeout()), SLOT(animate()));
271
d->t->start(MSECSPERFRAME);
275
void BusyWidget::stop()
280
if ( d->stopInProgress )
283
d->stopInProgress = true;
284
QTimer::singleShot(0, d, SLOT(stop()));
287
void BusyWidget::animate()
290
for(i = 0; i < 5; i++)
291
d->panel[i].Spin(SPINRATE);
294
if(!(--d->pcountdown % SPINOFFSET))
295
d->panel[d->pcountdown / SPINOFFSET].spinning = true;
298
if(!(--d->ocountdown % SPINOFFSET))
299
d->panel[d->ocountdown / SPINOFFSET].spinning = false;
303
bool isValid = false;
304
for(i = 0; i < 5; i++)
305
if(d->panel[i].spinning || d->panel[i].GetAngle() != 0 || d->panel[i].alpha != 0)
319
void BusyWidget::paintEvent(QPaintEvent *)
322
p.drawPixmap(0,0, d->pix);
325
void BusyWidget::resizeEvent(QResizeEvent *)
330
/////////////////////////////////////////////////////////////////////////
331
// stuff beyond here for animating rotating psi panels
335
CColor CColor::Alpha(CColor clr, int alpha)
337
int ialpha = 256 - alpha;
340
r = (alpha * GetR() + ialpha * clr.GetR()) / 256;
341
g = (alpha * GetG() + ialpha * clr.GetG()) / 256;
342
b = (alpha * GetB() + ialpha * clr.GetB()) / 256;
344
return CColor(r, g, b);
348
CPanel::CPanel(int _height)
356
int CPanel::GetModHeight()
361
double radangle = (double) 3.1415926f * (double) l / (double) 512;
362
int tmp = (int)(cos(radangle)* (double) height);
363
return tmp < 0 ? -tmp : tmp;
366
int CPanel::GetShade()
368
int l = GetAngle() + 128;
369
if(GetAngle() >= 256 && GetAngle() < 768)
375
double radangle = (double) 3.1415926f * (double) l / (double) 512;
376
return 128 + (int)(cos(radangle)* (double) 128);
379
void CPanel::Spin(int n)
386
if(angle < 512 && i >= 512)
405
char psigraphic[304*5] =
407
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
408
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
409
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
410
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
411
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
412
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
413
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
414
0, 0, 0, 0, 0, 0, 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