kdeui Library API Documentation

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., 59 Temple Place - Suite 330, 00018 Boston, MA 02111-1307, 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->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 // TODO 00151 // We have to clear the widget when repainting a transparent image 00152 // By doing it like this we get a bit of flicker though. A better 00153 // way might be to merge it with the background in drawContents. 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 { /*BASE::virtual_hook( id, data );*/ } 00200 00201 #include "kanimwidget.moc"
KDE Logo
This file is part of the documentation for kdeui Library Version 3.2.3.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Fri Aug 20 09:48:43 2004 by doxygen 1.3.7 written by Dimitri van Heesch, © 1997-2003