kanimwidget.cpp
00001 // -*- c-basic-offset: 2 -*- 00002 00003 /* This file is part of the KDE libraries 00004 Copyright (C) 2000 Kurt Granroth <granroth@kde.org> 00005 00006 This library is free software; you can redistribute it and/or 00007 modify it under the terms of the GNU Library General Public 00008 License version 2 as published by the Free Software Foundation. 00009 00010 This library is distributed in the hope that it will be useful, 00011 but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00013 Library General Public License for more details. 00014 00015 You should have received a copy of the GNU Library General Public License 00016 along with this library; see the file COPYING.LIB. If not, write to 00017 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00018 Boston, MA 02110-1301, USA. 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 && 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 QString 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(QShowEvent* e) 00110 { 00111 if (!d->initDone) 00112 { 00113 d->initDone = true; 00114 updateIcons(); 00115 } 00116 QFrame::showEvent(e); 00117 } 00118 00119 void KAnimWidget::hideEvent(QHideEvent* e) 00120 { 00121 QFrame::hideEvent(e); 00122 } 00123 00124 void KAnimWidget::enterEvent( QEvent *e ) 00125 { 00126 setFrameStyle( Panel | Raised ); 00127 00128 QFrame::enterEvent( e ); 00129 } 00130 00131 void KAnimWidget::leaveEvent( QEvent *e ) 00132 { 00133 setFrameStyle( StyledPanel | Sunken ); 00134 00135 QFrame::leaveEvent( e ); 00136 } 00137 00138 void KAnimWidget::mousePressEvent( QMouseEvent *e ) 00139 { 00140 QFrame::mousePressEvent( e ); 00141 } 00142 00143 void KAnimWidget::mouseReleaseEvent( QMouseEvent *e ) 00144 { 00145 if ( e->button() == LeftButton && 00146 rect().contains( e->pos() ) ) 00147 emit clicked(); 00148 00149 QFrame::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 // TODO 00162 // We have to clear the widget when repainting a transparent image 00163 // By doing it like this we get a bit of flicker though. A better 00164 // way might be to merge it with the background in drawContents. 00165 repaint(d->transparent); 00166 } 00167 00168 void KAnimWidget::drawContents( QPainter *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(QPoint(x, y), d->pixmap, QRect(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( "KToolBar" )) 00186 d->size = ((KToolBar*)parent())->iconSize(); 00187 if (!d->size) 00188 d->size = KGlobal::iconLoader()->currentSize(KIcon::MainToolbar); 00189 00190 QString path = KGlobal::iconLoader()->iconPath(d->icon_name, -d->size); 00191 QImage 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 { /*BASE::virtual_hook( id, data );*/ } 00211 00212 #include "kanimwidget.moc"