
#include <QtGui>

#include "imageviewer.h"

ImageViewer::ImageViewer()
{
	imageLabel	= new QLabel();
	imageLabel->setBackgroundRole(QPalette::Base);
	imageLabel->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
	imageLabel->setScaledContents(true);

	my_widget = new MyWidget();

	scrollArea = new QScrollArea();
	scrollArea->setBackgroundRole(QPalette::Dark);
	//scrollArea->setWidget(imageLabel);
	scrollArea->setWidget(my_widget);
	setCentralWidget(scrollArea);

	createActions();
	createMenus();

	setWindowTitle(tr("Image Viewer"));
	resize(600, 600);

	farbe	= 0x00FF0000;
	reader	= NULL;
	//my_image = new QImage(2000 , 1000, QImage::Format_RGB32);
	//my_image->fill(farbe);

}

ImageViewer::~ImageViewer()
{

}


void ImageViewer::open()
{
	char *path;

	QString fileName = QFileDialog::getOpenFileName(this, tr("Open File"), QDir::currentPath());

	path = fileName.toLocal8Bit().data();

	if(!fileName.isEmpty()){
		reader = create_Reader_01(path);
		if(reader != NULL){
			updateImage();
			/**
			QImage image(fileName);
			if(image.isNull()){
				QMessageBox::information(this, tr("Image Viewer"), tr("Cannot loade %1.").arg(fileName));
				return;
			}

			imageLabel->setPixmap(QPixmap::fromImage(image));
			scaleFactor = 1.0;

			printAct->setEnabled(true);
			fitToWindowAct->setEnabled(true);

			updateActions();
			if(!fitToWindowAct->isChecked()){
				imageLabel->adjustSize();
			}
			**/
		}else{
			QMessageBox::information(this, tr("Image Viewer"), tr("Cannot loade %1.").arg(fileName));
			return;
		}
	}
}

void ImageViewer::updateImage()
{
	my_widget->set_reader(reader);
	/*
	unsigned int width, height;

	if(reader != NULL){

		width	= get_ZoomWidth(reader);
		height	= get_ZoomLength(reader);
	}
	*/
}

/*
void ImageViewer::open()
{
	uchar *data;
	int bytes, bytes_per_line, height, height_pos, width, width_pos;
	unsigned int *data1;

	if(farbe == 0){
		farbe = 0x00FF0000;

	}else{
		farbe >>= 8;

	}
	//my_image->fill(farbe);

	data 			= my_image->bits();
	bytes 			= my_image->byteCount();
	bytes_per_line	= my_image->bytesPerLine();
	height 			= my_image->height();
	width			= bytes_per_line / 4;
	for(height_pos = 0; height_pos < height / 2; height_pos += 1){
		data1 = (unsigned int*)(data + bytes_per_line * height_pos);
		for(width_pos = 0; width_pos < width / 2; width_pos +=1 ){
			data1[width_pos] = farbe;
		}
	}


	imageLabel->setPixmap(QPixmap::fromImage(*my_image));
	scaleFactor = 1.0;

	printAct->setEnabled(true);
	fitToWindowAct->setEnabled(true);

	updateActions();
	if(!fitToWindowAct->isChecked()){
		imageLabel->adjustSize();
	}
}
*/

void ImageViewer::print()
{
    Q_ASSERT(imageLabel->pixmap());
#ifndef QT_NO_PRINTER
    QPrintDialog dialog(&printer, this);
    if (dialog.exec()) {
        QPainter painter(&printer);
        QRect rect = painter.viewport();
        QSize size = imageLabel->pixmap()->size();
        size.scale(rect.size(), Qt::KeepAspectRatio);
        painter.setViewport(rect.x(), rect.y(), size.width(), size.height());
        painter.setWindow(imageLabel->pixmap()->rect());
        painter.drawPixmap(0, 0, *imageLabel->pixmap());
    }
#endif
}

void ImageViewer::zoomIn()
{
	my_widget->zoom_in();
	//scaleImage(1.25);
}

void ImageViewer::zoomOut()
{
	my_widget->zoom_out();
	//scaleImage(0.8);
}

void ImageViewer::normalSize()
{
	imageLabel->adjustSize();
	scaleFactor = 1.0;
}

void ImageViewer::fitToWindow()
{
	bool fitToWindow = fitToWindowAct->isChecked();
	scrollArea->setWidgetResizable(fitToWindow);
	if(!fitToWindow){
		normalSize();
	}
	updateActions();
}

void ImageViewer::about()
{
    QMessageBox::about(this, tr("About Image Viewer"),
            tr("<p>The <b>Image Viewer</b> example shows how to combine QLabel "
               "and QScrollArea to display an image. QLabel is typically used "
               "for displaying a text, but it can also display an image. "
               "QScrollArea provides a scrolling view around another widget. "
               "If the child widget exceeds the size of the frame, QScrollArea "
               "automatically provides scroll bars. </p><p>The example "
               "demonstrates how QLabel's ability to scale its contents "
               "(QLabel::scaledContents), and QScrollArea's ability to "
               "automatically resize its contents "
               "(QScrollArea::widgetResizable), can be used to implement "
               "zooming and scaling features. </p><p>In addition the example "
               "shows how to use QPainter to print an image.</p>"));
}

void ImageViewer::createActions()
{
	openAct = new QAction(tr("&Open..."), this);
	openAct->setShortcut(tr("Ctrl+O"));
	connect(openAct, SIGNAL(triggered()), this, SLOT(open()));

	printAct = new QAction(tr("&Print..."), this);
	printAct->setShortcut(tr("Ctrl+P"));
	printAct->setEnabled(false);
	connect(printAct, SIGNAL(triggered()), this, SLOT(print()));

	exitAct = new QAction(tr("E&xit"), this);
	exitAct->setShortcut(tr("Ctrl+Q"));
	connect(exitAct, SIGNAL(triggered()), this, SLOT(close()));

	zoomInAct = new QAction(tr("Zoom &In (25%)"), this);
	zoomInAct->setShortcut(tr("Ctrl++"));
	zoomInAct->setEnabled(true);
	connect(zoomInAct, SIGNAL(triggered()), this, SLOT(zoomIn()));

	zoomOutAct = new QAction(tr("Zoom &Out (25%)"), this);
	zoomOutAct->setShortcut(tr("Ctrl+-"));
	zoomOutAct->setEnabled(true);
	connect(zoomOutAct, SIGNAL(triggered()), this, SLOT(zoomOut()));

	normalSizeAct = new QAction(tr("&Normal Size"), this);
	normalSizeAct->setShortcut(tr("Ctrl+S"));
	normalSizeAct->setEnabled(false);
	connect(normalSizeAct, SIGNAL(triggered()), this, SLOT(normalSize()));

	fitToWindowAct = new QAction(tr("&Fit to Window"), this);
	fitToWindowAct->setEnabled(false);
	fitToWindowAct->setCheckable(true);
	fitToWindowAct->setShortcut(tr("Ctrl+F"));
	connect(fitToWindowAct, SIGNAL(triggered()), this, SLOT(fitToWindow()));

	aboutAct = new QAction(tr("&About"), this);
	connect(aboutAct, SIGNAL(triggered()), this, SLOT(about()));

	aboutQtAct = new QAction(tr("About &Qt"), this);
	connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt()));

}

void ImageViewer::createMenus()
{
    fileMenu = new QMenu(tr("&File"), this);
    fileMenu->addAction(openAct);
    fileMenu->addAction(printAct);
    fileMenu->addSeparator();
    fileMenu->addAction(exitAct);

    viewMenu = new QMenu(tr("&View"), this);
    viewMenu->addAction(zoomInAct);
    viewMenu->addAction(zoomOutAct);
    viewMenu->addAction(normalSizeAct);
    viewMenu->addSeparator();
    viewMenu->addAction(fitToWindowAct);

    helpMenu = new QMenu(tr("&Help"), this);
    helpMenu->addAction(aboutAct);
    helpMenu->addAction(aboutQtAct);

    menuBar()->addMenu(fileMenu);
    menuBar()->addMenu(viewMenu);
    menuBar()->addMenu(helpMenu);
}

void ImageViewer::updateActions()
 {
     zoomInAct->setEnabled(!fitToWindowAct->isChecked());
     zoomOutAct->setEnabled(!fitToWindowAct->isChecked());
     normalSizeAct->setEnabled(!fitToWindowAct->isChecked());
 }

void ImageViewer::scaleImage(double factor)
{
    Q_ASSERT(imageLabel->pixmap());
    scaleFactor *= factor;
    imageLabel->resize(scaleFactor * imageLabel->pixmap()->size());

    adjustScrollBar(scrollArea->horizontalScrollBar(), factor);
    adjustScrollBar(scrollArea->verticalScrollBar(), factor);

    zoomInAct->setEnabled(scaleFactor < 3.0);
    zoomOutAct->setEnabled(scaleFactor > 0.333);
}

void ImageViewer::adjustScrollBar(QScrollBar *scrollBar, double factor)
{
    scrollBar->setValue(int(factor * scrollBar->value()
                            + ((factor - 1) * scrollBar->pageStep()/2)));
}
