#ifndef CCTRLMIMGCONTAINER_H
#define CCTRLMIMGCONTAINER_H

#include "cmwidget.h"
#include <QImage>
#include "cshape.h"
#include "cimagecontainer.h"
#include "ccam.h"
#include <QWheelEvent>
#include <QResizeEvent>
#include <QPaintEvent>
#include <QScrollBar>
#include "cprecisetime.h"
#include <QMenu>
class CCtrlMImgContainer : public CMWidget
{
    Q_OBJECT
public:
    explicit CCtrlMImgContainer(QWidget *parent = 0);
    virtual ~CCtrlMImgContainer();
    virtual void paintEvent(QPaintEvent *pEvent);
    virtual void resizeEvent(QResizeEvent *);
    virtual void wheelEvent(QWheelEvent *pEvent);
    virtual void mousePressEvent(QMouseEvent *pEvent);
    virtual void mouseMoveEvent(QMouseEvent *pEvent);
    virtual void mouseReleaseEvent(QMouseEvent *pEvent);
    virtual void showEvent(QShowEvent * pEvent);
    virtual void hideEvent(QHideEvent *event);
	void MTimeFresh();
    CImageContainer* m_pContainer;
    CCam* m_pCam;
    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;
    void refreshPixmap();
    QRectF get_clipRect();

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

public slots:
    void setImgVPos(int pos);
    void setImgHPos(int pos);

    void saveImage();
    void saveContainer();
public:
    QScrollBar* m_pVbar;
    QScrollBar* m_pHbar;
private:
    void draw_container_shapes(QPainter& painter);
    void draw_container_strings(QPainter &painter);
    void draw_container_name(QPainter &painter);
    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:
    bool m_bKeepFit;

    QColor m_BKColor;
    CPreciseTime m_timeFPS;
    int m_iFPS;
    int m_iFrame;

    QMenu *m_contextMenu;
};

#endif // CCTRLMIMGCONTAINER_H
