#include <QtGui>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QDebug>
#include <QTimer>
#include "adc.h"
#include "ltc1090.h"

Dialog::Dialog(QWidget *parent) : QDialog(parent)
{
	kanal1 = new QLCDNumber(4);
	kanal2 = new QLCDNumber(4);
	kanal3 = new QLCDNumber(4);
	kanal4 = new QLCDNumber(4);
	kanal5 = new QLCDNumber(4);
	kanal6 = new QLCDNumber(4);
	kanal7 = new QLCDNumber(4);
	kanal8 = new QLCDNumber(4);

	klab1 = new QLabel(tr("V"));
	klab2 = new QLabel(tr("V"));
	klab3 = new QLabel(tr("V"));
	klab4 = new QLabel(tr("V"));
	klab5 = new QLabel(tr("V"));
	klab6 = new QLabel(tr("V"));
	klab7 = new QLabel(tr("V"));
	klab8 = new QLabel(tr("V"));
		
	QPushButton *endeButton = new QPushButton(tr("&Beenden"));
	ok = new QPushButton(tr("Messung &starten"));
	exit = new QPushButton(tr("&Messung beenden"));

	connect(endeButton, SIGNAL(clicked()), this, SLOT(close()));
	connect(ok, SIGNAL(clicked()), this, SLOT(measure()));
	connect(ok, SIGNAL(clicked()), this, SLOT(setTimer()));
	connect(exit, SIGNAL(clicked()), this, SLOT(stopMeasure()));
	
	timer = new QTimer(this);
	
	QGridLayout *layout = new QGridLayout;
	QHBoxLayout *button = new QHBoxLayout;
	QVBoxLayout *main = new QVBoxLayout(this);

	main->addLayout(layout);
	main->addStretch(1);
	main->addLayout(button);

     	layout->setColumnStretch(1, 1);
	layout->setColumnMinimumWidth(1, 1);

	layout->addWidget(LTC(), 0, 0);
	layout->addWidget(Kanal(), 1, 0);
	
	button->addStretch();
	button->addWidget(ok);
	button->addWidget(exit);
	button->addWidget(endeButton);

	setWindowTitle(tr("AD-Wandler LTC 1090/1290"));
}

QGroupBox *Dialog::LTC()
{
	QGroupBox *groupBox = new QGroupBox(tr("LTC auswählen"));

	LTC1090 = new QRadioButton(tr("LTC 1090"));
	LTC1290 = new QRadioButton(tr("LTC 1290"));
	
	QVBoxLayout *radio = new QVBoxLayout;

	radio->addWidget(LTC1090);
	radio->addWidget(LTC1290);

	LTC1090->setChecked(true);
	
	groupBox->setLayout(radio);

	return groupBox;	
}

QGroupBox *Dialog::Kanal()
{	
	QGroupBox *kanal = new QGroupBox(tr("Kanalwahl!"));
	
	Kanal1 = new QCheckBox(tr("Kanal 1"));
	kanal1->setPalette(QPalette(QColor(0,0,0)));
	
	Kanal2 = new QCheckBox(tr("Kanal 2"));
	kanal2->setPalette(QPalette(QColor(0,0,0)));

	Kanal3 = new QCheckBox(tr("Kanal 3"));
	kanal3->setPalette(QPalette(QColor(0,0,0)));

	Kanal4 = new QCheckBox(tr("Kanal 4"));
	kanal4->setPalette(QPalette(QColor(0,0,0)));

	Kanal5 = new QCheckBox(tr("Kanal 5"));
	kanal5->setPalette(QPalette(QColor(0,0,0)));

	Kanal6 = new QCheckBox(tr("Kanal 6"));
	kanal6->setPalette(QPalette(QColor(0,0,0)));

	Kanal7 = new QCheckBox(tr("Kanal 7"));
	kanal7->setPalette(QPalette(QColor(0,0,0)));

	Kanal8 = new QCheckBox(tr("Kanal 8"));
	kanal8->setPalette(QPalette(QColor(0,0,0)));
	
	QGridLayout *check = new QGridLayout;

	check->addWidget(Kanal1, 0, 0);
	check->addWidget(kanal1, 0, 1);
	check->addWidget(klab1, 0, 2);

	check->addWidget(Kanal2, 1, 0);
	check->addWidget(kanal2, 1, 1);
	check->addWidget(klab2, 1, 2);

	check->addWidget(Kanal3, 2, 0);
	check->addWidget(kanal3, 2, 1);
	check->addWidget(klab3, 2, 2);

	check->addWidget(Kanal4, 3, 0);
	check->addWidget(kanal4, 3, 1);
	check->addWidget(klab4, 3, 2);

	check->addWidget(Kanal5, 4, 0);
	check->addWidget(kanal5, 4, 1);
	check->addWidget(klab5, 4, 2);

	check->addWidget(Kanal6, 5, 0);
	check->addWidget(kanal6, 5, 1);
	check->addWidget(klab6, 5, 2);

	check->addWidget(Kanal7, 6, 0);
	check->addWidget(kanal7, 6, 1);
	check->addWidget(klab7, 6, 2);

	check->addWidget(Kanal8, 7, 0);
	check->addWidget(kanal8, 7, 1);
	check->addWidget(klab8, 7, 2);

	kanal->setLayout(check);
	return kanal;
}

void Dialog::measure()
{
	if(LTC1090->isChecked())
	{
		Ltc1090 adc(5.000);	// set to correct Refernce Voltage
		int i, cnt;

		i = adc.init(0);
	
		if(i!=0)
		{
			QMessageBox msg(QObject::tr("Ausgabe"), ("Error"),
			QMessageBox::NoIcon, 
			QMessageBox::Ok|QMessageBox::Default,
			QMessageBox::NoButton, QMessageBox::NoButton);
		}
	
		if(Kanal1->checkState())
		{	
			for (cnt = 0; cnt < 5 ; cnt++)
			{
				kanal1->display(adc.readVol(0));
			}
		}
		else
			kanal1->display(0);

		if(Kanal2->checkState())
		{
			for (cnt = 0; cnt < 5 ; cnt++)
			{
				kanal2->display(adc.readVol(1));
			}
		}
		else
			kanal2->display(0);

		if(Kanal3->checkState())
		{	
			for (cnt = 0; cnt < 5 ; cnt++)
			{
				kanal3->display(adc.readVol(2));
			}
		}
		else
			kanal3->display(0);

		if(Kanal4->checkState())
		{
			for (cnt = 0; cnt < 5 ; cnt++)
			{
				kanal4->display(adc.readVol(3));
			}
		}
		else
			kanal4->display(0);

		if(Kanal5->checkState())
		{
			for (cnt = 0; cnt < 5 ; cnt++)
			{
				kanal5->display(adc.readVol(4));
			}
		}
		else
			kanal5->display(0);

		if(Kanal6->checkState())
		{
			for (cnt = 0; cnt < 5 ; cnt++)
			{
				kanal6->display(adc.readVol(5));
			}
		}
		else
			kanal6->display(0);

		if(Kanal7->checkState())
		{
			for (cnt = 0; cnt < 5 ; cnt++)
			{
				kanal7->display(adc.readVol(6));
			}
		}
		else
			kanal7->display(0);

		if(Kanal8->checkState())
		{
			for (cnt = 0; cnt < 5 ; cnt++)
			{
				kanal8->display(adc.readVol(7));
			}
		}
		else
			kanal8->display(0);

		timer->start(30);
	}
	return;
}

void Dialog::stopMeasure()
{
	LTC1090->setDisabled(false);
	LTC1290->setDisabled(false);
	disconnect(timer, SIGNAL(timeout()), this, SLOT(measure()));

	kanal1->display(0);
	kanal2->display(0);
	kanal3->display(0);
	kanal4->display(0);
	kanal5->display(0);
	kanal6->display(0);
	kanal7->display(0);
	kanal8->display(0);
}

void Dialog::setTimer()
{
	LTC1090->setDisabled(true);
	LTC1290->setDisabled(true);
	connect(timer, SIGNAL(timeout()), this, SLOT(measure()));
}


