/***************************************************************************
 *   Copyright (C) 2005-2006 by Niklas Knutsson                            *
 *   nq@altern.org                                                         *
 *                                                                         *
 *   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 "qalculatepreferencesdialog.h"
#include "qalculate_tde_utils.h"

#include <tdelocale.h>
#include <tqbuttongroup.h>
#include <tqcheckbox.h>
#include <tqlabel.h>
#include <tqradiobutton.h>
#include <tqtooltip.h>
#include <tdefontrequester.h>
#include <ktabwidget.h>
#include <tqpushbutton.h>
#include <tqfont.h>
#include <tqpushbutton.h>
#include <tqlayout.h>
#include <kcolorbutton.h>
#include "kqalculate.h"

extern KQalculate *mainWin;
extern PrintOptions printops;
extern EvaluationOptions evalops;
extern bool fetch_exchange_rates_at_startup, save_mode_on_exit, save_defs_on_exit;
extern bool use_custom_result_font, use_custom_expression_font, use_custom_status_font;
extern TQString custom_result_font, custom_expression_font, custom_status_font;
extern TQWidget *expressionWidget, *resultWidget, *statusWidget_l;
extern bool close_to_systray;
extern bool display_expression_status;
extern TQColor status_error_color;
extern TQColor status_warning_color;
extern int use_icon_buttons;
extern bool rpn_keypad_only;

QalculatePreferencesDialog::QalculatePreferencesDialog(TQWidget *parent, const char *name) : KDialogBase(parent, name, true, i18n("Preferences"), Ok | Apply | Cancel, Ok, false) {

	TQTabWidget *tabs = new KTabWidget(this);
	setMainWidget(tabs);
	TQWidget *page1 = new TQWidget(this);
	TQWidget *page2 = new TQWidget(this);
	tabs->addTab(page1, i18n("General"));
	tabs->addTab(page2, i18n("Fonts"));

	TQVBoxLayout *vbox = new TQVBoxLayout(page1, spacingHint());
	systrayButton = new TQCheckBox(i18n("Close to system tray"), page1);
	TQToolTip::add(systrayButton, i18n("If the application will reside in the system tray when the window is closed"));
	vbox->addWidget(systrayButton);
	statusButton = new TQCheckBox(i18n("Display expression status"), page1);
	TQToolTip::add(statusButton, i18n("If as-you-type expression status shall be displayed below the expression entry"));
	vbox->addWidget(statusButton);
	iconsButton = new TQCheckBox(i18n("Use icon buttons"), page1);
	TQToolTip::add(iconsButton, i18n("Use icons instead of text on the top-right buttons. The third state uses the style setting for icons on buttons."));
	iconsButton->setTristate(true);
	vbox->addWidget(iconsButton);
	fetchExchangeRatesButton = new TQCheckBox(i18n("Update exchange rates on start"), page1);
	TQToolTip::add(fetchExchangeRatesButton, i18n("If current exchange rates shall be downloaded from the internet at program start"));
	vbox->addWidget(fetchExchangeRatesButton);
	saveModeButton = new TQCheckBox(i18n("Save mode on exit"), page1);
	TQToolTip::add(saveModeButton, i18n("If the mode of the calculator shall be restored"));
	vbox->addWidget(saveModeButton);
	saveDefsButton = new TQCheckBox(i18n("Save definitions on exit"), page1);
	TQToolTip::add(saveDefsButton, i18n("If changes to functions, units and variables shall be saved automatically"));
	vbox->addWidget(saveDefsButton);
	rpnKeypadOnlyButton = new TQCheckBox(i18n("Use only keypad keys for RPN"), page1);
	TQToolTip::add(rpnKeypadOnlyButton, i18n("Use only keypad keys for RPN operations (+-*/^) and use the rest of the keyboard for expression entry"));
	vbox->addWidget(rpnKeypadOnlyButton);
	dotAsSeparatorButton = new TQCheckBox(i18n("Ignores dots in numbers"), page1);
	TQToolTip::add(dotAsSeparatorButton, i18n("Allow dots, '.',  to be used as thousands separator instead of as an alternative to decimal sign"));
	if(CALCULATOR->getDecimalPoint() == ".") dotAsSeparatorButton->hide();
	vbox->addWidget(dotAsSeparatorButton);
	unicodeButton = new TQCheckBox(i18n("Enable unicode signs"), page1);
	TQToolTip::add(unicodeButton, i18n("Disable this if you have problems with some fancy characters"));
	vbox->addWidget(unicodeButton);
	lowerCaseButton = new TQCheckBox(i18n("Use lower case in numbers"), page1);
	TQToolTip::add(lowerCaseButton, i18n("If lower case letters should be used in numbers with non-decimal base"));
	vbox->addWidget(lowerCaseButton);
	lowerCaseEButton = new TQCheckBox(i18n("Use lower case \"e\""), page1);
	TQToolTip::add(lowerCaseEButton, i18n("If \"e\"; shall be used instead of \"E\" in numbers"));
	vbox->addWidget(lowerCaseEButton);
	alternativeBasePrefixButton = new TQCheckBox(i18n("Alternative base prefixes"), page1);
	TQToolTip::add(alternativeBasePrefixButton, i18n("If hexadecimal numbers shall be displayed with \"0x0\" and binary numbers with \"0b00\" as prefixes"));
	vbox->addWidget(alternativeBasePrefixButton);
	spellOutLogicalOperatorsButton = new TQCheckBox(i18n("Spell out logical operators"), page1);
	TQToolTip::add(spellOutLogicalOperatorsButton, i18n("If logical and/or shall be displayed as \"&&\"/\"||\" or \"and\"/\"or\""));
	vbox->addWidget(spellOutLogicalOperatorsButton);
	TQGridLayout *grid = new TQGridLayout(vbox);
	multiplicationLabel = new TQLabel(i18n("Multiplication sign:"), page1);
	grid->addWidget(multiplicationLabel, 0, 0);
	TQButtonGroup *group = new TQButtonGroup();
	dotButton = new TQRadioButton(SIGN_MULTIDOT, page1);
	group->insert(dotButton, 0);
	grid->addWidget(dotButton, 0, 1);
	exButton = new TQRadioButton(SIGN_MULTIPLICATION, page1);
	group->insert(exButton, 1);
	grid->addWidget(exButton, 0, 2);
	asteriskButton = new TQRadioButton("*", page1);
	group->insert(asteriskButton, 2);
	grid->addWidget(asteriskButton, 0, 3);
	divisionLabel = new TQLabel(i18n("Division sign:"), page1);
	grid->addWidget(divisionLabel, 1, 0);
	group = new TQButtonGroup();
	divisionSlashButton = new TQRadioButton(" " SIGN_DIVISION_SLASH " ", page1);
	group->insert(divisionSlashButton, 0);
	grid->addWidget(divisionSlashButton, 1, 1);
	divisionButton = new TQRadioButton(SIGN_DIVISION, page1);
	group->insert(divisionButton, 1);
	grid->addWidget(divisionButton, 1, 2);
	slashButton = new TQRadioButton("/", page1);
	group->insert(slashButton, 2);
	grid->addWidget(slashButton, 1, 3);
	vbox->addItem(new TQSpacerItem(0, 0, TQSizePolicy::Minimum, TQSizePolicy::Expanding));
	
	vbox = new TQVBoxLayout(page2, spacingHint());
	useCustomResultFontButton = new TQCheckBox(i18n("Custom result font"), page2);
	vbox->addWidget(useCustomResultFontButton);
	resultFontEdit = new TDEFontRequester(page2);
	vbox->addWidget(resultFontEdit);
	useCustomExpressionFontButton = new TQCheckBox(i18n("Custom expression font"), page2);
	vbox->addWidget(useCustomExpressionFontButton);
	expressionFontEdit = new TDEFontRequester(page2);
	vbox->addWidget(expressionFontEdit);
	useCustomStatusFontButton = new TQCheckBox(i18n("Custom status font"), page2);
	vbox->addWidget(useCustomStatusFontButton);
	statusFontEdit = new TDEFontRequester(page2);
	vbox->addWidget(statusFontEdit);
	grid = new TQGridLayout(vbox);
	grid->addWidget(new TQLabel(i18n("Status error color:"), page2), 0, 0);
	statusErrorColorButton = new KColorButton("red", "red", page2);
	grid->addWidget(statusErrorColorButton, 0, 1);
	grid->addWidget(new TQLabel(i18n("Status warning color:"), page2), 1, 0);
	statusWarningColorButton = new KColorButton("blue", "blue", page2);
	grid->addWidget(statusWarningColorButton, 1, 1);
	vbox->addItem(new TQSpacerItem(0, 0, TQSizePolicy::Minimum, TQSizePolicy::Expanding));
	

	connect(useCustomResultFontButton, TQ_SIGNAL(toggled(bool)), resultFontEdit, TQ_SLOT(setEnabled(bool)));
	connect(useCustomExpressionFontButton, TQ_SIGNAL(toggled(bool)), expressionFontEdit, TQ_SLOT(setEnabled(bool)));
	connect(useCustomStatusFontButton, TQ_SIGNAL(toggled(bool)), statusFontEdit, TQ_SLOT(setEnabled(bool)));
	connect(unicodeButton, TQ_SIGNAL(toggled(bool)), multiplicationLabel, TQ_SLOT(setEnabled(bool)));
	connect(unicodeButton, TQ_SIGNAL(toggled(bool)), dotButton, TQ_SLOT(setEnabled(bool)));
	connect(unicodeButton, TQ_SIGNAL(toggled(bool)), exButton, TQ_SLOT(setEnabled(bool)));
	connect(unicodeButton, TQ_SIGNAL(toggled(bool)), asteriskButton, TQ_SLOT(setEnabled(bool)));
	connect(unicodeButton, TQ_SIGNAL(toggled(bool)), divisionLabel, TQ_SLOT(setEnabled(bool)));
	connect(unicodeButton, TQ_SIGNAL(toggled(bool)), divisionSlashButton, TQ_SLOT(setEnabled(bool)));
	connect(unicodeButton, TQ_SIGNAL(toggled(bool)), divisionButton, TQ_SLOT(setEnabled(bool)));
	connect(unicodeButton, TQ_SIGNAL(toggled(bool)), slashButton, TQ_SLOT(setEnabled(bool)));

}

QalculatePreferencesDialog::~QalculatePreferencesDialog() {}

void QalculatePreferencesDialog::slotApply() {
	rpn_keypad_only = rpnKeypadOnlyButton->isChecked();
	evalops.parse_options.dot_as_separator = dotAsSeparatorButton->isChecked();
	close_to_systray = systrayButton->isChecked();
	display_expression_status = statusButton->isChecked();
	switch(iconsButton->state()) {
		case TQButton::Off: {
			use_icon_buttons = 0;
			break;
		}
		case TQButton::On: {
			use_icon_buttons = 1;
			break;
		}
		case TQButton::NoChange: {
			use_icon_buttons = -1;
			break;
		}
	}
	fetch_exchange_rates_at_startup = fetchExchangeRatesButton->isChecked();
	save_mode_on_exit = saveModeButton->isChecked();
	printops.use_unicode_signs = unicodeButton->isChecked();
	printops.lower_case_numbers = lowerCaseButton->isChecked();
	printops.lower_case_e = lowerCaseEButton->isChecked();
	if(alternativeBasePrefixButton->isChecked()) printops.base_display = BASE_DISPLAY_ALTERNATIVE;
	else printops.base_display = BASE_DISPLAY_NORMAL;
	printops.spell_out_logical_operators = spellOutLogicalOperatorsButton->isChecked();
	save_defs_on_exit = saveDefsButton->isChecked();
	use_custom_result_font = useCustomResultFontButton->isChecked();
	use_custom_expression_font = useCustomExpressionFontButton->isChecked();
	use_custom_status_font = useCustomStatusFontButton->isChecked();
	custom_result_font = resultFontEdit->font().toString();
	custom_expression_font = expressionFontEdit->font().toString();
	custom_status_font = statusFontEdit->font().toString();
	status_error_color = statusErrorColorButton->color();
	if(!status_error_color.isValid()) status_error_color.setNamedColor("red");
	status_warning_color = statusWarningColorButton->color();
	if(!status_warning_color.isValid()) status_warning_color.setNamedColor("blue");
	if(dotButton->isChecked()) {
		printops.multiplication_sign = MULTIPLICATION_SIGN_DOT;
	} else if(exButton->isChecked()) {
		printops.multiplication_sign = MULTIPLICATION_SIGN_X;
	} else {
		printops.multiplication_sign = MULTIPLICATION_SIGN_ASTERISK;
	}
	if(divisionSlashButton->isChecked()) {
		printops.division_sign = DIVISION_SIGN_DIVISION_SLASH;
	} else if(divisionButton->isChecked()) {
		printops.division_sign = DIVISION_SIGN_DIVISION;
	} else {
		printops.division_sign = DIVISION_SIGN_SLASH;
	}
	mainWin->applyPreferences();
}
bool QalculatePreferencesDialog::editPreferences() {

	rpnKeypadOnlyButton->setChecked(rpn_keypad_only);
	dotAsSeparatorButton->setChecked(evalops.parse_options.dot_as_separator);
	systrayButton->setChecked(close_to_systray);
	statusButton->setChecked(display_expression_status);
	if(use_icon_buttons < 0) {
		iconsButton->setNoChange();
	} else {
		iconsButton->setChecked(use_icon_buttons > 0);
	}
	fetchExchangeRatesButton->setChecked(fetch_exchange_rates_at_startup);
	saveModeButton->setChecked(save_mode_on_exit);
	unicodeButton->setChecked(printops.use_unicode_signs);
	lowerCaseButton->setChecked(printops.lower_case_numbers);
	lowerCaseEButton->setChecked(printops.lower_case_e);
	alternativeBasePrefixButton->setChecked(printops.base_display == BASE_DISPLAY_ALTERNATIVE);
	spellOutLogicalOperatorsButton->setChecked(printops.spell_out_logical_operators);
	saveDefsButton->setChecked(save_defs_on_exit);
	useCustomResultFontButton->setChecked(use_custom_result_font);
	useCustomExpressionFontButton->setChecked(use_custom_expression_font);
	useCustomStatusFontButton->setChecked(use_custom_status_font);
	TQFont font1(resultWidget->font());
	font1.fromString(custom_result_font);
	resultFontEdit->setFont(font1);
	resultFontEdit->setEnabled(use_custom_result_font);
	TQFont font2(expressionWidget->font());
	font2.fromString(custom_expression_font);
	expressionFontEdit->setFont(font2);
	expressionFontEdit->setEnabled(use_custom_expression_font);
	TQFont font3(statusWidget_l->font());
	font3.fromString(custom_status_font);
	statusFontEdit->setFont(font3);
	statusFontEdit->setEnabled(use_custom_status_font);
	statusErrorColorButton->setColor(status_error_color);
	statusWarningColorButton->setColor(status_warning_color);
	if(can_display_unicode_string_function(SIGN_MULTIDOT, (void*) dotButton)) dotButton->setText(SIGN_MULTIDOT);
	else dotButton->setText(SIGN_SMALLCIRCLE);
	switch(printops.multiplication_sign) {
		case MULTIPLICATION_SIGN_DOT: {
			dotButton->setChecked(true);
			break;
		}
		case MULTIPLICATION_SIGN_X: {
			exButton->setChecked(true);
			break;
		}
		default: {
			asteriskButton->setChecked(true);
			break;
		}
	}
	asteriskButton->setEnabled(printops.use_unicode_signs);
	exButton->setEnabled(printops.use_unicode_signs);
	dotButton->setEnabled(printops.use_unicode_signs);
	multiplicationLabel->setEnabled(printops.use_unicode_signs);
	switch(printops.division_sign) {
		case DIVISION_SIGN_DIVISION_SLASH: {
			divisionSlashButton->setChecked(true);
			break;
		}
		case DIVISION_SIGN_DIVISION: {
			divisionButton->setChecked(true);
			break;
		}
		default: {
			slashButton->setChecked(true);
			break;
		}
	}
	slashButton->setEnabled(printops.use_unicode_signs);
	divisionSlashButton->setEnabled(printops.use_unicode_signs);
	divisionButton->setEnabled(printops.use_unicode_signs);
	divisionLabel->setEnabled(printops.use_unicode_signs);

	if(exec() == Accepted) {
		slotApply();
		return true;
	}
	return false;

}


#include "qalculatepreferencesdialog.moc"
