116 lines
3.1 KiB
C++
116 lines
3.1 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()
|
||
|
|
{
|
||
|
|
}
|
||
|
|
|
||
|
|
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
|
||
|
|
{
|
||
|
|
QPainterPath path;
|
||
|
|
//path.addPath(m_points);
|
||
|
|
path.addPath(m_pointsBoundingRect);
|
||
|
|
return path;
|
||
|
|
}
|
||
|
|
|
||
|
|
QRectF ElectricBaseModelLineItem::boundingRect() const
|
||
|
|
{
|
||
|
|
return m_boundingRect;
|
||
|
|
}
|
||
|
|
|
||
|
|
void ElectricBaseModelLineItem::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
|
||
|
|
{
|
||
|
|
if (option->state & QStyle::State_Selected)
|
||
|
|
{
|
||
|
|
painter->setPen(Qt::red);
|
||
|
|
}
|
||
|
|
else
|
||
|
|
{
|
||
|
|
painter->setPen(m_pen);
|
||
|
|
}
|
||
|
|
|
||
|
|
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();
|
||
|
|
}
|