#include #include #include "topologyManager.h" #include "powerEntity.h" #include "graphicsItem/graphicsBaseItem.h" 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(); m_allTerminals.clear(); //端点由父亲entity释放 } PowerEntity* TopologyManager::createEntity(EntityType type,const QString& uuid, const QString& name,ModelFunctionType funType) { 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); } m_entities.insert(entity->id(), entity); emit entityCreated(entity->id()); // 发送信号通知 return entity; } else if(funType == ModelFunctionType::BaseModel) { PowerEntity* entity = nullptr; entity = new PowerComponent(type, uuid,name); m_baseEntities.insert(entity->id(), entity); emit entityCreated(entity->id()); // 发送信号通知 return entity; } return nullptr; } PowerEntity* TopologyManager::findEntity(const QString& id,ModelFunctionType funType) const { if(funType == ModelFunctionType::ProjectModel) return m_entities.value(id, nullptr); // 避免异常的安全查询 else if(funType == ModelFunctionType::BaseModel) return m_baseEntities.value(id, nullptr); return nullptr; } bool TopologyManager::deleteEntity(const QString& id,ModelFunctionType funType) { if(funType == ModelFunctionType::ProjectModel) { if (!m_entities.contains(id)) return false; PowerEntity* entity = m_entities[id]; // 步骤1:删除所有相关连接 auto relatedConns = getConnectionsFor(id); for (auto conn : relatedConns) { removeConnection(conn->id()); } // 步骤2:从父节点移除(防止悬空指针) if (PowerEntity* parent = entity->parent()) { parent->removeChild(entity); } // 步骤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; } 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); // 递归删除 } // 步骤4:从哈希表移除并释放内存 m_baseEntities.remove(id); delete entity; // 触发PowerEntity析构函数 emit entityDeleted(id); return true; } return false; } PowerConnection* TopologyManager::createConnection(const QString& connId,const QString& fromTerId, const QString& toTerId,const QString& fromId,const QString& toId,ModelFunctionType funType) { PowerConnection* conn = nullptr; if(funType == ModelFunctionType::ProjectModel) { // 验证有效性 if (!m_allTerminals.contains(fromTerId) || !m_allTerminals.contains(toTerId) || fromTerId == toTerId) { qWarning() << "Invalid connection endpoints"; return nullptr; } // 防止重复连接 foreach (auto conn, m_connections) { if (conn->fromTerminalId() == fromTerId && conn->toTerminalId() == toTerId) { return conn; // 返回已存在的连接 } } // 创建新连接 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); } else if(funType == ModelFunctionType::BaseModel) { // 验证有效性 if (!m_baseAllTerminals.contains(fromTerId) || !m_baseAllTerminals.contains(toTerId) || fromTerId == toTerId) { qWarning() << "Invalid connection endpoints"; return nullptr; } // 防止重复连接 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; // 更新索引 m_baseConnectionIndex.insert(fromTerId, conn); m_baseConnectionIndex.insert(toTerId, conn); emit connectionCreated(connId); } return conn; } QList TopologyManager::getConnectionsForTerminal(const QString& terminalId,ModelFunctionType funType) const { if(funType == ModelFunctionType::ProjectModel) return m_connectionIndex.values(terminalId); else if(funType == ModelFunctionType::BaseModel) return m_baseConnectionIndex.values(terminalId); return QList(); } void TopologyManager::removeConnection(const QString& connId,ModelFunctionType funType) { 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); // 清理内存 m_connections.remove(connId); delete conn; 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); // 清理内存 m_baseConnections.remove(connId); delete conn; emit connectionRemoved(connId); } } bool TopologyManager::validateConnection(const QString& fromTermId, const QString& toTermId,ModelFunctionType funType) const { if(funType == ModelFunctionType::ProjectModel){ PowerTerminal* fromTerm = getTerminal(fromTermId); PowerTerminal* toTerm = getTerminal(toTermId); 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()); } else if(funType == ModelFunctionType::BaseModel){ PowerTerminal* fromTerm = getTerminal(fromTermId,funType); PowerTerminal* 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()); } return false; } // 连接查询接口 /*QList TopologyManager::connectionsFrom(const QString& elementId) const { QList res; QList lst = m_connectionIndex.values(elementId); for(auto &val:lst) { if(val->fromTerminalId() == elementId) res.append(val); } return res; }*/ /*QList TopologyManager::connectionsTo(const QString& elementId) const { QList res; QList lst = m_connectionIndex.values(elementId); for(auto &val:lst) { if(val->toTerminalId() == elementId) res.append(val); } return res; }*/ QList TopologyManager::getConnectionsFor(const QString& entityId,ModelFunctionType funType) const { QList lst; if(funType == ModelFunctionType::ProjectModel) { QList lstTerminal = getTerminalsForEntity(entityId); for(auto &terminal:lstTerminal) { PowerConnection* con = getConnectionContainsTerminal(terminal->id()); if(con) lst.append(con); } } else if(funType == ModelFunctionType::BaseModel) { QList lstTerminal = getTerminalsForEntity(entityId,funType); for(auto &terminal:lstTerminal) { PowerConnection* con = getConnectionContainsTerminal(terminal->id(),funType); if(con) lst.append(con); } } return lst; } PowerConnection* TopologyManager::connection(const QString& conId,ModelFunctionType funType) const { if(funType == ModelFunctionType::ProjectModel) return m_connections.value(conId,nullptr); else if(funType == ModelFunctionType::BaseModel) return m_baseConnections.value(conId,nullptr); return nullptr; } PowerConnection* TopologyManager::connection(const QString& fromPin,const QString& toPin,ModelFunctionType funType) { if(funType == ModelFunctionType::ProjectModel){ for(auto &con:m_connections) { if(con->fromTerminalId() == fromPin && con->toTerminalId() == toPin) return con; } } else if(funType == ModelFunctionType::BaseModel){ for(auto &con:m_baseConnections) { if(con->fromTerminalId() == fromPin && con->toTerminalId() == toPin) return con; } } return nullptr; } QHash TopologyManager::getAllConnections(ModelFunctionType funType) { if(funType == ModelFunctionType::ProjectModel) return m_connections; else if(funType == ModelFunctionType::BaseModel) return m_baseConnections; return QHash(); } PowerEntity* TopologyManager::getEntity(const QString& id,ModelFunctionType funType) const { if(funType == ModelFunctionType::ProjectModel){ auto it = m_entities.find(id); return (it != m_entities.end()) ? it.value() : nullptr; } else if(funType == ModelFunctionType::BaseModel){ auto it = m_baseEntities.find(id); return (it != m_baseEntities.end()) ? it.value() : nullptr; } return nullptr; } QList TopologyManager::findEntitiesByName(const QString& name,ModelFunctionType funType) const { QList 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); } } } return results; } PowerEntity* TopologyManager::createDiagram(const QString& id,const QString& name,ModelFunctionType funType) { 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); } } return entity; } PowerEntity* TopologyManager::findDiagram(const QString& id,ModelFunctionType funType) const { if(funType == ModelFunctionType::ProjectModel) return m_diagrams.value(id, nullptr); // 避免异常的安全查询 else if(funType == ModelFunctionType::BaseModel) return m_baseDiagrams.value(id, nullptr); return nullptr; } bool TopologyManager::deleteDiagram(const QString& id,ModelFunctionType funType) { if(funType == ModelFunctionType::ProjectModel){ if (!m_diagrams.contains(id)) return false; PowerEntity* entity = m_diagrams[id]; // 步骤2:从父节点移除(防止悬空指针) if (PowerEntity* parent = entity->parent()) { parent->removeChild(entity); } // 步骤3:递归删除子实体 auto children = entity->children(); for (auto child : children) { deleteDiagram(child->id()); // 递归删除 } // 步骤4:从哈希表移除并释放内存 m_diagrams.remove(id); delete entity; // 触发析构函数 return true; } 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); // 递归删除 } // 步骤4:从哈希表移除并释放内存 m_baseDiagrams.remove(id); delete entity; // 触发析构函数 return true; } return false; } PowerTerminal* TopologyManager::createTerminal(const QString& parentEntityId, TerminalType type, const QString& name, const QPointF& relPos, const QString& uuid, ModelFunctionType funType) { if(funType == ModelFunctionType::ProjectModel){ if (!m_entities.contains(parentEntityId)) return nullptr; PowerTerminal* term = new PowerTerminal(parentEntityId, type, name, relPos, uuid); m_allTerminals[term->id()] = term; m_terminalsByEntity[parentEntityId].append(term); // 关联到父实体 if (PowerEntity* parent = getEntity(parentEntityId)) { parent->addTerminal(term); } return term; } else if(funType == ModelFunctionType::BaseModel){ if (!m_baseEntities.contains(parentEntityId)) return nullptr; PowerTerminal* term = new PowerTerminal(parentEntityId, type, name, relPos, uuid); m_baseAllTerminals[term->id()] = term; m_baseTerminalsByEntity[parentEntityId].append(term); // 关联到父实体 if (PowerEntity* parent = getEntity(parentEntityId,funType)) { parent->addTerminal(term); } return term; } return nullptr; } 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); } // 清理全局存储 m_terminalsByEntity[parentId].removeAll(term); m_allTerminals.remove(terminalId); return true; } else if(funType == ModelFunctionType::BaseModel){ if (!m_baseAllTerminals.contains(terminalId)) return false; PowerTerminal* term = m_baseAllTerminals[terminalId]; QString parentId = term->parentEntityId(); // 从父实体移除 if (PowerEntity* parent = getEntity(parentId,funType)) { parent->removeTerminal(terminalId); } // 清理全局存储 m_baseTerminalsByEntity[parentId].removeAll(term); m_baseAllTerminals.remove(terminalId); return true; } return false; } PowerTerminal* TopologyManager::getTerminal(const QString& terminalId,ModelFunctionType funType) const { 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; } return nullptr; } QList 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); return QList(); } PowerEntity* TopologyManager::getEntityByTerminal(const QString& terminalId,ModelFunctionType funType) const { if(funType == ModelFunctionType::ProjectModel){ QHash>::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>::ConstIterator iter; for(iter = m_baseTerminalsByEntity.begin();iter != m_baseTerminalsByEntity.end();++iter) { for(auto &terminal:iter.value()) { if(terminal->id() == terminalId) { return m_baseEntities[iter.key()]; } } } } return nullptr; } PowerConnection* TopologyManager::getConnectionContainsTerminal(const QString& terminalId,ModelFunctionType funType) const { if(funType == ModelFunctionType::ProjectModel){ for(auto &con:m_connections) { 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; } } } return nullptr; }