84 lines
2.4 KiB
C++
84 lines
2.4 KiB
C++
#include "baseModelItem/electricBaseModelSvgItem.h"
|
||
#include "graphicsItem/itemControlHandle.h"
|
||
|
||
#include <QPainter>
|
||
#include <QStyleOption>
|
||
#include <QSvgRenderer>
|
||
#include <QDebug>
|
||
|
||
ElectricBaseModelSvgItem::ElectricBaseModelSvgItem(const QRect &rect, bool autoGenPort,QGraphicsItem *parent)
|
||
: GraphicsBaseModelItem(parent),m_pRender(nullptr)
|
||
{
|
||
m_lastBoudingRect = rect;
|
||
m_boundingRect = rect;
|
||
m_dWidth = rect.width();
|
||
m_dHeight = rect.height();
|
||
}
|
||
|
||
ElectricBaseModelSvgItem::~ElectricBaseModelSvgItem()
|
||
{
|
||
|
||
}
|
||
|
||
QPainterPath ElectricBaseModelSvgItem::shape()
|
||
{
|
||
QPainterPath path;
|
||
double dHandleX = 0.0;
|
||
double dHandleY = 0.0;
|
||
path.addRect(m_boundingRect);
|
||
return path;
|
||
}
|
||
|
||
void ElectricBaseModelSvgItem::updateCoordinate() //当执行了resie和editShape函数后,boundingRect发生了变换,需要将item的原点(以中心点为原点)校准至boundingRect.center()
|
||
{
|
||
if (!parentItem())
|
||
{
|
||
QPointF pt1, pt2, delta;
|
||
pt1 = mapToScene(QPointF(0, 0));
|
||
pt2 = mapToScene(m_boundingRect.center());
|
||
delta = pt1 - pt2;
|
||
|
||
prepareGeometryChange();
|
||
//将boundingRect设置成中心点和原点(也是默认变换原点),这样三点重合,有助于简化计算
|
||
m_boundingRect = QRectF(-m_dWidth / 2, -m_dHeight / 2, m_dWidth, m_dHeight);
|
||
//setTransformOriginPoint(m_boundingRect.center()); //变换中心默认为item的(0,0)点,所以不执行这句话也没有问题
|
||
//更新bouondingRect后重回会显示位置会有变化,需要做对应的移动
|
||
moveBy(-delta.x(), -delta.y());
|
||
updateHandles();
|
||
}
|
||
|
||
m_lastBoudingRect = m_boundingRect;
|
||
}
|
||
|
||
void ElectricBaseModelSvgItem::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
|
||
{
|
||
if(m_pRender)
|
||
{
|
||
m_pRender->render(painter,m_boundingRect);
|
||
}
|
||
|
||
painter->setPen(m_pen);
|
||
painter->setBrush(m_brush);
|
||
|
||
if (option->state & QStyle::State_Selected) //是选中状态,绘制选中框
|
||
{
|
||
renderSelectBackground(painter);
|
||
}
|
||
}
|
||
|
||
void ElectricBaseModelSvgItem::loadSvg(const QString& str)
|
||
{
|
||
m_pRender = new QSvgRenderer(str);
|
||
}
|
||
|
||
void ElectricBaseModelSvgItem::move(const QPointF& point)
|
||
{
|
||
moveBy(point.x(), point.y());
|
||
}
|
||
|
||
void ElectricBaseModelSvgItem::editShape(int nHandle,const QPointF& ptMouse)
|
||
{
|
||
prepareGeometryChange();
|
||
updateHandles();
|
||
}
|