/***************************************************************************
 *   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 "qalculateperiodictabledialog.h"
#include "qalculate_tde_utils.h"
#include <tdelocale.h>
#include <tqlayout.h>
#include <tqlabel.h>
#include <tqtooltip.h>
#include <tqcolor.h>
#include <tqpushbutton.h>
#include <tqvbox.h>
#include <tqgrid.h>
#include "qalculatebuttonwithdata.h"
#include <kiconloader.h>
#include <tqfontmetrics.h>
#include <kactivelabel.h>
#include <tqlineedit.h>

extern PrintOptions printops;
extern TQWidget *expressionWidget;

QalculatePeriodicTableDialog::QalculatePeriodicTableDialog(TQWidget *parent, const char *name)
		: KDialogBase(parent, name, false, i18n("Periodic Table"), Close, Close, true) {

	actionButton(Close)->setAutoDefault(false);
	actionButton(Close)->setDefault(false);

	grid = new TQGridLayout(makeMainWidget(), 11, 20, 0, 1);

	grid->addWidget(new TQLabel("1", mainWidget()), 0, 1, TQt::AlignHCenter);
	grid->addWidget(new TQLabel("2", mainWidget()), 0, 2, TQt::AlignHCenter);
	grid->addWidget(new TQLabel("3", mainWidget()), 0, 4, TQt::AlignHCenter);
	grid->addWidget(new TQLabel("4", mainWidget()), 0, 5, TQt::AlignHCenter);
	grid->addWidget(new TQLabel("5", mainWidget()), 0, 6, TQt::AlignHCenter);
	grid->addWidget(new TQLabel("6", mainWidget()), 0, 7, TQt::AlignHCenter);
	grid->addWidget(new TQLabel("7", mainWidget()), 0, 8, TQt::AlignHCenter);
	grid->addWidget(new TQLabel("8", mainWidget()), 0, 9, TQt::AlignHCenter);
	grid->addWidget(new TQLabel("9", mainWidget()), 0, 10, TQt::AlignHCenter);
	grid->addWidget(new TQLabel("10", mainWidget()), 0, 11, TQt::AlignHCenter);
	grid->addWidget(new TQLabel("11", mainWidget()), 0, 12, TQt::AlignHCenter);
	grid->addWidget(new TQLabel("12", mainWidget()), 0, 13, TQt::AlignHCenter);
	grid->addWidget(new TQLabel("13", mainWidget()), 0, 14, TQt::AlignHCenter);
	grid->addWidget(new TQLabel("14", mainWidget()), 0, 15, TQt::AlignHCenter);
	grid->addWidget(new TQLabel("15", mainWidget()), 0, 16, TQt::AlignHCenter);
	grid->addWidget(new TQLabel("16", mainWidget()), 0, 17, TQt::AlignHCenter);
	grid->addWidget(new TQLabel("17", mainWidget()), 0, 18, TQt::AlignHCenter);
	grid->addWidget(new TQLabel("18", mainWidget()), 0, 19, TQt::AlignHCenter);

	grid->addWidget(new TQLabel("1 ", mainWidget()), 1, 0, TQt::AlignRight);
	grid->addWidget(new TQLabel("2 ", mainWidget()), 2, 0, TQt::AlignRight);
	grid->addWidget(new TQLabel("3 ", mainWidget()), 3, 0, TQt::AlignRight);
	grid->addWidget(new TQLabel("4 ", mainWidget()), 4, 0, TQt::AlignRight);
	grid->addWidget(new TQLabel("5 ", mainWidget()), 5, 0, TQt::AlignRight);
	grid->addWidget(new TQLabel("6 ", mainWidget()), 6, 0, TQt::AlignRight);
	grid->addWidget(new TQLabel("7 ", mainWidget()), 7, 0, TQt::AlignRight);

	grid->addWidget(new TQLabel("*", mainWidget()), 6, 3, TQt::AlignRight);
	grid->addWidget(new TQLabel("**", mainWidget()), 7, 3, TQt::AlignRight);
	grid->addWidget(new TQLabel("*", mainWidget()), 9, 3, TQt::AlignRight);
	grid->addWidget(new TQLabel("**", mainWidget()), 10, 3, TQt::AlignRight);
	grid->addWidget(new TQLabel(" ", mainWidget()), 8, 0);

	DataSet *dc = CALCULATOR->getDataSet("atom");
	if(!dc) {
		return;
	}
	DataObject *e;
	QalculateButtonWithData *e_button;
	TQString tip;
	DataProperty *p_xpos = dc->getProperty("x_pos");
	DataProperty *p_ypos = dc->getProperty("y_pos");
	DataProperty *p_weight = dc->getProperty("weight");
	DataProperty *p_number = dc->getProperty("number");
	DataProperty *p_symbol = dc->getProperty("symbol");
	DataProperty *p_class = dc->getProperty("class");
	DataProperty *p_name = dc->getProperty("name");
	int x_pos = 0, y_pos = 0, group = 0;
	TQString weight;
	int w = -1;
	for(size_t i = 1; i < 120; i++) {
		e = dc->getObject(i2s(i));
		if(e) {
			x_pos = s2i(e->getProperty(p_xpos));
			y_pos = s2i(e->getProperty(p_ypos));
		}
		if(e && x_pos > 0 && x_pos <= 18 && y_pos > 0 && y_pos <= 10) {
			e_button = new QalculateButtonWithData((void*) e, e->getProperty(p_symbol).c_str(), mainWidget());
			e_button->setFlat(true);
			group = s2i(e->getProperty(p_class));
			switch(group) {
				case 1: {
					e_button->setPaletteBackgroundColor(TQColor(0xee, 0xcc, 0xee));
					e_button->setPaletteForegroundColor(TQColor(0, 0, 0));
					break;
				}
				case 2: {
					e_button->setPaletteBackgroundColor(TQColor(0xdd, 0xcc, 0xee));
					e_button->setPaletteForegroundColor(TQColor(0, 0, 0));
					break;
				}
				case 3: {
					e_button->setPaletteBackgroundColor(TQColor(0xdd, 0xdd, 0xff));
					e_button->setPaletteForegroundColor(TQColor(0, 0, 0));
					break;
				}
				case 4: {
					e_button->setPaletteBackgroundColor(TQColor(0xdd, 0xee, 0xff));
					e_button->setPaletteForegroundColor(TQColor(0, 0, 0));
					break;
				}
				case 5: {
					e_button->setPaletteBackgroundColor(TQColor(0xcc, 0xee, 0xee));
					e_button->setPaletteForegroundColor(TQColor(0, 0, 0));
					break;
				}
				case 6: {
					e_button->setPaletteBackgroundColor(TQColor(0xbb, 0xff, 0xbb));
					e_button->setPaletteForegroundColor(TQColor(0, 0, 0));
					break;
				}
				case 7: {
					e_button->setPaletteBackgroundColor(TQColor(0xee, 0xff, 0xdd));
					e_button->setPaletteForegroundColor(TQColor(0, 0, 0));
					break;
				}
				case 8: {
					e_button->setPaletteBackgroundColor(TQColor(0xff, 0xff, 0xaa));
					e_button->setPaletteForegroundColor(TQColor(0, 0, 0));
					break;
				}
				case 9: {
					e_button->setPaletteBackgroundColor(TQColor(0xff, 0xdd, 0xaa));
					e_button->setPaletteForegroundColor(TQColor(0, 0, 0));
					break;
				}
				case 10: {
					e_button->setPaletteBackgroundColor(TQColor(0xff, 0xcc, 0xdd));
					e_button->setPaletteForegroundColor(TQColor(0, 0, 0));
					break;
				}
				case 11: {
					e_button->setPaletteBackgroundColor(TQColor(0xaa, 0xee, 0xdd));
					e_button->setPaletteForegroundColor(TQColor(0, 0, 0));
					break;
				}
			}
			if(w < 0) {
				w = e_button->fontMetrics().width("Uuo") + 12;
			}
			e_button->setFixedWidth(w);
			e_button->setFixedHeight(w);
			if(x_pos > 2) {
				grid->addWidget(e_button, y_pos, x_pos + 1);
			} else {
				grid->addWidget(e_button, y_pos, x_pos);
			}
			tip = e->getProperty(p_number).c_str();
			tip += " ";
			tip += e->getProperty(p_name).c_str();
			weight = e->getPropertyDisplayString(p_weight).c_str();
			if(!weight.isEmpty() && weight != "-") {
				tip += "\n";
				tip += weight;
			}
			TQToolTip::add(e_button, tip);
			TQObject::connect(e_button, TQ_SIGNAL(clickedWithData(void*)), this, TQ_SLOT(showElementInfo(void*)));
		}
	}

}
QalculatePeriodicTableDialog::~QalculatePeriodicTableDialog() {}
void QalculatePeriodicTableDialog::showElementInfo(void *e) {
	DataObject *o = (DataObject*) e;
	QalculateElementInfoDialog *dialog = new QalculateElementInfoDialog(o, this);
	dialog->show();
}

QalculateElementInfoDialog::QalculateElementInfoDialog(DataObject *element, TQWidget *parent, const char *name) : KDialogBase(parent, name, false, i18n("Element Data"), Close, Close, true) {

	setWFlags(getWFlags() | TQt::WDestructiveClose);
	e = element;
	DataSet *ds = e->parentSet();
	if(!ds) {
		close();
		return;
	}
	TQVBox *vbox = makeVBoxMainWidget();
	TQVBox *vbox2 = new TQVBox(vbox);
	DataProperty *p_number = ds->getProperty("number");
	DataProperty *p_symbol = ds->getProperty("symbol");
	DataProperty *p_class = ds->getProperty("class");
	DataProperty *p_name = ds->getProperty("name");
	TQString str = "<div align=\"right\"><font size=4>"; str += e->getProperty(p_number).c_str(); str += "</font></div>";
	new KActiveLabel(str, vbox2);
	str = "<font size=6>"; str += e->getProperty(p_symbol).c_str(); str += "</font>";
	new KActiveLabel(str, vbox2);
	str = "<font size=5>"; str += e->getProperty(p_name).c_str(); str += "</font>  ";
	new KActiveLabel(str, vbox2);

	QalculateButtonWithData *button;
	TQGrid *grid = new TQGrid(3, TQt::Horizontal, vbox);
	int group = s2i(e->getProperty(p_class));
	if(group > 0) {
		str = "<b>"; str += i18n("Classification"); str += ":"; str += "</b>";
		new TQLabel(str, grid);
		switch(group) {
			case ALKALI_METALS: {str = i18n("Alkali Metal"); break;}
			case ALKALI_EARTH_METALS: {str = i18n("Alkaline-Earth Metal"); break;}
			case LANTHANIDES: {str = i18n("Lanthanide"); break;}
			case ACTINIDES: {str = i18n("Actinide"); break;}
			case TRANSITION_METALS: {str = i18n("Transition Metal"); break;}
			case METALS: {str = i18n("Metal"); break;}
			case METALLOIDS: {str = i18n("Metalloid"); break;}
			case NONMETALS: {str = i18n("Non-Metal"); break;}
			case HALOGENS: {str = i18n("Halogen"); break;}
			case NOBLE_GASES: {str = i18n("Noble Gas"); break;}
			case TRANSACTINIDES: {str = i18n("Transactinide"); break;}
			default: {str = i18n("Unknown"); break;}
		}
		new KActiveLabel(str, grid);
		new TQFrame(grid);
	}
		
	DataPropertyIter it;
	DataProperty *dp = ds->getFirstProperty(&it);
	TQString sval;
	while(dp) {
		if(!dp->isHidden() && dp != p_number && dp != p_class && dp != p_symbol && dp != p_name) {
			sval = e->getPropertyDisplayString(dp).c_str();
			if(!sval.isEmpty()) {
				str = "<b>"; str += dp->title().c_str(); str += ":"; str += "</b>";
				new TQLabel(str, grid);
				new KActiveLabel(sval, grid);
				button = new QalculateButtonWithData((void*) dp, TQIconSet(SmallIcon("edit-paste")), "", grid);
				TQObject::connect(button, TQ_SIGNAL(clickedWithData(void*)), TQ_SLOT(insertElementData(void*)));
			}
		}
		dp = ds->getNextProperty(&it);
	}
}
QalculateElementInfoDialog::~QalculateElementInfoDialog() {}

void QalculateElementInfoDialog::insertElementData(void *data) {
	DataProperty *dp = (DataProperty*) data;
	DataSet *ds = dp->parentSet();
	if(ds) {
		TQString str = ds->preferredInputName(printops.abbreviate_names, printops.use_unicode_signs, false, false, &can_display_unicode_string_function, (void*) expressionWidget).name.c_str();
		str += "(";
		str += e->getProperty(ds->getPrimaryKeyProperty()).c_str();
		str += CALCULATOR->getComma().c_str();
		str += " ";
		str += dp->getName().c_str();
		str += ")";
		insert_text_in_expression(str);
	}
}


#include "qalculateperiodictabledialog.moc"
