/***************************************************************************
                           testdialog.cpp
                           -------------------
    copyright            : (C) 2004 by Jos van den Oever
    email                : jos@vandenoever.info
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 ***************************************************************************/
#include "testdialog.h"
#include "cubedialog.h"
#include "scorekeeper.h"
#include "sidelib.h"
#include "filesidestyle.h"
#include <qfileinfo.h>
#include <qapplication.h>
#include <qdir.h>
#include <qlabel.h>
#include <qpushbutton.h>
#include <qcombobox.h>
#include <q3hbox.h>
#include <q3textbrowser.h>
//Added by qt3to4:
#include <QResizeEvent>
#include <QPixmap>
#include <QMouseEvent>
#include <QKeyEvent>
#include <iostream>
using std::cerr;
using std::endl;

class ClickableQLabel : public QLabel {
public:
	ClickableQLabel(QWidget* parent=0, const char* name=0, Qt::WFlags f=0);
	void setAlternative(const QString &);
protected:
	void mouseReleaseEvent(QMouseEvent *e);
private:
	QString alternative;
};
ClickableQLabel::ClickableQLabel(QWidget* parent, const char* name, Qt::WFlags f)
		: QLabel(parent,name,f) {}
void
ClickableQLabel::setAlternative(const QString &s) {
	alternative = s;
}
void
ClickableQLabel::mouseReleaseEvent(QMouseEvent *) {
	QString temp = text();
	setText(alternative);
	alternative = temp;
}


TestDialog::TestDialog(QWidget* parent, const char* name, Qt::WFlags f)
		: Q3VBox(parent, name, f) {

	setBackgroundPixmap("background.jpg");

	welcome = new ClickableQLabel(this);
	welcome->setTextFormat(Qt::RichText);
	welcome->setText(QString("<p><b>")
		+tr("Welcome to the cube test.") +"</b></p><p>"
		+tr("We are going to compare cubes.") +"</p><p>"
		+tr("Choose the cube that is the same as the top cube.")
		+"</p><p>"
		+tr("You can turn the cubes with the mouse.")+"</p><p>"
		+tr("The six squares next to the cube")+"</p><p>"
		+tr("cube are the same as the six sides of the cube.")
			 +"</p><p>"+tr("Click \"Start\" to begin.")+"</p>");
	welcome->setAlternative(tr("CubeTest is written by Jos van den Oever and "
				   "released under the General Public License."));
	welcome->setWordWrap(true);

	welcome->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter
			      | Qt::WordBreak);
	conclusion = new QLabel("", this);
	conclusion->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
	conclusion->setWordWrap(true);
	conclusion->hide();

	options = new Q3VBox(this);
	options->setSizePolicy(QSizePolicy(QSizePolicy::Preferred,
					   QSizePolicy::Fixed));
	Q3HBox *row = new Q3HBox(options);
	QLabel *l = new QLabel(tr("Difficulty "), row);
	l->setAlignment(Qt::AlignRight | Qt::AlignVCenter);
	QComboBox *levels = new QComboBox(row);
	levels->insertItem(tr("Easy"));
	levels->insertItem(tr("Medium"));
	levels->insertItem(tr("Difficult"));
	levels->insertItem(tr("Challenging"));
	connect(levels, SIGNAL(activated(int)), this, SLOT(setLevel(int)));
	fss = new FileSideStyle();
	if (fss->getStyles().size() > 1) {
		row = new Q3HBox(options);
		l = new QLabel(tr("Style "), row);
		l->setAlignment(Qt::AlignRight | Qt::AlignVCenter);
		QComboBox *stylelist = new QComboBox(row);
		stylelist->insertStringList(fss->getStyles());
		connect(stylelist, SIGNAL(activated(const QString &)),
			this, SLOT(changeStyle(const QString &)));
		fss->loadStyle(stylelist->currentText());
		ss = fss;
	} else if (fss->getStyles().size() == 1) {
		fss->loadStyle(fss->getStyles()[0]);
		ss = fss;
	} else {
		delete fss;
		fss = 0;
		ss = new SideLib();
	}
	start = new QPushButton(tr("Start"), this);
	connect(start, SIGNAL(clicked()), this, SLOT(startQuestions()));
	bugmail = new QLabel(tr("Please send suggestions, themes, translations,"
				" and bugsreports to jos@vandenoever.info"), this);
	//bugmail->setWordWrap(true);
	bugmail->setSizePolicy(QSizePolicy(QSizePolicy::Ignored,
					   QSizePolicy::Fixed));

	cd = new CubeDialog(this);
	connect(cd, SIGNAL(finished()), this, SLOT(finishQuestions()));
	cd->setSideStyle(ss);
	cd->hide();
	setLevel(0);
	start->setFocus();
}
TestDialog::~TestDialog() {
	delete ss;
}
void
TestDialog::startQuestions() {
	welcome->hide();
	options->hide();
	start->hide();
	conclusion->hide();
	start->hide();
	bugmail->hide();
	cd->startQuestions();
	cd->show();
}
void
TestDialog::setQuestion(Question &q) {
	cd->setQuestion(q);
	startQuestions();
}
void
TestDialog::finishQuestions() {
	cd->hide();
	QString message
	= QString(tr("You've answered %1 out of %2 questions correctly."))
	  .arg(QString::number(cd->getScore().getPoints()))
	  .arg(QString::number(cd->getScore().getQuestions()));
	conclusion->setText(message);
	conclusion->show();
	start->show();
	options->show();
	bugmail->show();
	start->setFocus();
}
void
TestDialog::setBackgroundPixmap(const QString &s) const {
/*	QPixmap qpm = QPixmap(qApp->applicationDirPath()
		+ "/../share/cubetest/images/"+s);
	Qt::BackgroundMode mode = Qt::PaletteBackground;
	QPalette pal = QApplication::palette();
	QColorGroup::ColorRole role = QPalette::backgroundRoleFromMode( mode );
	pal.setBrush( QPalette::Active, role,
		      QBrush( pal.color( QPalette::Active, role ), qpm ) );
	pal.setBrush( QPalette::Inactive, role,
		      QBrush( pal.color( QPalette::Inactive, role ), qpm ) );
	pal.setBrush( QPalette::Disabled, role,
		      QBrush( pal.color( QPalette::Disabled, role ), qpm ) );
	QApplication::setPalette( pal,true );*/
}
void
TestDialog::changeStyle(const QString &s) {
	if (fss) {
		fss->loadStyle(s);
	}
}
void
TestDialog::keyReleaseEvent(QKeyEvent *e) {
	if (e->key() == Qt::Key_Escape) {
		finishQuestions();
	} else if (e->key() == Qt::Key_F1) {
		Q3TextBrowser *doc = new Q3TextBrowser();
		doc->setSource(qApp->applicationDirPath()
			+ "/../share/cubetest/doc/readme.html");
		doc->resize(600,400);
		doc->setCaption("http://www.vandenoever.info/software/cubetest/");
		doc->show();
	} else {
		e->ignore();
	}
}
void
TestDialog::resizeEvent(QResizeEvent *) {
	// adjust the fonts in rough steps to fit to the widget size
/*	int size = (width()>height()) ? height() : width();
	int fontsize = size/32;
	if (fontsize < 12)
		fontsize = 12;
	QFont f = font();
	f.setPixelSize(fontsize);
	setFont(f);*/
}
void
TestDialog::setLevel(int i) {
	cd->blanks = 3-i;
}
