#ifndef CANVAS_FACTORY_H
#define CANVAS_FACTORY_H


#include <QApplication>
#include <QString>
#include <QPalette>
#include <canvas.h>
#include <qrectangle.h>
#include <qtextlabel.h>
#include <QPaintEvent>
#include <canvasitem.h>
#include <calculator.h>


extern bool alphaPaintingEnabled;


// Add PythonQt Bindings
class CanvasFactory : public QObject
{
    Q_OBJECT

public slots:
    // Constructor and Destructor
    Canvas *new_Canvas(int x, int y, int w, int h) {
        Canvas *o = new Canvas();
        o->setGeometry(x, y, w, h);
        o->show();
	fntFamily = fnt.family();
	fntSize = fnt.pixelSize();
        return o;
    }
    void delete_Canvas(Canvas *o) { delete o; }
    QWidget *qwidget_cast(Canvas *o) { return o; }

    CanvasItem *newRect(Canvas *parent, int x, int y, int w, int h) {
        CanvasItem *o = new CanvasItem(parent, x, y, w, h);
	delete o->widget;
	o->widget = new QRectangle(o,parent);
        o->type = 1;
        o->color = QColor(0,0,0,0);
        return o;
    }
    CanvasItem *newImage(Canvas *parent, int x, int y, QString file) {
        CanvasItem *o = new CanvasItem(parent, x, y, 0, 0);
        o->type = 0;
        o->file_set(file);
        o->color = QColor(0,0,0,0);
        return o;
    }
    CanvasItem *newText(Canvas *parent, int x, int y, QString text, QString font, int fontSize, QColor col) {
        CanvasItem *o = new CanvasItem(parent, x, y, 0, 0);
        o->type = 2;
        o->text_set(text);
	if (fontSize != fntSize) {
		fntSize = fontSize;
		fnt.setPixelSize(fontSize);
	}
	if (font == fntFamily) {
		fntFamily = font;
		fnt.setFamily(font);
	}
        o->widget->setFont(fnt);
        o->color = col;
        pal.setColor(QPalette::WindowText,col);
        o->widget->setPalette(pal);
        return o;
    }

    CanvasItem *newOutlineLabelEx(Canvas *parent, QString text, QString font, int fontSize, int x, int y, QColor col, QColor outline_color, int align, int abbr_width, int valign) {

	QPixmap pix(2,2);
	QFont f(font);
	f.setPixelSize(fontSize);
	QFontMetrics fm(f, &pix);
	int w = fm.width(text) + 3;
	int h = fm.height() + 3;
	x = x - w / 2; 
	y = y - h / 2;
	QString file = "outlined_" + text + "_text";
	if ( !QPixmapCache::find(file, pix) ) {
		pix = QPixmap(w, h);
		pix.fill(QColor(0,0,0,0));
		QPainter p(&pix);
		p.setFont(f);
		p.setPen(QColor(outline_color));
		for (int i = 0; i < 3; i++)	
			for (int j = 0; j < 3; j++)	
				p.drawText(i, j, w, h, Qt::AlignLeft | Qt::AlignTop, text); 
		p.setPen(QColor(col));
		p.drawText(1, 1, w, h, Qt::AlignLeft | Qt::AlignTop, text); 
		p.end();
		QPixmapCache::insert(file, pix);
	}
        CanvasItem *o = new CanvasItem(parent, x, y, w, h);
        o->type = 0;
        o->file_set(file);
        o->color = QColor(0,0,0,0);
        return o;
    }

 
    // Calculator wrapper so we can create calculator widgets in python
    CanvasItem *newCalculator(Canvas *parent) {
        CanvasItem *o = new CanvasItem(parent, 0, 0, 480, 272);
        o->type = 0;
        o->fill_set(0, 0, 480, 272);
        o->color = QColor(0,0,0,0);
    	Calculator *calc = new Calculator(o->widget);
	calc->setGeometry(0, 0, 480, 272);
	o->setCustomWidget(calc);
        return o;
    }

    void processEvents(Canvas *o) { qApp->processEvents(); }
    void render(Canvas *o) { o->update(); }
    //void paint(Canvas *o) { o->update(); }
    //void update(Canvas *o, int x, int y, int w, int h) { o->update(x,y,w,h); }
    void setTransparency(Canvas *o, bool on) { alphaPaintingEnabled = on; }

private:
    QFont fnt;
    QString fntFamily;
    int fntSize;
    QPalette pal;
};


#endif // CANVAS_FACTORY_H

