//****************************************************************************
//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 "DJMatrixCanvasImage.h"

const int DJMatrixCanvasImage::RTTI = 5000;

DJMatrixCanvasImage::DJMatrixCanvasImage( Q3Canvas *canvas, const QPixmap& pix, int type, quint32 id )
    : Q3CanvasRectangle( canvas ), m_type(type), m_id(id)
{
	djDebug() << "DJMatrixCanvasImage constructor";
	m_x	= 0;
	m_y	= 0;
	m_alignment	= 0;
	initWithPixmap( pix );
}
DJMatrixCanvasImage::~DJMatrixCanvasImage()
{
	djDebug() << "DJMatrixCanvasImage destructor";
}
int DJMatrixCanvasImage::rtti() const
{
	return RTTI;
}
void DJMatrixCanvasImage::setOriginalPixmap( const QPixmap& pix )
{
	initWithPixmap( pix );
}
void DJMatrixCanvasImage::setAlignment( Qt::Alignment align )
{
	if ( m_alignment != align ) {
		m_alignment	= align;
		//adjust the current position
    	move( m_x, m_y );
	}
}
void DJMatrixCanvasImage::setInnerMatrix( const QMatrix& matrix )
{
	m_innerMatrix	= matrix;
	initWithPixmap( m_originalPixmap );
}
void DJMatrixCanvasImage::setExternalMatrix( const QMatrix& matrix )
{
	m_externalMatrix	= matrix;
	initWithPixmap( m_originalPixmap );
}
QSize DJMatrixCanvasImage::realSize() const
{
	int w	= m_originalPixmap.size().width();
	int h	= m_originalPixmap.size().height();
	int tw, th;
	m_innerMatrix.map( w, h, &tw, &th );
	return QSize(tw,th);
}
void DJMatrixCanvasImage::matrimove(  double x, double y )
{
	//save the original coordinate
	m_x	= x;
	m_y	= y;
	
	//transformed coordinate
	double tx	= x;
	double ty	= y;
	
	if ( !m_externalMatrix.isIdentity() ) {
		m_externalMatrix.map( x, y, &tx, &ty );
	}
	
	move( tx, ty );
}
void DJMatrixCanvasImage::move(  double x, double y )
{
	if( m_alignment & Qt::AlignRight )
		x -= width();
	else if( m_alignment & Qt::AlignHCenter )
		x -= width() >> 1;
	if( m_alignment & Qt::AlignBottom )
		y -= height();
	else if( m_alignment & Qt::AlignVCenter )
		y -= height() >> 1;
	
	Q3CanvasRectangle::move( x, y );
}
void DJMatrixCanvasImage::drawShape( QPainter &p )
{	
	p.save();
	p.setClipping( false );
	QPointF	pointf( Q3CanvasRectangle::x(), Q3CanvasRectangle::y() );
	p.drawPixmap( pointf, m_pixmap );
	p.restore();
}
void DJMatrixCanvasImage::initWithPixmap( const QPixmap& pix )
{
	QMatrix	matrix	= m_innerMatrix * m_externalMatrix;
	m_originalPixmap	= pix;
	if ( !matrix.isIdentity() ) {
		m_pixmap	= m_originalPixmap.transformed( matrix, Qt::SmoothTransformation );
	}else {
		m_pixmap	= m_originalPixmap;
	}
    setSize( m_pixmap.width(), m_pixmap.height() );
    
    //adjust the current position
    matrimove( m_x, m_y );
}
