//****************************************************************************
//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 "DJMatrixCanvasTimer.h"
#include "DJMatrixCanvasSprite.h"
#include "DJMatrixCanvasText.h"

DJMatrixCanvasTimer::DJMatrixCanvasTimer( Q3Canvas *canvas )
	: DJMatrixCanvasImage( canvas )
{
	djDebug() << "DJMatrixCanvasTimer constructor";
	
	m_seconds	= 0;
	m_clockPanel	= QPixmap(":/BaseRes/image/clock/panel.png");
	m_directions << QPixmap()
					<< QPixmap(":/BaseRes/image/clock/down.png")
					<< QPixmap(":/BaseRes/image/clock/left.png")
					<< QPixmap(":/BaseRes/image/clock/up.png")
					<< QPixmap(":/BaseRes/image/clock/right.png");
	startTimer(1000);
}
DJMatrixCanvasTimer::~DJMatrixCanvasTimer()
{
	djDebug() << "DJMatrixCanvasTimer destructor";
}
void DJMatrixCanvasTimer::setTimeout( const QList<quint8>& views, int seconds )
{
	QPixmap	pixmap	= m_clockPanel;
	QPainter painter( &pixmap );
	foreach( quint8 view, views ) {
		painter.drawPixmap( 0, 0, m_directions.at( view ) );
	}
	setOriginalPixmap( pixmap );
	m_seconds	= seconds;	
	show();
}
void DJMatrixCanvasTimer::timerEvent( QTimerEvent * event )
{
	if ( 0 != m_seconds ) {
		m_seconds--;
	}
	update();
}
void DJMatrixCanvasTimer::drawShape( QPainter &p )
{	
	DJMatrixCanvasImage::drawShape( p );
	
	p.save();
	p.setClipping( false );
	QList<QPixmap> pixmaps;
	 
	int seconds	= m_seconds;
	int bai	= seconds / 100;
	seconds	-= bai * 100;
	int shi	= seconds / 10;
	seconds	-= shi * 10;
	int num	= 0;
	if ( 0 != bai ) {
		num	= 3;
		pixmaps << QPixmap( QString(":/BaseRes/image/clock/%1.png").arg(bai) )
				<< QPixmap( QString(":/BaseRes/image/clock/%1.png").arg(shi) )
				<< QPixmap( QString(":/BaseRes/image/clock/%1.png").arg(seconds) );
	}else {
		num	= 2;
		pixmaps << QPixmap( QString(":/BaseRes/image/clock/%1.png").arg(shi) )
				<< QPixmap( QString(":/BaseRes/image/clock/%1.png").arg(seconds) );
	}
	int width	= 34;
	int height	= 64;
	QPixmap conjoinedPixmap( width * num, height );
	conjoinedPixmap.fill( Qt::black );
	QBitmap	mask	= conjoinedPixmap.createMaskFromColor( Qt::black );
	conjoinedPixmap.setMask( mask );
	
	QPainter painter( &conjoinedPixmap );
	int offsetX	= 0;
	foreach( QPixmap pix, pixmaps ) {
		painter.drawPixmap( offsetX, 0, width, height, pix );
		offsetX	+= width;
	}
	QPoint c	= rect().center();
	int w	= rect().width()/3;
	int h	= rect().height()/3;
	QRect r( c.x()-w/2, c.y()-h/2, w, h );
	p.drawPixmap( r, conjoinedPixmap );
	
	p.restore();
}
