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

KDEUI

  • sources
  • kde-4.12
  • kdelibs
  • kdeui
  • widgets
kanimatedbutton.cpp
Go to the documentation of this file.
1 /* This file is part of the KDE libraries
2  Copyright (C) 2000 Kurt Granroth <granroth@kde.org>
3  Copyright (C) 2006 Hamish Rodda <rodda@kde.org>
4  Copyright (C) 2008 Pino Toscano <pino@kde.org>
5 
6  This library is free software; you can redistribute it and/or
7  modify it under the terms of the GNU Library General Public
8  License version 2 as published by the Free Software Foundation.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Library General Public License for more details.
14 
15  You should have received a copy of the GNU Library General Public License
16  along with this library; see the file COPYING.LIB. If not, write to
17  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  Boston, MA 02110-1301, USA.
19 */
20 
21 #include <kanimatedbutton.h>
22 
23 #include <QAction>
24 #include <QPixmap>
25 #include <QTimer>
26 #include <QImage>
27 #include <QToolBar>
28 #include <QPainter>
29 #include <QMovie>
30 
31 #include <kdebug.h>
32 #include <kiconloader.h>
33 
34 class KAnimatedButtonPrivate
35 {
36 public:
37  KAnimatedButtonPrivate(KAnimatedButton *qq)
38  : q(qq), movie(0)
39  {
40  }
41 
42  void updateCurrentIcon();
43  void _k_movieFrameChanged(int number);
44  void _k_movieFinished();
45 
46  KAnimatedButton *q;
47  QMovie *movie;
48 
49  int frames;
50  int current_frame;
51  QPixmap pixmap;
52  QTimer timer;
53  QString icon_name;
54  QVector<QPixmap*> framesCache; // We keep copies of each frame so that
55  // the icon code can properly cache them in QPixmapCache,
56  // and not fill it up with dead copies
57 };
58 
59 KAnimatedButton::KAnimatedButton( QWidget *parent )
60  : QToolButton(parent), d(new KAnimatedButtonPrivate(this))
61 {
62  connect( &d->timer, SIGNAL(timeout()), this, SLOT(slotTimerUpdate()));
63 }
64 
65 KAnimatedButton::~KAnimatedButton()
66 {
67  d->timer.stop();
68  qDeleteAll(d->framesCache);
69  delete d->movie;
70 
71  delete d;
72 }
73 
74 void KAnimatedButton::start()
75 {
76  if (d->movie) {
77  d->movie->start();
78  } else {
79  d->current_frame = 0;
80  d->timer.start(50);
81  }
82 }
83 
84 void KAnimatedButton::stop()
85 {
86  if (d->movie) {
87  d->movie->stop();
88  d->movie->jumpToFrame(0);
89  d->_k_movieFrameChanged(0);
90  } else {
91  d->current_frame = 0;
92  d->timer.stop();
93  d->updateCurrentIcon();
94  }
95 }
96 
97 void KAnimatedButton::setIcons( const QString& icons )
98 {
99  if ( d->icon_name == icons )
100  return;
101 
102  d->timer.stop();
103  d->icon_name = icons;
104  updateIcons();
105 }
106 
107 QString KAnimatedButton::icons( ) const
108 {
109  return d->icon_name;
110 }
111 
112 void KAnimatedButton::slotTimerUpdate()
113 {
114  if(!isVisible())
115  return;
116 
117  d->current_frame++;
118  if (d->current_frame == d->frames)
119  d->current_frame = 0;
120 
121  d->updateCurrentIcon();
122 }
123 
124 void KAnimatedButtonPrivate::updateCurrentIcon()
125 {
126  if (pixmap.isNull())
127  return;
128 
129  QPixmap* frame = framesCache[current_frame];
130  if (!frame)
131  {
132  const int icon_size = q->iconDimensions();
133  const int row_size = pixmap.width() / icon_size;
134  const int row = current_frame / row_size;
135  const int column = current_frame % row_size;
136  frame = new QPixmap(icon_size, icon_size);
137  frame->fill(Qt::transparent);
138  QPainter p(frame);
139  p.drawPixmap(QPoint(0, 0), pixmap, QRect(column * icon_size, row * icon_size, icon_size, icon_size));
140  p.end();
141  framesCache[current_frame] = frame;
142  }
143 
144  q->setIcon(QIcon(*frame));
145 }
146 
147 void KAnimatedButtonPrivate::_k_movieFrameChanged(int number)
148 {
149  Q_UNUSED(number);
150  q->setIcon(QIcon(movie->currentPixmap()));
151 }
152 
153 void KAnimatedButtonPrivate::_k_movieFinished()
154 {
155  // if not running, make it loop
156  if (movie->state() == QMovie::NotRunning) {
157  movie->start();
158  }
159 }
160 
161 void KAnimatedButton::updateIcons()
162 {
163  const int icon_size = iconDimensions();
164  d->pixmap = QPixmap();
165  QMovie *movie = KIconLoader::global()->loadMovie(d->icon_name, KIconLoader::NoGroup, -icon_size);
166  if (movie) {
167  d->frames = 0;
168  movie->setCacheMode(QMovie::CacheAll);
169  connect(movie, SIGNAL(frameChanged(int)), this, SLOT(_k_movieFrameChanged(int)));
170  connect(movie, SIGNAL(finished()), this, SLOT(_k_movieFinished()));
171  } else {
172  const QString path = KIconLoader::global()->iconPath(d->icon_name, -icon_size);
173  QImage img(path);
174  if (img.isNull())
175  return;
176 
177  if ((img.width() % icon_size != 0) || (img.height() % icon_size != 0))
178  return;
179 
180  d->frames = (img.height() / icon_size) * (img.width() / icon_size);
181  d->pixmap = QPixmap::fromImage(img);
182  }
183 
184  d->current_frame = 0;
185  qDeleteAll(d->framesCache);
186  d->framesCache.fill(0);
187  d->framesCache.resize(d->frames);
188  delete d->movie;
189  d->movie = movie;
190 
191  if (d->movie) {
192  d->movie->jumpToFrame(0);
193  d->_k_movieFrameChanged(0);
194  } else {
195  d->updateCurrentIcon();
196  }
197 }
198 
199 int KAnimatedButton::iconDimensions() const
200 {
201  return qMin(iconSize().width(), iconSize().height());
202 }
203 
204 #include "kanimatedbutton.moc"
KIconLoader::loadMovie
QMovie * loadMovie(const QString &name, KIconLoader::Group group, int size=0, QObject *parent=0) const
Loads an animated icon.
Definition: kiconloader.cpp:1240
kdebug.h
KAnimatedButton::icons
QString icons() const
Returns the current icons.
KIconLoader::global
static KIconLoader * global()
Returns the global icon loader initialized with the global KComponentData.
KAnimatedButton::KAnimatedButton
KAnimatedButton(QWidget *parent=0L)
Construct an animated tool button.
Definition: kanimatedbutton.cpp:59
timeout
int timeout
QWidget
KAnimatedButton::setIcons
void setIcons(const QString &icons)
Sets the name of the animated icons to load.
Definition: kanimatedbutton.cpp:97
KAnimatedButton
An extended version of QToolButton which can display an animated icon.
Definition: kanimatedbutton.h:38
kiconloader.h
QString
KIconLoader::NoGroup
No group.
Definition: kiconloader.h:129
KAnimatedButton::~KAnimatedButton
virtual ~KAnimatedButton()
Destructor.
Definition: kanimatedbutton.cpp:65
KAnimatedButton::stop
void stop()
Stops the animation.
Definition: kanimatedbutton.cpp:84
KAnimatedButton::iconDimensions
int iconDimensions() const
Returns the current maximum dimension (width or length) for an icon.
Definition: kanimatedbutton.cpp:199
KAnimatedButton::slotTimerUpdate
void slotTimerUpdate()
Definition: kanimatedbutton.cpp:112
KAnimatedButton::updateIcons
void updateIcons()
Updates the icons by reloading them if required.
Definition: kanimatedbutton.cpp:161
kanimatedbutton.h
QPoint
QRect
KAnimatedButton::start
void start()
Starts the animation from frame 1.
Definition: kanimatedbutton.cpp:74
QToolButton
KIconLoader::iconPath
QString iconPath(const QString &name, int group_or_size, bool canReturnNull=false) const
Returns the path of an icon.
Definition: kiconloader.cpp:1009
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:49:13 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KDEUI

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

kdelibs API Reference

Skip menu "kdelibs API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  • kjsembed
  •   WTF
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Nepomuk-Core
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal