/***************************************************************************
                           fun.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 "fun.h"
#include <qapplication.h>
#include "../src/block.xpm"
#include <cmath>
#include <qpixmap.h>
#include <qsound.h>
#include <iostream>
#include <qmessagebox.h>
#include <qapplication.h>
#include <qfileinfo.h>
#include <qimage.h>
#include <qpicture.h>
#include <qpainter.h>
using namespace std;

QPicture *Fun::geit, *Fun::haan,*Fun::eend, *Fun::varken, *Fun::schaap, *Fun::koe;

double zc = 1.0/3.0*sqrt(6.0);
double ya = 1.0/6.0*sqrt(3.0);
double yb = 1.0/2.0*sqrt(3.0);
double yc = 1.0/3.0*sqrt(3.0);

double side1[9] = { 0.5,   ya, zc,  -0.5,  ya, zc,  0.5,  yb, 0};
double side2[9] = {-0.5,   ya, zc,     0, -yc, zc,   -1,   0, 0};
double side3[9] = {   0,  -yc, zc,   0.5,  ya, zc,  0.5, -yb, 0};
double side4[9] = {   1,    0,  0,   0.5,  yb,  0,  0.5, -ya, -zc};
double side5[9] = {-0.5,   yb,  0,    -1,   0,  0,    0,  yc, -zc};
double side6[9] = {-0.5,  -yb,  0,   0.5, -yb,  0, -0.5, -ya, -zc};

QPicture
getPicture(const QString &file) {
	QPixmap pix(file);
	QPainter p;
	QPicture pict;
	p.begin(&pict);
	p.fillRect(10,10,pix.width(),pix.height(),Qt::white);
	p.drawPixmap(10,10,pix);
	p.end();
	return pict;
}
Fun::Fun()
	: Object(NULL) {
	setCaption("Boe!");
        QFileInfo f(qApp->argv()[0]);
        while (f.isSymLink()) {
                f.setFile(f.readLink());
        }
        path = qApp->applicationDirPath()+"/../share/cubetest/images/";
	Side s;
	geit = new QPicture(getPicture(path+"geit.gif"));
	haan = new QPicture(getPicture(path+"haan.png"));
	eend = new QPicture(getPicture(path+"eend.png"));
	varken = new QPicture(getPicture(path+"varken.png"));
	schaap = new QPicture(getPicture(path+"schaap.png"));
	koe = new QPicture(getPicture(path+"koe.png"));
	s.setPicture(geit);
	s.setPoints(side1);
	addSide(s);
	s.setPicture(haan);
	s.setPoints(side2);
	addSide(s);
	s.setPicture(eend);
	s.setPoints(side3);
	addSide(s);
	s.setPicture(varken);
	s.setPoints(side4);
	addSide(s);
	s.setPicture(schaap);
	s.setPoints(side5);
	addSide(s);
	s.setPicture(koe);
	s.setPoints(side6);
	addSide(s);
	connect(&getSide(5), SIGNAL(clicked()), this, SLOT(cow()));
	background.load(path+"wei.jpg");
	QMessageBox::information(this, "Boe", "Klik op de koe!");
}
void
Fun::cow() {
	if (!QSound::available()) {
		QMessageBox::information(this, "Boe", "Goedzo!");
	} else {
		QSound::play(path+"koe.wav");
	}
}
void
Fun::resizeEvent(QResizeEvent*e) {
	QImage qi = background.convertToImage();
	background.convertFromImage(qi.scale(size(), QImage::ScaleMax));
	setBackgroundPixmap(background);
	Object::resizeEvent(e);
}
int
main( int argc, char **argv ) {
	QApplication a( argc, argv );
	Fun f;
        f.setIcon(QPixmap(block_xpm));
	a.setMainWidget( &f );
	f.resize(300,200);
	f.show();

	return a.exec();
}
