#include "baseModelItem/electricBaseModelLineItem.h" #include #include #include 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 { 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(QColor(135,206,235,180)); } 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