#ifndef _CANVAS_H_
#define _CANVAS_H_

#include "Processor.h"
#include <QImage>
#include <QGLWidget>

class Canvas : public QGLWidget
{
	Q_OBJECT

public:
	
    Canvas( QWidget *parent );
    ~Canvas();

	inline int getNumber() const { return _number; }
	inline void setNumber( int number ) { _number = number; }

	inline Processor* getProcessor() const { return _processor; }
	void setProcessor( Processor* processor );

	void setFrameBuffer( int width, int height, GLenum format, const unsigned char* buffer );

	int textWidth( const QString &str ) const;
	void renderText( int imgX, int imgY, const QString &str );

	virtual void initializeGL();
	virtual void resizeGL( int width, int height );
	virtual void paintGL();

signals:
	void mousePressed( QMouseEvent* event );
	void mouseReleased( QMouseEvent* event );

protected:
	void mousePressEvent( QMouseEvent* event );
	void mouseReleaseEvent( QMouseEvent* event );

private:
	inline void updatePixelZoom()
	{
		_pixelZoomX = static_cast<float>( _screenWidth ) / _imageWidth;
		_pixelZoomY = static_cast<float>( _screenHeight ) / _imageHeight;
		_pixelZoom = min( _pixelZoomX, _pixelZoomY );
	}

private:
	int _number;

	int _imageWidth;
	int _imageHeight;
	int _screenWidth;
	int _screenHeight;
	float _pixelZoomX;
	float _pixelZoomY;
	float _pixelZoom;
	GLenum _format;
    const unsigned char* _buffer;
	Processor* _processor;

	QFont _font;
	QFontMetrics _fontMetrics;
};

#endif
