#include "MathPlot.h"

MathPlot::MathPlot(QWidget * parent) : QWidget(parent){
	createUI();
}

MathPlot::~MathPlot(){}



// Event Override

// Move is needed else it is not working..
void MathPlot::dragLeaveEvent(QDragLeaveEvent *){}
void MathPlot::dragMoveEvent(QDragMoveEvent *){}

void MathPlot::dragEnterEvent( QDragEnterEvent * event){
	if (event->mimeData()->hasFormat("text/signal")){
		event->accept();
	}
	else
		event->ignore();
}

void MathPlot::dropEvent(QDropEvent *){}



// Private Methods
void MathPlot::createUI(){
	topLayout = new QGridLayout;
	
	
	connect(&plotPanel,SIGNAL(signalAdded()),this,SLOT(signalAddedToPanel()));
	topLayout->addWidget(&plotPanel,0,1);

	setLayout(topLayout);

	rmGroup=new QButtonGroup(this);
	connect(rmGroup,SIGNAL(buttonClicked(int)),this,SLOT(removeClicked(int)));

	setAttribute(Qt::WA_DeleteOnClose);
}

void MathPlot::createControl(int id){

	hide();

	QHBoxLayout * widLayout= new QHBoxLayout;
	QWidget * wid = new QWidget;
	
	QPushButton * rmbut = new QPushButton("Remove");
	rmbut->setFixedSize(50,20);
	rmButtons.append(rmbut);
	rmGroup->addButton(rmbut,id);
	
	QCheckBox * ch = new QCheckBox;
	ch->setFixedSize(16,16);
	anChecks.append(ch);
	connect(ch, SIGNAL(stateChanged(int)), this, SLOT(checkChanged()) ); 
	
	
	QWidget * color = new QWidget;
	color->setFixedSize(10,10);
	color->setAutoFillBackground(true);
	QPalette palette;
	palette.setColor(color->backgroundRole(),plotPanel.getColorFor(id));
	color->setPalette(palette);
	
	QString labmsg="Not Saved...";
	if (plotPanel.getSignal(id).getFileName()!="") 
		labmsg=plotPanel.getSignal(id).getFileName().c_str();
	QLabel * lab = new QLabel(labmsg);

	widLayout->addWidget(color);
	widLayout->addWidget(lab);
	widLayout->addWidget(ch);
	widLayout->addWidget(rmbut);

	wid->setLayout(widLayout);

	topLayout->addWidget(wid,id+1,1);
	sigControls.append(wid);
	
	adjustSize();
	QApplication::processEvents();

	show();
}

void MathPlot::updateColors(int from){
	for (int i=from; i<sigControls.size(); i++){
		QWidget * color=sigControls[i]->layout()->itemAt(0)->widget();
		
		QPalette palette;
		palette.setColor(color->backgroundRole(),plotPanel.getColorFor(i));
		color->setPalette(palette);
	}
}

void MathPlot::updateLayout(int emptyPos){
	/*
 	* +1 for removed item...
 	* +1 for Layout numbering
 	* i<... +2 (i= layout rows)
 	*/
	for (int i=emptyPos+1; i<sigControls.size()+2; i++){
		topLayout->removeWidget(sigControls[i-2]);
		topLayout->addWidget(sigControls[i-2],i-1,1);
	}
		
}

void MathPlot::updateIds(int removed){
	for (int i=removed; i<sigControls.size(); i++){
		rmGroup->setId(rmButtons[i],i);
			
	}
}

// SLOTS
void MathPlot::signalAddedToPanel(){
	createControl(plotPanel.getSignalsCount()-1);
}

void MathPlot::removeClicked(int id){

	// FIXME remove buttons from group --->craches (removeButton(*but))
	plotPanel.removeSignal(id);
	rmButtons.removeAt(id);
	anChecks.removeAt(id);
	
	sigControls[id]->hide();
	topLayout->removeWidget(sigControls[id]);
	sigControls.removeAt(id);
		
	updateColors(id);
	updateLayout(id+1);
	updateIds(id);
		
	adjustSize();
	QApplication::processEvents();
}

void MathPlot::checkChanged(){

	for (int i=0; i<anChecks.size(); i++)
		if (anChecks[i]->isChecked()) 
			plotPanel.setAnalog(i,true);
		else
			plotPanel.setAnalog(i,false);
	plotPanel.redrawAll();
}

