139 lines
4.7 KiB
C++
139 lines
4.7 KiB
C++
#ifndef POWERENTITY_H
|
||
#define POWERENTITY_H
|
||
/****************************
|
||
* 电力实体类,实现拓扑层级的建立 grid-zone-station-diagram
|
||
* *************************/
|
||
#include <QString>
|
||
#include <QList>
|
||
#include "powerTerminal.h"
|
||
#include "topologyManager.h"
|
||
#include "global.h"
|
||
|
||
class PowerTerminal;
|
||
|
||
// 所有实体的基类(组合模式核心)
|
||
class PowerEntity :public QObject{
|
||
Q_OBJECT
|
||
public:
|
||
PowerEntity(EntityType type, const QString& id, const QString& name)
|
||
: m_type(type), m_id(id), m_name(name) {}
|
||
|
||
virtual ~PowerEntity() {
|
||
//qDeleteAll(m_children);
|
||
}
|
||
|
||
virtual PowerEntity* clone(){
|
||
// 1. 检查是否已经拷贝过
|
||
if (TopologyManager::instance().findEntity(this->m_id,ModelFunctionType::BaseModel)) { //拷贝的子项也加入到clonedMap
|
||
return TopologyManager::instance().findEntity(this->m_id,ModelFunctionType::BaseModel);
|
||
}
|
||
|
||
// 2. 创建当前对象的新副本
|
||
PowerEntity* newEntity = TopologyManager::instance().createEntity(m_type,m_id,m_name,ModelFunctionType::BaseModel);
|
||
|
||
// 3. 清空子列表(避免浅拷贝)
|
||
newEntity->m_children.clear();
|
||
newEntity->m_terminals.clear();
|
||
|
||
// 4. 递归克隆子项
|
||
for (PowerEntity* child : m_children) {
|
||
newEntity->addChild(child->clone());
|
||
}
|
||
|
||
// 5. 深拷贝所有 PowerTerminal
|
||
for (PowerTerminal* terminal : m_terminals) {
|
||
auto pTer = TopologyManager::instance().getTerminal(terminal->id(),ModelFunctionType::BaseModel);
|
||
if(pTer == nullptr){ //BaseModel中不存在则拷贝
|
||
double dX = terminal->getPerX();
|
||
double dY = terminal->getPerY();
|
||
auto newTer = TopologyManager::instance().createTerminal(m_id,terminal->type(),terminal->name(),terminal->relativePosition(),terminal->id(),ModelFunctionType::BaseModel,dX,dY);
|
||
if(newTer){
|
||
newTer->setPortLocate(terminal->getPortLocate());
|
||
}
|
||
}
|
||
}
|
||
|
||
return newEntity;
|
||
}
|
||
|
||
// 添加/删除子元素
|
||
void addChild(PowerEntity* child) {
|
||
if (child == this || getAllDescendants().contains(this)) {
|
||
qCritical() << "Detected cyclic parent-child relationship!";
|
||
return;
|
||
}
|
||
if (child && !m_children.contains(child)) {
|
||
m_children.append(child);
|
||
child->m_parent = this;
|
||
}
|
||
}
|
||
|
||
void removeChild(PowerEntity* child) {
|
||
if (child && m_children.removeOne(child)) {
|
||
child->m_parent = nullptr;
|
||
}
|
||
}
|
||
|
||
// 获取属性
|
||
EntityType type() const { return m_type; }
|
||
QString id() const { return m_id; }
|
||
QString name() const { return m_name; }
|
||
QList<PowerEntity*> children() const { return m_children; }
|
||
PowerEntity* parent() const { return m_parent; }
|
||
virtual void setBlock(const QString& s){m_bBlock = s;}
|
||
virtual QString block(){return m_bBlock;}
|
||
// 递归查找
|
||
QList<PowerEntity*> getAllDescendants() const {
|
||
QList<PowerEntity*> descendants;
|
||
for (PowerEntity* child : m_children) {
|
||
descendants.append(child);
|
||
descendants.append(child->getAllDescendants());
|
||
}
|
||
return descendants;
|
||
}
|
||
|
||
virtual QJsonObject toJson() const;
|
||
public:
|
||
// 接线点管理
|
||
void addTerminal(PowerTerminal* terminal);
|
||
void removeTerminal(const QString& terminalId);
|
||
QList<PowerTerminal*> terminals() const { return m_terminals; }
|
||
PowerTerminal* findTerminal(const QString& terminalId) const;
|
||
signals:
|
||
void terminalAdded(PowerTerminal* terminal);
|
||
void terminalRemoved(const QString& terminalId);
|
||
protected:
|
||
QList<PowerTerminal*> m_terminals;
|
||
private:
|
||
EntityType m_type;
|
||
QString m_id; // 唯一标识符(可用UUID生成)
|
||
QString m_name;
|
||
QString m_bBlock; //所属区块(编辑时)
|
||
PowerEntity* m_parent = nullptr;
|
||
QList<PowerEntity*> m_children;
|
||
};
|
||
|
||
// 组态图特殊属性(继承自PowerEntity)
|
||
class ConfigurationDiagram : public PowerEntity {
|
||
public:
|
||
ConfigurationDiagram(const QString& id, const QString& name)
|
||
: PowerEntity(EntityType::ConfigurationDiagram, id, name) {}
|
||
|
||
// 可扩展图特有属性(如版本、创建时间等)
|
||
};
|
||
|
||
//电网区域划分 grid-zone-station
|
||
class PowerDivision : public PowerEntity {
|
||
public:
|
||
PowerDivision(EntityType type,const QString& id, const QString& name)
|
||
: PowerEntity(type, id, name) {}
|
||
};
|
||
|
||
//电力元件(拓扑)
|
||
class PowerComponent : public PowerEntity {
|
||
public:
|
||
PowerComponent(EntityType type,const QString& id, const QString& name)
|
||
: PowerEntity(type, id, name) {}
|
||
};
|
||
#endif
|