logplus/Workflow/WFEngine/Component/WorkflowWidget/src/ModuleConnectGraphicsItem.cpp
2026-01-16 17:18:41 +08:00

221 lines
6.1 KiB
C++

/**
* @file ModuleConnectGraphicsItem.cpp
* @brief 连线
*/
#include <cmath>
#include <QPainter>
#include <QGradient>
#include <QPalette>
#include <QtDebug>
#include "ModuleGraphicsItem.h"
#include "ModuleConnection.h"
#include "ModuleInformation.h"
#include "ModuleConnectGraphicsItem.h"
using namespace pai::graphics2d;
using namespace pai::workflow;
ModuleConnectGraphicsItem::ModuleConnectGraphicsItem(QGraphicsItem *pParent) :
QGraphicsLineItem(pParent),
m_pStartItem(NULL),
m_pEndItem(NULL),
m_StartPortIndex(0),
m_EndPortIndex(0),
m_pConnection(NULL)
{
setZValue(1);
}
ModuleConnectGraphicsItem::ModuleConnectGraphicsItem(pai::graphics2d::ModuleGraphicsItem *pStartItem,
int startPortIndex,
pai::graphics2d::ModuleGraphicsItem *pEndItem,
int endPortIndex,
QGraphicsItem *pParent) :
QGraphicsLineItem(pParent),
m_pStartItem(pStartItem),
m_pEndItem(pEndItem),
m_StartPortIndex(startPortIndex),
m_EndPortIndex(endPortIndex),
m_pConnection(NULL)
{
if((NULL == m_pEndItem) || (NULL == m_pStartItem))
{
return;
}
setFlag(QGraphicsItem::ItemIsSelectable, true);
setPen(QPen(Qt::black, 1, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
setLine(QLineF(m_pEndItem->GetPortPostion(pai::graphics2d::Top,m_EndPortIndex),
m_pStartItem->GetPortPostion(pai::graphics2d::Bottom,m_StartPortIndex)));
setZValue(1);
}
void ModuleConnectGraphicsItem::UpdatePosition()
{
if((NULL == m_pEndItem) || (NULL == m_pStartItem))
{
return;
}
setLine(QLineF(m_pEndItem->GetPortPostion(pai::graphics2d::Top,m_EndPortIndex),
m_pStartItem->GetPortPostion(pai::graphics2d::Bottom,m_StartPortIndex)));
}
ModuleConnectGraphicsItem::~ModuleConnectGraphicsItem()
{
//连线的信息在连线从工作流中移除时析构
if(m_pConnection)
{
m_pConnection=NULL;
}
}
int ModuleConnectGraphicsItem::type() const
{
return Type;
}
QRectF ModuleConnectGraphicsItem::boundingRect() const
{
qreal extra = (pen().width() + 20) / 2.0;
return QRectF(line().p1(), QSizeF(line().p2().x() - line().p1().x(),
line().p2().y() - line().p1().y()))
.normalized()
.adjusted(-extra, -extra, extra, extra);
}
QPainterPath ModuleConnectGraphicsItem::shape() const
{
QPainterPath path = QGraphicsLineItem::shape();
path.addPolygon(m_ArrowHead);
return path;
}
void ModuleConnectGraphicsItem::paint(QPainter *pPainter, const QStyleOptionGraphicsItem */*pOption*/, QWidget */*pWidget*/)
{
if((NULL == m_pEndItem) || (NULL == m_pStartItem))
{
return;
}
const qreal Pi = 3.14159;
const qreal arrowSize = 7;
const qreal arrawAngle = 2*0.463648;
const qreal arrawHeaderRadius=3;
const qreal dPenWidth=1.0;
const QColor arrawColor("#517284");
const QColor arrawSelectColor("#59CAFA");
QPen myPen = pen();
myPen.setWidthF(dPenWidth);
myPen.setColor(isSelected()?arrawSelectColor:arrawColor);
QBrush brush(isSelected()?arrawSelectColor:arrawColor);
pPainter->setPen(myPen);
pPainter->setBrush(brush);
setLine(QLineF(m_pEndItem->GetPortPostion(pai::graphics2d::Top,m_EndPortIndex),
m_pStartItem->GetPortPostion(pai::graphics2d::Bottom,m_StartPortIndex)));
double angle = 0;
if(line().length()>1e-8)
{
angle = ::acos(line().dx() / line().length());
}
if (line().dy() >= 0)
angle = (Pi * 2) - angle;
QPainterPath path;
QPointF p1(line().p1());
QPointF p2(line().p2());
path.moveTo(p2);
path.lineTo(p1);
m_ArrowHead.clear();
if (isSelected())
{
path.addRect(p2.x()-arrawHeaderRadius,p2.y()-arrawHeaderRadius,arrawHeaderRadius+arrawHeaderRadius,arrawHeaderRadius+arrawHeaderRadius);
path.addRect(p1.x()-arrawHeaderRadius,p1.y()-arrawHeaderRadius,arrawHeaderRadius+arrawHeaderRadius,arrawHeaderRadius+arrawHeaderRadius);
}
else
{
QPointF arrowP1 = p1 + QPointF(sin(angle + arrawAngle) * arrowSize,
cos(angle + arrawAngle) * arrowSize);
QPointF arrowP2 = p1 + QPointF(sin(angle + Pi - arrawAngle) * arrowSize,
cos(angle + Pi - arrawAngle) * arrowSize);
m_ArrowHead << p1 << arrowP1 << arrowP2;
path.addPolygon(m_ArrowHead);
}
pPainter->drawPath(path);
}
CModuleConnection* ModuleConnectGraphicsItem::GetConnection()
{
if((NULL == m_pEndItem) || (NULL == m_pStartItem))
{
return NULL;
}
if(NULL==m_pConnection)
{
m_pConnection=new CModuleConnection();
}
CModuleInformation* pStartMoudle = m_pStartItem->GetModule();
CModuleInformation* pEndMoudle = m_pEndItem->GetModule();
if(pStartMoudle != NULL)
{
m_pConnection->SetSourceId(pStartMoudle->GetStepID());
}
if(pEndMoudle != NULL)
{
m_pConnection->SetDestId(pEndMoudle->GetStepID());
}
m_pConnection->SetOutPort(QString::number(m_StartPortIndex).toStdString());
m_pConnection->SetInPort(QString::number(m_EndPortIndex).toStdString());
return m_pConnection;
}
void ModuleConnectGraphicsItem::GetConnectLineStartEnd(pai::graphics2d::ModuleGraphicsItem *&pStart,
pai::graphics2d::ModuleGraphicsItem *&pEnd)
{
pStart = m_pStartItem;
pEnd = m_pEndItem;
}
int ModuleConnectGraphicsItem::GetStartPortIndex() const
{
return m_StartPortIndex;
}
int ModuleConnectGraphicsItem::GetEndPortIndex() const
{
return m_EndPortIndex;
}
ModuleGraphicsItem * ModuleConnectGraphicsItem::GetStartItem() const
{
return m_pStartItem;
}
void ModuleConnectGraphicsItem::SetStartItem(ModuleGraphicsItem *pStartItem)
{
m_pStartItem = pStartItem;
}
ModuleGraphicsItem * ModuleConnectGraphicsItem::GetEndItem() const
{
return m_pEndItem;
}
void ModuleConnectGraphicsItem::SetEndItem(ModuleGraphicsItem *pEndItem)
{
m_pEndItem = pEndItem;
}