tdeui
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 <tqpixmap.h>
00022 #include <tqtimer.h>
00023 #include <tqpainter.h>
00024 #include <tqimage.h>
00025 #include <tdetoolbar.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 TQPixmap pixmap;
00038 TQTimer timer;
00039 TQString icon_name;
00040 int size;
00041 };
00042
00043 KAnimWidget::KAnimWidget( const TQString& icons, int size, TQWidget *parent,
00044 const char *name )
00045 : TQFrame( parent, name ),
00046 d( new KAnimWidgetPrivate )
00047 {
00048 connect( &d->timer, TQT_SIGNAL(timeout()), this, TQT_SLOT(slotTimerUpdate()));
00049
00050 if (parent && parent->inherits( "TDEToolBar" ))
00051 connect(parent, TQT_SIGNAL(modechange()), this, TQT_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 TQString& icons )
00090 {
00091 if ( d->icon_name == icons )
00092 return;
00093
00094 d->icon_name = icons;
00095 updateIcons();
00096 }
00097
00098 TQString KAnimWidget::icons( ) const
00099 {
00100 return d->icon_name;
00101 }
00102
00103 int KAnimWidget::size( ) const
00104 {
00105 return d->size;
00106 }
00107
00108
00109 void KAnimWidget::showEvent(TQShowEvent* e)
00110 {
00111 if (!d->initDone)
00112 {
00113 d->initDone = true;
00114 updateIcons();
00115 }
00116 TQFrame::showEvent(e);
00117 }
00118
00119 void KAnimWidget::hideEvent(TQHideEvent* e)
00120 {
00121 TQFrame::hideEvent(e);
00122 }
00123
00124 void KAnimWidget::enterEvent( TQEvent *e )
00125 {
00126 setFrameStyle( Panel | Raised );
00127
00128 TQFrame::enterEvent( e );
00129 }
00130
00131 void KAnimWidget::leaveEvent( TQEvent *e )
00132 {
00133 setFrameStyle( StyledPanel | Sunken );
00134
00135 TQFrame::leaveEvent( e );
00136 }
00137
00138 void KAnimWidget::mousePressEvent( TQMouseEvent *e )
00139 {
00140 TQFrame::mousePressEvent( e );
00141 }
00142
00143 void KAnimWidget::mouseReleaseEvent( TQMouseEvent *e )
00144 {
00145 if ( e->button() == Qt::LeftButton &&
00146 rect().contains( e->pos() ) )
00147 emit clicked();
00148
00149 TQFrame::mouseReleaseEvent( e );
00150 }
00151
00152 void KAnimWidget::slotTimerUpdate()
00153 {
00154 if(!isVisible())
00155 return;
00156
00157 d->current_frame++;
00158 if (d->current_frame == d->frames)
00159 d->current_frame = 0;
00160
00161
00162
00163
00164
00165 repaint(d->transparent);
00166 }
00167
00168 void KAnimWidget::drawContents( TQPainter *p )
00169 {
00170 if ( d->pixmap.isNull() )
00171 return;
00172
00173 int w = d->pixmap.width();
00174 int h = w;
00175 int x = (width() - w) / 2;
00176 int y = (height() - h) / 2;
00177 p->drawPixmap(TQPoint(x, y), d->pixmap, TQRect(0, d->current_frame*h, w, h));
00178 }
00179
00180 void KAnimWidget::updateIcons()
00181 {
00182 if (!d->initDone)
00183 return;
00184
00185 if (parent()->inherits( "TDEToolBar" ))
00186 d->size = ((TDEToolBar*)parent())->iconSize();
00187 if (!d->size)
00188 d->size = TDEGlobal::iconLoader()->currentSize(TDEIcon::MainToolbar);
00189
00190 TQString path = TDEGlobal::iconLoader()->iconPath(d->icon_name, -d->size);
00191 TQImage img(path);
00192
00193 if (img.isNull())
00194 return;
00195
00196 d->current_frame = 0;
00197 d->frames = img.height() / img.width();
00198 d->transparent = img.hasAlphaBuffer();
00199 if (d->pixmap.width() != d->size)
00200 {
00201 img = img.smoothScale(d->size, d->size*d->frames);
00202 }
00203 d->pixmap = img;
00204
00205 setFixedSize( d->size+2, d->size+2 );
00206 resize( d->size+2, d->size+2 );
00207 }
00208
00209 void KAnimWidget::virtual_hook( int, void* )
00210 { }
00211
00212 #include "kanimwidget.moc"