#ifndef GRIDSVIEW_H
#define GRIDSVIEW_H

#include <QGraphicsView>
#include <QGraphicsWidget>
#include <QGraphicsGridLayout>
#include <QGLWidget>
#include <QTimer>

//////////////////////////////////////////////////////////////////////////
class CGLScene : public QGraphicsScene
{
    
public:
    CGLScene(QObject* parent = 0): QGraphicsScene(parent){}
    virtual ~CGLScene(){}

    void MakeDirty(){ m_Dirty = true; }
    bool IsDirty(){ return m_Dirty; }
    void ResetDirtyFlag(){ m_Dirty = false; }

protected:
    virtual void drawBackground(QPainter* painter, const QRectF& rect )
    {
        glClearColor(0.13f, 0.13f, 0.13f, 1.0f);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    }

private:
    bool m_Dirty;
};

//////////////////////////////////////////////////////////////////////////
class CLayoutPos
{
public:
    CLayoutPos(){}
    CLayoutPos(int r, int c, int rS, int cS){ row=r; col=c; rowS=rS; colS=cS; }
    ~CLayoutPos(){}

    int row;
    int col;
    int rowS;
    int colS;
};

//////////////////////////////////////////////////////////////////////////
class CGridsView : public QGraphicsView
{
    Q_OBJECT
public:
    CGridsView(QWidget* parent);
    ~CGridsView();

    // Add the graphics widget into the graphics layout as a graphics item.
    void AddWidget(QGraphicsWidget* pWidget, int row, int col, int rowS, int colS);

    // Return the graphics item at the index/position.
    QGraphicsWidget* WidgetAt(int idx){ return dynamic_cast<QGraphicsWidget*>(m_pGGridLayout->itemAt(idx)); }
    QGraphicsWidget* WidgetAt(int row, int col){ return dynamic_cast<QGraphicsWidget*>(m_pGGridLayout->itemAt(row, col)); }

    // Remove the graphics item at the index/position from the layout and return it.
    QGraphicsWidget* TakeAt(int idx);

    // Get the index value of the input widget.
    int IndexOf(QGraphicsWidget* pInputWidget);

    // Get the layout position of the input widget.
    // Return value: whether the widget can be found or not.
    bool GetWidgetPosition(QGraphicsWidget* pInputWidget, int* row, int* col, int* rowS, int* colS);

    // Display the grid view in full screen mode.
    void EnableFullScreen(bool bEnable);

	// Get the amount of widgets in the layout
    int Count(){ return m_pGGridLayout->count(); }
    int RowCount(){ return m_pGGridLayout->rowCount(); }
    int ColCount(){ return m_pGGridLayout->columnCount(); }

    // For Girds View updating.
    void StartUpdate(){ m_FrameUpdateTimer.start(); }
    void StopUpdate(){ m_FrameUpdateTimer.stop(); }
    
public slots:
    void UpdateGridsView()
    {
       // if( isVisible() && m_pScene->IsDirty())
        {
       //     m_pScene->ResetDirtyFlag();
            m_pScene->update();
        }
    }
protected:
    virtual void showEvent(QShowEvent *event)
    { 
        m_pGLWidget->makeCurrent();
        QGraphicsView::showEvent(event);
    }
    virtual void resizeEvent(QResizeEvent* event)
    {
        if( event )
        {
            setSceneRect(0,0,event->size().width(),event->size().height());
            m_pForm->setGeometry(0,0,event->size().width(),event->size().height());
        }

        QGraphicsView::resizeEvent(event);
    }

private:
    QGraphicsGridLayout* m_pGGridLayout;
    QGraphicsWidget* m_pForm;
    QGLWidget* m_pGLWidget;
    CGLScene* m_pScene;
    std::map<QGraphicsWidget*, CLayoutPos> m_PositionLookupTable;

    QTimer m_FrameUpdateTimer;
};
#endif
