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