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

const int DJMatrixCanvasText::RTTI = 6000;
	
DJMatrixCanvasText::DJMatrixCanvasText( Q3Canvas *canvas, Type type, quint32 id )
    :Q3CanvasText( canvas ), m_type(type), m_id(id)
{
	djDebug() << "DJMatrixCanvasText constructor";
	init();
}
DJMatrixCanvasText::DJMatrixCanvasText( Q3Canvas *canvas, const QString& string, Type type, quint32 id )
    :Q3CanvasText( string, canvas ), m_type(type), m_id(id)
{
	djDebug() << "DJMatrixCanvasText constructor";
	init();
}
DJMatrixCanvasText::DJMatrixCanvasText( Q3Canvas *canvas, const QString& string, const QFont& font, Type type, quint32 id )
	:Q3CanvasText( string, font, canvas ), m_type(type), m_id(id)
{
	init();
}
DJMatrixCanvasText::~DJMatrixCanvasText()
{
	djDebug() << "DJMatrixCanvasText destructor";
}
int DJMatrixCanvasText::rtti() const
{
	return RTTI;
}
void DJMatrixCanvasText::setFont( const QFont& f )
{
	m_font = f;
	QFont font = f;
	
	QMatrix	matrix	= m_innerMatrix * m_externalMatrix;
	
	int fontSize = font.pointSize()* qMin(matrix.m11(),matrix.m22());
	font.setPointSize( fontSize );
		
	Q3CanvasText::setFont( font );
	
	//adjust the current position
    matrimove( m_x, m_y );
}
void DJMatrixCanvasText::setInnerMatrix( const QMatrix& matrix )
{
	m_innerMatrix	= matrix;
	setFont( m_font );
}
void DJMatrixCanvasText::setExternalMatrix( const QMatrix& matrix )
{
	m_externalMatrix	= matrix;
	setFont( m_font );
}
QSize DJMatrixCanvasText::realSize() const
{
	QMatrix	matrix	= m_innerMatrix * m_externalMatrix;
	qreal scale	= qMin(matrix.m11(),matrix.m22());
	if ( 0 == scale )
		scale	= 1;
	QSize realsize(boundingRect().width(),boundingRect().height());
	realsize	/= scale;
	return realsize;
	/*
	QString string	= text();
	Q3CanvasText	tem( string, m_font, canvas() );
	QRect	rect	= tem.boundingRect();
	return QSize( rect.width(), rect.height() );
	*/
}
void DJMatrixCanvasText::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 DJMatrixCanvasText::init()
{
	m_x	= 0;
	m_y	= 0;
	m_font	= Q3CanvasText::font();
}
