00001 /**************************************************************** 00002 * Vidalia is distributed under the following license: 00003 * 00004 * Copyright (C) 2007, Matt Edman, Justin Hipple 00005 * 00006 * This program is free software; you can redistribute it and/or 00007 * modify it under the terms of the GNU General Public License 00008 * as published by the Free Software Foundation; either version 2 00009 * of the License, or (at your option) any later version. 00010 * 00011 * This program is distributed in the hope that it will be useful, 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 * GNU General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU General Public License 00017 * along with this program; if not, write to the Free Software 00018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, 00019 * Boston, MA 02110-1301, USA. 00020 ****************************************************************/ 00021 00022 /** 00023 * \file animatedpixmap.cpp 00024 * \version $Id: animatedpixmap.cpp 1699 2007-04-09 01:50:59Z edmanm $ 00025 */ 00026 00027 #include <QSize> 00028 #include <QRect> 00029 00030 #include "animatedpixmap.h" 00031 00032 00033 /** Default constructor. */ 00034 AnimatedPixmap::AnimatedPixmap() 00035 : _frameNumber(-1) 00036 { 00037 _frameTimer.setInterval(100); 00038 connect(&_frameTimer, SIGNAL(timeout()), this, SLOT(frameTimeout())); 00039 } 00040 00041 /** Creates an animated pixmap from the specified file. */ 00042 AnimatedPixmap::AnimatedPixmap(const QString &fileName) 00043 { 00044 _frameTimer.setInterval(100); 00045 setPixmap(QPixmap(fileName)); 00046 connect(&_frameTimer, SIGNAL(timeout()), this, SLOT(frameTimeout())); 00047 } 00048 00049 /** Starts the animation. */ 00050 void 00051 AnimatedPixmap::start() 00052 { 00053 _frameTimer.start(); 00054 _frameNumber = 0; 00055 } 00056 00057 /** Stops the animated image. */ 00058 void 00059 AnimatedPixmap::stop() 00060 { 00061 _frameTimer.stop(); 00062 } 00063 00064 /** Sets the duration of each animation frame to <b>frameDelay</b>. */ 00065 void 00066 AnimatedPixmap::setFrameDelay(int frameDelay) 00067 { 00068 _frameTimer.setInterval(frameDelay); 00069 } 00070 00071 /** Sets the source image for the animation to <b>pixmap</b>. */ 00072 void 00073 AnimatedPixmap::setPixmap(const QPixmap &pixmap) 00074 { 00075 _pixmap = pixmap; 00076 _frameNumber = 0; 00077 } 00078 00079 /** Returns the number of frames in the animation. */ 00080 int 00081 AnimatedPixmap::frameCount() const 00082 { 00083 return (_pixmap.width()/_pixmap.height()); 00084 } 00085 00086 /** Called when the current animation frame should be changed. */ 00087 void 00088 AnimatedPixmap::frameTimeout() 00089 { 00090 _frameNumber = (_frameNumber + 1) % frameCount(); 00091 emit frameChanged(_frameNumber); 00092 } 00093 00094 /** Returns the current animation frame. */ 00095 QPixmap 00096 AnimatedPixmap::currentFrame() const 00097 { 00098 return _pixmap.copy(_frameNumber * _pixmap.height(), 00099 0, 00100 _pixmap.height(), 00101 _pixmap.height()); 00102 } 00103