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"
28
/////////////////////////////////////////////////////////////////////////////
31
#define FPS 20 // frequency of update
33
#define SPINRATE 24 // 1024ths of a revolution per frame
34
#define SPINOFFSET 4 // frames each panel is offset by
36
#define COLORSTOPPED 0xFFFFFF // color when stopped
37
#define COLORSPINNING 0xFFFFFF // color when spinning
38
#define COLORSHADOW 0x000000 // color of shadow
40
/////////////////////////////////////////////////////////////////////////////
43
#define MSECSPERFRAME (1000 / FPS)
45
/////////////////////////////////////////////////////////////////////////////
48
extern char psigraphic[];
59
CPanel(int height = 1);
64
void SetAngle(int _angle) { angle = _angle % 1024; }
65
void SetHeight(int _height) { height = _height; }
66
int GetAngle() { return angle % 1024; }
67
int GetHeight() { return height; }
68
int GetModOffset() { return (height - GetModHeight()) / 2; }
77
CColor(int _r, int _g, int _b)
85
inline void SetColor(int _r, int _g, int _b)
87
SetColor((_r << 16) + (_g << 8) + _b);
89
inline void SetColor(int _clr)
99
return (m_clr >> 8) & 255;
105
CColor Alpha(CColor clr, int alpha = 256);
108
class BusyWidget::Private : public QObject
115
Private(BusyWidget *b)
119
stopInProgress = false;
137
p.drawPixmap(0,0, pix);
142
pix.resize(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, const char *name)
205
:QWidget(parent, name)
207
d = new Private(this);
218
setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed));
221
for(i = 0; i < 5; i++)
222
d->panel[i].SetHeight(17);
227
BusyWidget::~BusyWidget()
232
bool BusyWidget::isActive() const
234
return (d->isActive && !d->stopInProgress);
237
void BusyWidget::setActive(bool a)
245
void BusyWidget::start()
247
d->stopInProgress = false;
255
d->pcountdown = SPINOFFSET * 4 + 1;
259
d->t = new QTimer(this);
260
connect(d->t, SIGNAL(timeout()), SLOT(animate()));
262
d->t->start(MSECSPERFRAME);
266
void BusyWidget::stop()
271
if ( d->stopInProgress )
274
d->stopInProgress = true;
275
QTimer::singleShot(0, d, SLOT(stop()));
278
void BusyWidget::animate()
281
for(i = 0; i < 5; i++)
282
d->panel[i].Spin(SPINRATE);
285
if(!(--d->pcountdown % SPINOFFSET))
286
d->panel[d->pcountdown / SPINOFFSET].spinning = true;
289
if(!(--d->ocountdown % SPINOFFSET))
290
d->panel[d->ocountdown / SPINOFFSET].spinning = false;
294
bool isValid = false;
295
for(i = 0; i < 5; i++)
296
if(d->panel[i].spinning || d->panel[i].GetAngle() != 0 || d->panel[i].alpha != 0)
310
void BusyWidget::paintEvent(QPaintEvent *)
315
void BusyWidget::resizeEvent(QResizeEvent *)
320
/////////////////////////////////////////////////////////////////////////
321
// stuff beyond here for animating rotating psi panels
325
CColor CColor::Alpha(CColor clr, int alpha)
327
int ialpha = 256 - alpha;
330
r = (alpha * GetR() + ialpha * clr.GetR()) / 256;
331
g = (alpha * GetG() + ialpha * clr.GetG()) / 256;
332
b = (alpha * GetB() + ialpha * clr.GetB()) / 256;
334
return CColor(r, g, b);
338
CPanel::CPanel(int _height)
346
int CPanel::GetModHeight()
351
double radangle = (double) 3.1415926f * (double) l / (double) 512;
352
int tmp = (int)(cos(radangle)* (double) height);
353
return tmp < 0 ? -tmp : tmp;
356
int CPanel::GetShade()
358
int l = GetAngle() + 128;
359
if(GetAngle() >= 256 && GetAngle() < 768)
365
double radangle = (double) 3.1415926f * (double) l / (double) 512;
366
return 128 + (int)(cos(radangle)* (double) 128);
369
void CPanel::Spin(int n)
376
if(angle < 512 && i >= 512)
395
char psigraphic[304*5] =
397
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
398
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
399
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
400
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
401
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
402
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
403
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
404
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
405
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 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