DiagramDesigner/diagramCavas/source/topologyManager.cpp

1085 lines
35 KiB
C++
Raw Normal View History

2025-04-22 10:10:55 +08:00
#include <QJsonArray>
#include <QJsonDocument>
#include "topologyManager.h"
#include "powerEntity.h"
2025-05-09 19:36:32 +08:00
#include "graphicsItem/graphicsBaseItem.h"
2025-04-22 10:10:55 +08:00
TopologyManager& TopologyManager::instance()
{
static TopologyManager instance;
return instance;
}
TopologyManager::TopologyManager(QObject* parent)
: QObject(parent) {}
TopologyManager::~TopologyManager() {
clearAllData();
}
void TopologyManager::clearAllData()
{
// 删除所有连接
qDeleteAll(m_connections);
m_connections.clear();
m_connectionIndex.clear();
// 删除所有实体(自动删除子对象)
qDeleteAll(m_entities);
m_entities.clear();
2025-04-30 16:29:17 +08:00
m_allTerminals.clear(); //端点由父亲entity释放
// 删除所有连接
qDeleteAll(m_baseConnections);
m_baseConnections.clear();
m_baseConnectionIndex.clear();
// 删除所有实体(自动删除子对象)
qDeleteAll(m_baseEntities);
m_baseEntities.clear();
m_baseAllTerminals.clear(); //端点由父亲entity释放
// 删除所有连接
qDeleteAll(m_editorConnections);
m_editorConnections.clear();
m_editorConnectionIndex.clear();
// 删除所有实体(自动删除子对象)
qDeleteAll(m_editorEntities);
m_editorEntities.clear();
m_editorAllTerminals.clear(); //端点由父亲entity释放
2025-08-28 10:59:04 +08:00
qDeleteAll(m_blockConnections);
m_blockConnections.clear();
m_blockConnectionIndex.clear();
// 删除所有实体(自动删除子对象)
qDeleteAll(m_blockEntities);
m_blockEntities.clear();
m_blockAllTerminals.clear(); //端点由父亲entity释放
2025-04-22 10:10:55 +08:00
}
PowerEntity* TopologyManager::createEntity(EntityType type,const QString& uuid, const QString& name,ModelFunctionType funType,const QString& block)
2025-04-22 10:10:55 +08:00
{
if(funType == ModelFunctionType::ProjectModel)
{
PowerEntity* entity = nullptr;
switch(type) {
case EntityType::Grid:
case EntityType::Zone:
case EntityType::Station:
entity = new PowerDivision(type, uuid,name);
default:
entity = new PowerComponent(type, uuid,name);
}
if(entity == nullptr)
return nullptr;
m_entities.insert(entity->id(), entity);
emit entityCreated(entity->id()); // 发送信号通知
return entity;
2025-04-22 10:10:55 +08:00
}
else if(funType == ModelFunctionType::BaseModel)
{
PowerEntity* entity = nullptr;
entity = new PowerComponent(type, uuid,name);
2025-04-22 10:10:55 +08:00
m_baseEntities.insert(entity->id(), entity);
emit entityCreated(entity->id()); // 发送信号通知
return entity;
}
else if(funType == ModelFunctionType::EditorModel)
{
PowerEntity* entity = nullptr;
entity = new PowerComponent(type, uuid,name);
m_editorEntities.insert(entity->id(), entity);
return entity;
}
else if(funType == ModelFunctionType::BlockEditorModel)
{
PowerEntity* entity = nullptr;
entity = new PowerComponent(type, uuid,name);
entity->setBlock(block);
m_blockEntities.insert(entity->id(), entity);
return entity;
}
return nullptr;
2025-04-22 10:10:55 +08:00
}
PowerEntity* TopologyManager::findEntity(const QString& id,ModelFunctionType funType) const
2025-04-22 10:10:55 +08:00
{
if(funType == ModelFunctionType::ProjectModel)
return m_entities.value(id, nullptr); // 避免异常的安全查询
else if(funType == ModelFunctionType::BaseModel)
return m_baseEntities.value(id, nullptr);
else if(funType == ModelFunctionType::EditorModel)
return m_editorEntities.value(id, nullptr);
else if(funType == ModelFunctionType::BlockEditorModel)
return m_blockEntities.value(id, nullptr);
return nullptr;
2025-04-22 10:10:55 +08:00
}
bool TopologyManager::deleteEntity(const QString& id,ModelFunctionType funType)
2025-04-22 10:10:55 +08:00
{
if(funType == ModelFunctionType::ProjectModel)
{
if (!m_entities.contains(id)) return false;
2025-04-22 10:10:55 +08:00
PowerEntity* entity = m_entities[id];
2025-04-22 10:10:55 +08:00
// 步骤1删除所有相关连接
auto relatedConns = getConnectionsFor(id);
2025-04-30 16:29:17 +08:00
for (auto conn : relatedConns) {
removeConnection(conn->id());
}
2025-04-22 10:10:55 +08:00
// 步骤2从父节点移除防止悬空指针
if (PowerEntity* parent = entity->parent()) {
parent->removeChild(entity);
}
2025-04-22 10:10:55 +08:00
// 步骤3递归删除子实体
auto children = entity->children();
for (auto child : children) {
deleteEntity(child->id()); // 递归删除
}
// 步骤4从哈希表移除并释放内存
m_entities.remove(id);
delete entity; // 触发PowerEntity析构函数
emit entityDeleted(id);
return true;
2025-04-22 10:10:55 +08:00
}
else if(funType == ModelFunctionType::BaseModel)
{
if (!m_baseEntities.contains(id)) return false;
PowerEntity* entity = m_baseEntities[id];
// 步骤1删除所有相关连接
auto relatedConns = getConnectionsFor(id,funType);
for (auto conn : relatedConns) {
removeConnection(conn->id(),funType);
}
// 步骤2从父节点移除防止悬空指针
if (PowerEntity* parent = entity->parent()) {
parent->removeChild(entity);
}
// 步骤3递归删除子实体
auto children = entity->children();
for (auto child : children) {
deleteEntity(child->id(),funType); // 递归删除
}
2025-04-22 10:10:55 +08:00
// 步骤4从哈希表移除并释放内存
m_baseEntities.remove(id);
delete entity; // 触发PowerEntity析构函数
2025-04-22 10:10:55 +08:00
emit entityDeleted(id);
return true;
}
else if(funType == ModelFunctionType::EditorModel)
{
if (!m_editorEntities.contains(id)) return false;
PowerEntity* entity = m_editorEntities[id];
// 步骤1删除所有相关连接
auto relatedConns = getConnectionsFor(id,funType);
for (auto conn : relatedConns) {
removeConnection(conn->id(),funType);
}
// 步骤2从父节点移除防止悬空指针
if (PowerEntity* parent = entity->parent()) {
parent->removeChild(entity);
}
// 步骤3递归删除子实体
auto children = entity->children();
for (auto child : children) {
deleteEntity(child->id(),funType); // 递归删除
}
// 步骤4从哈希表移除并释放内存
m_editorEntities.remove(id);
delete entity; // 触发PowerEntity析构函数
emit entityDeleted(id);
return true;
}
else if(funType == ModelFunctionType::BlockEditorModel)
{
if (!m_blockEntities.contains(id)) return false;
PowerEntity* entity = m_blockEntities[id];
// 步骤1删除所有相关连接
auto relatedConns = getConnectionsFor(id,funType);
for (auto conn : relatedConns) {
removeConnection(conn->id(),funType);
}
// 步骤2从父节点移除防止悬空指针
if (PowerEntity* parent = entity->parent()) {
parent->removeChild(entity);
}
// 步骤3递归删除子实体
auto children = entity->children();
for (auto child : children) {
deleteEntity(child->id(),funType); // 递归删除
}
// 步骤4从哈希表移除并释放内存
m_blockEntities.remove(id);
delete entity; // 触发PowerEntity析构函数
emit entityDeleted(id);
return true;
}
return false;
2025-04-22 10:10:55 +08:00
}
QList<PowerEntity*> TopologyManager::getEntitiesByBlock(const QString& str)
{
QList<PowerEntity*> lst;
for(auto& entity:m_blockEntities){
if(entity->block() == str){
lst.append(entity);
}
}
return lst;
}
2025-04-22 10:10:55 +08:00
2025-08-28 10:59:04 +08:00
void TopologyManager::moveTempBlockData()
{
auto tempEntities = std::move(m_blockEntities);
m_editorEntities.insert(tempEntities);
auto tempConnections = std::move(m_blockConnections);
m_editorConnections.insert(tempConnections);
auto tempConIndex = std::move(m_blockConnectionIndex);
m_editorConnectionIndex.unite(tempConIndex);
auto tempTer = std::move(m_blockAllTerminals);
m_editorAllTerminals.insert(tempTer);
auto tempEntityTer = std::move(m_blockTerminalsByEntity);
m_editorTerminalsByEntity.insert(tempEntityTer);
auto tempEntityCon = std::move(m_blockEntityConnections);
m_editorEntityConnections.insert(tempEntityCon);
}
void TopologyManager::clearGlobalBlockData(const QString& sName)
{
QList<PowerEntity*> lst = getEntitiesByBlock(sName);
for(auto p:lst){
deleteEntity(p->id(),ModelFunctionType::EditorModel);
}
}
2025-09-26 18:50:21 +08:00
void TopologyManager::cloneEditorToBase()
{
for(auto& editorEntity:m_editorEntities){
editorEntity->clone();
}
for(auto& con:m_editorConnections){
if(connection(con->id(),ModelFunctionType::BaseModel) == nullptr)
createConnection(con->id(),con->fromTerminalId(),con->toTerminalId(),con->fromComponent(),con->toComponent(),ModelFunctionType::BaseModel);
}
}
PowerConnection* TopologyManager::createConnection(const QString& connId,const QString& fromTerId, const QString& toTerId,const QString& fromId,const QString& toId,ModelFunctionType funType)
2025-04-22 10:10:55 +08:00
{
PowerConnection* conn = nullptr;
if(funType == ModelFunctionType::ProjectModel)
2025-04-22 10:10:55 +08:00
{
// 验证有效性
if (!m_allTerminals.contains(fromTerId) ||
!m_allTerminals.contains(toTerId) ||
fromTerId == toTerId)
2025-04-22 10:10:55 +08:00
{
qWarning() << "Invalid connection endpoints";
return nullptr;
}
// 防止重复连接
foreach (auto conn, m_connections) {
if (conn->fromTerminalId() == fromTerId &&
conn->toTerminalId() == toTerId)
{
return conn; // 返回已存在的连接
}
2025-04-22 10:10:55 +08:00
}
// 创建新连接
conn = new PowerConnection(connId,fromTerId, toTerId,fromId,toId);
m_connections[connId] = conn;
// 更新索引
m_connectionIndex.insert(fromTerId, conn);
m_connectionIndex.insert(toTerId, conn);
emit connectionCreated(connId);
2025-04-22 10:10:55 +08:00
}
else if(funType == ModelFunctionType::BaseModel)
{
// 验证有效性
if (!m_baseAllTerminals.contains(fromTerId) ||
!m_baseAllTerminals.contains(toTerId) ||
fromTerId == toTerId)
{
qWarning() << "Invalid connection endpoints";
return nullptr;
}
2025-04-22 10:10:55 +08:00
// 防止重复连接
foreach (auto conn, m_baseConnections) {
if (conn->fromTerminalId() == fromTerId &&
conn->toTerminalId() == toTerId)
{
return conn; // 返回已存在的连接
}
}
// 创建新连接
conn = new PowerConnection(connId,fromTerId, toTerId,fromId,toId);
m_baseConnections[connId] = conn;
2025-04-22 10:10:55 +08:00
// 更新索引
m_baseConnectionIndex.insert(fromTerId, conn);
m_baseConnectionIndex.insert(toTerId, conn);
2025-04-22 10:10:55 +08:00
emit connectionCreated(connId);
}
else if(funType == ModelFunctionType::EditorModel)
{
// 验证有效性
if (!m_editorAllTerminals.contains(fromTerId) ||
!m_editorAllTerminals.contains(toTerId) ||
fromTerId == toTerId)
{
qWarning() << "Invalid connection endpoints";
return nullptr;
}
// 防止重复连接
foreach (auto conn, m_editorConnections) {
if (conn->fromTerminalId() == fromTerId &&
conn->toTerminalId() == toTerId)
{
return conn; // 返回已存在的连接
}
}
// 创建新连接
conn = new PowerConnection(connId,fromTerId, toTerId,fromId,toId);
m_editorConnections[connId] = conn;
// 更新索引
m_editorConnectionIndex.insert(fromTerId, conn);
m_editorConnectionIndex.insert(toTerId, conn);
emit connectionCreated(connId);
}
else if(funType == ModelFunctionType::BlockEditorModel)
{
// 验证有效性
if (!m_blockAllTerminals.contains(fromTerId) ||
!m_blockAllTerminals.contains(toTerId) ||
fromTerId == toTerId)
{
qWarning() << "Invalid connection endpoints";
return nullptr;
}
// 防止重复连接
foreach (auto conn, m_blockConnections) {
if (conn->fromTerminalId() == fromTerId &&
conn->toTerminalId() == toTerId)
{
return conn; // 返回已存在的连接
}
}
// 创建新连接
conn = new PowerConnection(connId,fromTerId, toTerId,fromId,toId);
m_blockConnections[connId] = conn;
// 更新索引
m_blockConnectionIndex.insert(fromTerId, conn);
m_blockConnectionIndex.insert(toTerId, conn);
emit connectionCreated(connId);
}
2025-04-22 10:10:55 +08:00
return conn;
}
QList<PowerConnection*> TopologyManager::getConnectionsForTerminal(const QString& terminalId,ModelFunctionType funType) const
2025-04-22 10:10:55 +08:00
{
if(funType == ModelFunctionType::ProjectModel)
return m_connectionIndex.values(terminalId);
else if(funType == ModelFunctionType::BaseModel)
return m_baseConnectionIndex.values(terminalId);
else if(funType == ModelFunctionType::EditorModel)
return m_editorConnectionIndex.values(terminalId);
else if(funType == ModelFunctionType::BlockEditorModel)
return m_blockConnectionIndex.values(terminalId);
return QList<PowerConnection*>();
2025-04-22 10:10:55 +08:00
}
void TopologyManager::removeConnection(const QString& connId,ModelFunctionType funType)
2025-04-22 10:10:55 +08:00
{
if(funType == ModelFunctionType::ProjectModel){
if (!m_connections.contains(connId)) return;
PowerConnection* conn = m_connections[connId];
// 更新索引
m_connectionIndex.remove(conn->fromTerminalId(), conn);
m_connectionIndex.remove(conn->toTerminalId(), conn);
2025-04-22 10:10:55 +08:00
// 清理内存
m_connections.remove(connId);
delete conn;
2025-04-22 10:10:55 +08:00
emit connectionRemoved(connId);
}
else if(funType == ModelFunctionType::BaseModel){
if (!m_baseConnections.contains(connId)) return;
PowerConnection* conn = m_baseConnections[connId];
// 更新索引
m_baseConnectionIndex.remove(conn->fromTerminalId(), conn);
m_baseConnectionIndex.remove(conn->toTerminalId(), conn);
2025-04-22 10:10:55 +08:00
// 清理内存
m_baseConnections.remove(connId);
delete conn;
2025-04-22 10:10:55 +08:00
emit connectionRemoved(connId);
}
else if(funType == ModelFunctionType::EditorModel){
if (!m_editorConnections.contains(connId)) return;
2025-04-22 10:10:55 +08:00
PowerConnection* conn = m_editorConnections[connId];
// 更新索引
m_editorConnectionIndex.remove(conn->fromTerminalId(), conn);
m_editorConnectionIndex.remove(conn->toTerminalId(), conn);
// 清理内存
m_editorConnections.remove(connId);
delete conn;
emit connectionRemoved(connId);
}
else if(funType == ModelFunctionType::BlockEditorModel){
if (!m_blockConnections.contains(connId)) return;
PowerConnection* conn = m_blockConnections[connId];
// 更新索引
m_blockConnectionIndex.remove(conn->fromTerminalId(), conn);
m_blockConnectionIndex.remove(conn->toTerminalId(), conn);
// 清理内存
m_blockConnections.remove(connId);
delete conn;
emit connectionRemoved(connId);
}
2025-04-30 16:29:17 +08:00
}
bool TopologyManager::validateConnection(const QString& fromTermId, const QString& toTermId,ModelFunctionType funType) const
2025-04-22 10:10:55 +08:00
{
PowerTerminal* fromTerm = nullptr;
PowerTerminal* toTerm = nullptr;
if(funType == ModelFunctionType::ProjectModel){
fromTerm = getTerminal(fromTermId);
toTerm = getTerminal(toTermId);
}
else if(funType == ModelFunctionType::BaseModel){
fromTerm = getTerminal(fromTermId,funType);
toTerm = getTerminal(toTermId,funType);
}
else if(funType == ModelFunctionType::EditorModel){
fromTerm = getTerminal(fromTermId,funType);
toTerm = getTerminal(toTermId,funType);
}
else if(funType == ModelFunctionType::BlockEditorModel){
fromTerm = getTerminal(fromTermId,funType);
toTerm = getTerminal(toTermId,funType);
}
if (!fromTerm || !toTerm) return false;
// 类型兼容性检查(示例规则)
switch(fromTerm->type()) {
case TerminalType::PowerOutput:
if (toTerm->type() != TerminalType::PowerInput && toTerm->type() != TerminalType::PowerConnect) return false;
break;
case TerminalType::PowerInput:
if (toTerm->type() != TerminalType::PowerOutput && toTerm->type() != TerminalType::PowerConnect) return false;
break;
case TerminalType::PowerConnect:
break;
case TerminalType::ControlSignal:
if (toTerm->type() != TerminalType::ControlSignal) return false;
break;
default:
return false;
}
// 禁止自连接
return (fromTerm->parentEntityId() != toTerm->parentEntityId());
2025-04-22 10:10:55 +08:00
}
2025-04-22 10:10:55 +08:00
QList<PowerConnection*> TopologyManager::getConnectionsFor(const QString& entityId,ModelFunctionType funType) const
2025-04-22 10:10:55 +08:00
{
2025-04-30 16:29:17 +08:00
QList<PowerConnection*> lst;
if(funType == ModelFunctionType::ProjectModel)
{
QList<PowerTerminal*> lstTerminal = getTerminalsForEntity(entityId);
for(auto &terminal:lstTerminal)
{
PowerConnection* con = getConnectionContainsTerminal(terminal->id());
if(con)
lst.append(con);
}
}
else if(funType == ModelFunctionType::BaseModel)
2025-04-30 16:29:17 +08:00
{
QList<PowerTerminal*> lstTerminal = getTerminalsForEntity(entityId,funType);
for(auto &terminal:lstTerminal)
{
PowerConnection* con = getConnectionContainsTerminal(terminal->id(),funType);
if(con)
lst.append(con);
}
2025-04-30 16:29:17 +08:00
}
else if(funType == ModelFunctionType::EditorModel)
{
QList<PowerTerminal*> lstTerminal = getTerminalsForEntity(entityId,funType);
for(auto &terminal:lstTerminal)
{
PowerConnection* con = getConnectionContainsTerminal(terminal->id(),funType);
if(con)
lst.append(con);
}
}
else if(funType == ModelFunctionType::BlockEditorModel)
{
QList<PowerTerminal*> lstTerminal = getTerminalsForEntity(entityId,funType);
for(auto &terminal:lstTerminal)
{
PowerConnection* con = getConnectionContainsTerminal(terminal->id(),funType);
if(con)
lst.append(con);
}
}
2025-04-30 16:29:17 +08:00
return lst;
}
PowerConnection* TopologyManager::connection(const QString& conId,ModelFunctionType funType) const
2025-04-30 16:29:17 +08:00
{
if(funType == ModelFunctionType::ProjectModel)
return m_connections.value(conId,nullptr);
else if(funType == ModelFunctionType::BaseModel)
return m_baseConnections.value(conId,nullptr);
else if(funType == ModelFunctionType::EditorModel)
return m_editorConnections.value(conId,nullptr);
else if(funType == ModelFunctionType::BlockEditorModel)
return m_blockConnections.value(conId,nullptr);
return nullptr;
2025-04-22 10:10:55 +08:00
}
PowerConnection* TopologyManager::connection(const QString& fromPin,const QString& toPin,ModelFunctionType funType)
2025-05-16 19:20:46 +08:00
{
if(funType == ModelFunctionType::ProjectModel){
for(auto &con:m_connections)
{
if(con->fromTerminalId() == fromPin && con->toTerminalId() == toPin)
return con;
}
2025-05-16 19:20:46 +08:00
}
else if(funType == ModelFunctionType::BaseModel){
for(auto &con:m_baseConnections)
{
if(con->fromTerminalId() == fromPin && con->toTerminalId() == toPin)
return con;
}
}
else if(funType == ModelFunctionType::EditorModel){
for(auto &con:m_editorConnections)
{
if(con->fromTerminalId() == fromPin && con->toTerminalId() == toPin)
return con;
}
}
else if(funType == ModelFunctionType::BlockEditorModel){
for(auto &con:m_blockConnections)
{
if(con->fromTerminalId() == fromPin && con->toTerminalId() == toPin)
return con;
}
}
2025-05-16 19:20:46 +08:00
return nullptr;
}
2025-08-28 10:59:04 +08:00
PowerConnection* TopologyManager::ifConnection(const QString& entityId1,const QString& entityId2,ModelFunctionType funType)
{
QList<PowerConnection*> lst1;
QList<PowerConnection*> lst2;
QList<PowerTerminal*> lstTerminal1 = getTerminalsForEntity(entityId1,funType);
for(auto &terminal:lstTerminal1)
{
PowerConnection* con = getConnectionContainsTerminal(terminal->id(),funType);
if(con)
lst1.append(con);
}
QList<PowerTerminal*> lstTerminal2 = getTerminalsForEntity(entityId2,funType);
for(auto &terminal:lstTerminal2)
{
PowerConnection* con = getConnectionContainsTerminal(terminal->id(),funType);
if(con)
lst2.append(con);
}
for(auto pCon1:lst1){
for(auto pCon2:lst2){
if(pCon1->id() == pCon2->id()){ //两个item的连接有重合返回该连接
return pCon1;
}
}
}
return nullptr;
}
QHash<QString,PowerConnection*> TopologyManager::getAllConnections(ModelFunctionType funType)
2025-05-16 19:20:46 +08:00
{
if(funType == ModelFunctionType::ProjectModel)
return m_connections;
else if(funType == ModelFunctionType::BaseModel)
return m_baseConnections;
else if(funType == ModelFunctionType::EditorModel)
return m_editorConnections;
else if(funType == ModelFunctionType::BlockEditorModel)
return m_blockConnections;
return QHash<QString,PowerConnection*>();
2025-05-16 19:20:46 +08:00
}
PowerEntity* TopologyManager::getEntity(const QString& id,ModelFunctionType funType) const
2025-04-22 10:10:55 +08:00
{
if(funType == ModelFunctionType::ProjectModel){
auto it = m_entities.find(id);
return (it != m_entities.end()) ? it.value() : nullptr;
2025-04-30 16:29:17 +08:00
}
else if(funType == ModelFunctionType::BaseModel){
auto it = m_baseEntities.find(id);
return (it != m_baseEntities.end()) ? it.value() : nullptr;
2025-04-22 10:10:55 +08:00
}
else if(funType == ModelFunctionType::EditorModel){
auto it = m_editorEntities.find(id);
return (it != m_editorEntities.end()) ? it.value() : nullptr;
}
else if(funType == ModelFunctionType::BlockEditorModel){
auto it = m_blockEntities.find(id);
return (it != m_blockEntities.end()) ? it.value() : nullptr;
}
return nullptr;
2025-04-22 10:10:55 +08:00
}
QList<PowerEntity*> TopologyManager::findEntitiesByName(const QString& name,ModelFunctionType funType) const
2025-04-22 10:10:55 +08:00
{
QList<PowerEntity*> results;
if(funType == ModelFunctionType::ProjectModel){
foreach (auto entity, m_entities) {
if (entity->name() == name) {
results.append(entity);
}
}
}
else if(funType == ModelFunctionType::BaseModel){
foreach (auto entity, m_baseEntities) {
if (entity->name() == name) {
results.append(entity);
}
2025-04-22 10:10:55 +08:00
}
}
else if(funType == ModelFunctionType::EditorModel){
foreach (auto entity, m_editorEntities) {
if (entity->name() == name) {
results.append(entity);
}
}
}
else if(funType == ModelFunctionType::BlockEditorModel){
foreach (auto entity, m_blockEntities) {
if (entity->name() == name) {
results.append(entity);
}
}
}
2025-04-22 10:10:55 +08:00
return results;
}
PowerEntity* TopologyManager::createDiagram(const QString& id,const QString& name,ModelFunctionType funType)
2025-05-09 19:36:32 +08:00
{
PowerEntity* entity = nullptr;
if(funType == ModelFunctionType::ProjectModel){
if(!m_diagrams.contains(id))
{
entity = new ConfigurationDiagram(id,name);
m_diagrams.insert(entity->id(), entity);
}
}
else if(funType == ModelFunctionType::BaseModel){
if(!m_baseDiagrams.contains(id))
{
entity = new ConfigurationDiagram(id,name);
m_baseDiagrams.insert(entity->id(), entity);
}
2025-05-16 19:20:46 +08:00
}
2025-05-09 19:36:32 +08:00
return entity;
}
PowerEntity* TopologyManager::findDiagram(const QString& id,ModelFunctionType funType) const
2025-05-09 19:36:32 +08:00
{
if(funType == ModelFunctionType::ProjectModel)
return m_diagrams.value(id, nullptr); // 避免异常的安全查询
else if(funType == ModelFunctionType::BaseModel)
return m_baseDiagrams.value(id, nullptr);
return nullptr;
2025-05-09 19:36:32 +08:00
}
bool TopologyManager::deleteDiagram(const QString& id,ModelFunctionType funType)
2025-05-09 19:36:32 +08:00
{
if(funType == ModelFunctionType::ProjectModel){
if (!m_diagrams.contains(id)) return false;
2025-05-09 19:36:32 +08:00
PowerEntity* entity = m_diagrams[id];
2025-05-09 19:36:32 +08:00
// 步骤2从父节点移除防止悬空指针
if (PowerEntity* parent = entity->parent()) {
parent->removeChild(entity);
}
// 步骤3递归删除子实体
auto children = entity->children();
for (auto child : children) {
deleteDiagram(child->id()); // 递归删除
}
2025-05-09 19:36:32 +08:00
// 步骤4从哈希表移除并释放内存
m_diagrams.remove(id);
delete entity; // 触发析构函数
return true;
2025-05-09 19:36:32 +08:00
}
else if(funType == ModelFunctionType::BaseModel){
if (!m_baseDiagrams.contains(id)) return false;
PowerEntity* entity = m_baseDiagrams[id];
// 步骤2从父节点移除防止悬空指针
if (PowerEntity* parent = entity->parent()) {
parent->removeChild(entity);
}
// 步骤3递归删除子实体
auto children = entity->children();
for (auto child : children) {
deleteDiagram(child->id(),funType); // 递归删除
}
2025-05-09 19:36:32 +08:00
// 步骤4从哈希表移除并释放内存
m_baseDiagrams.remove(id);
delete entity; // 触发析构函数
2025-05-09 19:36:32 +08:00
return true;
}
return false;
2025-05-09 19:36:32 +08:00
}
2025-04-22 10:10:55 +08:00
PowerTerminal* TopologyManager::createTerminal(const QString& parentEntityId,
TerminalType type,
const QString& name,
2025-04-30 16:29:17 +08:00
const QPointF& relPos,
const QString& uuid,
2025-10-11 18:51:33 +08:00
ModelFunctionType funType,
double dPerX,
double dPerY) {
if(funType == ModelFunctionType::ProjectModel){
if (!m_entities.contains(parentEntityId)) return nullptr;
2025-10-11 18:51:33 +08:00
PowerTerminal* term = new PowerTerminal(parentEntityId, type, name, relPos, uuid, dPerX, dPerY);
m_allTerminals[term->id()] = term;
m_terminalsByEntity[parentEntityId].append(term);
// 关联到父实体
if (PowerEntity* parent = getEntity(parentEntityId)) {
parent->addTerminal(term);
}
2025-04-22 10:10:55 +08:00
return term;
2025-04-22 10:10:55 +08:00
}
else if(funType == ModelFunctionType::BaseModel){
if (!m_baseEntities.contains(parentEntityId)) return nullptr;
2025-10-11 18:51:33 +08:00
PowerTerminal* term = new PowerTerminal(parentEntityId, type, name, relPos, uuid, dPerX, dPerY);
m_baseAllTerminals[term->id()] = term;
m_baseTerminalsByEntity[parentEntityId].append(term);
2025-04-22 10:10:55 +08:00
// 关联到父实体
if (PowerEntity* parent = getEntity(parentEntityId,funType)) {
parent->addTerminal(term);
}
return term;
}
else if(funType == ModelFunctionType::EditorModel){
if (!m_editorEntities.contains(parentEntityId)) return nullptr;
2025-10-11 18:51:33 +08:00
PowerTerminal* term = new PowerTerminal(parentEntityId, type, name, relPos, uuid, dPerX, dPerY);
m_editorAllTerminals[term->id()] = term;
m_editorTerminalsByEntity[parentEntityId].append(term);
// 关联到父实体
if (PowerEntity* parent = getEntity(parentEntityId,funType)) {
parent->addTerminal(term);
}
return term;
}
else if(funType == ModelFunctionType::BlockEditorModel){
if (!m_blockEntities.contains(parentEntityId)) return nullptr;
2025-10-11 18:51:33 +08:00
PowerTerminal* term = new PowerTerminal(parentEntityId, type, name, relPos, uuid, dPerX, dPerY);
m_blockAllTerminals[term->id()] = term;
m_blockTerminalsByEntity[parentEntityId].append(term);
// 关联到父实体
if (PowerEntity* parent = getEntity(parentEntityId,funType)) {
parent->addTerminal(term);
}
return term;
}
return nullptr;
2025-04-22 10:10:55 +08:00
}
bool TopologyManager::deleteTerminal(const QString& terminalId,ModelFunctionType funType) {
if(funType == ModelFunctionType::ProjectModel){
if (!m_allTerminals.contains(terminalId)) return false;
PowerTerminal* term = m_allTerminals[terminalId];
QString parentId = term->parentEntityId();
// 从父实体移除
if (PowerEntity* parent = getEntity(parentId)) {
parent->removeTerminal(terminalId);
}
2025-04-22 10:10:55 +08:00
// 清理全局存储
m_terminalsByEntity[parentId].removeAll(term);
m_allTerminals.remove(terminalId);
2025-04-22 10:10:55 +08:00
return true;
2025-04-22 10:10:55 +08:00
}
else if(funType == ModelFunctionType::BaseModel){
if (!m_baseAllTerminals.contains(terminalId)) return false;
PowerTerminal* term = m_baseAllTerminals[terminalId];
QString parentId = term->parentEntityId();
2025-04-22 10:10:55 +08:00
// 从父实体移除
if (PowerEntity* parent = getEntity(parentId,funType)) {
parent->removeTerminal(terminalId);
}
// 清理全局存储
m_baseTerminalsByEntity[parentId].removeAll(term);
m_baseAllTerminals.remove(terminalId);
return true;
}
else if(funType == ModelFunctionType::EditorModel){
if (!m_editorAllTerminals.contains(terminalId)) return false;
PowerTerminal* term = m_editorAllTerminals[terminalId];
QString parentId = term->parentEntityId();
// 从父实体移除
if (PowerEntity* parent = getEntity(parentId,funType)) {
parent->removeTerminal(terminalId);
}
// 清理全局存储
m_editorTerminalsByEntity[parentId].removeAll(term);
m_editorAllTerminals.remove(terminalId);
return true;
}
else if(funType == ModelFunctionType::BlockEditorModel){
if (!m_blockAllTerminals.contains(terminalId)) return false;
PowerTerminal* term = m_blockAllTerminals[terminalId];
QString parentId = term->parentEntityId();
// 从父实体移除
if (PowerEntity* parent = getEntity(parentId,funType)) {
parent->removeTerminal(terminalId);
}
// 清理全局存储
m_blockTerminalsByEntity[parentId].removeAll(term);
m_blockAllTerminals.remove(terminalId);
return true;
}
return false;
2025-04-22 10:10:55 +08:00
}
PowerTerminal* TopologyManager::getTerminal(const QString& terminalId,ModelFunctionType funType) const
2025-04-30 16:29:17 +08:00
{
if(funType == ModelFunctionType::ProjectModel){
auto it = m_allTerminals.find(terminalId);
return (it != m_allTerminals.end()) ? it.value() : nullptr;
}
else if(funType == ModelFunctionType::BaseModel){
auto it = m_baseAllTerminals.find(terminalId);
return (it != m_baseAllTerminals.end()) ? it.value() : nullptr;
}
else if(funType == ModelFunctionType::EditorModel){
auto it = m_editorAllTerminals.find(terminalId);
return (it != m_editorAllTerminals.end()) ? it.value() : nullptr;
}
else if(funType == ModelFunctionType::BlockEditorModel){
auto it = m_blockAllTerminals.find(terminalId);
return (it != m_blockAllTerminals.end()) ? it.value() : nullptr;
}
return nullptr;
2025-04-30 16:29:17 +08:00
}
QList<PowerTerminal*> TopologyManager::getTerminalsForEntity(const QString& entityId,ModelFunctionType funType) const {
if(funType == ModelFunctionType::ProjectModel)
return m_terminalsByEntity.value(entityId);
else if(funType == ModelFunctionType::BaseModel)
return m_baseTerminalsByEntity.value(entityId);
else if(funType == ModelFunctionType::EditorModel)
return m_editorTerminalsByEntity.value(entityId);
else if(funType == ModelFunctionType::BlockEditorModel)
return m_blockTerminalsByEntity.value(entityId);
return QList<PowerTerminal*>();
2025-04-22 10:10:55 +08:00
}
2025-04-30 16:29:17 +08:00
PowerEntity* TopologyManager::getEntityByTerminal(const QString& terminalId,ModelFunctionType funType) const
2025-04-30 16:29:17 +08:00
{
if(funType == ModelFunctionType::ProjectModel){
QHash<QString, QList<PowerTerminal*>>::ConstIterator iter;
for(iter = m_terminalsByEntity.begin();iter != m_terminalsByEntity.end();++iter)
{
for(auto &terminal:iter.value())
{
if(terminal->id() == terminalId)
{
return m_entities[iter.key()];
}
}
}
}
else if(funType == ModelFunctionType::BaseModel){
QHash<QString, QList<PowerTerminal*>>::ConstIterator iter;
for(iter = m_baseTerminalsByEntity.begin();iter != m_baseTerminalsByEntity.end();++iter)
2025-04-30 16:29:17 +08:00
{
for(auto &terminal:iter.value())
2025-04-30 16:29:17 +08:00
{
if(terminal->id() == terminalId)
{
return m_baseEntities[iter.key()];
}
2025-04-30 16:29:17 +08:00
}
}
}
else if(funType == ModelFunctionType::EditorModel){
QHash<QString, QList<PowerTerminal*>>::ConstIterator iter;
for(iter = m_editorTerminalsByEntity.begin();iter != m_editorTerminalsByEntity.end();++iter)
{
for(auto &terminal:iter.value())
{
if(terminal->id() == terminalId)
{
return m_editorEntities[iter.key()];
}
}
}
}
else if(funType == ModelFunctionType::BlockEditorModel){
QHash<QString, QList<PowerTerminal*>>::ConstIterator iter;
for(iter = m_blockTerminalsByEntity.begin();iter != m_blockTerminalsByEntity.end();++iter)
{
for(auto &terminal:iter.value())
{
if(terminal->id() == terminalId)
{
return m_blockEntities[iter.key()];
}
}
}
}
2025-04-30 16:29:17 +08:00
return nullptr;
}
PowerConnection* TopologyManager::getConnectionContainsTerminal(const QString& terminalId,ModelFunctionType funType) const
2025-04-30 16:29:17 +08:00
{
if(funType == ModelFunctionType::ProjectModel){
for(auto &con:m_connections)
2025-04-30 16:29:17 +08:00
{
if(con->fromTerminalId() == terminalId || con->toTerminalId() == terminalId)
{
return con;
}
}
}
else if(funType == ModelFunctionType::BaseModel){
for(auto &con:m_baseConnections)
{
if(con->fromTerminalId() == terminalId || con->toTerminalId() == terminalId)
{
return con;
}
2025-04-30 16:29:17 +08:00
}
}
else if(funType == ModelFunctionType::EditorModel){
for(auto &con:m_editorConnections)
{
if(con->fromTerminalId() == terminalId || con->toTerminalId() == terminalId)
{
return con;
}
}
}
else if(funType == ModelFunctionType::BlockEditorModel){
for(auto &con:m_blockConnections)
{
if(con->fromTerminalId() == terminalId || con->toTerminalId() == terminalId)
{
return con;
}
}
}
2025-04-30 16:29:17 +08:00
return nullptr;
}