File indexing completed on 2024-04-14 14:31:55

0001 /*
0002  * Copyright 1999 by Martin R. Jones <mjones@kde.org>
0003  * Copyright 2010 by Stefan Böhmann <kde@hilefoks.org>
0004  *
0005  * This program is free software; you can redistribute it and/or modify
0006  * it under the terms of the GNU General Public License as published by
0007  * the Free Software Foundation; either version 2 of the License, or
0008  * (at your option) any later version.
0009  *
0010  * This program is distributed in the hope that it will be useful,
0011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0013  * GNU General Public License for more details.
0014  *
0015  * You should have received a copy of the GNU General Public License
0016  * along with this program; if not, write to the Free Software
0017  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
0018  */
0019 #include "amoranimation.h"
0020 #include "amorpixmapmanager.h"
0021 
0022 #include <QPixmap>
0023 #include <QSettings>
0024 
0025 
0026 AmorAnimation::AmorAnimation(const QSettings *config)
0027   : mCurrent( 0 ),
0028     mTotalMovement( 0 ),
0029     mMaximumSize(0, 0)
0030 {
0031     readConfig(config);
0032 }
0033 
0034 
0035 void AmorAnimation::reset()
0036 {
0037     mCurrent = 0;
0038 }
0039 
0040 
0041 bool AmorAnimation::next()
0042 {
0043     return ++mCurrent < mSequence.count();
0044 }
0045 
0046 
0047 int AmorAnimation::frameNum() const
0048 {
0049     return mCurrent;
0050 }
0051 
0052 
0053 bool AmorAnimation::validFrame() const
0054 {
0055     return mCurrent < mSequence.count();
0056 }
0057 
0058 
0059 int AmorAnimation::totalMovement() const
0060 {
0061     return mTotalMovement;
0062 }
0063 
0064 
0065 QSize AmorAnimation::maximumSize() const
0066 {
0067     return mMaximumSize;
0068 }
0069 
0070 
0071 int AmorAnimation::delay() const
0072 {
0073     return validFrame() && mCurrent < mDelay.size() ? mDelay.at( mCurrent ) : 100;
0074 }
0075 
0076 
0077 QPoint AmorAnimation::hotspot() const
0078 {
0079     return validFrame() && mCurrent < mHotspot.size() ? mHotspot.at( mCurrent ) : QPoint( 16, 16 );
0080 }
0081 
0082 
0083 int AmorAnimation::movement() const
0084 {
0085     return validFrame() && mCurrent < mMovement.size() ? mMovement.at( mCurrent ) : 0;
0086 }
0087 
0088 
0089 const QPixmap *AmorAnimation::frame()
0090 {
0091     return validFrame() ? AmorPixmapManager::manager()->pixmap( mSequence.at( mCurrent ) ) : 0;
0092 }
0093 
0094 
0095 void AmorAnimation::readConfig(const QSettings *config)
0096 {
0097     // Read the list of frames to display and load them into the pixmap manager.
0098     mSequence = config->value(QLatin1String("Sequence")).toStringList();
0099     int frames = mSequence.count();
0100     for(QStringList::Iterator it = mSequence.begin(); it != mSequence.end(); ++it) {
0101         const QPixmap *pixmap = AmorPixmapManager::manager()->load( *it );
0102         if( pixmap ) {
0103             mMaximumSize = mMaximumSize.expandedTo( pixmap->size() );
0104         }
0105     }
0106 
0107     // Read the delays between frames.
0108     QStringList list;
0109     list = config->value(QLatin1String("Delay")).toStringList();
0110     mDelay.resize( list.count() );
0111     for(int i = 0; i < list.count() && i < frames; ++i) {
0112         mDelay[i] = list.at( i ).toInt();
0113     }
0114 
0115     // Read the distance to move between frames and calculate the total
0116     // distance that this aniamtion moves from its starting position.
0117     list = config->value(QLatin1String("Movement")).toStringList();
0118     mMovement.resize( frames );
0119     for(int i = 0; i < list.count() && i < frames; ++i) {
0120         mMovement[i] = list.at( i ).toInt();
0121         mTotalMovement += mMovement[i];
0122     }
0123 
0124     // Read the hotspot for each frame.
0125     QStringList entries = config->value(QLatin1String("HotspotX")).toStringList();
0126     mHotspot.resize( frames );
0127     for(int i = 0; i < entries.count() && i < frames; ++i) {
0128         mHotspot[i].setX( entries.at( i ).toInt() );
0129     }
0130 
0131     entries = config->value(QLatin1String("HotspotY")).toStringList();
0132     for(int i = 0; i < entries.count() && i < frames; ++i) {
0133         mHotspot[i].setY( entries.at( i ).toInt() );
0134     }
0135 
0136     // Add the overlap of the last frame to the total movement.
0137     const QPoint &lastHotspot = mHotspot[ mHotspot.size()-1 ];
0138     if( mTotalMovement > 0 ) {
0139         const QPixmap *lastFrame =  AmorPixmapManager::manager()->pixmap( mSequence.last() );
0140         if( lastFrame ) {
0141             mTotalMovement += ( lastFrame->width() - lastHotspot.x() );
0142         }
0143     }
0144     else if( mTotalMovement < 0 ) {
0145         mTotalMovement -= lastHotspot.x();
0146     }
0147 }
0148 
0149 
0150 // kate: word-wrap off; encoding utf-8; indent-width 4; tab-width 4; line-numbers on; mixed-indent off; remove-trailing-space-save on; replace-tabs-save on; replace-tabs on; space-indent on;
0151 // vim:set spell et sw=4 ts=4 nowrap cino=l1,cs,U1: