/***************************************************************************
 *   Copyright (C) 2005 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 "qalculateeditfunctiondialog.h"
#include "qalculate_tde_utils.h"
#include "qalculateeditnamesdialog.h"

#include <string>
#include <tqgrid.h>
#include <klineedit.h>
#include <kpushbutton.h>
#include <kcombobox.h>
#include <tqlabel.h>
#include <tdelocale.h>
#include <tqvbox.h>
#include <tqhbox.h>
#include <tqcheckbox.h>
#include <tdemessagebox.h>
#include <tqlayout.h>
#include <ktextedit.h>
#include <tdelistview.h>
#include <ktabwidget.h>
#include <tqheader.h>
#include <tdeapplication.h>
#include <kstdguiitem.h>

extern PrintOptions printops;
extern EvaluationOptions evalops;
extern tree_struct function_cats;

QalculateEditFunctionDialog::QalculateEditFunctionDialog(TQWidget *parent, const char *name) : KDialogBase(parent, name, true, i18n("Edit Function"), Ok | Cancel | Help, Ok, false) {

	names_edit_dialog = NULL;
	subfunctions_edit_dialog = NULL;
	edited_function = NULL;
	argument_edit_dialog = NULL;

	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("Function"));
	TQGridLayout *grid = new TQGridLayout(page1, 1, 1, spacingHint());
	TQVBoxLayout *vbox = new TQVBoxLayout(page2, spacingHint());
	grid->addWidget(new TQLabel(i18n("Name:"), page1), 0, 0);
	nameEdit = new KLineEdit(page1);
	grid->addWidget(nameEdit, 0, 1);
	TQHBoxLayout *hbox = new TQHBoxLayout(0, 0, spacingHint());
	grid->addMultiCellLayout(hbox, 1, 1, 0, 1);
	namesLabel = new TQLabel(page1);
	namesLabel->setAlignment(TQt::AlignRight);
	namesLabel->setSizePolicy(TQSizePolicy::Expanding, TQSizePolicy::Preferred);
	hbox->addWidget(namesLabel);
	namesButton = new TQPushButton(i18n("Names Details"), page1);
	hbox->addWidget(namesButton);
	grid->addWidget(new TQLabel(i18n("Category:"), page1), 2, 0);
	categoryCombo = new KComboBox(page1);
	categoryCombo->setMaximumWidth(250);
	categoryCombo->setEditable(true);
	grid->addWidget(categoryCombo, 2, 1);
	grid->addWidget(new TQLabel(i18n("Descriptive name:"), page1), 3, 0);
	titleEdit = new KLineEdit(page1);
	grid->addWidget(titleEdit, 3, 1);
	hbox = new TQHBoxLayout(0, 0, spacingHint());
	grid->addMultiCellLayout(hbox, 4, 4, 0, 1);
	hbox->addItem(new TQSpacerItem(0, 0, TQSizePolicy::Expanding, TQSizePolicy::Minimum));
	hideButton = new TQCheckBox(i18n("Hide function"), page1);
	hbox->addWidget(hideButton);
	grid->addWidget(new TQLabel(i18n("Description:"), page1), 5, 0);	
	descriptionEdit = new KTextEdit(page1);
	descriptionEdit->setTextFormat(TQTextEdit::PlainText);
	grid->addMultiCellWidget(descriptionEdit, 6, 6, 0, 1);
	vbox->addWidget(new TQLabel(i18n("Expression:"), page2));
	expressionEdit = new KTextEdit(page2);
	expressionEdit->setTextFormat(TQTextEdit::PlainText);
	vbox->addWidget(expressionEdit);
	hbox = new TQHBoxLayout(vbox, spacingHint());
	hbox->addWidget(new TQLabel(i18n("Condition:"), page2));
	conditionEdit = new KLineEdit(page2);
	hbox->addWidget(conditionEdit);
	hbox = new TQHBoxLayout(vbox, spacingHint());
	hbox->addItem(new TQSpacerItem(0, 0, TQSizePolicy::Expanding, TQSizePolicy::Minimum));
	subFunctionsButton = new TQPushButton(i18n("Sub-Functions"), page2);
	hbox->addWidget(subFunctionsButton);
	vbox->addWidget(new TQLabel(i18n("Arguments:"), page2));
	argumentsView = new TDEListView(page2);
	argumentsView->addColumn(i18n("Name"));
	argumentsView->addColumn(i18n("Type"));
	argumentsView->setSorting(-1);
	argumentsView->setRootIsDecorated(false);
	argumentsView->setItemsRenameable(true);
	argumentsView->setItemsMovable(false);
	vbox->addWidget(argumentsView);
	hbox = new TQHBoxLayout(vbox, spacingHint());
	hbox->addItem(new TQSpacerItem(0, 0, TQSizePolicy::Expanding, TQSizePolicy::Minimum));
	newArgumentButton = new TQPushButton(i18n("New"), page2);
	hbox->addWidget(newArgumentButton);
	editArgumentButton = new TQPushButton(i18n("Edit"), page2);
	hbox->addWidget(editArgumentButton);
	deleteArgumentButton = new TQPushButton(i18n("Delete"), page2);
	hbox->addWidget(deleteArgumentButton);

	connect(namesButton, TQ_SIGNAL(clicked()), this, TQ_SLOT(editNames()));
	connect(subFunctionsButton, TQ_SIGNAL(clicked()), this, TQ_SLOT(editSubFunctions()));
	connect(nameEdit, TQ_SIGNAL(textChanged(const TQString&)), this, TQ_SLOT(nameChanged(const TQString&)));
	connect(newArgumentButton, TQ_SIGNAL(clicked()), this, TQ_SLOT(newArgument()));
	connect(editArgumentButton, TQ_SIGNAL(clicked()), this, TQ_SLOT(editArgument()));
	connect(deleteArgumentButton, TQ_SIGNAL(clicked()), this, TQ_SLOT(deleteArgument()));
	connect(argumentsView, TQ_SIGNAL(itemRenamed(TQListViewItem*, const TQString&, int)), this, TQ_SLOT(argumentRenamed(TQListViewItem*, const TQString&, int)));
	connect(argumentsView, TQ_SIGNAL(selectionChanged()), this, TQ_SLOT(argumentSelectionChanged()));
	
}

QalculateEditFunctionDialog::~QalculateEditFunctionDialog() {}


void QalculateEditFunctionDialog::slotHelp() {
	TDEApplication::kApplication()->invokeHelp("qalculate-function-creation");
}

void QalculateEditFunctionDialog::argumentSelectionChanged() {
	TQListViewItem *i = argumentsView->selectedItem();
	if(i == NULL || (edited_function && edited_function->isBuiltin()) || argumentsItems[i]->type() > ARGUMENT_TYPE_ANGLE) {
		editArgumentButton->setEnabled(false);
		deleteArgumentButton->setEnabled(false);
	} else {
		editArgumentButton->setEnabled(true);
		deleteArgumentButton->setEnabled(true);
	}
}
void QalculateEditFunctionDialog::argumentRenamed(TQListViewItem *i, const TQString &str, int col) {
	if(!i || col != 0) return;
	if(col == 0) {
		if(!argumentsItemsEdited[i]) {
			argumentsItems[i] = argumentsItems[i]->copy();
			argumentsItemsEdited[i] = true;
		}
		argumentsItems[i]->setName(str.ascii());
	}
}

void QalculateEditFunctionDialog::slotOk() {

	std::string str = nameEdit->text().ascii();
	remove_blank_ends(str);
	std::string str2 = CALCULATOR->unlocalizeExpression(expressionEdit->text().ascii(), evalops.parse_options);
	remove_blank_ends(str2);
	gsub("\n", " ", str2);
	if(str.empty() && (!names_edit_dialog || names_edit_dialog->isEmpty())) {
		//no name -- open dialog again
		tabs->setCurrentPage(0);
		nameEdit->setFocus();
		KMessageBox::error(this, i18n("Empty name field"));
		return;
	}
	if(!(edited_function && edited_function->isBuiltin()) && str2.empty()) {
		//no expression/relation -- open dialog again		
		tabs->setCurrentPage(1);
		expressionEdit->setFocus();
		KMessageBox::error(this, i18n("Empty expression field"));
		return;
	}
	//function with the same name exists -- overwrite or open dialog again
	if((!edited_function || !edited_function->hasName(str)) && (!names_edit_dialog || names_edit_dialog->isEmpty()) && CALCULATOR->functionNameTaken(str, edited_function) && KMessageBox::questionYesNo(this, i18n("A function with the same name already exists.\nDo you want to overwrite it?")) == KMessageBox::No) {
		tabs->setCurrentPage(0);
		nameEdit->setFocus();
		return;
	}
	
	MathFunction *f = edited_function;

	bool add_func = false;
	if(f) {
		f->setLocal(true);
		//edited an existing function
		f->setCategory(categoryCombo->lineEdit()->text().ascii());
		f->setTitle(titleEdit->text().ascii());
		f->setDescription(descriptionEdit->text().ascii());
	} else {
		//new function
		f = new UserFunction(categoryCombo->lineEdit()->text().ascii(), "", "", true, -1, titleEdit->text().ascii(), descriptionEdit->text().ascii());
		add_func = true;
	}
	f->setCondition(CALCULATOR->unlocalizeExpression(conditionEdit->text().ascii(), evalops.parse_options));
	size_t index = 1;
	TQListViewItemIterator it(argumentsView);
	while(it.current()) {
		if(argumentsItemsEdited[it.current()]) {
			if(f->isBuiltin() && f->getArgumentDefinition(index)) {
				f->getArgumentDefinition(index)->setName(argumentsItems[it.current()]->name());
				delete argumentsItems[it.current()];
			} else {
				f->setArgumentDefinition(index, argumentsItems[it.current()]);
			}
		}
		++it;
		index++;
	}
	if(!f->isBuiltin()) {
		for(; index <= f->lastArgumentDefinitionIndex(); index++) {
			f->setArgumentDefinition(index, NULL);
		}
	}
	argumentsItemsEdited.clear();
	argumentsItems.clear();
	f->setHidden(hideButton->isChecked());
	if(!f->isBuiltin()) {
		if(names_edit_dialog) {
			names_edit_dialog->saveNames(f, str.c_str());
		} else {
			if(f->countNames() == 0) {
				ExpressionName ename(str);
				ename.reference = true;
				f->setName(ename, 1);
			} else {
				f->setName(str, 1);
			}
		}
		if(subfunctions_edit_dialog) {
			subfunctions_edit_dialog->saveSubFunctions((UserFunction*) f);
		}
		((UserFunction*) f)->setFormula(str2);
	}
	if(add_func) {
		CALCULATOR->addFunction(f);
	}
	edited_function = f;
	
	accept();

}

MathFunction *QalculateEditFunctionDialog::editFunction(TQString category, MathFunction *f) {

	nameEdit->setFocus();

	tabs->setCurrentPage(0);
	if(names_edit_dialog) {
		delete names_edit_dialog;
		names_edit_dialog = NULL;
	}
	if(subfunctions_edit_dialog) {
		delete subfunctions_edit_dialog;
		subfunctions_edit_dialog = NULL;
	}
	edited_function = f;
	if(f) {
		if(f->isLocal())
			setCaption(i18n("Edit Function"));
		else
			setCaption(i18n("Edit Function (global)"));
	} else {
		setCaption(i18n("New Function"));
	}

	TQString catstr;
	categoryCombo->clear();
	tree_struct *item, *item2;
	function_cats.it = function_cats.items.begin();
	if(function_cats.it != function_cats.items.end()) {
		item = &*function_cats.it;
		++function_cats.it;
		item->it = item->items.begin();
	} else {
		item = NULL;
	}

	catstr = "";
	while(item) {
		if(!catstr.isEmpty()) catstr += "/";
		catstr += item->item.c_str();
		categoryCombo->insertItem(catstr);
		while(item && item->it == item->items.end()) {
			int str_i = catstr.findRev("/");
			if(str_i < 0) {
				catstr = "";
			} else {
				catstr.truncate(str_i);
			}
			item = item->parent;
		}
		if(item) {
			item2 = &*item->it;
			++item->it;
			item = item2;
			item->it = item->items.begin();	
		}
	}

	//clear entries
	nameEdit->clear();
	namesLabel->clear();
	conditionEdit->clear();
	nameEdit->setReadOnly(f && f->isBuiltin());
	expressionEdit->setEnabled(!f || !f->isBuiltin());
	categoryCombo->lineEdit()->setText(category);
	titleEdit->clear();
	descriptionEdit->clear();
	expressionEdit->clear();
	hideButton->setChecked(false);
	actionButton(Ok)->setEnabled(true);
	subFunctionsButton->setEnabled(!f || !f->isBuiltin());
	conditionEdit->setReadOnly(f && f->isBuiltin());
	newArgumentButton->setEnabled(!f || !f->isBuiltin());
	if(f) {
		//fill in original paramaters
		set_name_label_and_entry(f, nameEdit, namesLabel);
		if(!f->isBuiltin()) {
			expressionEdit->setText(CALCULATOR->localizeExpression(((UserFunction*) f)->formula()).c_str());
		}
		categoryCombo->lineEdit()->setText(f->category().c_str());
		titleEdit->setText(f->title(false).c_str());
		conditionEdit->setText(CALCULATOR->localizeExpression(f->condition()).c_str());
		hideButton->setChecked(f->isHidden());
		descriptionEdit->setText(f->description().c_str());
		
	}
	argumentsView->clear();
	argumentsItems.clear();
	argumentsItemsEdited.clear();
	editArgumentButton->setEnabled(false);
	deleteArgumentButton->setEnabled(false);
	Argument defarg;
	if(f) {
		Argument *arg;
		int args = f->maxargs();
		if(args < 0) {
			args = f->minargs() + 1;	
		}
		TQString str, str2;
		TQListViewItem *i_prev = NULL;
		for(int i = 1; i <= args; i++) {
			arg = f->getArgumentDefinition(i);
			if(arg) {
				arg = arg->copy();
				str = arg->printlong().c_str();
				str2 = arg->name().c_str();
			} else {
				arg = &defarg;
				str = defarg.printlong().c_str();
				str2 = "";
			}
			TQListViewItem *i_new = new TDEListViewItem(argumentsView, i_prev, str2, str);
			i_prev = i_new;
			argumentsItems[i_new] = arg;
			argumentsItemsEdited[i_new] = false;
		}
	}
	argumentsView->setResizeMode(TDEListView::AllColumns);

	if(exec() == TQDialog::Accepted) {
		f = edited_function;
		edited_function = NULL;
		return f;
	}
	TQListViewItemIterator it(argumentsView);
	while(it.current()) {
		if(argumentsItemsEdited[it.current()]) {
			delete argumentsItems[it.current()];
		}
		++it;
	}
	argumentsItemsEdited.clear();
	argumentsItems.clear();
	edited_function = NULL;
	return NULL;
}

void QalculateEditFunctionDialog::editNames() {
	if(!names_edit_dialog) {
		names_edit_dialog = new QalculateEditNamesDialog(TYPE_FUNCTION, this);
		names_edit_dialog->setNames(edited_function, nameEdit->text(), false);
	} else {
		names_edit_dialog->setNames(edited_function, nameEdit->text(), true);
	}
	names_edit_dialog->exec();
	names_edit_dialog->setNamesLE(nameEdit, namesLabel);
}
void QalculateEditFunctionDialog::newArgument() {
	if(!argument_edit_dialog) {
		argument_edit_dialog = new QalculateEditArgumentDialog(this);
	}
	Argument *arg = argument_edit_dialog->editArgument();
	if(arg) {
		TQListViewItem *i = new TDEListViewItem(argumentsView, argumentsView->lastChild(), arg->name().c_str(), arg->printlong().c_str());
		argumentsItems[i] = arg;
		argumentsItemsEdited[i] = true;
	}
}
void QalculateEditFunctionDialog::editArgument() {
	TQListViewItem *i = argumentsView->selectedItem();
	if(i == NULL) return;
	if(!argument_edit_dialog) {
		argument_edit_dialog = new QalculateEditArgumentDialog(this);
	}
	Argument *arg = argument_edit_dialog->editArgument(argumentsItems[i]);
	if(arg) {
		if(argumentsItemsEdited[i]) {
			delete argumentsItems[i];
		}
		argumentsItemsEdited[i] = true;
		argumentsItems[i] = arg;
		i->setText(0, arg->name().c_str());
		i->setText(1, arg->printlong().c_str());
	}
}
void QalculateEditFunctionDialog::deleteArgument() {
	TQListViewItem *i = argumentsView->selectedItem();
	if(i == NULL) return;
	if(argumentsItemsEdited[i]) {
		delete argumentsItems[i];
	}
	argumentsItemsEdited.erase(i);
	argumentsItems.erase(i);
	argumentsView->removeItem(i);
}
void QalculateEditFunctionDialog::editSubFunctions() {
	if(edited_function && edited_function->isBuiltin()) return;
	if(!subfunctions_edit_dialog) {
		subfunctions_edit_dialog = new QalculateEditSubFunctionsDialog(this);
		subfunctions_edit_dialog->setSubFunctions((UserFunction*) edited_function, false);
	} else {
		subfunctions_edit_dialog->setSubFunctions((UserFunction*) edited_function, true);
	}
	subfunctions_edit_dialog->exec();
}

void QalculateEditFunctionDialog::nameChanged(const TQString &name) {
	if(name.isNull()) return;
	if(!CALCULATOR->functionNameIsValid(name.ascii())) {
		nameEdit->blockSignals(true);
		nameEdit->setText(CALCULATOR->convertToValidFunctionName(name.ascii()).c_str());
		nameEdit->blockSignals(false);
	}
}

QalculateEditSubFunctionsDialog::QalculateEditSubFunctionsDialog(TQWidget *parent, const char *name) : KDialog(parent, name, true) {

	setCaption(i18n("Sub-Functions"));

	TQVBoxLayout *layout = new TQVBoxLayout(this, marginHint(), spacingHint());

	subFunctionsView = new TDEListView(this);
	subFunctionsView->addColumn(i18n("Reference"));
	subFunctionsView->addColumn(i18n("Expression"));
	subFunctionsView->addColumn(i18n("Precalculate"));
	subFunctionsView->setSorting(-1);
	subFunctionsView->setItemsRenameable(true);
	subFunctionsView->setRenameable(0, false);
	subFunctionsView->setRenameable(1, true);
	subFunctionsView->setItemsMovable(false);
	layout->addWidget(subFunctionsView);

	TQHBoxLayout *buttonLayout = new TQHBoxLayout(layout, spacingHint());
	newButton = new TQPushButton(i18n("New"), this);
	buttonLayout->addWidget(newButton);
	deleteButton = new TQPushButton(i18n("Delete"), this);
	deleteButton->setEnabled(false);
	buttonLayout->addWidget(deleteButton);
	buttonLayout->addItem(new TQSpacerItem(0, 0, TQSizePolicy::Expanding, TQSizePolicy::Minimum));
	buttonClose = new KPushButton(KStdGuiItem::close(), this);
	buttonClose->setAutoDefault(true);
	buttonClose->setDefault(true);
	buttonLayout->addWidget(buttonClose);

	connect(subFunctionsView, TQ_SIGNAL(selectionChanged()), this, TQ_SLOT(selectionChanged()));
	connect(buttonClose, TQ_SIGNAL(clicked()), this, TQ_SLOT(accept()));
	connect(newButton, TQ_SIGNAL(clicked()), this, TQ_SLOT(newSubFunction()));
	connect(deleteButton, TQ_SIGNAL(clicked()), this, TQ_SLOT(deleteSubFunction()));
	connect(subFunctionsView, TQ_SIGNAL(clicked(TQListViewItem*, const TQPoint&, int)), this, TQ_SLOT(itemClicked(TQListViewItem*, const TQPoint&, int)));
}

QalculateEditSubFunctionsDialog::~QalculateEditSubFunctionsDialog() {}

void QalculateEditSubFunctionsDialog::newSubFunction() {
	ExpressionName name;
	TQString str = "\\";
	str += TQString::number(subFunctionsView->childCount() + 1);
	TQListViewItem *i = new TDEListViewItem(subFunctionsView, subFunctionsView->lastChild(), str, "", i18n("Yes"));
	subFunctionsView->setSelected(i, true);
	tqApp->processEvents();
	subFunctionsView->rename(i, 1);
}

void QalculateEditSubFunctionsDialog::setSubFunctions(UserFunction *f, bool edited) {
	if(!edited) {
		deleteButton->setEnabled(false);
		subFunctionsView->clear();
	}
	if(!edited && f && f->countSubfunctions() > 0) {
		TQListViewItem *i_prev = NULL;
		for(size_t index = 1; index <= f->countSubfunctions(); index++) {
			TQListViewItem *i = new TDEListViewItem(subFunctionsView, i_prev);
			i_prev = i;
			TQString str = "\\";
			str += TQString::number(index);
			i->setText(0, str);
			i->setText(1, f->getSubfunction(index).c_str());
			if(f->subfunctionPrecalculated(index)) i->setText(2, i18n("Yes"));
			else i->setText(2, i18n("No"));
		}
	}
}
void QalculateEditSubFunctionsDialog::saveSubFunctions(UserFunction *f) {
	f->clearSubfunctions();
	TQListViewItemIterator it(subFunctionsView);
	while(it.current()) {
		f->addSubfunction(it.current()->text(1).ascii(), it.current()->text(2) == i18n("Yes"));
		++it;
	}
}
bool QalculateEditSubFunctionsDialog::isEmpty() {
	return subFunctionsView->childCount() <= 0;
}

void QalculateEditSubFunctionsDialog::deleteSubFunction() {
	subFunctionsView->removeItem(subFunctionsView->selectedItem());
	TQListViewItemIterator it(subFunctionsView);
	int index = 1;
	while(it.current()) {
		TQString str = "\\";
		str += TQString::number(index);
		it.current()->setText(0, str);
		++it;
		index++;
	}
}

void QalculateEditSubFunctionsDialog::itemClicked(TQListViewItem *i, const TQPoint &, int c) {
	if(i && c == 2) {
		if(i->text(c) == i18n("Yes"))
			i->setText(c, i18n("No"));
		else
			i->setText(c, i18n("Yes"));
	}
}

void QalculateEditSubFunctionsDialog::selectionChanged() {
	deleteButton->setEnabled(subFunctionsView->selectedItem() != NULL);
}

QalculateEditArgumentDialog::QalculateEditArgumentDialog(TQWidget *parent, const char *name) : KDialogBase(parent, name, true, i18n("Edit Argument"), Ok | Cancel, Ok, true) {

	setMainWidget(new TQWidget(this));
	TQGridLayout *grid = new TQGridLayout(mainWidget(), 1, 1, 0, spacingHint());
	grid->addWidget(new TQLabel(i18n("Name:"), mainWidget()), 0, 0);
	nameEdit = new KLineEdit(mainWidget());
	grid->addWidget(nameEdit, 0, 1);
	grid->addWidget(new TQLabel(i18n("Type:"), mainWidget()), 1, 0);
	typeCombo = new KComboBox(mainWidget());
	typeCombo->setEditable(false);
	typeCombo->insertItem(i18n("Free"));
	typeCombo->insertItem(i18n("Number"));
	typeCombo->insertItem(i18n("Integer"));
	typeCombo->insertItem(i18n("Symbol"));
	typeCombo->insertItem(i18n("Text"));
	typeCombo->insertItem(i18n("Date"));
	typeCombo->insertItem(i18n("Vector"));
	typeCombo->insertItem(i18n("Matrix"));
	typeCombo->insertItem(i18n("Boolean"));
	typeCombo->insertItem(i18n("Object"));
	typeCombo->insertItem(i18n("Function"));
	typeCombo->insertItem(i18n("Unit"));
	typeCombo->insertItem(i18n("Variable"));
	typeCombo->insertItem(i18n("File"));
	typeCombo->insertItem(i18n("Angle"));
	grid->addWidget(typeCombo, 1, 1);
	testButton = new TQCheckBox(i18n("Enable rules and type test"), mainWidget());
	grid->addMultiCellWidget(testButton, 2, 2, 0, 1);
	conditionButton = new TQCheckBox(i18n("Custom condition:"), mainWidget());
	grid->addWidget(conditionButton, 3, 0);
	conditionEdit = new KLineEdit(mainWidget());
	grid->addWidget(conditionEdit, 3, 1);
	matrixButton = new TQCheckBox(i18n("Allow matrix"), mainWidget());
	grid->addMultiCellWidget(matrixButton, 4, 4, 0, 1);
	zeroButton = new TQCheckBox(i18n("Forbid zero"), mainWidget());
	grid->addMultiCellWidget(zeroButton, 5, 5, 0, 1);
	complexButton = new TQCheckBox(i18n("Allow complex"), mainWidget());
	grid->addMultiCellWidget(complexButton, 6, 6, 0, 1);
	minButton = new TQCheckBox(i18n("Min:"), mainWidget());
	grid->addWidget(minButton, 7, 0);
	minEdit = new KLineEdit(mainWidget());
	grid->addWidget(minEdit, 7, 1);
	TQHBoxLayout *hbox = new TQHBoxLayout(0, 0, spacingHint());
	grid->addMultiCellLayout(hbox, 8, 8, 0, 1);
	hbox->addItem(new TQSpacerItem(0, 0, TQSizePolicy::Expanding, TQSizePolicy::Minimum));
	minIncludeEqualsButton = new TQCheckBox(i18n("Include equals"), mainWidget());
	hbox->addWidget(minIncludeEqualsButton);
	maxButton = new TQCheckBox(i18n("Max:"), mainWidget());
	grid->addWidget(maxButton, 9, 0);
	maxEdit = new KLineEdit(mainWidget());
	grid->addWidget(maxEdit, 9, 1);
	hbox = new TQHBoxLayout(0, 0, spacingHint());
	grid->addMultiCellLayout(hbox, 10, 10, 0, 1);
	hbox->addItem(new TQSpacerItem(0, 0, TQSizePolicy::Expanding, TQSizePolicy::Minimum));
	maxIncludeEqualsButton = new TQCheckBox(i18n("Include equals"), mainWidget());
	hbox->addWidget(maxIncludeEqualsButton);

	connect(conditionButton, TQ_SIGNAL(toggled(bool)), conditionEdit, TQ_SLOT(setEnabled(bool)));
	connect(minButton, TQ_SIGNAL(toggled(bool)), this, TQ_SLOT(minEnabled(bool)));
	connect(maxButton, TQ_SIGNAL(toggled(bool)), this, TQ_SLOT(maxEnabled(bool)));
	connect(typeCombo, TQ_SIGNAL(activated(int)), this, TQ_SLOT(typeChanged(int)));
	
}

QalculateEditArgumentDialog::~QalculateEditArgumentDialog() {}

void QalculateEditArgumentDialog::typeChanged(int i) {
	maxButton->setEnabled(i == 1 || i == 2);
	maxEdit->setEnabled(maxButton->isChecked() && (i == 1 || i == 2));
	maxIncludeEqualsButton->setEnabled(maxButton->isChecked() && i == 1);
	maxIncludeEqualsButton->setChecked(i == 1 || i  == 2);
	minButton->setEnabled(i == 1 || i == 2);
	minEdit->setEnabled(maxButton->isChecked() && (i == 1 || i == 2));
	minIncludeEqualsButton->setEnabled(minButton->isChecked() && i == 1);
	minIncludeEqualsButton->setChecked(i == 1 || i  == 2);
	matrixButton->setEnabled(i != 7 && i != 0);
	matrixButton->setChecked(i == 7 || i == 0);
	complexButton->setEnabled(i == 1);
	complexButton->setChecked(i == 1);
}
void QalculateEditArgumentDialog::maxEnabled(bool b) {
	maxEdit->setEnabled(b);
	if(typeCombo->currentItem() == 2) {
		maxIncludeEqualsButton->setEnabled(b);
	}
}
void QalculateEditArgumentDialog::minEnabled(bool b) {
	minEdit->setEnabled(b);
	if(typeCombo->currentItem() == 2) {
		minIncludeEqualsButton->setEnabled(b);
	}
}

Argument *QalculateEditArgumentDialog::editArgument(Argument *arg) {

	if(arg) {
		setCaption(i18n("Edit Argument"));
	} else {
		setCaption(i18n("New Argument"));
	}

	nameEdit->setFocus();

	typeCombo->setCurrentItem(0);
	conditionEdit->clear();
	conditionButton->setChecked(false);
	nameEdit->clear();
	maxEdit->setText("0");
	minEdit->setText("0");
	testButton->setChecked(true);
	zeroButton->setChecked(false);
	matrixButton->setChecked(false);
	complexButton->setChecked(false);
	PrintOptions po;
	po.number_fraction_format = FRACTION_DECIMAL_EXACT;
	if(arg) {
		switch(arg->type()) {
			case ARGUMENT_TYPE_NUMBER: {
				typeCombo->setCurrentItem(1);
				NumberArgument *farg = (NumberArgument*) arg;
				minButton->setChecked(farg->min() != NULL);
				minIncludeEqualsButton->setChecked(farg->includeEqualsMin());
				if(farg->min()) minEdit->setText(farg->min()->print(po).c_str());
				maxButton->setChecked(farg->max() != NULL);
				maxIncludeEqualsButton->setChecked(farg->includeEqualsMax());
				if(farg->max()) maxEdit->setText(farg->max()->print(po).c_str());
				complexButton->setChecked(farg->complexAllowed());
				break;
			}
			case ARGUMENT_TYPE_INTEGER: {
				typeCombo->setCurrentItem(2);
				IntegerArgument *iarg = (IntegerArgument*) arg;
				minButton->setChecked(iarg->min() != NULL);
				if(iarg->min()) minEdit->setText(iarg->min()->print(po).c_str());
				maxButton->setChecked(iarg->max() != NULL);
				if(iarg->max()) maxEdit->setText(iarg->max()->print(po).c_str());
				break;
			}
			case ARGUMENT_TYPE_TEXT: {typeCombo->setCurrentItem(4); break;}
			case ARGUMENT_TYPE_SYMBOLIC: {typeCombo->setCurrentItem(3); break;}
			case ARGUMENT_TYPE_DATE: {typeCombo->setCurrentItem(5); break;}
			case ARGUMENT_TYPE_VECTOR: {typeCombo->setCurrentItem(6); break;}
			case ARGUMENT_TYPE_MATRIX: {typeCombo->setCurrentItem(7); break;}
			case ARGUMENT_TYPE_EXPRESSION_ITEM: {typeCombo->setCurrentItem(9); break;}
			case ARGUMENT_TYPE_FUNCTION: {typeCombo->setCurrentItem(10); break;}
			case ARGUMENT_TYPE_UNIT: {typeCombo->setCurrentItem(11); break;}
			case ARGUMENT_TYPE_VARIABLE: {typeCombo->setCurrentItem(12); break;}
			case ARGUMENT_TYPE_FILE: {typeCombo->setCurrentItem(13); break;}
			case ARGUMENT_TYPE_BOOLEAN: {typeCombo->setCurrentItem(8); break;}
			case ARGUMENT_TYPE_ANGLE: {typeCombo->setCurrentItem(14); break;}
		}
		nameEdit->setText(arg->name().c_str());
		matrixButton->setChecked(arg->matrixAllowed());
		zeroButton->setChecked(arg->zeroForbidden());
		testButton->setChecked(arg->tests());
		if(!arg->getCustomCondition().empty()) {
			conditionButton->setChecked(true);
			conditionEdit->setText(arg->getCustomCondition().c_str());
		}
	}
	typeChanged(typeCombo->currentItem());

	if(exec() == TQDialog::Accepted) {
		switch(typeCombo->currentItem()) {
			case 1: {
				arg = new NumberArgument();
				if(minButton->isChecked()) {
					Number nr(minEdit->text().ascii());
					((NumberArgument*) arg)->setMin(&nr);
				}
				((NumberArgument*) arg)->setIncludeEqualsMin(minIncludeEqualsButton->isChecked());
				if(maxButton->isChecked()) {
					Number nr(maxEdit->text().ascii());
					((NumberArgument*) arg)->setMax(&nr);
				}
				((NumberArgument*) arg)->setIncludeEqualsMax(maxIncludeEqualsButton->isChecked());
				break;
			}
			case 2: {
				arg = new IntegerArgument();
				if(minButton->isChecked()) {
					Number nr(minEdit->text().ascii());
					((IntegerArgument*) arg)->setMin(&nr);
				}
				if(maxButton->isChecked()) {
					Number nr(maxEdit->text().ascii());
					((IntegerArgument*) arg)->setMax(&nr);
				}
				break;
			}
			case 3: {arg = new SymbolicArgument(); break;}
			case 4: {arg = new TextArgument(); break;}
			case 5: {arg = new DateArgument(); break;}
			case 6: {arg = new VectorArgument(); break;}
			case 7: {arg = new MatrixArgument(); break;}
			case 8: {arg = new BooleanArgument(); break;}
			case 9: {arg = new ExpressionItemArgument(); break;}
			case 10: {arg = new FunctionArgument(); break;}
			case 11: {arg = new UnitArgument(); break;}
			case 12: {arg = new VariableArgument(); break;}
			case 13: {arg = new FileArgument(); break;}
			case 14: {arg = new AngleArgument(); break;}
			default: {arg = new Argument(); break;}
		}
		arg->setName(nameEdit->text().ascii());
		arg->setZeroForbidden(zeroButton->isChecked());
		arg->setTests(testButton->isChecked());
		if(conditionButton->isChecked()) {
			arg->setCustomCondition(conditionEdit->text().ascii());
		} else {
			arg->setCustomCondition("");
		}
		if(arg->type() != ARGUMENT_TYPE_FREE && arg->type() != ARGUMENT_TYPE_MATRIX) arg->setMatrixAllowed(matrixButton->isChecked());
		return arg;	
	}
	return NULL;

}

#include "qalculateeditfunctiondialog.moc"

