PlotterHMI/datafile/view/mygraphicsitem.cpp
huahaiyan 3ce55cebbd 20240308
1、梳理代码,修改绘制会绘制两次的bug;
2、增加正向旋转功能,线宽设置功能;
3、测试压缩算法,完成;
2024-03-08 16:33:05 +08:00

148 lines
3.7 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "mygraphicsitem.h"
MyGraphicsItem::MyGraphicsItem()
{
m_point.setX(0);
m_point.setY(0);
m_fileType = TYPE_FILE;
//使item可以被选择
//this->setFlag(QGraphicsItem::ItemIsSelectable);
//使item可以移动
//this->setFlag(QGraphicsItem::ItemIsMovable);
//item可以响应鼠标事件
//this->setAcceptHoverEvents(true);
}
MyGraphicsItem::~MyGraphicsItem()
{
}
void MyGraphicsItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
return QGraphicsItem::mousePressEvent(event);
}
void MyGraphicsItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
return QGraphicsItem::mouseMoveEvent(event);
}
void MyGraphicsItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
return QGraphicsItem::mouseReleaseEvent(event);
}
QPicture MyGraphicsItem::getPictureByDat(Marker marker,int penWidth)
{
m_fileType = TYPE_FILE;
if(penWidth != 1)
{
QPicture pic;
pic = creatPictureByData(marker,m_drawPath,penWidth);
m_boundingRect = pic.boundingRect();
return pic;
}
return m_picture;
}
QPicture MyGraphicsItem::getPictureByBmp(QPixmap pixmap,int penWidth)
{
m_fileType = TYPE_IMAGE;
m_picture = creatPictureByBmp(pixmap,penWidth);
m_boundingRect = m_picture.boundingRect();
return m_picture;
}
QPainterPath MyGraphicsItem::getDrawPath()
{
return m_drawPath;
}
void MyGraphicsItem::setPicture(QPicture pic)
{
m_fileType = TYPE_IMAGE;
m_picture = pic;
// 外矩形
m_boundingRect = m_picture.boundingRect();
update();
}
void MyGraphicsItem::setPaintPath(QPainterPath path)
{
m_fileType = TYPE_FILE;
m_drawPath = path;
m_boundingRect = m_drawPath.boundingRect();
update();
}
void MyGraphicsItem::setPaintPathAndPic(QPainterPath path, QPicture pic)
{
m_fileType = TYPE_FILE;
m_drawPath = path;
m_picture = pic;
m_boundingRect = m_drawPath.boundingRect();
update();
}
void MyGraphicsItem::reflushBlockPos(QPoint p)
{
m_point = p;
m_blockPixmap = QPixmap(PIXMAPWIDTH,m_picture.height());
m_blockPixmap.fill(Qt::transparent);//透明色
if(m_point.x() >= 0 && m_point.y() >= 0)
{
QPainter painter(&m_blockPixmap);
painter.setCompositionMode(QPainter::CompositionMode_Source);
painter.fillRect(m_blockPixmap.rect(), QColor(0, 0, 0, 80));
painter.end();
}
update();
}
QRectF MyGraphicsItem::boundingRect() const
{
return m_boundingRect;
}
void MyGraphicsItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
Q_UNUSED(option);
Q_UNUSED(widget);
QPen pen;
QBrush brush;
brush.setStyle(Qt::SolidPattern);
pen.setWidth(1);
// 获取当前的变换矩阵
QTransform transform = painter->worldTransform();
double scaleFactor = std::sqrt(transform.m11() * transform.m11() +
transform.m22() * transform.m22());
double width = pen.width()/scaleFactor;
pen.setWidthF(width); // 线段保持原来的线宽
pen.setColor(QColor(Qt::black));
brush.setColor(QColor(255,255,255));
painter->setPen(pen);
painter->setBrush(brush);
//防锯齿,效果不好,所以注释掉
// painter->setRenderHint(QPainter::Antialiasing, true);
// painter->setRenderHint(QPainter::SmoothPixmapTransform, true);
//用MyGraphicsItem画图缩放时如果笔宽不为1直接画QPicture时线条会很轻所以用m_drawPath绘制pic就为原笔宽pic
//文件类型用m_drawPath图片类型为m_picture
if(m_fileType == TYPE_FILE)
{
painter->drawPath(m_drawPath);
}
else if(m_fileType == TYPE_IMAGE)
{
painter->drawPicture(0,0,m_picture);
}
painter->drawPixmap(m_point.x(),m_point.y(),m_blockPixmap);
}