#ifndef CCTRLMIMG_OPENGL_H
#define CCTRLMIMG_OPENGL_H

#include "cmwidget.h"
#include <QImage>
#include "cshape.h"
#include "cimagecontainer.h"
//#include "ccam.h"
#include <QWheelEvent>
#include <QResizeEvent>
#include <QPaintEvent>
#include <QScrollBar>

#include <QOpenGLWidget>
#include <QOpenGLFunctions>
#include "cprecisetime.h"
#include <QMenu>
class QOpenGLPaintDevice;
class CCtrlMImg_OPenGL : public QOpenGLWidget, protected QOpenGLFunctions
{
    Q_OBJECT
public:
    void initializeGL() Q_DECL_OVERRIDE;
    void resizeGL(int w, int h) Q_DECL_OVERRIDE;
    void paintGL() Q_DECL_OVERRIDE;

    explicit CCtrlMImg_OPenGL(QWidget *parent = 0);
    virtual ~CCtrlMImg_OPenGL();
    void wheelEvent(QWheelEvent *pEvent);
    void mousePressEvent(QMouseEvent *pEvent);
    void mouseMoveEvent(QMouseEvent *pEvent);
    void mouseReleaseEvent(QMouseEvent *pEvent);

    void showEvent(QShowEvent* pEvent);
    virtual void MTimeFresh();
    CImageContainer* m_pContainer;
    double m_dcurScale;
    double m_dmaxScale;
    double m_dminScale;
    QRectF   m_rectImageView;
    int32_t m_imargin;
    QTransform m_trans_LP2DP;
    QTransform m_trans_DP2LP;
    void  adjust_imgviewrect();

    QRect m_rubberBandRect;
    bool m_bCapture;
    void updaterubberBandRect();
    QPixmap m_Pixmap;
    virtual void refreshPixmap();
    QRectF get_clipRect();

    virtual void ZoomIn();
    virtual void ZoomOut();
    virtual void ZoomFit();
    virtual void ZoomOrginal();
    bool_t m_bEnableMouseZoom;
    bool_t m_bEnableClickZoom;
public:
    bool_t m_bContainerInside;
    virtual void MInit();
    void setImage(CMImage& Image);
    void MFresh();
signals:

public slots:
    void saveImage();
    void saveContainer();
    void setImgVPos(int pos);
    void setImgHPos(int pos);
public:
    QScrollBar* m_pVbar;
    QScrollBar* m_pHbar;
protected:
    void draw_container_image(QPainter& painter);
    void draw_container_shapes(QPainter& painter);
    void draw_container_strings(QPainter &painter);
    void draw_container_name(QPainter &painter,bool bedge);
    void draw_sel_shape(CShape* pshape,QPainter& painter);
    void draw_shape( CShape* pshape,QPainter& painter);
    enum _hitTest_type
    {
        HIT_OUT,
        HIT_LEFT, HIT_TOP, HIT_BOTTOM,HIT_RIGHT,
        LEFT_OUT, TOP_OUT, BOTTOM_OUT,RIGHT_OUT,
        TOP_LEFT,BOTTOM_LEFT,
        TOP_RIGHT,BOTTOM_RIGHT,
        ROTATE,
        HIT_IN,
    };

    void hittest_container_shapes(QPointF &point);
    int32_t hit_shape(const QPointF &point, CShape* pshape);

    void resize_sel_shape(const QPointF &point,const  QPointF &oldpoint);
    CShape* m_pSelected_shape;
    int32_t m_hitType;
    QPointF m_oldPoint;
    bool is_shape_selected(CShape* pshape);
    bool is_shape_can_selected(CShape* pshape);

    bool is_hitted();
    QRectF  rectTest;
    bool_t m_bInited;
public:
	//默认缩放
	double m_dOrinScale;
    QColor m_BKColor;
    QColor m_PenColor;
//    QOpenGLPaintDevice *m_device;

    bool m_bFit;
	//是否相机
	bool m_bIsCamera;

    int m_iCurWidth;
    int m_iCurHeight;
    QMenu *m_contextMenu;
    QAction *savecontainer ;
    QAction *saveimage ;
};

#endif // CCTRLMIMG_OPENGL_H
