#ifndef GL_WIDGET2_H
#define GL_WIDGET2_H

#include "./../../lib/GLee/GLee.h"	//GL header file, including extensions

#include <string>
#include <vector>
using namespace std;

#include <QGLWidget>
#include <QMouseEvent>
#include <QWheelEvent>
#include <QTimer>
#include <QGLPixelBuffer>
#include <QGLContext>

#include <qtableview.h>
#include <QStandardItemModel>
#include <QModelIndex>
#include <qpushbutton.h>

#include "./openGlSupport/VBOmanager.h"
#include "./openGlSupport/OpenGLwrapper.h"
#include "./texture/TextureManager.h"
#include "./apps/AbstractAPP.h"
#include "./Context/QtContext.h"



typedef unsigned int key_code;



///Advanced OpenGL widget.
/**
*	This widget is based on QGLWidget to work with Qt + OpenGL.
*	The graphic output is rendered into this widget. To connect this
*	widget with your render logic you need to inherit class from AbstractAPP
*	and call #GLwidget2::setApp( AbstractAPP* app ).
*/
class GLwidget2 : public QGLWidget
{
	Q_OBJECT

private:
	AbstractAPP* app; /**< Pointer to application logic*/
	QContext *qContext;	/**< Pointer to context*/
	QTimer oneSecondTimer;	/**< This timer tick every 1 second*/
	unsigned int loop;	/**< Internal loop counter*/
	bool initialized;	/**< Flag if #GLwidget2::initializeGL(void) was executed*/

public:
	void setApp( AbstractAPP* app ); /**< Sets pointer to application logic, all important events are forwarded there from this widget.*/
	
	//GLwidget2( QWidget *parent = 0 );
	GLwidget2 ( QWidget * parent = 0, const QGLWidget * shareWidget = 0, Qt::WindowFlags f = 0 );
	void initializeGL(void);	/**<Internal OpenGL stuff initialization.*/
	void mouseReleaseEvent( QMouseEvent *e );	/**< Is called on mouse button release event.*/
	void mousePressEvent( QMouseEvent *e );		/**< Is called on mouse button press event.*/
	void mouseMoveEvent( QMouseEvent *e );		/**< Is called on mouse move event.*/
	void keyPressEvent( QKeyEvent *e);			/**< Is called on key button press event.*/
	void keyReleaseEvent( QKeyEvent *e);		/**< Is called on key button release event.*/
	void wheelEvent( QWheelEvent *e );			/**< Is called on mouse wheel event.*/
	void paintGL(void);							/**< Is called on OpenGL rendering.*/
	void focusOutEvent( QFocusEvent *e );		/**< Is called on focus lost event.*/
	Context* getContext(void);					/**< Returns context. */

	bool isInitialized(void);					/**< Returns value of  #GLwidget2::initialized flag */
	void forceInitialize(void);					/**< Try to initialize application now. WARNING: user is responsible for OpenGL context!!! */


public slots:
	virtual void updateGL();					/**<Repaints OpenGL graphic*/
	void oneSecondTimeout();					/**<Is called when #GLwidget2::oneSecondTimer tick */

signals:
	void lostFocusSignal(void);	
	void refresh(void);	//emited when paintGL() called

	void mouseReleaseEventSignal( QMouseEvent *e );
	void mousePressEventSignal( QMouseEvent *e );
	void mouseMoveEventSignal( QMouseEvent *e );
	void keyPressEventSignal( QKeyEvent *e);
	void keyReleaseEventSignal( QKeyEvent *e);
	void wheelEventSignal( QWheelEvent *e );

	void initializationEventSignal(void);
};


#endif