/*
 * Countours.cpp
 *
 *  Created on: 26.12.2010
 *      Author: dgrat
 */

#include <iostream>
#include "Contours.h"
#include "ImageC.h"

ContourViewer::ContourViewer(QWidget *parent) : CViewerPrivate(parent) {
	m_lblMin = new QLabel("Bias - Minimum: ");
	m_lblMax = new QLabel("Bias - Maximum: ");
	m_pNrColLbl = new QLabel("No colonies found yet");

	m_MinSizeSl.setMinimum(0);
	m_MinSizeSl.setMaximum(99);
	m_MinSizeSl.setSingleStep(1);
	m_MinSizeSl.setOrientation(Qt::Horizontal);
	m_MaxSizeSl.setMinimum(512);
	m_MaxSizeSl.setMaximum(16*512);
	m_MaxSizeSl.setSingleStep(256);
	m_MaxSizeSl.setOrientation(Qt::Horizontal);
	m_MaxSizeSB.setMinimum(512);
	m_MaxSizeSB.setMaximum(16*512);
	m_MaxSizeSB.setSingleStep(256);

	m_MinSizeSl.setValue(1);
	m_MaxSizeSl.setValue(16*512);
	m_MinSizeSB.setValue(1);
	m_MaxSizeSB.setValue(16*512);

	m_QImg = QImage(640, 640, QImage::Format_RGB32);
	m_pImageLbl = new QLabel(0);
	m_pImageLbl->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
	m_pImageLbl->setScaledContents(true);
	m_pImageLbl->setPixmap(QPixmap::fromImage(m_QImg));

	QScrollArea *scrollArea = new QScrollArea;
	scrollArea->setBackgroundRole(QPalette::Dark);
	scrollArea->setWidget(m_pImageLbl);
	QGridLayout *pMainLayout = new QGridLayout(this);
	pMainLayout->addWidget(m_lblMin, 0, 0);
	pMainLayout->addWidget(&m_MinSizeSl, 0, 1);
	pMainLayout->addWidget(&m_MinSizeSB, 0, 2);
	pMainLayout->addWidget(m_lblMax, 1, 0);
	pMainLayout->addWidget(&m_MaxSizeSl, 1, 1);
	pMainLayout->addWidget(&m_MaxSizeSB, 1, 2);
	pMainLayout->addWidget(scrollArea, 2, 0, 1, 3);
	pMainLayout->addWidget(m_pNrColLbl, 3, 0, 1, 3);

	// Display Funktion als Thread
	QObject::connect(&m_MinSizeSl, SIGNAL(valueChanged(int)), this, SLOT(slDisplayImage() ) );
	QObject::connect(&m_MaxSizeSl, SIGNAL(valueChanged(int)), this, SLOT(slDisplayImage() ) );

	QObject::connect(&m_MinSizeSl, SIGNAL(valueChanged(int)), &m_MinSizeSB, SLOT(setValue(int) ) );
	QObject::connect(&m_MaxSizeSl, SIGNAL(valueChanged(int)), &m_MaxSizeSB, SLOT(setValue(int) ) );
	QObject::connect(&m_MinSizeSB, SIGNAL(valueChanged(int)), &m_MinSizeSl, SLOT(setValue(int) ) );
	QObject::connect(&m_MaxSizeSB, SIGNAL(valueChanged(int)), &m_MaxSizeSl, SLOT(setValue(int) ) );
}

ContourViewer::~ContourViewer() {

}

void ContourViewer::slDisplayImage() {
	this->slChangeConfig(m_MinSizeSl.value(), m_MaxSizeSl.value() );
	m_QImg = QImage( (const uchar *)this->GetImg()->imageData,
							this->GetImg()->width,
							this->GetImg()->height,
							QImage::Format_RGB888);
	m_pImageLbl->setPixmap( QPixmap::fromImage(m_QImg) );
	QString sNrColLbl = QString::number(this->GetNrColonies()) + " surfaces";
	m_pNrColLbl->setText( sNrColLbl );
}

/*
 * PRIVATE
 */
void CViewerPrivate::slChangeConfig(int iVal1, int iVal2) {
	if(m_pArtImg)
		cvReleaseImage( &m_pArtImg );
	if(pContourStorage)
		cvReleaseMemStorage( &pContourStorage );

	m_iNrColonies = -1;
	m_pArtImg = DrawCountours(m_pContour, cvGetSize(m_pImg), iVal1, iVal2, m_iNrColonies);
}

void CViewerPrivate::slSetImg(IplImage *pImg) {
	if(pImg == NULL)
		return;

	if(m_pArtImg)
		cvReleaseImage( &m_pArtImg );
	if(pContourStorage)
		cvReleaseMemStorage( &pContourStorage );

	m_pImg = pImg;
	m_pContour = FindArtifacts(m_pImg, pContourStorage);
}


/*
 * THREAD
 */
/*
DisplayThread::DisplayThread(CViewerPrivate *pPriv, int iMin, int iMax) : m_pParent(pPriv) {
	assert( pPriv != NULL );
	m_iMin = iMin;
	m_iMax = iMax;
}

void DisplayThread::run() {
	while(true) {
		m_pParent->slChangeConfig(m_iMin, m_iMax );
		QImage m_pImage = QImage( (const uchar *)m_pParent->GetImg()->imageData,
								m_pParent->GetImg()->width,
								m_pParent->GetImg()->height,
								QImage::Format_RGB888);
		QString NrColLbl = QString::number(m_pParent->GetNrColonies()) + " surfaces";
		emit ImageProcessed(m_pImage, NrColLbl);

		mutex.lock();
		condition.wait(&mutex);
		mutex.unlock();
	}
}

void DisplayThread::slChangeMax(int val) {
	SetConfig(m_iMin, val);
}

void DisplayThread::slChangeMin(int val) {
	SetConfig(val, m_iMax);
}

void DisplayThread::SetConfig(int iMin, int iMax) {
	QMutexLocker locker(&mutex);

	m_iMin = iMin;
	m_iMax = iMax;

	if (!isRunning()) {
		start(LowPriority);
	} else {
		restart = true;
		condition.wakeOne();
	}
}
*/
