//****************************************************************************
//Copyright (C) 2005-2006 Beijing BlueDJ Technology Co.,Ltd. All rights reserved.

//This program is free software; you can redistribute it and/or
//modify it under the terms of the GNU General Public License
//as published by the Free Software Foundation; either version 2
//of the License, or (at your option) any later version.

//This program is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU General Public License for more details.

//You should have received a copy of the GNU General Public License
//along with this program (in the file LICENSE.GPL); if not, write to the Free Software
//Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

//Please visit http://www.bluedj.com for more infomation about us.
//Contact us at ggwizard@gmail.com or darkdong@gmail.com.
//****************************************************************************/

#include "DJAnimatedImage.h"

DJAnimatedImage::DJAnimatedImage( const QString& imagePath, QObject * parent )
	: QObject( parent )
{
	djDebug() << "DJAnimatedImage constructor";
	
	setImage( imagePath );
}
DJAnimatedImage::DJAnimatedImage( QObject * parent )
	: QObject( parent )
{
	djDebug() << "DJAnimatedImage constructor";
	m_currentFrame	= -1;
}
DJAnimatedImage::~DJAnimatedImage()
{
	djDebug() << "DJAnimatedImage destructor";
}
QImage DJAnimatedImage::currentImage()
{
	if ( -1 != m_currentFrame )	{
		return m_images.at(m_currentFrame);
	}else {
		return QImage();
	}
}
QImage DJAnimatedImage::nextImage()
{
	if ( -1 != m_currentFrame ) {
		m_currentFrame++;
		if ( m_currentFrame >= m_images.size() ) {
			m_currentFrame	= 0;
		}
		return m_images.at(m_currentFrame);
	}else
		return QImage();
}
void DJAnimatedImage::setImage( const QString& imagePath )
{
	djDebug()<<"DJAnimatedImage::setImage"<<imagePath;
	
	m_images.clear();
	m_currentFrame	= -1;
	
	if ( QFile::exists( imagePath ) ) {
		QImageReader	imageReader( imagePath );
    	QImage	image	= imageReader.read();
    	while( !image.isNull() ) {
    		m_images << image;
    		imageReader.jumpToNextImage();
    		image	= imageReader.read();
    	}
    	if ( m_images.isEmpty() ) {
    		djDebug() << "m_images is empty";
    		m_currentFrame	= -1;
    	}else {
    		djDebug() << "m_images has frames" << m_images.size();
    		m_currentFrame	= 0;
    	}
	}
}
QUrl DJAnimatedImage::name() const
{
	return m_name;
}
void DJAnimatedImage::setName( const QUrl& name )
{
	m_name	= name;
}
