#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
QtHalcon
- 粉丝: 2488
- 资源: 20
最新资源
- (178548844)zotero文献阅读以及主题和翻译插件
- (179839044)64402-MySQL数据库基础实例教程(第3版)(微课版)-源代码(含例题、案例、实训、实战四个项目).zip.zip
- 基于微信小程序的户外旅游小程序.zip
- 双摇臂履带底盘sw16可编辑全套技术开发资料100%好用.zip
- 国外某地气温数据(extend:2011-2016年).zip
- (18695238)libsvm文档
- 数据分析-51-小红书达人画像
- 基于微信小程序的华云智慧园区(包括数据库,源码).zip
- 步进电机驱动 C#上位机和STM32下位机源程序 步数方向控制
- 船上用品检测12-YOLO(v5至v11)、COCO、CreateML、Paligemma、TFRecord、VOC数据集合集.rar
- 非常好用 的,局域网,文件共享,文档管理 工作,方便检索文件 ,支持HTTP服务
- 手机组装自动镭焊机step全套技术开发资料100%好用.zip
- java项目,毕业设计-基于协同过滤算法商品推荐系统
- 大三-一个简单的安卓移动开发课程设计Android Studio
- 数据分析-53-「猛男的童年回忆」三大类型玩具在京东平台的销售分析
- C# TouchSocket的基础使用,连接,发送,接收WPF
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈