00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
#include <kanimwidget.h>
00021
#include <qpixmap.h>
00022
#include <qtimer.h>
00023
#include <qpainter.h>
00024
#include <qimage.h>
00025
#include <ktoolbar.h>
00026
#include <kdebug.h>
00027
#include <kiconloader.h>
00028
00029
class KAnimWidgetPrivate
00030 {
00031
public:
00032
bool loadingCompleted : 1;
00033
bool initDone : 1;
00034
bool transparent : 1;
00035
int frames;
00036
int current_frame;
00037
QPixmap pixmap;
00038
QTimer timer;
00039
QString icon_name;
00040
int size;
00041 };
00042
00043 KAnimWidget::KAnimWidget(
const QString& icons,
int size,
QWidget *parent,
00044
const char *name )
00045 :
QFrame( parent, name ),
00046 d( new KAnimWidgetPrivate )
00047 {
00048 connect( &d->timer, SIGNAL(timeout()),
this, SLOT(slotTimerUpdate()));
00049
00050
if (parent->inherits(
"KToolBar" ))
00051 connect(parent, SIGNAL(modechange()),
this, SLOT(updateIcons()));
00052
00053 d->loadingCompleted =
false;
00054 d->size = size;
00055 d->initDone =
false;
00056
setIcons( icons );
00057
setFrameStyle( StyledPanel | Sunken );
00058 }
00059
00060 KAnimWidget::~KAnimWidget()
00061 {
00062 d->timer.stop();
00063
00064
delete d; d = 0;
00065 }
00066
00067 void KAnimWidget::start()
00068 {
00069 d->current_frame = 0;
00070 d->timer.start( 50 );
00071 }
00072
00073 void KAnimWidget::stop()
00074 {
00075 d->current_frame = 0;
00076 d->timer.stop();
00077 repaint();
00078 }
00079
00080 void KAnimWidget::setSize(
int size )
00081 {
00082
if ( d->size == size )
00083
return;
00084
00085 d->size = size;
00086 updateIcons();
00087 }
00088
00089 void KAnimWidget::setIcons(
const QString& icons )
00090 {
00091
if ( d->icon_name == icons )
00092
return;
00093
00094 d->icon_name = icons;
00095 updateIcons();
00096 }
00097
00098
void KAnimWidget::showEvent(
QShowEvent* e)
00099 {
00100
if (!d->initDone)
00101 {
00102 d->initDone =
true;
00103 updateIcons();
00104 }
00105 QFrame::showEvent(e);
00106 }
00107
00108
void KAnimWidget::hideEvent(
QHideEvent* e)
00109 {
00110 QFrame::hideEvent(e);
00111 }
00112
00113
void KAnimWidget::enterEvent(
QEvent *e )
00114 {
00115
setFrameStyle( WinPanel | Raised );
00116
00117 QFrame::enterEvent( e );
00118 }
00119
00120
void KAnimWidget::leaveEvent(
QEvent *e )
00121 {
00122
setFrameStyle( StyledPanel | Sunken );
00123
00124 QFrame::enterEvent( e );
00125 }
00126
00127
void KAnimWidget::mousePressEvent(
QMouseEvent *e )
00128 {
00129 QFrame::mousePressEvent( e );
00130 }
00131
00132
void KAnimWidget::mouseReleaseEvent(
QMouseEvent *e )
00133 {
00134
if ( e->
button() == LeftButton &&
00135 rect().contains( e->
pos() ) )
00136 emit clicked();
00137
00138 QFrame::mouseReleaseEvent( e );
00139 }
00140
00141
void KAnimWidget::slotTimerUpdate()
00142 {
00143
if(!isVisible())
00144
return;
00145
00146 d->current_frame++;
00147
if (d->current_frame == d->frames)
00148 d->current_frame = 0;
00149
00150
00151
00152
00153
00154 repaint(d->transparent);
00155 }
00156
00157
void KAnimWidget::drawContents(
QPainter *p )
00158 {
00159
if ( d->pixmap.isNull() )
00160
return;
00161
00162
int w = d->pixmap.width();
00163
int h = w;
00164
int x = (width() - w) / 2;
00165
int y = (height() - h) / 2;
00166 p->
drawPixmap(
QPoint(x, y), d->pixmap,
QRect(0, d->current_frame*h, w, h));
00167 }
00168
00169
void KAnimWidget::updateIcons()
00170 {
00171
if (!d->initDone)
00172
return;
00173
00174
if (parent()->inherits(
"KToolBar" ))
00175 d->size = ((
KToolBar*)parent())->iconSize();
00176
if (!d->size)
00177 d->size =
KGlobal::iconLoader()->
currentSize(KIcon::MainToolbar);
00178
00179
QString path =
KGlobal::iconLoader()->
iconPath(d->icon_name, -d->size);
00180
QImage img(path);
00181
00182
if (img.isNull())
00183
return;
00184
00185 d->current_frame = 0;
00186 d->frames = img.height() / img.width();
00187 d->transparent = img.hasAlphaBuffer();
00188
if (d->pixmap.width() != d->size)
00189 {
00190 img = img.smoothScale(d->size, d->size*d->frames);
00191 }
00192 d->pixmap = img;
00193
00194 setFixedSize( d->size+2, d->size+2 );
00195 resize( d->size+2, d->size+2 );
00196 }
00197
00198
void KAnimWidget::virtual_hook(
int,
void* )
00199 { }
00200
00201
#include "kanimwidget.moc"