
#ifndef DRAWQWIDGET_H
#define DRAWQWIDGET_H
#include <QWidget>
#include <QKeyEvent>
#include <qpoint.h>
#include <qpen.h>
/*
*
*/
typedef enum draw_shap_e{
DRAW_RECT, //画矩形
DRAW_ELLIPSE, //画椭圆
DRAW_NO //不画
}DRAW_SHAP_E;
/* 用来表示鼠标在矩形区域的位置信息
*
*/
typedef enum rect_mouse_position_e{
RECT_UPPER=0, //上边缘
RECT_LOWER=1, //下边缘
RECT_LEFT, //左边缘
RECT_RIGHT, //右边缘
RECT_LEFTUPPER, //左上角
RECT_LEFTLOWER, //左下角
RECT_RIGHTLOWER, //右下角
RECT_RIGHTUPPER, //右上角
RECT_INSIDE, //区域内部
RECT_OUTSIDE //区域外部
}RECT_MOUSE_POSITION_E;
/* 用来表示鼠标在椭圆区域的位置信息
*
*/
typedef enum ellipse_mouse_position_e{
ELLIPSE_UPPER=0, //上顶角
ELLIPSE_LOWER=1, //下顶角
ELLIPSE_LEFT, //左顶角
ELLIPSE_RIGHT, //右顶角
ELLIPSE_INSIDE, //区域内部
ELLIPSE_OUTSIDE //区域外部
}ELLIPSE_MOUSE_POSITION_E;
class DrawQWidget : public QWidget
{
Q_OBJECT
public:
explicit DrawQWidget(QWidget *parent = 0);
~DrawQWidget();
void set_draw_shap(DRAW_SHAP_E d);
void set_picture_image(QString file_name);
protected:
void timerEvent(QTimerEvent*);
void paintEvent(QPaintEvent*) override;
void mousePressEvent(QMouseEvent *event);
void mouseMoveEvent(QMouseEvent *event);
void mouseReleaseEvent(QMouseEvent *event);
private:
bool is_mouse_pressed;//是否按下鼠标
DRAW_SHAP_E draw_shap;
QPoint new_mouse_pos;
QPoint old_mouse_pos;
int m_difference_x;
int m_difference_y;
QPainter *painter; //用来绘制图像
QPen frame_pen; //用来绘制区域边框
QPen red_point_pen; //用来绘制红色点
const int BoundaryRange = 6;//用来表示边界的宽度范围,用于拖拽
/* 矩形区域相关变量和函数
* 注意:QPoint的0点是左上角,横向向右为X的正方向,竖向向下为Y的正方向
*/
int rect_left; //表示矩形右上角的X坐标
int rect_top; //表示矩形右上角的Y坐标
int rect_width; //表示矩形的宽(即水平长度)
int rect_height; //表示矩形的高(即垂直长度)
int rect_top_left_x, rect_top_left_y; //左上
int rect_top_right_x, rect_top_right_y; //右上
int rect_low_left_x, rect_low_left_y; //左下
int rect_low_right_x, rect_low_right_y; //右下
QPolygon rect_polygon; //装载8个红点的坐标
RECT_MOUSE_POSITION_E rect_mouse_pos;
void rect_init_region();
void rect_update_region();
void rect_change_region();
RECT_MOUSE_POSITION_E rect_get_mouse_pos(int pos_x, int pos_y);