/*
 * MyWidget.cpp
 *
 *  Created on: 13.08.2010
 *      Author: uwe
 */

#include "mywidget.h"
#include <QtGui>

MyWidget::MyWidget() {
	size.setHeight(0);
	size.setWidth(0);
	reader = NULL;

	this->setMinimumSize(size);
	farbe = 0x00FF0000;

	//my_image = new QImage(1950 , 1100, QImage::Format_RGB32);
	my_image = new QImage(1950 , 1100, QImage::Format_RGB16);
	my_image->fill(farbe);

	current_zoom = 1.0f;
}

MyWidget::~MyWidget() {
	// TODO Auto-generated destructor stub
}

void MyWidget::paintEvent(QPaintEvent *event)
{
	const QRect target = event->rect();
	const QRect source(0,0, target.width(), target.height());
	unsigned int *data, scanline;
	unsigned short *short_data;
	unsigned width, height, x0, y0;

	width	= 0;
	height	= 0;
	x0		= 0;
	y0		= 0;
	data	= NULL;
	scanline = 0;

	/*
	if(farbe == 0){
		farbe = 0x00ff0000;
	}else{
		farbe >>= 1;
	}
	my_image->fill(farbe);
	QColor hcolor(127, 0, 127);
	*/

	if(target.width() > 0 && target.height() > 0 && target.x() >= 0 && target.y() >= 0){
		if(target.width() < my_image->width() && target.height() < my_image->height() ){

			width 		= (unsigned int)target.width();
			height		= (unsigned int)target.height();
			x0			= (unsigned int) target.x();
			y0			= (unsigned int) target.y();
			if(my_image->format() == QImage::Format_RGB32){
				data 		= (unsigned int*)	my_image->bits();
				scanline	= (unsigned int)	my_image->bytesPerLine() / 4;

				get_zoom_image(	reader,
								data,
								scanline,
								width,
								height,
								0,
								0,
								x0,
								y0);
			}else if(my_image->format() == QImage::Format_RGB16){
				short_data 		= (unsigned short*)	my_image->bits();
				scanline	= (unsigned int)	my_image->bytesPerLine() / 2;

				get_zoom_image_02(	reader,
									short_data,
									scanline,
									width,
									height,
									0,
									0,
									x0,
									y0);

			}
			QPainter painter(this);
			painter.drawPixmap(target,QPixmap::fromImage(*my_image), source);
		}
	}
}

void MyWidget::set_reader(void *reader)
{
	temp_reader = reader;
	this->start();
	/*
	unsigned int width, height;
	this->reader = NULL;
	int zoomsize[2];
	if(reader != NULL){
		this->reader = reader;
		set_Zoom(reader, current_zoom, zoomsize);
		width 	= zoomsize[0];
		height	= zoomsize[1];
		//width	= get_ZoomWidth(reader);
		//height	= get_ZoomLength(reader);
		size.setHeight(height);
		size.setWidth(width);
		this->resize(size);
		//this->setMinimumSize(size);
	}
	*/
}
void MyWidget::run(){
	set_reader();

}

void MyWidget::set_reader()
{
	if(this->reader != NULL){
		delete_Reader(this->reader);
		this->reader = NULL;
	}
	this->reader = this->temp_reader;
	unsigned int width, height;
	int zoomsize[2];
	if(reader != NULL){
		set_Zoom(reader, current_zoom, zoomsize);
		width 	= zoomsize[0];
		height	= zoomsize[1];
		size.setHeight(height);
		size.setWidth(width);
		this->resize(size);
		update();
	}
}

void MyWidget::zoom_in()
{
	float temp_current_zoom = current_zoom;

	temp_current_zoom += 0.2f;

	if(temp_current_zoom  > 3.f){
		current_zoom = 3.f;
	}else{
		current_zoom = temp_current_zoom;
	}
	update_image();
}

void MyWidget::zoom_out()
{
	float temp_current_zoom = current_zoom;

	temp_current_zoom -= 0.2f;

	if(temp_current_zoom  < 0.05f){
		current_zoom = 0.05f;
	}else{
		current_zoom = temp_current_zoom;
	}
	update_image();
}

void MyWidget::update_image()
{
	int zoomsize[2];

	if(reader != NULL){
		set_Zoom(reader, current_zoom, zoomsize);
		size.setHeight(zoomsize[1]);
		size.setWidth(zoomsize[0]);
		this->resize(size);
		//this->setMinimumSize(size);
	}
	update();
}
