animatedpixmap.cpp

Go to the documentation of this file.
00001 /*
00002 **  This file is part of Vidalia, and is subject to the license terms in the
00003 **  LICENSE file, found in the top level directory of this distribution. If you
00004 **  did not receive the LICENSE file with this file, you may obtain it from the
00005 **  Vidalia source package distributed by the Vidalia Project at
00006 **  http://www.vidalia-project.net/. No part of Vidalia, including this file,
00007 **  may be copied, modified, propagated, or distributed except according to the
00008 **  terms described in the LICENSE file.
00009 */
00010 
00011 /*
00012 ** \file animatedpixmap.cpp
00013 ** \version $Id: animatedpixmap.cpp 2362 2008-02-29 04:30:11Z edmanm $
00014 */
00015 
00016 #include <QSize>
00017 #include <QRect>
00018 
00019 #include "animatedpixmap.h"
00020 
00021 
00022 /** Default constructor. */
00023 AnimatedPixmap::AnimatedPixmap()
00024 : _frameNumber(-1)
00025 {
00026   _frameTimer.setInterval(100);
00027   connect(&_frameTimer, SIGNAL(timeout()), this, SLOT(frameTimeout()));
00028 }
00029 
00030 /** Creates an animated pixmap from the specified file. */
00031 AnimatedPixmap::AnimatedPixmap(const QString &fileName)
00032 {
00033   _frameTimer.setInterval(100);
00034   setPixmap(QPixmap(fileName));
00035   connect(&_frameTimer, SIGNAL(timeout()), this, SLOT(frameTimeout()));
00036 }
00037 
00038 /** Starts the animation. */
00039 void
00040 AnimatedPixmap::start()
00041 {
00042   _frameTimer.start();
00043   _frameNumber = 0;
00044 }
00045 
00046 /** Stops the animated image. */
00047 void
00048 AnimatedPixmap::stop()
00049 {
00050   _frameTimer.stop();
00051 }
00052 
00053 /** Sets the duration of each animation frame to <b>frameDelay</b>. */
00054 void
00055 AnimatedPixmap::setFrameDelay(int frameDelay)
00056 {
00057   _frameTimer.setInterval(frameDelay);
00058 }
00059 
00060 /** Sets the source image for the animation to <b>pixmap</b>. */
00061 void
00062 AnimatedPixmap::setPixmap(const QPixmap &pixmap)
00063 {
00064   _pixmap = pixmap;
00065   _frameNumber = 0;
00066 }
00067 
00068 /** Returns the number of frames in the animation. */
00069 int
00070 AnimatedPixmap::frameCount() const
00071 {
00072   return (_pixmap.width()/_pixmap.height());
00073 }
00074 
00075 /** Called when the current animation frame should be changed. */
00076 void
00077 AnimatedPixmap::frameTimeout()
00078 {
00079   _frameNumber = (_frameNumber + 1) % frameCount();
00080   emit frameChanged(_frameNumber);  
00081 }
00082 
00083 /** Returns the current animation frame. */
00084 QPixmap
00085 AnimatedPixmap::currentFrame() const
00086 {
00087   return _pixmap.copy(_frameNumber * _pixmap.height(),
00088                       0,
00089                       _pixmap.height(),
00090                       _pixmap.height());
00091 }
00092 

Generated on Sat Aug 16 17:38:35 2008 for Vidalia by  doxygen 1.5.6