/*
 * Cueplot: a GUI front-end to gnuplot
 * Copyright (C) 2007, 2008 Muneyuki Noguchi
 *
 * 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
#include "TerminalPostScriptOptionWidget.h"
#include "TerminalPostScript.h"

#include <QtCore/QCoreApplication>
#include <QtGui/QCheckBox>
#include <QtGui/QComboBox>
#include <QtGui/QLabel>
#include <QtGui/QLineEdit>
#include <QtGui/QGridLayout>
#include <QtGui/QSpinBox>

TerminalPostScriptOptionWidget::TerminalPostScriptOptionWidget(QWidget *parent)
: OptionWidget(parent), terminalPostScript(0)
{
}

TerminalPostScriptOptionWidget::~TerminalPostScriptOptionWidget()
{
	delete terminalPostScript;
}

void TerminalPostScriptOptionWidget::construct()
{
	modeComboBox = new QComboBox;
	for (int i = 0; TerminalPostScript::MODE[i][0]; i++) {
		modeComboBox->addItem(QCoreApplication::translate(
					"TerminalPostScript", TerminalPostScript::MODE[i]));
	}
	connect(modeComboBox, SIGNAL(activated(int)),
			this, SIGNAL(modified()));

	QLabel *modeLabel = new QLabel;
	modeLabel->setText(tr("&Mode:"));
	modeLabel->setBuddy(modeComboBox);

	colorComboBox = new QComboBox;
	for (int i = 0; TerminalPostScript::COLOR[i][0]; i++) {
		colorComboBox->addItem(QCoreApplication::translate(
					"TerminalPostScript", TerminalPostScript::COLOR[i]));
	}
	connect(colorComboBox, SIGNAL(activated(int)),
			this, SIGNAL(modified()));

	QLabel *colorLabel = new QLabel;
	colorLabel->setText(tr("Co&lor:"));
	colorLabel->setBuddy(colorComboBox);

	solidComboBox = new QComboBox;
	for (int i = 0; TerminalPostScript::LINE_STYLE[i][0]; i++) {
		solidComboBox->addItem(QCoreApplication::translate(
					"TerminalPostScript", TerminalPostScript::LINE_STYLE[i]));
	}
	connect(solidComboBox, SIGNAL(activated(int)),
			this, SIGNAL(modified()));

	QLabel *solidLabel = new QLabel;
	solidLabel->setText(tr("Da&shed:"));
	solidLabel->setBuddy(solidComboBox);

	fontNameLineEdit = new QLineEdit;
	connect(fontNameLineEdit, SIGNAL(textChanged(const QString &)),
			this, SIGNAL(modified()));

	QLabel *fontNameLabel = new QLabel;
	fontNameLabel->setText(tr("&Font name:"));
	fontNameLabel->setBuddy(fontNameLineEdit);

	fontSizeSpinBox = new QSpinBox;
	connect(fontSizeSpinBox, SIGNAL(valueChanged(int)),
			this, SIGNAL(modified()));

	QLabel *fontSizeLabel = new QLabel;
	fontSizeLabel->setText(tr("Fo&nt size:"));
	fontSizeLabel->setBuddy(fontSizeSpinBox);

	enhancedCheckBox = new QCheckBox;
	enhancedCheckBox->setText(tr("&Enhanced"));
	connect(enhancedCheckBox, SIGNAL(toggled(bool)),
			this, SIGNAL(modified()));

	QGridLayout *mainLayout = new QGridLayout;
	mainLayout->addWidget(modeLabel, 0, 0);
	mainLayout->addWidget(modeComboBox, 0, 1);
	mainLayout->addItem(new QSpacerItem(40, 20, 
				QSizePolicy::Expanding, QSizePolicy::Minimum), 0, 2);
	mainLayout->addWidget(colorLabel, 1, 0);
	mainLayout->addWidget(colorComboBox, 1, 1);
	mainLayout->addWidget(solidLabel, 2, 0);
	mainLayout->addWidget(solidComboBox, 2, 1);
	mainLayout->addWidget(fontNameLabel, 3, 0);
	mainLayout->addWidget(fontNameLineEdit, 3, 1, 1, 2);
	mainLayout->addWidget(fontSizeLabel, 4, 0);
	mainLayout->addWidget(fontSizeSpinBox, 4, 1);
	mainLayout->addWidget(enhancedCheckBox, 5, 1);

	setLayout(mainLayout);
}

void TerminalPostScriptOptionWidget::init()
{
	delete terminalPostScript;
	terminalPostScript = new TerminalPostScript;
	setWidget();
}

void TerminalPostScriptOptionWidget::apply()
{
	terminalPostScript->setMode(modeComboBox->currentIndex());
	terminalPostScript->setColor(!colorComboBox->currentIndex());
	terminalPostScript->setSolid(!solidComboBox->currentIndex());
	terminalPostScript->setEnhanced(enhancedCheckBox->isChecked());
	terminalPostScript->setFontName(fontNameLineEdit->text());
	terminalPostScript->setFontSize(fontSizeSpinBox->value());

	emit executed(terminalPostScript->command());
}

void TerminalPostScriptOptionWidget::setWidget()
{
	modeComboBox->setCurrentIndex(terminalPostScript->mode());
	colorComboBox->setCurrentIndex(!terminalPostScript->isColor());
	solidComboBox->setCurrentIndex(!terminalPostScript->isSolid());
	fontNameLineEdit->setText(terminalPostScript->fontName());
	fontSizeSpinBox->setValue(terminalPostScript->fontSize());
	enhancedCheckBox->setChecked(terminalPostScript->enhances());
}
