/****************************************************************************
**
** One line to give the program's name and a brief idea of what it does.
** Copyright (C) 2005      Romain Gaucher
**
** This program is free software; you can redistribute it and/or modify it
** under the terms of the GNU General Public License as published by the
** Free Software Foundation; either version 2 of the License, or (at your option)
** any later version.
**
** This program is distributed in the hope that it will be useful, but WITHOUT
** ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
** FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License along with
** this program; if not, write to the
** Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
**
****************************************************************************/

#include <QtGui>
#include <QTabWidget>
#include "options.h"

Options::Options(QWidget *parent)
  : QDialog(parent)
{
	// Open the settings of the application
	QSettings settings("Modelib", "LPditor");
	// Build the Tab widget's object
	tabWidget = new QTabWidget;

	// allocate all pixmaps with the good size (32X20)
	pixOperator = new QPixmap(32,20);
	pixFunction = new QPixmap(32,20);
	pixName     = new QPixmap(32,20);
	pixComment  = new QPixmap(32,20);

	QPushButton *quit = new QPushButton(tr("Apply the modifications"));
	connect (quit, SIGNAL(clicked()), this, SLOT(saveClose()));

	/*
	 *  General options
	 *       - Path to save the files
	 *       - Solver to choose
	 */
	QWidget *general = new QWidget;
		QGridLayout *layoutGeneral = new QGridLayout;
			QLabel *l_favoritePath = new QLabel;
				l_favoritePath->setText(tr("Choose the path where you prefer put all of your LP files:"));
			favoritePath = new QLineEdit;
				QString sett = settings.value("favoritePath").toString();
				if (sett.isNull())
					sett = QDir(".").absolutePath();
				favoritePath->setText(sett);
			QPushButton *getFavoritePathButton = new QPushButton("Get a directory");
			connect(getFavoritePathButton, SIGNAL(clicked()), this, SLOT(getFavoritePath()));

			QLabel *l_solver = new QLabel;
			l_solver->setText("Choose the solver you want to use: ");

			solver = new QComboBox;
				solver->addItem("GLPSOL");
				solver->addItem("CPLEX");

				// which one is selected bye default
				sett = settings.value("solver").toString();
				if (sett.isNull())
					sett = "GLPSOL";
				solver->setCurrentIndex(solver->findText(sett));

			QLabel *l_solverPath = new QLabel;
				l_solverPath->setText(tr("Choose the path where the solver is: "));
			solverPath = new QLineEdit;
				sett = settings.value("solverPath").toString();
				if (sett.isNull())
					sett = QDir(".").absolutePath();
				solverPath->setText(sett);
			QPushButton *getSolverPathButton = new QPushButton("Get a directory");
			connect(getSolverPathButton, SIGNAL(clicked()), this, SLOT(getSolverPath()));

			QGridLayout *layoutGeneralFavoritePath = new QGridLayout;
				layoutGeneralFavoritePath->addWidget(favoritePath, 0, 0);
				layoutGeneralFavoritePath->addWidget(getFavoritePathButton, 0, 1);

			QGridLayout *layoutGeneralSolverPath = new QGridLayout;
				layoutGeneralSolverPath->addWidget(solverPath, 0, 0);
				layoutGeneralSolverPath->addWidget(getSolverPathButton, 0, 1);

		layoutGeneral->addWidget(l_favoritePath, 0, 0);
		layoutGeneral->addLayout(layoutGeneralFavoritePath, 1, 0);
		layoutGeneral->addWidget(l_solver, 2, 0);
		layoutGeneral->addWidget(solver, 3, 0);
		layoutGeneral->addWidget(l_solverPath, 4, 0);
		layoutGeneral->addLayout(layoutGeneralSolverPath, 5, 0);

		general->setLayout(layoutGeneral);

	tabWidget->addTab(general, tr("General"));

	/*
		*  Syntax Highlighting options
		*       - Choose colors for each references {comments, keywords, operators, names}
		*       - Choose font
		*/
	QWidget *syntaxHighlighting = new QWidget;
		QGridLayout *layoutHighlighting = new QGridLayout;

				// Font choose dialog
				QLabel *l_font = new QLabel;
				l_font->setText(tr("Choose the font you want to use: "));
				font = new QComboBox;
					font->addItem("SansSerif");
					font->addItem("Serif");
					font->addItem("Helvetica");
					font->addItem("Times");
					font->addItem("TypeWriter");
					font->addItem("Courier");
					font->addItem("Arial");
					font->addItem("Verdana");
					font->addItem("OldEnglish");
					font->addItem("System");
					// which one is selected bye default
					sett = settings.value("fontFamily").toString();
					if (sett.isNull())
						sett = "Helvetica";
					font->setCurrentIndex(font->findText(sett));

				fontSizeBox = new QSpinBox;
				fontSizeBox->setRange(8, 48);
				int f = settings.value("fontSize").toInt();
				if (f < 8 || f > 48)
					f = 10;
				fontSizeBox->setValue(f);


			// Colors dialogs
			QGridLayout *chooseColorLayout = new QGridLayout;

				// setting up the label (text)
				QLabel *l_comment = new QLabel;
				l_comment->setText("Color of the comments : ");
				// build the QLineEdit object
				comment = new QLineEdit;
					// fill it with the value saved or default
					sett = settings.value("colorComment").toString();
					if (sett.isNull())
						sett = "#AAAAAA";
					comment->setText(sett);
					currentComment.setNamedColor(sett);
					// set the color of the pixmal
					pixComment->fill(currentComment);
				// build the colored button
				colorCommentButton = new QToolButton;
					colorCommentButton->setIcon(QIcon(*pixComment));
					colorCommentButton->setIconSize(QSize(32,20));
					colorCommentButton->setToolButtonStyle(Qt::ToolButtonIconOnly);
				connect (colorCommentButton, SIGNAL(clicked()), this, SLOT(getColorComment()));

				// X 3

                QLabel *l_function = new QLabel;
				l_function->setText("Color of the functions : ");
				function = new QLineEdit;
					sett = settings.value("colorFunction").toString();
					if (sett.isNull())
						sett = "#2266FF";
					function->setText(sett);
					currentFunction.setNamedColor(sett);
					pixFunction->fill(currentFunction);
				colorFunctionButton = new QToolButton;
					colorFunctionButton->setIcon(QIcon(*pixFunction));
					colorFunctionButton->setIconSize(QSize(32,20));
					colorFunctionButton->setToolButtonStyle(Qt::ToolButtonIconOnly);
				connect (colorFunctionButton, SIGNAL(clicked()), this, SLOT(getColorFunction()));


				QLabel *l_name = new QLabel;
				l_name->setText("Color of the name of constraints etc. : ");
				name = new QLineEdit;
					sett = settings.value("colorName").toString();
					if (sett.isNull())
						sett = "#308b2b";
					name->setText(sett);
					currentName.setNamedColor(sett);
					pixName->fill(currentName);;
				colorNameButton = new QToolButton;
					colorNameButton->setIcon(QIcon(*pixName));
					colorNameButton->setIconSize(QSize(32,20));
					colorNameButton->setToolButtonStyle(Qt::ToolButtonIconOnly);
				connect (colorNameButton, SIGNAL(clicked()), this, SLOT(getColorName()));


				QLabel *l_operator = new QLabel;
				l_operator->setText("Color of the name of operators : ");
				operators = new QLineEdit;
					sett = settings.value("colorOperator").toString();
					if (sett.isNull())
						sett = "#C08646";
					operators->setText(sett);
					currentOperator.setNamedColor(sett);
					pixOperator->fill(currentOperator);
				colorOperatorButton = new QToolButton;
					colorOperatorButton->setIcon(QIcon(*pixOperator));
					colorOperatorButton->setIconSize(QSize(32,20));
					colorOperatorButton->setToolButtonStyle(Qt::ToolButtonIconOnly);
				connect (colorOperatorButton, SIGNAL(clicked()), this, SLOT(getColorOperator()));

                // fill the main Layout
				chooseColorLayout->addWidget(font, 0, 0);
				chooseColorLayout->addWidget(fontSizeBox,   0, 2);

				chooseColorLayout->addWidget(l_comment, 1, 0);
				chooseColorLayout->addWidget(comment,   1, 1);
				chooseColorLayout->addWidget(colorCommentButton,   1, 2);

				chooseColorLayout->addWidget(l_function, 2, 0);
				chooseColorLayout->addWidget(function,   2, 1);
				chooseColorLayout->addWidget(colorFunctionButton,   2, 2);

				chooseColorLayout->addWidget(l_name, 3, 0);
				chooseColorLayout->addWidget(name,   3, 1);
				chooseColorLayout->addWidget(colorNameButton,   3, 2);

				chooseColorLayout->addWidget(l_operator, 4, 0);
				chooseColorLayout->addWidget(operators,   4, 1);
				chooseColorLayout->addWidget(colorOperatorButton,   4, 2);

			layoutHighlighting->addWidget(l_font, 0, 0);
			layoutHighlighting->addWidget(font,   1, 0);
			layoutHighlighting->setMargin(10);
			layoutHighlighting->addLayout(chooseColorLayout, 2, 0);

		syntaxHighlighting->setLayout(layoutHighlighting);

	tabWidget->addTab(syntaxHighlighting, tr("Syntax highlighting"));


	QVBoxLayout *mainLayout = new QVBoxLayout;
		mainLayout->addWidget(tabWidget);
		mainLayout->addWidget(quit);

    setLayout(mainLayout);
	setWindowTitle(tr("LPditor options"));
	show();

    connect (this, SIGNAL(optionsRepaintColors_Signal()), this, SLOT(repaintActualisePixmap()));
}

QString Options::getPath()
{
	QString s = QFileDialog::getExistingDirectory(
					this,
					"Choose a directory",
					QDir(".").absolutePath(),
					QFileDialog::DontResolveSymlinks);
	return s;
}

//! Get your favorite path
void Options::getFavoritePath()
{
	QString p = getPath();
	favoritePath->setText(p);
}

//! Get your favorite path
void Options::getSolverPath()
{
	QString p = getPath();
	solverPath->setText(p);
}

//! Repaint all pixmaps and repaint
void Options::repaintActualisePixmap()
{
	pixOperator->fill(currentOperator);
	pixName->fill(currentName);
    pixFunction->fill(currentFunction);
    pixComment->fill(currentComment);
/*
	QList<QWidget *> widgets = qFindChildren<QWidget *>(this);
	foreach (QWidget *w, widgets)
		w->repaint(w->rect());
*/
}

//! Actualize the 'currentColor' get by the QColorDialog
void Options::getColor(const QColor& c)
{
	QColor color = QColorDialog::getColor(c, this);
	if (color.isValid())
	{
		currentColor = color;
	}
	else
		currentColor = c;
}

//! Get the color of a name and actualize all...
void Options::getColorName()
{
	getColor(currentName);
	currentName = currentColor;
	name->setText(currentColor.name());
   	emit optionsRepaintColors_Signal();
	colorNameButton->setIcon(QIcon(*pixName));
}

//! Get the color of a function and actualize all...
void Options::getColorFunction()
{
	getColor(currentFunction);
	currentFunction = currentColor;
	function->setText(currentColor.name());
   	emit optionsRepaintColors_Signal();
	colorFunctionButton->setIcon(QIcon(*pixFunction));
}

//! Get the color of an operator and actualize all...
void Options::getColorOperator()
{
	getColor(currentOperator);
	currentOperator = currentColor;
	operators->setText(currentColor.name());
   	emit optionsRepaintColors_Signal();
	colorOperatorButton->setIcon(QIcon(*pixOperator));
}

//! Get the color of a comment and actualize all...
void Options::getColorComment()
{
	getColor(currentComment);
	currentComment = currentColor;
	comment->setText(currentColor.name());
   	emit optionsRepaintColors_Signal();
	colorCommentButton->setIcon(QIcon(*pixComment));
}


//! Save the soft informations and settings
void Options::save()
{
    QSettings settings("Modelib", "LPditor");
	// save the favorite path;
	QString p = favoritePath->text();
	if (!p.isNull())
		settings.setValue("favoritePath", p);
	p = solverPath->text();
	if (!p.isNull())
		settings.setValue("solverPath", p);
	p = solver->currentText();
	if (!p.isNull())
		settings.setValue("solver", p);
	else
		settings.setValue("solver", "GLPSOL");

	p = font->currentText();
	if (!p.isNull())
		settings.setValue("fontFamily", p);
	else
		settings.setValue("fontFamily", "Helvetica");

	int f = fontSizeBox->value();
	if (f < 8 || f > 48)
		f = 10;
	settings.setValue("fontSize", f);

	p = comment->text();
	if (!p.isNull())
		settings.setValue("colorComment", p);
	else
		settings.setValue("colorComment", "#AAAAAA");

	p = operators->text();
	if (!p.isNull())
		settings.setValue("colorOperator", p);
	else
		settings.setValue("colorOperator", "#C08646");

	p = name->text();
	if (!p.isNull())
		settings.setValue("colorName", p);
	else
		settings.setValue("colorName", "#308b2b");

	p = function->text();
	if (!p.isNull())
		settings.setValue("colorFunction", p);
	else
		settings.setValue("colorFunction", "#2266FF");

}

void Options::saveClose()
{
	save();
	close();
	emit optionsRedraw_Signal();
}


