#include "SignalEditor.h"
#include <iostream>

SignalEditor::SignalEditor(QWidget * parent) :
 QWidget(parent){

	QVBoxLayout * topLayout=new QVBoxLayout;
	
	preview = new Plot(this);
	
	QHBoxLayout * inputLayout = new QHBoxLayout;
	
	func = new QLineEdit(this);
	connect(func,SIGNAL(returnPressed()),this,SLOT(previewClicked()));
	
	from = new QLineEdit(this);
	from->setAlignment(Qt::AlignRight);
	from->setValidator(new QDoubleValidator(from));
	from->setMaximumWidth (45);
	from->setMaxLength (5);
	connect(from,SIGNAL(returnPressed()),this,SLOT(previewClicked()));

	
	to = new QLineEdit(this);
	to->setAlignment(Qt::AlignRight);
	to->setValidator(new QDoubleValidator(to));
	to->setMaximumWidth (45);
	to->setMaxLength (5);
	connect(to,SIGNAL(returnPressed()),this,SLOT(previewClicked()));

	
	step = new QLineEdit(this);
	step->setAlignment(Qt::AlignRight);
	step->setValidator(new QDoubleValidator(0,999,3,step));
	step->setMaximumWidth (45);
	step->setMaxLength (5);
	connect(step,SIGNAL(returnPressed()),this,SLOT(previewClicked()));

	
	showPreview = new QPushButton(tr("P&review"));
	connect(showPreview,SIGNAL(clicked()),this,SLOT(previewClicked()));
	
	showHelp = new QPushButton(QIcon(":help.png"),"");
	showHelp->setMaximumSize(18,18);
	showHelp->setFlat(true);
	connect(showHelp,SIGNAL(clicked()),this,SLOT(helpClicked()));
	
	inputLayout->addWidget(new QLabel("Function:"));
	inputLayout->addWidget(func);
	inputLayout->addWidget(new QLabel("from:"));
	inputLayout->addWidget(from);
	inputLayout->addWidget(new QLabel("to:"));
	inputLayout->addWidget(to);
	inputLayout->addWidget(new QLabel("and step:"));
	inputLayout->addWidget(step);
	inputLayout->addWidget(showPreview);
	inputLayout->addWidget(showHelp);
	
	topLayout->addWidget(preview);
	topLayout->addLayout(inputLayout);

	setLayout(topLayout);
	setMaximumHeight(sizeHint().height());
	setWindowTitle("Signal Editor");

}

SignalEditor::~SignalEditor(){}

// Slots
void SignalEditor::helpClicked(){
	QFile help_file(":src/fparser.help",this);
	help_file.open(QIODevice::ReadOnly);
	QString msg=help_file.readAll();
	help_file.close();
	
	HelpDialog hlp(msg);
	hlp.setWindowTitle("Signal Editor Help");
	hlp.exec();
	
}

void SignalEditor::previewClicked(){
	// Check Fields
	if (func->text()=="" || from->text()=="" || to->text()=="" || step->text()==""){
		QMessageBox::critical(this,"Editor ERROR","ALL fields are required");
		return;
	}
	//Check From->To
	if (from->text().toDouble() >= to->text().toDouble()){
		from->setText("");
		to->setText("");
		QMessageBox::critical(this,"Editor ERROR","\'From\' Can't be greater than (or equal to) \'To\'");
		return;
	}

	if (step->text().toDouble() == 0){
		QMessageBox::critical(this,"Editor ERROR","\'Step\' Can't be zero (0)");
		return;
	}

	setCursor(Qt::WaitCursor);
	setEnabled(false);

	thread= new CreateSignalThread(func->text(),from->text().toDouble(),to->text().toDouble(),step->text().toDouble(),this);
	connect(thread,SIGNAL(finished()),this,SLOT(threadFinished()));	

	thread->start();

}

void SignalEditor::threadFinished(){

	setCursor(Qt::ArrowCursor);
	setEnabled(true);

	if (!thread->isOK()) {
		QMessageBox::critical(this,"Parser ERROR",thread->getErrorMsg());
		return;
	}
	
	preview->setSignal(thread->getSignal());
	thread->terminate();
}
