• Skip to content
  • Skip to link menu
KDE 4.1 API Reference
  • KDE API Reference
  • kdelibs
  • Sitemap
  • Contact Us
 

KDEUI

kanimatedbutton.cpp

Go to the documentation of this file.
00001 /* This file is part of the KDE libraries
00002    Copyright (C) 2000 Kurt Granroth <granroth@kde.org>
00003    Copyright (C) 2006 Hamish Rodda <rodda@kde.org>
00004 
00005    This library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Library General Public
00007    License version 2 as published by the Free Software Foundation.
00008 
00009    This library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Library General Public License for more details.
00013 
00014    You should have received a copy of the GNU Library General Public License
00015    along with this library; see the file COPYING.LIB.  If not, write to
00016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017    Boston, MA 02110-1301, USA.
00018 */
00019 
00020 #include <kanimatedbutton.h>
00021 
00022 #include <QAction>
00023 #include <QPixmap>
00024 #include <QTimer>
00025 #include <QImage>
00026 #include <QToolBar>
00027 #include <QPainter>
00028 
00029 #include <kdebug.h>
00030 #include <kiconloader.h>
00031 
00032 class KAnimatedButtonPrivate
00033 {
00034 public:
00035     KAnimatedButtonPrivate(KAnimatedButton *qq)
00036         : q(qq)
00037     {
00038     }
00039 
00040     void updateCurrentIcon();
00041 
00042     KAnimatedButton *q;
00043 
00044   int                    frames;
00045   int                    current_frame;
00046   QPixmap                pixmap;
00047   QTimer                 timer;
00048   QString                icon_name;
00049   QVector<QPixmap*>      framesCache; // We keep copies of each frame so that
00050                                       // the icon code can properly cache them in QPixmapCache,
00051                                       // and not fill it up with dead copies
00052 };
00053 
00054 KAnimatedButton::KAnimatedButton( QWidget *parent )
00055     : QToolButton(parent), d(new KAnimatedButtonPrivate(this))
00056 {
00057   connect( &d->timer, SIGNAL(timeout()), this, SLOT(slotTimerUpdate()));
00058 }
00059 
00060 KAnimatedButton::~KAnimatedButton()
00061 {
00062   d->timer.stop();
00063   qDeleteAll(d->framesCache);
00064 
00065   delete d;
00066 }
00067 
00068 void KAnimatedButton::start()
00069 {
00070   d->current_frame = 0;
00071   d->timer.start( 50 );
00072 }
00073 
00074 void KAnimatedButton::stop()
00075 {
00076   d->current_frame = 0;
00077   d->timer.stop();
00078   d->updateCurrentIcon();
00079 }
00080 
00081 void KAnimatedButton::setIcons( const QString& icons )
00082 {
00083   if ( d->icon_name == icons )
00084     return;
00085 
00086   d->icon_name = icons;
00087   updateIcons();
00088 }
00089 
00090 QString KAnimatedButton::icons( ) const
00091 {
00092    return d->icon_name;
00093 }
00094 
00095 void KAnimatedButton::slotTimerUpdate()
00096 {
00097   if(!isVisible())
00098     return;
00099 
00100   d->current_frame++;
00101   if (d->current_frame == d->frames)
00102      d->current_frame = 0;
00103 
00104   d->updateCurrentIcon();
00105 }
00106 
00107 void KAnimatedButtonPrivate::updateCurrentIcon()
00108 {
00109   if (pixmap.isNull())
00110     return;
00111 
00112   int w = pixmap.width();
00113   int h = w;
00114 
00115 
00116   QPixmap* frame = framesCache[current_frame];
00117   if (!frame)
00118   {
00119     frame = new QPixmap(w, h);
00120     QPainter p(frame);
00121     p.drawPixmap(QPoint(0,0), pixmap, QRect(0, current_frame * h, w, h));
00122     p.end();
00123     framesCache[current_frame] = frame;
00124   }
00125 
00126   q->setIcon(QIcon(*frame));
00127 }
00128 
00129 void KAnimatedButton::updateIcons()
00130 {
00131   QString path = KIconLoader::global()->iconPath(d->icon_name, -iconDimensions());
00132   QImage img(path);
00133 
00134   if (img.isNull())
00135      return;
00136 
00137   d->current_frame = 0;
00138   d->frames = img.height() / img.width();
00139   if (d->pixmap.width() != iconDimensions())
00140   {
00141      img = img.scaled(iconDimensions(), iconDimensions()*d->frames, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
00142   }
00143   d->pixmap = QPixmap::fromImage(img);
00144 
00145   qDeleteAll(d->framesCache);
00146   d->framesCache.resize(d->frames);
00147 
00148   d->updateCurrentIcon();
00149 }
00150 
00151 int KAnimatedButton::iconDimensions() const
00152 {
00153   return qMin(iconSize().width(), iconSize().height());
00154 }
00155 
00156 #include "kanimatedbutton.moc"

KDEUI

Skip menu "KDEUI"
  • Main Page
  • Modules
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

kdelibs

Skip menu "kdelibs"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • Kate
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • KIO
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • Kross
  • KUtils
  • Nepomuk
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal