#include <QtGui>

#include "MainWin.h"
#include <fstream>

using namespace std;


MainWin::MainWin(QWidget* parent, Qt::WindowFlags flags) : QMainWindow(parent, flags){
	// create gui elements defined in the Ui_MainWindow class
	createMenuBar();
	createUI();
	connectActions();
	windowsNum=0;
	
}
MainWin::~MainWin()
{
// no need to delete child widgets, QT does it all for us
}

void MainWin::connectActions(){
	connect(actionNewSingle, SIGNAL(triggered()), this, SLOT(openNewSingle()));
	connect(actionQuit, SIGNAL(triggered()), this, SLOT(close()));
	connect(actionOpenFiles, SIGNAL(triggered()), this, SLOT(load()));
	connect(actionNewMulti, SIGNAL(triggered()), this, SLOT(openNewMulti()));
	connect(actionOpenSignalEditor, SIGNAL(triggered()), this, SLOT(openEditor()));
	connect(actionNewMath, SIGNAL(triggered()), this, SLOT(openNewMath()));
	
	connect(actionPrevWin, SIGNAL(triggered()), workspace, SLOT(activatePreviousWindow()) );
	connect(actionNextWin, SIGNAL(triggered()), workspace, SLOT(activateNextWindow()) );
	connect(actionCascade, SIGNAL(triggered()), workspace, SLOT(cascade()) );
	
	connect(actionCloseAct, SIGNAL(triggered()), workspace, SLOT(closeActiveWindow()));	
	connect(actionCloseAll, SIGNAL(triggered()), workspace, SLOT(closeAllWindows()));	
}

void MainWin::createMenuBar(){
	
	// Menu Bar
	menubar = new QMenuBar(this);
	
	// File Menu Actions
	actionOpenFiles = new QAction(this);
	actionOpenFiles->setShortcut(tr("Ctrl+O"));
	actionOpenFiles->setText(QApplication::translate("MainWin", "&Open File(s)", 0, QApplication::UnicodeUTF8));

	actionNewSingle = new QAction(this);
	actionNewSingle->setShortcut(tr("Ctrl+N"));
	actionNewSingle->setText(QApplication::translate("MainWin", "New &Single Plot", 0, QApplication::UnicodeUTF8));
	
	actionNewMulti = new QAction(this);
	actionNewMulti->setShortcut(tr("Ctrl+M"));
	actionNewMulti->setText(QApplication::translate("MainWin", "New &Multi Plot", 0, QApplication::UnicodeUTF8));

	actionNewMath= new QAction(this);
	actionNewMath->setShortcut(tr("Ctrl+T"));
	actionNewMath->setText(QApplication::translate("MainWin", "New Ma&th Plot", 0, QApplication::UnicodeUTF8));

	actionOpenSignalEditor= new QAction(this);
	actionOpenSignalEditor->setShortcut(tr("Ctrl+E"));
	actionOpenSignalEditor->setText(QApplication::translate("MainWin", "Open Signal &Editor", 0, QApplication::UnicodeUTF8));	

	actionQuit = new QAction(this);
	actionQuit->setShortcut(tr("Ctrl+Q"));
	actionQuit->setText(QApplication::translate("MainWin", "&Quit", 0, QApplication::UnicodeUTF8));
	
	// File Menu
	menuFile = new QMenu(menubar);
	menuFile->setTitle(QApplication::translate("MainWin", "&File", 0, QApplication::UnicodeUTF8));
	
	menuFile->addAction(actionOpenFiles);
	menuFile->addAction(actionNewSingle);
	menuFile->addAction(actionNewMulti);
	menuFile->addAction(actionNewMath);
	menuFile->addAction(actionOpenSignalEditor);
	menuFile->addSeparator();
	menuFile->addAction(actionQuit);
	
	// Windows Menu Actions
	
	actionPrevWin = new QAction(this);
	actionPrevWin->setShortcut(tr("Ctrl+Left"));
	actionPrevWin->setText(QApplication::translate("MainWin", "&Prev Window", 0, QApplication::UnicodeUTF8));
	
	actionNextWin = new QAction(this);
	actionNextWin->setShortcut(tr("Ctrl+Right"));
	actionNextWin->setText(QApplication::translate("MainWin", "&Next Window", 0, QApplication::UnicodeUTF8));
	
	actionCascade = new QAction(this);
	actionCascade->setText(QApplication::translate("MainWin", "&Cascade", 0, QApplication::UnicodeUTF8));
	
	actionCloseAct = new QAction(this);
	actionCloseAct->setShortcut(tr("Delete"));
	actionCloseAct->setText(QApplication::translate("MainWin", "Close &Active", 0, QApplication::UnicodeUTF8));
	
	actionCloseAll = new QAction(this);
	actionCloseAll->setShortcut(tr("Ctrl+Delete"));
	actionCloseAll->setText(QApplication::translate("MainWin", "C&lose All", 0, QApplication::UnicodeUTF8));
  
	// Windows Menu
	menuWindows=new QMenu(menubar);
	menuWindows->setTitle(QApplication::translate("MainWin", "&Windows", 0, QApplication::UnicodeUTF8));
	
	menuWindows->addAction(actionPrevWin);
	menuWindows->addAction(actionNextWin);
	menuWindows->addSeparator();
	menuWindows->addAction(actionCascade);
	menuWindows->addSeparator();
	menuWindows->addAction(actionCloseAct);
	menuWindows->addAction(actionCloseAll);

	// Help Menu Actions
	
	menubar->addAction(menuFile->menuAction());
	menubar->addAction(menuWindows->menuAction());
	// Add Menu Bar
	setMenuBar(menubar);

}

void MainWin::createUI(){
	//Staus Bar
	statusbar = new QStatusBar(this);
	statusbar->setObjectName(QString::fromUtf8("statusbar"));
	setStatusBar(statusbar);
	
	// Configure MainWin
	setWindowModality(Qt::NonModal);
	setWindowTitle(QApplication::translate("MainWin", "Signals", 0, QApplication::UnicodeUTF8));
	
	// Main Pane
	workspace = new QWorkspace(this);
	
	//workspace->setScrollBarsEnabled(true);
	setCentralWidget(workspace);
}

// SLOTS
void MainWin::openEditor(){
	SignalEditor *ed=new SignalEditor(this);

	// On Close Event -> Delete
	//ed->setAttribute(Qt::WA_DeleteOnClose);

	workspace->addWindow(ed)->setMaximumWidth(1024);
	ed->show();
}

void MainWin::openNewSingle(){
	QVBoxLayout * fLayout=new QVBoxLayout;
	QWidget * wid = new QWidget;

	Plot * p=new Plot(this);
	p->setWindowTitle("Single Plot ...");
	fLayout->addWidget(p);
	wid->setLayout(fLayout);
	//wid->setMaximumHeight(wid->sizeHint().height());
	wid->setMaximumWidth(1024);
	wid->setFixedHeight(wid->sizeHint().height());
	workspace->addWindow(wid);

	// On Close Event -> Delete 
	// We don't want properties dialog to exist if the window is closed!!!!
	wid->setAttribute(Qt::WA_DeleteOnClose);
		
	wid->show();
}

void MainWin::openNewMulti(){
	MultiPlot * p = new MultiPlot(this);
	p->setWindowTitle("Multi Plot ...");
	workspace->addWindow(p)->setMaximumWidth(1024);

	// On Close Event -> Delete (In Constructor)
	//p->setAttribute(Qt::WA_DeleteOnClose);

	p->show();
}

void MainWin::openNewMath(){
	MathPlot * mp=new MathPlot(this);
	workspace->addWindow(mp);
	mp->show();

}

void MainWin::load(){

	//File Dialog
	QFileDialog dialog(this,tr("Open File(s)"));
	
	// Initial Directory
	QString dir=QDir::homePath();
/*	
	#ifdef Q_OS_WIN32 
		dir="C:\\";
	#endif
*/	
	dialog.setDirectory(dir);
	
	// File Extension Filters
	 QStringList filters;
		filters << "DAT Files (*.dat *.data)"
				<< "Sig Files (*.sig )"
				<< "Any files (*.*)";
	
	dialog.setFilters(filters);
	dialog.setFileMode(QFileDialog::ExistingFiles);
	dialog.setViewMode(QFileDialog::Detail);
	dialog.setAcceptMode(QFileDialog::AcceptOpen);
	
	// Execute
	QStringList fileNames;
	 if (dialog.exec()) 
	 	fileNames = dialog.selectedFiles();
	 else
	 	return;
	
	
	QStringList::Iterator it = fileNames.begin();
	while(it != fileNames.end()) {
	    Plot * p=new Plot(*it,this);
		workspace->addWindow(p);
		p->show();
	    ++it;
	    windowsNum++;
	}

}

