2024-12-07 17:24:36 +08:00
|
|
|
#include "graphicsItem/electricConnectLineItem.h"
|
|
|
|
|
#include <QGraphicsScene>
|
|
|
|
|
#include <QPainter>
|
2024-12-13 18:08:00 +08:00
|
|
|
#include <QStyleOptionGraphicsItem>
|
2024-12-07 17:24:36 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
ElectricConnectLineItem::ElectricConnectLineItem(QGraphicsItem *parent)
|
|
|
|
|
: GraphicsBaseItem(parent)
|
|
|
|
|
{
|
2024-12-13 18:08:00 +08:00
|
|
|
m_boundingRect = QRectF();
|
2024-12-07 17:24:36 +08:00
|
|
|
m_pen = QPen(Qt::black);
|
|
|
|
|
m_brush = QBrush(Qt::NoBrush);
|
|
|
|
|
setHandleVisible(false);
|
|
|
|
|
setFunctionHandleIfShow(false);
|
|
|
|
|
setFunctionHandleEnaable(false);
|
2024-12-13 18:08:00 +08:00
|
|
|
m_lstPoints.push_back(QPointF()); //起点
|
|
|
|
|
m_lstPoints.push_back(QPointF()); //终点
|
|
|
|
|
_curLine = QPoint();
|
2024-12-07 17:24:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ElectricConnectLineItem::~ElectricConnectLineItem()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-13 18:08:00 +08:00
|
|
|
void ElectricConnectLineItem::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 ElectricConnectLineItem::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 ElectricConnectLineItem::shape() const
|
2024-12-07 17:24:36 +08:00
|
|
|
{
|
|
|
|
|
QPainterPath path;
|
2024-12-13 18:08:00 +08:00
|
|
|
//path.addPath(m_points);
|
|
|
|
|
path.addPath(m_pointsBoundingRect);
|
2024-12-07 17:24:36 +08:00
|
|
|
return path;
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-13 18:08:00 +08:00
|
|
|
QRectF ElectricConnectLineItem::boundingRect() const
|
|
|
|
|
{
|
|
|
|
|
return m_boundingRect;
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-07 17:24:36 +08:00
|
|
|
void ElectricConnectLineItem::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
|
|
|
|
|
{
|
2024-12-13 18:08:00 +08:00
|
|
|
if (option->state & QStyle::State_Selected)
|
|
|
|
|
{
|
|
|
|
|
painter->setPen(Qt::red);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
painter->setPen(m_pen);
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-07 17:24:36 +08:00
|
|
|
painter->setBrush(m_brush);
|
|
|
|
|
|
|
|
|
|
painter->drawPath(m_points);
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-13 18:08:00 +08:00
|
|
|
void ElectricConnectLineItem::moveLine(QPointF pos)
|
|
|
|
|
{
|
|
|
|
|
QPointF delta = pos - m_lastPoint;
|
|
|
|
|
if(_curLine.isNull())
|
|
|
|
|
{
|
|
|
|
|
if(m_lstPoints.size() > 1)
|
|
|
|
|
{
|
|
|
|
|
int n = m_lstPoints.size();
|
|
|
|
|
for(int i = 0;i < m_lstPoints.size() -1;++i)
|
|
|
|
|
{
|
|
|
|
|
QPointF p1 = m_lstPoints[i];
|
|
|
|
|
QPointF p2 = m_lstPoints[i+1];
|
|
|
|
|
|
|
|
|
|
if(p1.x() == p2.x() && abs(p1.x() - pos.x()) < 2) //在相同竖线
|
|
|
|
|
{
|
|
|
|
|
if(p1.y() > p2.y())
|
|
|
|
|
{
|
|
|
|
|
if(pos.y() > p2.y() && pos.y() < p1.y()) //在竖线上
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
if(i == 0 ) //起点是连接点,创建复制点
|
|
|
|
|
{
|
|
|
|
|
m_lstPoints.prepend(p1);
|
|
|
|
|
m_lstPoints.prepend(p1);
|
|
|
|
|
|
|
|
|
|
i+=2;
|
|
|
|
|
if(m_lstPoints.size() < 6) //补齐后端
|
|
|
|
|
{
|
|
|
|
|
m_lstPoints.append(m_lstPoints.last());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if(i+1 == n-1) //终点是连接点,创建复制点
|
|
|
|
|
{
|
|
|
|
|
m_lstPoints.append(p2);
|
|
|
|
|
m_lstPoints.append(p2);
|
|
|
|
|
|
|
|
|
|
m_lstPoints[i+1] = p2;
|
|
|
|
|
if(m_lstPoints.size() < 6) //补齐前端,移动后至少6个点
|
|
|
|
|
{
|
|
|
|
|
m_lstPoints.prepend(m_lstPoints.first());
|
|
|
|
|
i+=1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
//qDebug() << 1 <<" "<<i;
|
|
|
|
|
_curLine = QPoint(i,1);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if(p1.y() < p2.y())
|
|
|
|
|
{
|
|
|
|
|
if(pos.y() < p2.y() && pos.y() > p1.y()) //在竖线上
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
if(i == 0) //起点是连接点,创建复制点
|
|
|
|
|
{
|
|
|
|
|
m_lstPoints.prepend(p1);
|
|
|
|
|
m_lstPoints.prepend(p1);
|
|
|
|
|
i+=2;
|
|
|
|
|
if(m_lstPoints.size() < 6) //补齐后端
|
|
|
|
|
{
|
|
|
|
|
m_lstPoints.append(m_lstPoints.last());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if(i+1 == n-1) //终点是连接点,创建复制点
|
|
|
|
|
{
|
|
|
|
|
m_lstPoints.append(p2);
|
|
|
|
|
m_lstPoints.append(p2);
|
|
|
|
|
|
|
|
|
|
m_lstPoints[i+1] = p2;
|
|
|
|
|
if(m_lstPoints.size() < 6) //补齐前端
|
|
|
|
|
{
|
|
|
|
|
m_lstPoints.prepend(m_lstPoints.first());
|
|
|
|
|
i+=1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
//qDebug() << 2 <<" "<<i;
|
|
|
|
|
_curLine = QPoint(i,1);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if(p1.y() == p2.y() && abs(p1.y() - pos.y()) < 2) //在相同横线
|
|
|
|
|
{
|
|
|
|
|
if(p1.x() > p2.x())
|
|
|
|
|
{
|
|
|
|
|
if(pos.x() > p2.x() && pos.x() < p1.x())
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
if(i == 0) //起点是连接点,创建复制点
|
|
|
|
|
{
|
|
|
|
|
m_lstPoints.prepend(p1);
|
|
|
|
|
m_lstPoints.prepend(p1);
|
|
|
|
|
|
|
|
|
|
i+=2;
|
|
|
|
|
if(m_lstPoints.size() < 6) //补齐后端
|
|
|
|
|
{
|
|
|
|
|
m_lstPoints.append(m_lstPoints.last());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if(i+1 == n-1) //终点是连接点,创建复制点
|
|
|
|
|
{
|
|
|
|
|
m_lstPoints.append(p2);
|
|
|
|
|
m_lstPoints.append(p2);
|
|
|
|
|
|
|
|
|
|
if(m_lstPoints.size() < 6) //补齐前端,移动后至少6个点
|
|
|
|
|
{
|
|
|
|
|
m_lstPoints.prepend(m_lstPoints.first());
|
|
|
|
|
i+=1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
//qDebug() << 3 <<" "<<i;
|
|
|
|
|
_curLine = QPoint(i,-1);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if(p1.x() < p2.x())
|
|
|
|
|
{
|
|
|
|
|
if(pos.x() < p2.x() && pos.x() > p1.x())
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
if(i == 0) //起点是连接点,创建复制点
|
|
|
|
|
{
|
|
|
|
|
m_lstPoints.prepend(p1);
|
|
|
|
|
m_lstPoints.prepend(p1);
|
|
|
|
|
|
|
|
|
|
i+=2;
|
|
|
|
|
m_lstPoints[i] = p1;
|
|
|
|
|
if(m_lstPoints.size() < 6) //补齐后端
|
|
|
|
|
{
|
|
|
|
|
m_lstPoints.append(m_lstPoints.last());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if(i+1 == n-1) //终点是连接点,创建复制点
|
|
|
|
|
{
|
|
|
|
|
m_lstPoints.append(p2);
|
|
|
|
|
m_lstPoints.append(p2);
|
|
|
|
|
|
|
|
|
|
if(m_lstPoints.size() < 6) //补齐前端
|
|
|
|
|
{
|
|
|
|
|
m_lstPoints.prepend(m_lstPoints.first());
|
|
|
|
|
i+=1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
//qDebug() << 4 <<" "<<i;
|
|
|
|
|
_curLine = QPoint(i,-1);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
int n = _curLine.x();
|
|
|
|
|
//qDebug() << n <<" "<<_curLine.y();
|
|
|
|
|
//if(m_lstPoints.size() > 3)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
if(_curLine.y() == 1) //竖线
|
|
|
|
|
{
|
|
|
|
|
if(n == 0) //起点是原点
|
|
|
|
|
{
|
|
|
|
|
QPointF p(0,delta.y()); //原点不动
|
|
|
|
|
m_lstPoints[n +1] = m_lstPoints[n +1] + p; //第二个点只竖直移动
|
|
|
|
|
m_lstPoints[n +2] = m_lstPoints[n +2] + p; //第三个点只竖直移动
|
|
|
|
|
}
|
|
|
|
|
else if(n == 1)
|
|
|
|
|
{
|
|
|
|
|
QPointF p(delta.x(),0);
|
|
|
|
|
m_lstPoints[n] += p; //第一个点只水平移动
|
|
|
|
|
m_lstPoints[n +1] += p; //第二个点只水平移动
|
|
|
|
|
}
|
|
|
|
|
else if(n == m_lstPoints.size()-2) //起点是倒数第二个点
|
|
|
|
|
{
|
|
|
|
|
QPointF p(0,delta.y());
|
|
|
|
|
m_lstPoints[n] += p; //起点只上下移动
|
|
|
|
|
m_lstPoints[n-1] += p; //上个点只竖直移动
|
|
|
|
|
}
|
|
|
|
|
else if(n == m_lstPoints.size()-3) //起点是倒数第三个点
|
|
|
|
|
{
|
|
|
|
|
QPointF py(0,delta.y());
|
|
|
|
|
QPointF px(delta.x(),0);
|
|
|
|
|
m_lstPoints[n-1] += py; //上个点只垂直移动
|
|
|
|
|
m_lstPoints[n] += delta; //起点任意动
|
|
|
|
|
m_lstPoints[n+1] += px; //第二个点只水平移动
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
m_lstPoints[n -1].setY(m_lstPoints[n ].y());
|
|
|
|
|
m_lstPoints[n] += delta;
|
|
|
|
|
m_lstPoints[n +1] += delta;
|
|
|
|
|
m_lstPoints[n +2].setY(m_lstPoints[n +1].y());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else //横线
|
|
|
|
|
{
|
|
|
|
|
if(n == 0) //起点是原点
|
|
|
|
|
{
|
|
|
|
|
QPointF p(delta.x(),0); //原点不动
|
|
|
|
|
m_lstPoints[n +1] = m_lstPoints[n +1] + p; //第二个点只水平移动
|
|
|
|
|
m_lstPoints[n +2] = m_lstPoints[n +2] + p; //第三个点只水平移动
|
|
|
|
|
}
|
|
|
|
|
else if(n == 1)
|
|
|
|
|
{
|
|
|
|
|
QPointF p(0,delta.y());
|
|
|
|
|
m_lstPoints[n] = m_lstPoints[n] + p; //第一个点只竖直移动
|
|
|
|
|
m_lstPoints[n +1] = m_lstPoints[n +1] + p; //第二个点只竖直移动
|
|
|
|
|
}
|
|
|
|
|
else if(n == m_lstPoints.size()-2) //起点是倒数第二个点
|
|
|
|
|
{
|
|
|
|
|
QPointF p(delta.x(),0);
|
|
|
|
|
m_lstPoints[n] += p; //起点水平移动
|
|
|
|
|
m_lstPoints[n-1] += p; //上个点只水平移动
|
|
|
|
|
}
|
|
|
|
|
else if(n == m_lstPoints.size()-3) //起点是倒数第三个点
|
|
|
|
|
{
|
|
|
|
|
QPointF py(0,delta.y());
|
|
|
|
|
QPointF px(delta.x(),0);
|
|
|
|
|
m_lstPoints[n-1] += px; //上个点只水平移动
|
|
|
|
|
m_lstPoints[n] += delta; //起点任意动
|
|
|
|
|
m_lstPoints[n+1] += py; //第二个点只上下移动
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
m_lstPoints[n -1].setX(m_lstPoints[n].x());
|
|
|
|
|
m_lstPoints[n] += delta;
|
|
|
|
|
m_lstPoints[n +1] += delta;
|
|
|
|
|
m_lstPoints[n +2].setX(m_lstPoints[n +1].x());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
calculatePath();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m_lastPoint = pos;
|
|
|
|
|
}
|
2024-12-07 17:24:36 +08:00
|
|
|
|
|
|
|
|
void ElectricConnectLineItem::calculatePath()
|
|
|
|
|
{
|
2024-12-13 18:08:00 +08:00
|
|
|
int n = m_lstPoints.size();
|
|
|
|
|
/*for(int i = 0;i < n-1;++i)
|
|
|
|
|
{
|
|
|
|
|
if(m_lstPoints[i] == m_lstPoints[i+1])
|
|
|
|
|
{
|
|
|
|
|
if(i == 0) //头重复去除一个
|
|
|
|
|
{
|
|
|
|
|
m_lstPoints.removeAt(i+1);
|
|
|
|
|
}
|
|
|
|
|
else if((i+1 == n-1)) //尾重复去除一个
|
|
|
|
|
{
|
|
|
|
|
m_lstPoints.removeAt(i+1);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
m_lstPoints.removeAt(i+1);
|
|
|
|
|
m_lstPoints.removeAt(i);
|
|
|
|
|
}
|
|
|
|
|
qDebug()<<"reset";
|
|
|
|
|
resetCurLine();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}*/
|
2024-12-07 17:24:36 +08:00
|
|
|
prepareGeometryChange();
|
|
|
|
|
m_points.clear();
|
2024-12-13 18:08:00 +08:00
|
|
|
m_pointsBoundingRect.clear();
|
2024-12-07 17:24:36 +08:00
|
|
|
|
2024-12-13 18:08:00 +08:00
|
|
|
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();
|
2024-12-07 17:24:36 +08:00
|
|
|
}
|
|
|
|
|
|
2025-02-06 16:36:50 +08:00
|
|
|
void ElectricConnectLineItem::updateConnection(QUuid callerId,QPointF pos)
|
2024-12-07 17:24:36 +08:00
|
|
|
{
|
2024-12-13 18:08:00 +08:00
|
|
|
//qDebug()<<pos;
|
2024-12-07 17:24:36 +08:00
|
|
|
if(callerId == m_connectState.nSrcNodeId)
|
|
|
|
|
setStartPoint(pos);
|
|
|
|
|
else if(callerId == m_connectState.nDestNodeId)
|
|
|
|
|
setEndPoint(pos);
|
|
|
|
|
else
|
|
|
|
|
qDebug()<<"ConnectLine update err";
|
|
|
|
|
calculatePath();
|
|
|
|
|
}
|
2025-02-06 16:36:50 +08:00
|
|
|
|
|
|
|
|
QUuid ElectricConnectLineItem::getOppositeId(QUuid id)
|
|
|
|
|
{
|
|
|
|
|
if(id == m_connectState.nSrcNodeId)
|
|
|
|
|
return m_connectState.nDestNodeId;
|
|
|
|
|
else
|
|
|
|
|
return m_connectState.nSrcNodeId;
|
|
|
|
|
}
|