262 lines
7.1 KiB
C++
262 lines
7.1 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);
|
||
|
}
|
||
|
|
||
|
QPicture MyGraphicsItem::getPicture(Marker marker,int penWidth)
|
||
|
{
|
||
|
if(penWidth != 1)
|
||
|
{
|
||
|
QPicture pic;
|
||
|
CBitmapInfo bitmapInfo;
|
||
|
QPainterPath painterPath;
|
||
|
QRect rect = marker.GetRect();
|
||
|
int minX = rect.left();
|
||
|
int minY = rect.top();
|
||
|
int maxY = rect.bottom();
|
||
|
|
||
|
int nLineCount = marker.m_listPolyline.size();
|
||
|
for(int i = 0; i < nLineCount; i++)
|
||
|
{
|
||
|
CRPPolyline polyLine = marker.m_listPolyline.at(i);
|
||
|
int type = polyLine.m_nDrawingType;
|
||
|
|
||
|
if(type == 0)//直线
|
||
|
{
|
||
|
int nPointCount = polyLine.m_listPoint.size();
|
||
|
for(int j = 0; j < nPointCount; j++)
|
||
|
{
|
||
|
double x = (polyLine.m_listPoint.at(j).x() - minX)/(double)M_IDPMM*MMPIXELY;
|
||
|
double y = ((0 - (polyLine.m_listPoint.at(j).y() - minY))+(maxY-minY))/(double)M_IDPMM*MMPIXELY;
|
||
|
QPointF point(x,y);
|
||
|
|
||
|
if(j == 0)
|
||
|
{
|
||
|
painterPath.moveTo(point);
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
painterPath.lineTo(point);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
else if(type == 1)//位图
|
||
|
{
|
||
|
bitmapInfo = polyLine.m_bitmapInfo;
|
||
|
int x = bitmapInfo.m_ptAbPostLU.x();
|
||
|
int y = bitmapInfo.m_ptAbPostLU.y();
|
||
|
|
||
|
int nx = (x - minX)/M_IDPMM*MMPIXELY;
|
||
|
int ny = ((0 - (y - minY))+(maxY-minY))/M_IDPMM*MMPIXELY;
|
||
|
|
||
|
bitmapInfo.m_ptAbPostLU.setX(nx);
|
||
|
bitmapInfo.m_ptAbPostLU.setY(ny);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if(painterPath.isEmpty())
|
||
|
{
|
||
|
qDebug()<<"painterPath isEmpty";
|
||
|
}
|
||
|
|
||
|
//将路径画在picture上
|
||
|
QPen pen;
|
||
|
pen.setWidth(penWidth);//设置笔号
|
||
|
pen.setColor(QColor(Qt::black));
|
||
|
|
||
|
QPainter painter;
|
||
|
painter.begin(&pic);
|
||
|
painter.setPen(pen);
|
||
|
painter.drawPath(painterPath);
|
||
|
if(bitmapInfo.m_iBytes > 0)//有位图
|
||
|
{
|
||
|
painter.drawPixmap(bitmapInfo.m_ptAbPostLU.x(),bitmapInfo.m_ptAbPostLU.y(),bitmapInfo.m_pBitmap);
|
||
|
}
|
||
|
painter.end();
|
||
|
return pic;
|
||
|
}
|
||
|
return m_picture;
|
||
|
}
|
||
|
|
||
|
void MyGraphicsItem::setPicture(QPicture pic)
|
||
|
{
|
||
|
m_picture = pic;
|
||
|
// 外矩形
|
||
|
m_boundingRect = m_picture.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);
|
||
|
double scaleFactor = painter->matrix().m11();
|
||
|
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_picture);
|
||
|
painter->drawPixmap(m_point.x(),m_point.y(),m_blockPixmap);
|
||
|
}
|
||
|
|
||
|
void MyGraphicsItem::creatPicture(Marker marker)
|
||
|
{
|
||
|
CBitmapInfo bitmapInfo;
|
||
|
QPainterPath painterPath;
|
||
|
QRect rect = marker.GetRect();
|
||
|
int minX = rect.left();
|
||
|
int minY = rect.top();
|
||
|
int maxY = rect.bottom();
|
||
|
|
||
|
int nLineCount = marker.m_listPolyline.size();
|
||
|
for(int i = 0; i < nLineCount; i++)
|
||
|
{
|
||
|
CRPPolyline polyLine = marker.m_listPolyline.at(i);
|
||
|
int type = polyLine.m_nDrawingType;
|
||
|
// if(polyLine.m_nDrawingType == 3)//文字
|
||
|
// {
|
||
|
// CRPText text = polyLine.m_text;
|
||
|
// }
|
||
|
|
||
|
if(type == 0)//直线
|
||
|
{
|
||
|
int nPointCount = polyLine.m_listPoint.size();
|
||
|
for(int j = 0; j < nPointCount; j++)
|
||
|
{
|
||
|
double x = (polyLine.m_listPoint.at(j).x() - minX)/(double)M_IDPMM*MMPIXELY;
|
||
|
double y = ((0 - (polyLine.m_listPoint.at(j).y() - minY))+(maxY-minY))/(double)M_IDPMM*MMPIXELY;
|
||
|
QPointF point(x,y);
|
||
|
|
||
|
if(j == 0)
|
||
|
{
|
||
|
painterPath.moveTo(point);
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
painterPath.lineTo(point);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
else if(type == 1)//位图
|
||
|
{
|
||
|
bitmapInfo = polyLine.m_bitmapInfo;
|
||
|
int x = bitmapInfo.m_ptAbPostLU.x();
|
||
|
int y = bitmapInfo.m_ptAbPostLU.y();
|
||
|
|
||
|
int nx = (x - minX)/M_IDPMM*MMPIXELY;
|
||
|
int ny = ((0 - (y - minY))+(maxY-minY))/M_IDPMM*MMPIXELY;
|
||
|
|
||
|
bitmapInfo.m_ptAbPostLU.setX(nx);
|
||
|
bitmapInfo.m_ptAbPostLU.setY(ny);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if(painterPath.isEmpty())
|
||
|
{
|
||
|
qDebug()<<"painterPath.isEmpty";
|
||
|
}
|
||
|
|
||
|
//将路径画在picture上
|
||
|
QPicture pic;
|
||
|
QPen pen;
|
||
|
pen.setWidth(1);//设置笔号
|
||
|
pen.setColor(QColor(Qt::black));
|
||
|
|
||
|
QPainter painter;
|
||
|
painter.begin(&pic);
|
||
|
painter.setPen(pen);
|
||
|
painter.drawPath(painterPath);
|
||
|
if(bitmapInfo.m_iBytes > 0)//有位图
|
||
|
{
|
||
|
//bitmapInfo.m_pBitmap.save("D:\\1.bmp");
|
||
|
painter.drawPixmap(bitmapInfo.m_ptAbPostLU.x(),bitmapInfo.m_ptAbPostLU.y(),bitmapInfo.m_pBitmap);
|
||
|
}
|
||
|
painter.end();
|
||
|
m_picture = pic;
|
||
|
|
||
|
// 外矩形
|
||
|
m_boundingRect = m_picture.boundingRect();
|
||
|
}
|
||
|
|
||
|
void MyGraphicsItem::creatPicture(QPixmap pixmap)
|
||
|
{
|
||
|
//将图片画在picture上
|
||
|
QPicture pic;
|
||
|
QPen pen;
|
||
|
pen.setWidth(1);//设置笔号
|
||
|
pen.setColor(QColor(Qt::black));
|
||
|
|
||
|
QPainter painter;
|
||
|
painter.begin(&pic);
|
||
|
painter.setPen(pen);
|
||
|
painter.drawPixmap(0,0,pixmap);
|
||
|
painter.end();
|
||
|
m_picture = pic;
|
||
|
|
||
|
// 外矩形
|
||
|
m_boundingRect = m_picture.boundingRect();
|
||
|
}
|