KDEUI
kanimatedbutton.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include <kanimatedbutton.h>
00021
00022 #include <QAction>
00023 #include <QPixmap>
00024 #include <QTimer>
00025 #include <QImage>
00026 #include <QToolBar>
00027 #include <QPainter>
00028
00029 #include <kdebug.h>
00030 #include <kiconloader.h>
00031
00032 class KAnimatedButtonPrivate
00033 {
00034 public:
00035 KAnimatedButtonPrivate(KAnimatedButton *qq)
00036 : q(qq)
00037 {
00038 }
00039
00040 void updateCurrentIcon();
00041
00042 KAnimatedButton *q;
00043
00044 int frames;
00045 int current_frame;
00046 QPixmap pixmap;
00047 QTimer timer;
00048 QString icon_name;
00049 QVector<QPixmap*> framesCache;
00050
00051
00052 };
00053
00054 KAnimatedButton::KAnimatedButton( QWidget *parent )
00055 : QToolButton(parent), d(new KAnimatedButtonPrivate(this))
00056 {
00057 connect( &d->timer, SIGNAL(timeout()), this, SLOT(slotTimerUpdate()));
00058 }
00059
00060 KAnimatedButton::~KAnimatedButton()
00061 {
00062 d->timer.stop();
00063 qDeleteAll(d->framesCache);
00064
00065 delete d;
00066 }
00067
00068 void KAnimatedButton::start()
00069 {
00070 d->current_frame = 0;
00071 d->timer.start( 50 );
00072 }
00073
00074 void KAnimatedButton::stop()
00075 {
00076 d->current_frame = 0;
00077 d->timer.stop();
00078 d->updateCurrentIcon();
00079 }
00080
00081 void KAnimatedButton::setIcons( const QString& icons )
00082 {
00083 if ( d->icon_name == icons )
00084 return;
00085
00086 d->icon_name = icons;
00087 updateIcons();
00088 }
00089
00090 QString KAnimatedButton::icons( ) const
00091 {
00092 return d->icon_name;
00093 }
00094
00095 void KAnimatedButton::slotTimerUpdate()
00096 {
00097 if(!isVisible())
00098 return;
00099
00100 d->current_frame++;
00101 if (d->current_frame == d->frames)
00102 d->current_frame = 0;
00103
00104 d->updateCurrentIcon();
00105 }
00106
00107 void KAnimatedButtonPrivate::updateCurrentIcon()
00108 {
00109 if (pixmap.isNull())
00110 return;
00111
00112 int w = pixmap.width();
00113 int h = w;
00114
00115
00116 QPixmap* frame = framesCache[current_frame];
00117 if (!frame)
00118 {
00119 frame = new QPixmap(w, h);
00120 QPainter p(frame);
00121 p.drawPixmap(QPoint(0,0), pixmap, QRect(0, current_frame * h, w, h));
00122 p.end();
00123 framesCache[current_frame] = frame;
00124 }
00125
00126 q->setIcon(QIcon(*frame));
00127 }
00128
00129 void KAnimatedButton::updateIcons()
00130 {
00131 QString path = KIconLoader::global()->iconPath(d->icon_name, -iconDimensions());
00132 QImage img(path);
00133
00134 if (img.isNull())
00135 return;
00136
00137 d->current_frame = 0;
00138 d->frames = img.height() / img.width();
00139 if (d->pixmap.width() != iconDimensions())
00140 {
00141 img = img.scaled(iconDimensions(), iconDimensions()*d->frames, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
00142 }
00143 d->pixmap = QPixmap::fromImage(img);
00144
00145 qDeleteAll(d->framesCache);
00146 d->framesCache.resize(d->frames);
00147
00148 d->updateCurrentIcon();
00149 }
00150
00151 int KAnimatedButton::iconDimensions() const
00152 {
00153 return qMin(iconSize().width(), iconSize().height());
00154 }
00155
00156 #include "kanimatedbutton.moc"