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   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     emit clicked();
00135 
00136   QFrame::mouseReleaseEvent( e );
00137 }
00138 
00139 void KAnimWidget::slotTimerUpdate()
00140 {
00141   if(!isVisible())
00142     return;
00143 
00144   d->current_frame++;
00145   if (d->current_frame == d->frames)
00146      d->current_frame = 0;
00147 
00148   repaint(false);
00149 }
00150 
00151 void KAnimWidget::drawContents( QPainter *p )
00152 {
00153   if ( d->pixmap.isNull() )
00154     return;
00155 
00156   int w = d->pixmap.width();
00157   int h = w;
00158   int x = (width()  - w) / 2;
00159   int y = (height() - h) / 2;
00160   p->drawPixmap(QPoint(x, y), d->pixmap, QRect(0, d->current_frame*h, w, h));
00161 }
00162 
00163 void KAnimWidget::updateIcons()
00164 {
00165   if (!d->initDone)
00166      return;
00167 
00168   if (parent()->inherits( "KToolBar" ))
00169     d->size = ((KToolBar*)parent())->iconSize();
00170   if (!d->size)
00171      d->size = KGlobal::iconLoader()->currentSize(KIcon::MainToolbar);
00172 
00173   QString path = KGlobal::iconLoader()->iconPath(d->icon_name, -d->size);
00174   QImage img(path);
00175   
00176   if (img.isNull())
00177      return;
00178 
00179   d->current_frame = 0;
00180   d->frames = img.height() / img.width();
00181   if (d->pixmap.width() != d->size)
00182   {
00183      img = img.smoothScale(d->size, d->size*d->frames);     
00184   }
00185   d->pixmap = img;
00186 
00187   setFixedSize( d->size+2, d->size+2 );
00188   resize( d->size+2, d->size+2 );
00189 }
00190 
00191 void KAnimWidget::virtual_hook( int, void* )
00192 { /*BASE::virtual_hook( id, data );*/ }
00193 
00194 #include "kanimwidget.moc"
KDE Logo
This file is part of the documentation for kdelibs Version 3.1.0.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Wed Oct 8 12:20:57 2003 by doxygen 1.2.18 written by Dimitri van Heesch, © 1997-2001