/* ============================================================
 * File  : tetrahedron.cpp
 * Author: Stephane Gibault
 * Date  : Sun Feb 4 2007
 * Description :
 *
 * 
 *  (C) 2007 by Stephane Gibault
 *
 * 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, 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.
 *
 * ============================================================ */
#include <QtGui>
#include "texteditor.h"

texteditor::texteditor ( QWidget * parent )
    : QLabel(parent)
{
	this->setMouseTracking(true);
	this->setScaledContents(true);
	this->setAutoFillBackground(false);
	posdep.setX(9999);
	posdep.setY(9999);
	posend.setX(9999);
	posend.setY(9999);
	textx = 100;
	texty = 100;
	oldx = 100;
	oldy = 100;
	oldmx = -1;
	oldmy = -1;
	size = 12;
	isdragging = 0;
	mycolor.setRgb(255,255,255,255);
}

void texteditor::setMyText(QString valtxt)
{
	current_text = valtxt;
	QFontMetrics fm(myfont);
	myrect = fm.size ( Qt::TextExpandTabs, current_text, 0, 0 );
	this->repaint();
}

void texteditor::mousePressEvent ( QMouseEvent * event ) 
{
	//textx = event->x();
	//texty = event->y();
	//oldx = event->x();
	//oldy = event->y();

	//if ( oldmx < 0 )
	//{
	oldmx = event->x();
	oldmy = event->y();
	//}

	//qDebug() << oldx << " " << oldy << " " << event->x() << " " << event->y();
	isdragging = 1;
}

void texteditor::mouseReleaseEvent ( QMouseEvent * )
{
	isdragging = 0;
	oldx = textx;
	oldy = texty;
	//oldmx = event->x();
	//oldmy = event->y();
	/*oldmx = -1;
	oldmy = -1;*/
}

void texteditor::mouseMoveEvent ( QMouseEvent * event )
{
	if ( isdragging == 1 )
	{
		textx = oldx + (event->x()-oldmx);
		texty = oldy + (event->y()-oldmy);
		if ( textx < 0 )
		{
			textx = 0;
		}
		if ( textx+myrect.width() > size_dia_x )
		{
			textx = size_dia_x-myrect.width();
		}
		if ( texty < 0 )
		{
			texty = 0;
		}
		if ( texty > size_dia_y-myrect.height() )
		{
			texty = size_dia_y-myrect.height();
		}
	}
	this->repaint();
}

void texteditor::paintEvent(QPaintEvent *)
{
	QFontMetrics fm(myfont);
	QPainter p;
	p.begin(this);
	p.setPen (mycolor);
	p.setFont(myfont);
	p.drawPixmap ( 0,0,img2fond );
	p.fillRect ( 0, texty, size_dia_x, (current_text.count(QRegExp("\n")) + 1) * (fm.height()+leading), QBrush( QColor(backcolor.red(),backcolor.green(),backcolor.blue(),opacity), Qt::SolidPattern ) ); //size
	if ( alignement == 0 )
	{
		p.drawText ( QRect(textx,texty,myrect.width(),size_dia_y), (Qt::AlignLeft | Qt::AlignTop | Qt::TextExpandTabs),current_text );
	}
	else
	{
		if ( alignement == 1 )
		{
			p.drawText ( QRect(textx,texty,myrect.width(),size_dia_y), (Qt::AlignHCenter | Qt::AlignTop | Qt::TextExpandTabs),current_text );
		}
		else
		{
			if ( alignement == 2 )
			{
				p.drawText ( QRect(textx,texty,myrect.width(),size_dia_y), (Qt::AlignJustify | Qt::AlignTop | Qt::TextExpandTabs),current_text );
			}
			else
			{
				p.drawText ( QRect(textx,texty,myrect.width(),size_dia_y), (Qt::AlignRight | Qt::AlignTop | Qt::TextExpandTabs),current_text );
			}
		}
	}
	if ( current_text != "" )
	{
		p.drawPixmap(posdep,(QPixmap(":/new/prefix2/Interface/Theme/flag_green.png")));
		p.drawPixmap(posend,(QPixmap(":/new/prefix2/Interface/Theme/flag_red.png")));
	}
	p.end();
}


