#include "MultiPlot.h"

// Public Functions/Constructors
MultiPlot::MultiPlot(QWidget* parent) : QWidget(parent){
	createUI();
	setAttribute(Qt::WA_DeleteOnClose);
}

MultiPlot::~MultiPlot(){}

QStack<Plot *> MultiPlot::getPlots(){
	return plots;
}

// Private Functions
void MultiPlot::createUI(){
	// TOP
	QVBoxLayout * topLayout = new QVBoxLayout;
	
	  // PLOTS
	plotLayout = new QVBoxLayout;

	for (int i=0; i<DEFPLOTS; i++){
		Plot * p = new Plot(this);
		plots.push(p);

		plotLayout->addWidget(p);
		
	}

	  // Controls
	ctrlBox = new QGroupBox("Controls",this);
	ctrlBox->setSizePolicy(QSizePolicy::Maximum,QSizePolicy::Maximum);
	ctrlLayout = new QGridLayout;
	     // Add remove Buttons
	addButton = new QPushButton(QIcon(":add.png"),"");
	addButton->setMaximumSize(18,18);
	addButton->setFlat(true);
	connect(addButton,SIGNAL(clicked()),this, SLOT(addClicked()));
	
	QLabel * tmpLabel = new QLabel("/");
	tmpLabel->setMaximumSize(5,18);
	
	removeButton = new QPushButton(QIcon(":remove.png"),"");
	removeButton->setMaximumSize(18,18);
	removeButton->setFlat(true);
	connect(removeButton,SIGNAL(clicked()),this, SLOT(removeClicked()));
	
	plotSelectLayout = new QHBoxLayout;
	QWidget * selectWdg = new QWidget(this);
	
	QFrame * sepFrame = new QFrame();
	sepFrame->setFrameStyle(QFrame::VLine);
	sepFrame->setFrameShadow(QFrame::Sunken);
	
	     // Plot Selector
	QLabel * selLabel = new QLabel("Selected:");
	selLabel->setMaximumSize(50,18);
		
	for (int i=0; i<plots.size(); i++){
		checks.push( new QCheckBox(this));
		checkLabels.push(new QLabel(" "+QString::number(i+1),this));
		
		plotSelectLayout->addWidget(checkLabels.top());
		plotSelectLayout->addWidget(checks.top());
	}
	
	selectWdg->setLayout(plotSelectLayout);
	
	QFrame * sepFrame2 = new QFrame();
	sepFrame2->setFrameStyle(QFrame::VLine);
	sepFrame2->setFrameShadow(QFrame::Sunken);
	
	    // Animation Control
	playButton = new QPushButton(QIcon(":play.png"),"");
	playButton->setMaximumSize(18,18);
	playButton->setFlat(true);
	connect(playButton,SIGNAL(clicked()),this, SLOT(playClicked()));
	
	pauseButton = new QPushButton(QIcon(":pause.png"),"");
	pauseButton->setMaximumSize(18,18);
	pauseButton->setFlat(true);
	connect(pauseButton,SIGNAL(clicked()),this, SLOT(pauseClicked()));
	
	stopButton = new QPushButton(QIcon(":stop.png"),"");
	stopButton->setMaximumSize(18,18);
	stopButton->setFlat(true);
	connect(stopButton,SIGNAL(clicked()),this, SLOT(stopClicked()));
	
	QFrame * sepFrame3 = new QFrame();
	sepFrame3->setFrameStyle(QFrame::VLine);
	sepFrame3->setFrameShadow(QFrame::Sunken);
	
	editButton = new QPushButton(QIcon(":edit.png"),"");
	editButton->setMaximumSize(18,18);
	editButton->setFlat(true);
	connect(editButton,SIGNAL(clicked()),this, SLOT(editClicked()));
	
	
	 // Controls
	ctrlLayout->addWidget(addButton,0,0);
	ctrlLayout->addWidget(tmpLabel,0,1);
	ctrlLayout->addWidget(removeButton,0,2);
	ctrlLayout->addWidget(sepFrame,0,3);
	ctrlLayout->addWidget(selLabel,0,4);
	ctrlLayout->addWidget(selectWdg,0,5);
	ctrlLayout->addWidget(sepFrame2,0,6);
	ctrlLayout->addWidget(playButton,0,7);
	ctrlLayout->addWidget(pauseButton,0,8);
	ctrlLayout->addWidget(stopButton,0,9);
	ctrlLayout->addWidget(sepFrame3,0,10);
	ctrlLayout->addWidget(editButton,0,11);
	
	ctrlBox->setLayout(ctrlLayout);
	
	//
	topLayout->addLayout(plotLayout);
	topLayout->addWidget(ctrlBox,Qt::AlignCenter);
	setLayout(topLayout);
	setFixedHeight(sizeHint().height());
}

void MultiPlot::fixSize(){
	setFixedHeight(plots.size()*(plots.top()->minimumSize().height()+plotLayout->spacing())+ctrlBox->height()+2*ctrlLayout->margin());
	resize(width(),
	       plots.size()*(plots.top()->minimumSize().height()+plotLayout->spacing())+ctrlBox->height()+2*ctrlLayout->margin());
}

// slots
void MultiPlot::editClicked(){
	// Check -> at Least 3 plots and 1 not Empty
	if (plots.size()<3) return;
	bool oneSignal=false;
	for (int i=0; i<plots.size(); i++)
		if (!plots.at(i)->isEmpty()) oneSignal=true;
		
	if (!oneSignal) return;
	
	// OK
	calc=new CalcDlg(this);

	setCursor(Qt::BusyCursor);
	calc->show();
	setCursor(Qt::ArrowCursor);
}

void MultiPlot::addClicked(){
	
	if (plots.size()==MAXPLOTS) {
		// Never get in here...
		QMessageBox::critical(this, "E R R O R", "To many plots in this window.\nYou need to create another one...\nSorry");
		return;
	}
	
	// Make Plot
	Plot * p = new Plot(this);
	plots.push(p);
	plotLayout->addWidget(p);
	
	// Make CheckBox
	checks.push( new QCheckBox(this));
	checkLabels.push(new QLabel(" "+QString::number(plots.size()),this));
		
	plotSelectLayout->addWidget(checkLabels.top());
	plotSelectLayout->addWidget(checks.top());
	
	if (plots.size()==2) removeButton->setEnabled(true);
	if (plots.size()==MAXPLOTS) addButton->setEnabled(false);
	fixSize();
}

void MultiPlot::removeClicked(){
	plotLayout->removeWidget(plots.top());
	plots.top()->hide();
	delete plots.pop();
	
	plotSelectLayout->removeWidget(checks.top());
	checks.top()->hide();
	delete checks.pop();
	
	plotSelectLayout->removeWidget(checkLabels.top());
	checkLabels.top()->hide();
	delete checkLabels.pop();
	
	if (plots.size()==1) removeButton->setEnabled(false);
	if (plots.size()<MAXPLOTS) addButton->setEnabled(true);
	fixSize();
}

void MultiPlot::playClicked(){
	
	for (int i=0; i<checks.size(); i++){
		if (checks.at(i)->isChecked()) {
			if (plots.at(i)->getAnimStatus()==STOPED) plots.at(i)->startAnimation();
			else if (plots.at(i)->getAnimStatus()==PAUSED) plots.at(i)->resumeAnimation();
		}
	}
}

void MultiPlot::pauseClicked(){
	for (int i=0; i<checks.size(); i++)
		if (checks.at(i)->isChecked()) plots.at(i)->pauseAnimation();
}

void MultiPlot::stopClicked(){
	for (int i=0; i<checks.size(); i++)
		if (checks.at(i)->isChecked()) plots.at(i)->stopAnimation();
}

