154 lines
4.2 KiB
C++
154 lines
4.2 KiB
C++
#include "baseModelItem/electricBaseModelLineItem.h"
|
||
#include <QGraphicsScene>
|
||
#include <QPainter>
|
||
#include <QStyleOptionGraphicsItem>
|
||
|
||
ElectricBaseModelLineItem::ElectricBaseModelLineItem(QGraphicsItem *parent)
|
||
: GraphicsBaseModelItem(parent)
|
||
{
|
||
m_boundingRect = QRectF();
|
||
m_pen = QPen(Qt::black);
|
||
m_brush = QBrush(Qt::NoBrush);
|
||
setHandleVisible(false);
|
||
setFunctionHandleIfShow(false);
|
||
setFunctionHandleEnaable(false);
|
||
m_lstPoints.push_back(QPointF()); //起点
|
||
m_lstPoints.push_back(QPointF()); //终点
|
||
_curLine = QPoint();
|
||
}
|
||
|
||
ElectricBaseModelLineItem::~ElectricBaseModelLineItem()
|
||
{
|
||
}
|
||
|
||
ElectricBaseModelLineItem::ElectricBaseModelLineItem(const ElectricBaseModelLineItem& obj)
|
||
:GraphicsBaseModelItem(obj)
|
||
{
|
||
m_pen = QPen(Qt::black);
|
||
m_brush = QBrush(Qt::NoBrush);
|
||
setHandleVisible(false);
|
||
setFunctionHandleIfShow(false);
|
||
setFunctionHandleEnaable(false);
|
||
|
||
m_points = obj.m_points;
|
||
m_pointsBoundingRect = obj.m_pointsBoundingRect; //包裹点的矩形集合
|
||
m_lstPoints = obj.m_lstPoints;
|
||
_curLine = obj._curLine;
|
||
}
|
||
|
||
ElectricBaseModelLineItem* ElectricBaseModelLineItem::clone() const
|
||
{
|
||
ElectricBaseModelLineItem* newItem = new ElectricBaseModelLineItem(*this);
|
||
return newItem;
|
||
}
|
||
|
||
void ElectricBaseModelLineItem::setStartPoint(const QPointF& p)
|
||
{
|
||
int n = m_lstPoints.size();
|
||
if(n)
|
||
{
|
||
if(n >2)
|
||
{
|
||
if(m_lstPoints[0].x() == m_lstPoints[1].x()) //相邻点在垂直方向,水平移动,否则垂直移动
|
||
{
|
||
m_lstPoints[1].setX(p.x());
|
||
}
|
||
else
|
||
{
|
||
m_lstPoints[1].setY(p.y());
|
||
}
|
||
}
|
||
m_lstPoints[0] = p;
|
||
}
|
||
}
|
||
void ElectricBaseModelLineItem::setEndPoint(const QPointF& p)
|
||
{
|
||
int n = m_lstPoints.size();
|
||
if(n)
|
||
{
|
||
if(n >2)
|
||
{
|
||
if(m_lstPoints[n-1].x() == m_lstPoints[n-2].x()) //相邻点在垂直方向,水平移动,否则垂直移动
|
||
{
|
||
m_lstPoints[n-2].setX(p.x());
|
||
}
|
||
else
|
||
{
|
||
m_lstPoints[n-2].setY(p.y());
|
||
}
|
||
}
|
||
m_lstPoints[n-1] = p;
|
||
}
|
||
}
|
||
|
||
QPainterPath ElectricBaseModelLineItem::shape() const
|
||
{
|
||
// 使用路径的轮廓
|
||
if (m_points.isEmpty()) {
|
||
return QPainterPath();
|
||
}
|
||
|
||
QPainterPathStroker stroker;
|
||
stroker.setWidth(8.0); // 选择区域宽度
|
||
|
||
QPainterPath shape = stroker.createStroke(m_points);
|
||
return shape;
|
||
}
|
||
|
||
QRectF ElectricBaseModelLineItem::boundingRect() const
|
||
{
|
||
QRectF rect = shape().boundingRect();
|
||
|
||
// 如果shape为空,使用路径边界
|
||
if (rect.isNull() && !m_points.isEmpty()) {
|
||
rect = m_points.boundingRect().adjusted(-5, -5, 5, 5);
|
||
}
|
||
|
||
return rect;
|
||
}
|
||
|
||
void ElectricBaseModelLineItem::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
|
||
{
|
||
if (option->state & QStyle::State_Selected)
|
||
{
|
||
painter->setPen(QColor(135,206,235,180));
|
||
}
|
||
else
|
||
{
|
||
if(_stateMask)
|
||
painter->setPen(Qt::red);
|
||
else
|
||
painter->setPen(m_pen);
|
||
GraphicsBaseModelItem::paint(painter,option,widget);
|
||
}
|
||
|
||
painter->setBrush(m_brush);
|
||
|
||
painter->drawPath(m_points);
|
||
}
|
||
|
||
void ElectricBaseModelLineItem::calculatePath()
|
||
{
|
||
int n = m_lstPoints.size();
|
||
prepareGeometryChange();
|
||
m_points.clear();
|
||
m_pointsBoundingRect.clear();
|
||
|
||
if(m_lstPoints.size() == 2 && (m_lstPoints.first().x() != m_lstPoints.last().x()) && (m_lstPoints.first().y() != m_lstPoints.last().y()))
|
||
{
|
||
if(m_lstPoints.first().y() < m_lstPoints.last().y())
|
||
m_lstPoints.insert(1,QPointF(m_lstPoints.first().x(),m_lstPoints.last().y()));
|
||
else
|
||
m_lstPoints.insert(1,QPointF(m_lstPoints.last().x(),m_lstPoints.first().y()));
|
||
}
|
||
m_points.moveTo(m_lstPoints.first());
|
||
QPointF pLast = m_lstPoints.first();
|
||
for(int i = 1;i <m_lstPoints.size();++i)
|
||
{
|
||
m_points.lineTo(m_lstPoints[i]);
|
||
m_pointsBoundingRect.addRect(QRectF(QPointF(pLast.x()-1,pLast.y()-1),QPointF(m_lstPoints[i].x()+1,m_lstPoints[i].y()+1)));
|
||
pLast = m_lstPoints[i];
|
||
}
|
||
m_boundingRect = m_points.boundingRect();
|
||
}
|