191 lines
4.5 KiB
C++
191 lines
4.5 KiB
C++
#include <QPainter>
|
|
#include "diagramEditor/editItems.h"
|
|
|
|
EditBaseItem::EditBaseItem(QGraphicsItem *parent)
|
|
: QGraphicsWidget(parent)
|
|
,_pBlock(nullptr)
|
|
{
|
|
|
|
}
|
|
EditBaseItem::~EditBaseItem()
|
|
{
|
|
|
|
}
|
|
|
|
QPainterPath EditBaseItem::shape()
|
|
{
|
|
QPainterPath path;
|
|
path.addRect(m_boundingRect);
|
|
return path;
|
|
}
|
|
|
|
/********************bus*********************/
|
|
EditBusItem::EditBusItem(QGraphicsItem *parent)
|
|
: EditBaseItem(parent)
|
|
{
|
|
|
|
}
|
|
EditBusItem::~EditBusItem()
|
|
{
|
|
|
|
}
|
|
|
|
void EditBusItem::setGeometry(const QRectF &rect)
|
|
{
|
|
prepareGeometryChange();
|
|
|
|
QGraphicsWidget::setGeometry(rect);
|
|
|
|
}
|
|
|
|
void EditBusItem::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
|
|
{
|
|
painter->fillRect(m_boundingRect,Qt::black);
|
|
painter->drawText(QPointF(-10,0),sName);
|
|
}
|
|
|
|
/********************Bay*********************/
|
|
|
|
EditBayItem::EditBayItem(QGraphicsItem *parent)
|
|
: EditBaseItem(parent)
|
|
{
|
|
|
|
}
|
|
EditBayItem::~EditBayItem()
|
|
{
|
|
|
|
}
|
|
|
|
void EditBayItem::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
|
|
{
|
|
painter->setPen(QColor(220,220,230));
|
|
painter->drawRect(m_boundingRect);
|
|
painter->drawText(QPointF(-10,0),sName);
|
|
}
|
|
|
|
/********************trans*********************/
|
|
|
|
EditTransItem::EditTransItem(QGraphicsItem *parent)
|
|
: EditBaseItem(parent)
|
|
{
|
|
|
|
}
|
|
EditTransItem::~EditTransItem()
|
|
{
|
|
|
|
}
|
|
|
|
void EditTransItem::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
|
|
{
|
|
painter->setPen(QColor(180,235,155));
|
|
painter->drawRect(m_boundingRect);
|
|
painter->drawText(QPointF(-10,0),sName);
|
|
}
|
|
|
|
/********************连线*********************/
|
|
|
|
EditLineItem::EditLineItem(QGraphicsItem *parent)
|
|
: EditBaseItem(parent)
|
|
{
|
|
m_boundingRect = QRectF();
|
|
m_lstPoints.push_back(QPointF()); //起点
|
|
m_lstPoints.push_back(QPointF()); //终点
|
|
_curLine = QPoint();
|
|
}
|
|
|
|
EditLineItem::~EditLineItem()
|
|
{
|
|
}
|
|
|
|
void EditLineItem::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 EditLineItem::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 EditLineItem::shape() const
|
|
{
|
|
QPainterPath path;
|
|
path.addPath(m_pointsBoundingRect);
|
|
return path;
|
|
}
|
|
|
|
void EditLineItem::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
|
|
{
|
|
painter->setPen(Qt::black);
|
|
|
|
painter->drawPath(m_points);
|
|
}
|
|
|
|
void EditLineItem::calculatePath(int nSeg)
|
|
{
|
|
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(nSeg == 2)
|
|
{
|
|
/*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_lstPoints.insert(1,QPointF(m_lstPoints.last().x(),m_lstPoints.first().y()));
|
|
}
|
|
else
|
|
{
|
|
const QPointF p1 = m_lstPoints.first();
|
|
const QPointF p2 = m_lstPoints.last();
|
|
|
|
const qreal dx = (p2.x() - p1.x()) / 3;
|
|
const qreal dy = (p2.y() - p1.y()) / 3;
|
|
m_lstPoints.insert(1, QPointF(p1.x(), p1.y() + dy));
|
|
m_lstPoints.insert(2, QPointF(p2.x(), p1.y() + dy));
|
|
}
|
|
}
|
|
|
|
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();
|
|
}
|