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

wheel::wheel ( QWidget * parent )
    : QLabel(parent)
{
	myparent = parent;
	this->setMouseTracking (true);
	this->setCursor(Qt::CrossCursor);
	red = 0;
	green = 0;
	blue = 0;
	luma = 0;
}

void wheel::setColor(int pred, int pgreen, int pblue)
{
	red = pred;
	green = pgreen;
	blue = pblue;
	QPainter p;
	p.begin(&wlimg);
	p.setRenderHint(QPainter::Antialiasing,true);
	p.setPen(QColor(0,0,0));
	p.drawEllipse(QRect(0,0,this->width(),this->height()));
	p.setPen(Qt::NoPen);
	p.setBrush(QColor(red,green,blue));
	p.drawEllipse(QRect(0,0,10,10));
	p.end();
	this->setPixmap(QPixmap::fromImage ( wlimg, Qt::AutoColor ));
}

void wheel::mouseMoveEvent(QMouseEvent *event)
{
	if (!(event->buttons() & Qt::LeftButton)) return;
	if ( event->x() > 0 && event->x() < 150 && event->y() > 0 && event->y() < 150)
	{
		QRgb select_color = wlimg.pixel(event->x(),event->y());
		red = qRed(select_color);
		green = qGreen(select_color);
		blue = qBlue(select_color);
		//
		QPainter p;
		p.begin(&wlimg);
		//p.setRenderHint(QPainter::Antialiasing,true);
		p.setPen(Qt::NoPen);
		p.setBrush(QColor(select_color));
		p.drawEllipse(QRect(0,0,10,10));
		p.end();
		this->setPixmap(QPixmap::fromImage ( wlimg, Qt::AutoColor ));
		//
		emit gochange();
	}	
}

void wheel::mousePressEvent( QMouseEvent *event )
{
	if (!(event->buttons() & Qt::LeftButton)) return;
	if ( event->x() > 0 && event->x() < 150 && event->y() > 0 && event->y() < 150)
	{
		QRgb select_color = wlimg.pixel(event->x(),event->y());
		red = qRed(select_color);
		green = qGreen(select_color);
		blue = qBlue(select_color);
		//
		QPainter p;
		p.begin(&wlimg);
		//p.setRenderHint(QPainter::Antialiasing,true);
		p.setPen(Qt::NoPen);
		p.setBrush(QColor(select_color));
		p.drawEllipse(QRect(0,0,10,10));
		p.end();
		this->setPixmap(QPixmap::fromImage ( wlimg, Qt::AutoColor ));
		//
		emit gochange();
	}	
}

void wheel::wheelEvent(QWheelEvent *event)
{
	if ( event->delta() > 0 )
	{
		if ( luma < 255 )
		{
			luma = luma + 5;
		}
	}
	else
	{
		if ( luma > -255 )
		{
			luma = luma - 5;
		}
	}
	wlimg = tmpwheel;
	for (int y = 0; y < wlimg.height(); y++)
	{
		for (int x = 0; x < wlimg.width(); x++)
		{
			QRgb srcPixel = wlimg.pixel(x, y);
			trtcolor = srcPixel;
			//
			if (qAlpha(srcPixel) > 200 )
			{
				if ( luma > 0 )
				{
					if ( trtcolor.red() < 255 - luma)
					{
						trtcolor.setRed(trtcolor.red() + luma);
					}
					else
					{
						trtcolor.setRed(255);
					}
					//
					if ( trtcolor.green() < 255 - luma)
					{
						trtcolor.setGreen(trtcolor.green() + luma);
					}
					else
					{
						trtcolor.setGreen(255);
					}
					//
					if ( trtcolor.blue() < 255 - luma)
					{
						trtcolor.setBlue(trtcolor.blue() + luma);
					}
					else
					{
						trtcolor.setBlue(255);
					}
				}
				else
				{
					if ( trtcolor.red() > (luma*-1))
					{
						trtcolor.setRed(trtcolor.red() + luma);
					}
					else
					{
						trtcolor.setRed(0);
					}
					//
					if ( trtcolor.green() > (luma*-1))
					{
						trtcolor.setGreen(trtcolor.green() + luma);
					}
					else
					{
						trtcolor.setGreen(0);
					}
					//
					if ( trtcolor.blue() > (luma*-1))
					{
						trtcolor.setBlue(trtcolor.blue() + luma);
					}
					else
					{
						trtcolor.setBlue(0);
					}
				}
				wlimg.setPixel(x, y, trtcolor.rgb());
			}
		}
	}
	//
	QPainter p;
	p.begin(&wlimg);
	p.setRenderHint(QPainter::Antialiasing,true);
	p.setPen(QColor(0,0,0));
	p.drawEllipse(QRect(0,0,this->width(),this->height()));
	p.setPen(Qt::NoPen);
	p.setBrush(QColor(red,green,blue));
	p.drawEllipse(QRect(0,0,10,10));
	p.end();
	//
	this->setPixmap(QPixmap::fromImage ( wlimg, Qt::AutoColor ));
}

void wheel::mouseReleaseEvent ( QMouseEvent * event )
{
	emit finchange();
}

