DiagramDesigner/diagramCavas/source/powerTerminal.cpp

54 lines
1.7 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "powerTerminal.h"
PowerTerminal::PowerTerminal(const QString& parentEntityId,
TerminalType type,
const QString& name,
const QPointF& relativePos,
const QString& uuid,
const double dPerX,
const double dPerY,
QObject* parent)
: QObject(parent),
m_id(uuid),
m_parentEntityId(parentEntityId),
m_type(type),
m_name(name),
m_relativePosition(relativePos),
m_dPerX(dPerX),
m_dPerY(dPerY)
{
if(m_id.isEmpty())
m_id = QUuid::createUuid().toString();
}
void PowerTerminal::setRelativePosition(const QPointF& newPos) {
if (m_relativePosition != newPos) {
m_relativePosition = newPos;
emit positionChanged(newPos);
}
}
QJsonObject PowerTerminal::toJson() const {
QJsonObject obj;
obj["id"] = m_id;
obj["parentEntity"] = m_parentEntityId;
obj["type"] = static_cast<int>(m_type);
obj["name"] = m_name;
obj["relX"] = m_relativePosition.x();
obj["relY"] = m_relativePosition.y();
return obj;
}
PowerTerminal* PowerTerminal::fromJson(const QJsonObject& json, QObject* parent) {
QString id = json["id"].toString();
QString parentId = json["parentEntity"].toString();
TerminalType type = static_cast<TerminalType>(json["type"].toInt());
QString name = json["name"].toString();
qreal x = json["relX"].toDouble();
qreal y = json["relY"].toDouble();
PowerTerminal* term = new PowerTerminal(parentId, type, name, QPointF(x, y),id,0,0, parent); //***不再使用***
term->m_id = id; // 注意需要修改m_id为可写或使用其他机制保持ID一致
return term;
}