/* ============================================================
 * 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 <QImageReader>
#include "mypainter.h"

mypainter::mypainter ( QWidget * parent )
    : QLabel(parent)
{
	this->setMouseTracking(true);
	this->setScaledContents(true);
	//setAttribute(Qt::WA_NoSystemBackground);
	mydraw.load(":/new/prefix2/Interface/Theme/nullpixmap.png");
	mydraw = mydraw.scaled( 720, 576, Qt::IgnoreAspectRatio, Qt::FastTransformation );
	mydraw.fill(0);
	mycolor.setRed(255);
	mycolor.setGreen(255);
	mycolor.setBlue(255);
	mycolor.setAlpha(255);
	dstcolor.setRed(255);
	dstcolor.setGreen(255);
	dstcolor.setBlue(255);
	dstcolor.setAlpha(255);
	ptsize = 20;
	tool = 1;
	brush = 1;
	alphamask = 0;
}

void mypainter::scaledp(int x, int y)
{
	mydraw = mydraw.scaled( x, y, Qt::IgnoreAspectRatio, Qt::FastTransformation );
	mydraw.fill(0);	
}

void mypainter::leaveEvent(QEvent *event)
{
	if ( brush > 1)
	{
		alphamask = 0;
		this->repaint();
	}
}

void mypainter::mouseMoveEvent ( QMouseEvent * event )
{
    	if (event->buttons() & Qt::LeftButton) 
	{
		paint_x = event->x();
		paint_y = event->y();
		QPainter p;
		p.begin(&mydraw);
		p.setRenderHint(QPainter::Antialiasing,true);
		if ( tool == 1 )
		{
			QPen pen;
			pen.setWidth(ptsize);
			pen.setCapStyle(Qt::RoundCap);
			pen.setJoinStyle(Qt::RoundJoin);
			if ( brush == 1 )
			{
				pen.setBrush(mycolor);
				pen.setWidth(ptsize);
				p.setPen(pen);
				p.drawLine( lastPos.x(), lastPos.y(), paint_x, paint_y );
			}
			else
			{
				QImage wlimgbrush = imgbrush.scaled( ptsize*10, ((float)ptsize/((float)imgbrush.width() / (float)imgbrush.height()))*10, Qt::IgnoreAspectRatio, Qt::FastTransformation );
				for (int y = 0; y < wlimgbrush.height(); y++)
				{
					for (int x = 0; x < wlimgbrush.width(); x++)
					{
						QRgb srcPixel = wlimgbrush.pixel(x, y);
						if (qAlpha(srcPixel) > 0 )
						{
							dstcolor.setAlpha((float)qAlpha(srcPixel)*((float)mycolor.alpha()/(float)255));			
							wlimgbrush.setPixel(x, y, dstcolor.rgba());
						}
					}
				}
				p.setPen(pen);
				p.drawImage ( paint_x-((ptsize*10)/2), paint_y-(wlimgbrush.height()/2), wlimgbrush, 0, 0, -1, -1, Qt::AutoColor );
			}
		}
		else
		{
			QPainter::CompositionMode mode = (QPainter::CompositionMode)2;
			p.setCompositionMode ( mode );
			p.setBackgroundMode(Qt::TransparentMode);
			QPen pen;
			pen.setWidth(ptsize);
			pen.setCapStyle(Qt::RoundCap);
			pen.setJoinStyle(Qt::RoundJoin);
			//pen.setBrush(QColor(0,0,0,0));
			p.setPen(pen);
			p.drawLine( lastPos.x(), lastPos.y(), paint_x, paint_y );
		}
		p.end();

		this->repaint();
		lastPos = event->pos();
	}
	else
	{
		if ( brush > 1)
		{
			alphamask = 1;
		}
		paint_x = event->x();
		paint_y = event->y();
		this->repaint();
	}
}

void mypainter::mousePressEvent ( QMouseEvent * event ) 
{
    	if (event->buttons() & Qt::LeftButton) 
	{
		paint_x = event->x();
		paint_y = event->y();
		QPainter p;
		p.begin(&mydraw);
		p.setRenderHint(QPainter::Antialiasing,true);
		if ( tool == 1 )
		{
			QPen pen;
			pen.setWidth(ptsize);
			pen.setCapStyle(Qt::RoundCap);
			pen.setJoinStyle(Qt::RoundJoin);
			if ( brush == 1 )
			{
				pen.setBrush(mycolor);
				pen.setWidth(ptsize);
				p.setPen(pen);
				p.drawPoint ( paint_x, paint_y );
			}
			else
			{
				QImage wlimgbrush = imgbrush.scaled( ptsize*10, ((float)ptsize/((float)imgbrush.width() / (float)imgbrush.height()))*10, Qt::IgnoreAspectRatio, Qt::FastTransformation );
				for (int y = 0; y < wlimgbrush.height(); y++)
				{
					for (int x = 0; x < wlimgbrush.width(); x++)
					{
						QRgb srcPixel = wlimgbrush.pixel(x, y);
						if (qAlpha(srcPixel) > 0 )
						{
							dstcolor.setAlpha((float)qAlpha(srcPixel)*((float)mycolor.alpha()/(float)255));			
							wlimgbrush.setPixel(x, y, dstcolor.rgba());
						}
					}
				}
				p.setPen(pen);
				p.drawImage ( paint_x-((ptsize*10)/2), paint_y-(wlimgbrush.height()/2), wlimgbrush, 0, 0, -1, -1, Qt::AutoColor );
			}
		}
		else
		{
			QPainter::CompositionMode mode = (QPainter::CompositionMode)2;
			p.setCompositionMode ( mode );
			p.setBackgroundMode(Qt::TransparentMode);
			QPen pen;
			pen.setWidth(ptsize);
			pen.setCapStyle(Qt::RoundCap);
			pen.setJoinStyle(Qt::RoundJoin);
			pen.setBrush(QColor(0,0,0,0));
			p.setPen(pen);
			p.drawPoint ( paint_x, paint_y );
		}
		p.end();

		this->repaint();
		lastPos = event->pos();
	}
}

void mypainter::paintEvent(QPaintEvent *)
{
	QPainter p;
	p.begin(this);
	p.drawPixmap ( 0, 0, img2fond );
	p.drawImage ( 0, 0, mydraw );
	QColor nullc(0,0,0,0);
	if ( alphamask == 1 && tool == 1 )
	{
		QImage wlimgbrush = imgbrush.scaled( ptsize*10, ((float)ptsize/((float)imgbrush.width() / (float)imgbrush.height()))*10, Qt::IgnoreAspectRatio, Qt::FastTransformation );
		for (int y = 0; y < wlimgbrush.height(); y++)
		{
			for (int x = 0; x < wlimgbrush.width(); x++)
			{
				QRgb srcPixel = wlimgbrush.pixel(x, y);
				if (qAlpha(srcPixel) > 0 )
				{
					dstcolor.setAlpha((float)qAlpha(srcPixel)*((float)mycolor.alpha()/(float)255));			
					wlimgbrush.setPixel(x, y, dstcolor.rgba());
				}
			}
		}
		p.drawImage ( paint_x-((ptsize*10)/2), paint_y-(wlimgbrush.height()/2), wlimgbrush, 0, 0, -1, -1, Qt::AutoColor );
	}
	if ( brush < 2 )
	{
		QPen pen;
 		pen.setStyle(Qt::DashDotLine);
		pen.setWidth(1);
 		pen.setCapStyle(Qt::RoundCap);
 		pen.setJoinStyle(Qt::RoundJoin);
		p.setPen(pen);
		if ( tool != 2 )
		{
			p.setBrush(mycolor);
		}
		p.drawEllipse ( paint_x-(ptsize/2), paint_y-(ptsize/2), ptsize, ptsize );
	}
	p.end();
}

