109 lines
3.0 KiB
C++
109 lines
3.0 KiB
C++
#include "mygraphicsitem.h"
|
|
|
|
MyGraphicsItem::MyGraphicsItem()
|
|
{
|
|
m_point.setX(0);
|
|
m_point.setY(0);
|
|
|
|
//使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);
|
|
}
|
|
|
|
CreatPictureStr MyGraphicsItem::getPictureByDat(Marker marker,int paperWidth,int butSpace,int rightSpace,int penWidth)
|
|
{
|
|
m_pictureStr = creatPictureByData(marker,paperWidth,butSpace,rightSpace,penWidth);
|
|
m_boundingRect = m_pictureStr.showPic.boundingRect();
|
|
return m_pictureStr;
|
|
}
|
|
|
|
CreatPictureStr MyGraphicsItem::getPictureByBmp(QPixmap pixmap,int paperWidth,int butSpace,int rightSpace,int penWidth)
|
|
{
|
|
m_pictureStr = creatPictureByBmp(pixmap,paperWidth,butSpace,rightSpace,penWidth);
|
|
m_boundingRect = m_pictureStr.showPic.boundingRect();
|
|
return m_pictureStr;
|
|
}
|
|
|
|
void MyGraphicsItem::setPicture(QPicture pic)
|
|
{
|
|
m_pictureStr.showPic = pic;
|
|
// 外矩形
|
|
m_boundingRect = m_pictureStr.showPic.boundingRect();
|
|
update();
|
|
}
|
|
|
|
void MyGraphicsItem::reflushBlockPos(QPoint p)
|
|
{
|
|
m_point = p;
|
|
|
|
m_blockPixmap = QPixmap(PIXMAPWIDTH,m_pictureStr.showPic.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);
|
|
|
|
painter->drawPicture(0,0,m_pictureStr.showPic);
|
|
painter->drawPixmap(m_point.x(),m_point.y(),m_blockPixmap);
|
|
//painter->end();
|
|
}
|