#include "DrawQWidget.h"
#include <qdebug.h>
#include <qpainter.h>
DrawQWidget::DrawQWidget(QWidget *parent) : QWidget(parent)
{
setAttribute(Qt::WA_StyledBackground);
setStyleSheet("background-color: rgb(0, 0, 0);");
grabKeyboard();
setMouseTracking(true);
m_difference_x = 0;
m_difference_y = 0;
draw_shap = DRAW_NO;
painter = new QPainter(this);
frame_pen = QPen(QColor(0,174,255),2);
red_point_pen = QPen(QColor(255,0,0),4);
is_mouse_pressed = false;
timer_id = startTimer(20);
rect_init_region();
ellipse_init_region();
/// 开启鼠标实时追踪
setMouseTracking(true);
}
DrawQWidget::~DrawQWidget()
{
killTimer(timer_id);
}
/* Event function
*
*/
void DrawQWidget::timerEvent(QTimerEvent *)
{
this->update();
}
void DrawQWidget::paintEvent(QPaintEvent *)
{
painter->begin(this);
painter->drawImage(QRectF(0,0,width(),height()), picture_image);
switch (draw_shap) {
case (DRAW_RECT) :{
painter->setPen(frame_pen);//绘制边框线
painter->drawRect(QRect(rect_left, rect_top, rect_width, rect_height));
painter->setPen(red_point_pen);//绘制八个点
painter->drawPoints(rect_polygon);
}break;
case (DRAW_ELLIPSE) :{
painter->setPen(frame_pen);//绘制边框线
painter->drawEllipse(QRect(ellipse_left, ellipse_top, ellipse_width, ellipse_height));
painter->setPen(red_point_pen);//绘制四个点
painter->drawPoints(ellipse_polygon);
}break;
case (DRAW_NO) :break;
}
painter->end();
}
void DrawQWidget::mousePressEvent(QMouseEvent *event)
{
// if (!is_start_draw) return;
is_mouse_pressed = true;
}
void DrawQWidget::mouseMoveEvent(QMouseEvent *event)
{
// if (!is_start_draw) return;
new_mouse_pos = event->pos();
if (is_mouse_pressed) {
m_difference_x = new_mouse_pos.x() - old_mouse_pos.x();
m_difference_y = new_mouse_pos.y() - old_mouse_pos.y();
switch (draw_shap) {
case (DRAW_RECT) :rect_change_region();break;
case (DRAW_ELLIPSE) :ellipse_change_region();break;
case (DRAW_NO) :break;
}
}else{
switch (draw_shap) {
case (DRAW_RECT) :rect_mouse_pos = rect_get_mouse_pos(new_mouse_pos.x(), new_mouse_pos.y());break;
case (DRAW_ELLIPSE) :ellipse_mouse_pos = ellipse_get_mouse_pos(new_mouse_pos.x(), new_mouse_pos.y());break;
case (DRAW_NO) :break;
}
}
old_mouse_pos = new_mouse_pos;
}
void DrawQWidget::mouseReleaseEvent(QMouseEvent *event)
{
// if (!is_start_draw) return;
is_mouse_pressed = false;
}
/* Rect function
*
*/
void DrawQWidget::rect_init_region()
{
rect_left = 100;
rect_top = 200;
rect_width = 101;
rect_height = 101;
rect_mouse_pos = RECT_OUTSIDE;
rect_update_region();
}
void DrawQWidget::rect_update_region()
{
rect_top_left_x = rect_left; rect_top_left_y = rect_top;
rect_top_right_x = rect_left+rect_width; rect_top_right_y = rect_top;
rect_low_left_x = rect_left; rect_low_left_y = rect_top+rect_height;
rect_low_right_x = rect_left+rect_width; rect_low_right_y = rect_top+rect_height;
int Middle_X = rect_left + (rect_width>>1);
int Middle_Y = rect_top + (rect_height>>1);
rect_polygon.clear();
rect_polygon<<QPoint(Middle_X, rect_top) //上中
<<QPoint(rect_top_right_x, Middle_Y) //右中
<<QPoint(Middle_X, rect_low_left_y) //下中
<<QPoint(rect_left, Middle_Y) //左中
<<QPoint(rect_left, rect_top) //左上角
<<QPoint(rect_top_right_x, rect_top) //右上角
<<QPoint(rect_top_right_x, rect_low_left_y) //右下角
<<QPoint(rect_left, rect_low_left_y); //左下角
}
void DrawQWidget::rect_change_region()
{
switch (rect_mouse_pos) {
case (RECT_UPPER): rect_top += m_difference_y; rect_height -= m_difference_y;break;//上边界
case (RECT_LOWER): rect_height += m_difference_y;break; //下边界
case (RECT_LEFT) : rect_left += m_difference_x; rect_width -= m_difference_x;break;//左边界
case (RECT_RIGHT): rect_width += m_difference_x;break; //右边界
case (RECT_LEFTUPPER) : {//左上角
rect_top += m_difference_y; rect_height -= m_difference_y;
rect_left += m_difference_x; rect_width -= m_difference_x;
}break;
case (RECT_LEFTLOWER) : {//左下角
rect_height += m_difference_y;
rect_left += m_difference_x; rect_width -= m_difference_x;
}break;
case (RECT_RIGHTLOWER) : {//右下角
rect_height += m_difference_y;
rect_width += m_difference_x;
}break;
case (RECT_RIGHTUPPER) : {//右上角
rect_top += m_difference_y; rect_height -= m_difference_y;
rect_width += m_difference_x;
}break;
case (RECT_INSIDE) : {//内部
rect_top += m_difference_y;rect_left += m_difference_x;
}break;
case (RECT_OUTSIDE) : return;//外部
}
rect_update_region();
}
RECT_MOUSE_POSITION_E DrawQWidget::rect_get_mouse_pos(int pos_x, int pos_y)
{
if (pos_x < rect_top_left_x || pos_x > rect_top_right_x || pos_y < rect_top_left_y || pos_y > rect_low_left_y) {
this->setCursor(QCursor(Qt::ArrowCursor));
return RECT_OUTSIDE;
}else if (pos_y <= rect_top_left_y+BoundaryRange){ //1:左上角 2:右上角 3:上边缘
if (pos_x <= rect_top_left_x+BoundaryRange) {this->setCursor(QCursor(Qt::SizeFDiagCursor));return RECT_LEFTUPPER;}
else if (pos_x >= rect_top_right_x-BoundaryRange){this->setCursor(QCursor(Qt::SizeBDiagCursor));return RECT_RIGHTUPPER;}
else {this->setCursor(QCursor(Qt::SizeVerCursor)); return RECT_UPPER;}
}else if (pos_y >= rect_low_left_y-BoundaryRange){ //1:左下角 2:右下角 3:下边缘
if (pos_x <= rect_low_left_x+BoundaryRange) {this->setCursor(QCursor(Qt::SizeBDiagCursor));return RECT_LEFTLOWER;}
else if (pos_x >= rect_low_right_x-BoundaryRange){this->setCursor(QCursor(Qt::SizeFDiagCursor));return RECT_RIGHTLOWER;}
else {this->setCursor(QCursor(Qt::SizeVerCursor)); return RECT_LOWER;}
}else if (pos_x <= rect_top_left_x+BoundaryRange) { //左边缘
this->setCursor(QCursor(Qt::SizeHorCursor)); return RECT_LEFT;
}else if (pos_x >= rect_top_right_x-BoundaryRange) { //右边缘
this->setCursor(QCursor(Qt::SizeHorCursor)); return RECT_RIGHT;
}else {
this->setCursor(QCursor(Qt::SizeAllCursor));
return RECT_INSIDE;
}
}
/* Ellipse function
*
*/
void DrawQWidget::ellipse_init_region()
{
ellipse_left = 100;
ellipse_top = 200;
ellipse_width = 101;
ellipse_height = 101;
ellipse_mouse_pos = ELLIPSE_OUTSIDE;
ellipse_update_region();
}
void DrawQWidget::ellipse_update_region()
{
ellipse_middle_x = ellipse_left + (ellipse_width>>1);
ellipse_middle_y = ellipse_top + (ellipse_height>>1);
ellipse_polygon.clear();
ellipse_polygon<<QPoint(ellipse_middle_x, ellipse_top) //上顶角
<<QPoint(ellipse_left+ellipse_width, ellipse_middle_y) //右顶角
<<QPoint(ellipse_middle_x, ellipse_top+ellipse_height) //下顶角
<<QPoint(ellipse_left, ellipse_middle_y); //左顶角
}
void DrawQWidget::ellipse_change_region()
{
switch (ellipse_mouse_pos) {
case (ELLIPSE_UP