DiagramDesigner/source/topologyView.cpp

325 lines
11 KiB
C++
Raw Normal View History

2025-04-30 16:29:17 +08:00
#include "topologyView.h"
#include "ui_topologyView.h"
#include "tools.h"
2025-05-09 19:36:32 +08:00
#include "topologyTree.h"
2025-05-16 19:20:46 +08:00
#include "dataBase.h"
2025-05-23 10:30:52 +08:00
#include "basePropertyManager.h"
#include "baseProperty.h"
2025-04-30 16:29:17 +08:00
#include <QStandardItemModel>
#include <QMenu>
#include <QUuid>
TopologyView::TopologyView(QWidget *parent)
: QDialog(parent)
, ui(new Ui::topologyView)
,_pModel(nullptr)
2025-05-09 19:36:32 +08:00
,_treeView(nullptr)
2025-04-30 16:29:17 +08:00
{
ui->setupUi(this);
//setWindowFlags(windowFlags() | Qt::WindowMinMaxButtonsHint&Qt::WindowCloseButtonHint);
_pModel = new QStandardItemModel(this);
2025-05-09 19:36:32 +08:00
_treeView = new TopologyTree(this);
ui->verticalLayout->addWidget(_treeView);
_treeView->setContextMenuPolicy(Qt::CustomContextMenu);
2025-04-30 16:29:17 +08:00
}
TopologyView::~TopologyView()
{
delete ui;
}
void TopologyView::initial()
{
2025-05-09 19:36:32 +08:00
connect(_treeView, &QTreeView::customContextMenuRequested, this, &TopologyView::onIndexRbtnClicked);
connect(_treeView, &QTreeView::clicked, this, &TopologyView::onItemClicked);
2025-04-30 16:29:17 +08:00
connect(_pModel, &QStandardItemModel::itemChanged, this, &TopologyView::onItemChanged);
2025-05-23 10:30:52 +08:00
connect(&BasePropertyManager::instance(),&BasePropertyManager::dataChanged,this,&TopologyView::onDataChanged);
2025-05-09 19:36:32 +08:00
_treeView->setHeaderHidden(true);
2025-04-30 16:29:17 +08:00
// 设置模型的列数
_pModel->setColumnCount(1);
// 设置模型的标题
//_pModel->setHeaderData(0, Qt::Horizontal, QObject::tr("层级"));
2025-05-23 10:30:52 +08:00
QStandardItem *rootItem = _pModel->invisibleRootItem();
2025-05-16 19:20:46 +08:00
QList<componentInfo> lst= DataBase::GetInstance()->getAllComponents();
for(auto &info:lst)
{
QString nG= info.grid;
QString nZ = info.zone;
QString nS= info.station;
2025-07-22 20:00:46 +08:00
//QString sGrid = DataBase::GetInstance()->getGridNameById(nG.toInt());
//QString sZone = DataBase::GetInstance()->getZoneNameById(nZ.toInt());
//QString sStation = DataBase::GetInstance()->getStationNameById(nS.toInt());
QString sGrid = nG; //暂时不使用id查找名称
QString sZone = nZ;
QString sStation = nS;
2025-05-16 19:20:46 +08:00
2025-10-30 17:20:10 +08:00
if(info.tag.isEmpty())
continue;
2025-05-16 19:20:46 +08:00
QStandardItem *pItem = new QStandardItem(info.tag);
2025-05-23 10:30:52 +08:00
pItem->setData(info.uuid.toString(),Qt::UserRole);
addItemToView(sGrid,sZone,sStation,rootItem,pItem);
2025-05-16 19:20:46 +08:00
}
2025-04-30 16:29:17 +08:00
// 创建树视图
2025-05-09 19:36:32 +08:00
_treeView->setModel(_pModel);
2025-04-30 16:29:17 +08:00
// 展开所有节点
2025-05-09 19:36:32 +08:00
_treeView->expandAll();
2025-04-30 16:29:17 +08:00
// 显示树视图
2025-05-09 19:36:32 +08:00
_treeView->setWindowTitle(QObject::tr("拓扑树"));
}
void TopologyView::loadTopologyFromDB()
{
2025-04-30 16:29:17 +08:00
}
void TopologyView::onIndexRbtnClicked(const QPoint &pos)
{
// 获取当前点击的位置对应的索引
2025-05-09 19:36:32 +08:00
QModelIndex index = _treeView->indexAt(pos);
2025-04-30 16:29:17 +08:00
if (!index.isValid()) {
return; // 如果点击的是空白区域,直接返回
}
QMenu menu;
QStandardItem* item = _pModel->itemFromIndex(index);
if(item)
{
int nCount = item->rowCount();
int nLevel = getLevel(item);
if(nLevel == -1) //根节点
{
QAction *addAction = new QAction("添加电网", this);
connect(addAction,&QAction::triggered,this,[&](){
QString uuid = QUuid::createUuid().toString();
QStandardItem *gridItem = new QStandardItem(QString("电网_")+QString::number(nCount));
gridItem->setData(uuid,Qt::UserRole+1);
gridItem->setData(int(EntityType::Grid),Qt::UserRole+2);
item->appendRow(gridItem);
EntityInfo info;
info.eType = EntityType::Grid;
info.sName = gridItem->text();
info.sUuid = uuid;
emit entityCreate(info);
});
menu.addAction(addAction);
}
else if(nLevel == 1) //grid
{
QAction *addAction = new QAction("添加区域", this);
QString sParentid = item->data(Qt::UserRole+1).toString();
connect(addAction,&QAction::triggered,this,[&](){
QString uuid = QUuid::createUuid().toString();
QStandardItem *zoneItem = new QStandardItem(QString("区域_")+QString::number(nCount));
zoneItem->setData(uuid,Qt::UserRole+1);
zoneItem->setData(int(EntityType::Zone),Qt::UserRole+2);
item->appendRow(zoneItem);
EntityInfo info;
info.eType = EntityType::Zone;
info.sName = zoneItem->text();
info.sParentId = sParentid;
info.sUuid = uuid;
emit entityCreate(info);
2025-05-09 19:36:32 +08:00
_treeView->expandAll();
2025-04-30 16:29:17 +08:00
});
menu.addAction(addAction);
}
else if(nLevel == 2) //zone
{
QAction *addAction = new QAction("添加场站", this);
QString sParentid = item->data(Qt::UserRole+1).toString();
connect(addAction,&QAction::triggered,this,[&](){
QString uuid = QUuid::createUuid().toString();
QStandardItem *stationItem = new QStandardItem(QString("场站_")+QString::number(nCount));
stationItem->setData(uuid,Qt::UserRole+1);
stationItem->setData(int(EntityType::Station),Qt::UserRole+2);
item->appendRow(stationItem);
EntityInfo info;
info.eType = EntityType::Station;
info.sName = stationItem->text();
info.sUuid = uuid;
info.sParentId = sParentid;
emit entityCreate(info);
2025-05-09 19:36:32 +08:00
_treeView->expandAll();
2025-04-30 16:29:17 +08:00
});
menu.addAction(addAction);
}
if(nLevel != -1) //除了根节点其余都能删除
{
QAction *delAction = new QAction("删除", this);
connect(delAction,&QAction::triggered,this,[&](){
2025-05-09 19:36:32 +08:00
QModelIndex currentIndex = _treeView->currentIndex();
2025-04-30 16:29:17 +08:00
if(currentIndex.isValid())
{
QStandardItem* pItem = _pModel->itemFromIndex(currentIndex);
if(pItem)
{
QString str = item->text();
EntityInfo info;
info.sName = str;
info.sUuid = item->data(Qt::UserRole+1).toString();
info.eType = EntityType(item->data(Qt::UserRole+2).toInt());
emit entityDelete(info);
QStandardItem* pParent = item->parent();
if(pParent)
{
pParent->removeRow(currentIndex.row());
}
}
}
});
menu.addAction(delAction);
}
}
// 在点击位置显示菜单
2025-05-09 19:36:32 +08:00
menu.exec(_treeView->mapToGlobal(pos));
2025-04-30 16:29:17 +08:00
}
void TopologyView::onItemChanged(QStandardItem *item)
{
int nLevel = getLevel(item);
QString str = item->text();
EntityInfo info;
info.sName = str;
info.sUuid = item->data(Qt::UserRole+1).toString();
info.eType = EntityType(item->data(Qt::UserRole+2).toInt());
emit entityChange(info);
}
void TopologyView::onItemClicked(const QModelIndex &index)
{
QStandardItem* item = _pModel->itemFromIndex(index);
if(item)
{
EntityType type = EntityType(item->data(Qt::UserRole+2).toInt());
if(type == EntityType::ConfigurationDiagram)
{
EntityInfo info;
info.eType = EntityType::ConfigurationDiagram;
info.sName = item->text();
info.sUuid = item->data(Qt::UserRole+1).toString();
info.sParentId = item->parent()->data(Qt::UserRole+1).toString();
emit entitySelected(info);
}
}
}
2025-05-23 10:30:52 +08:00
void TopologyView::onDataCreated(QString uuid)
{
}
void TopologyView::onDataChanged(QString uuid)
{
QModelIndex itemIndex = findIndex(_pModel,uuid,Qt::UserRole);
BaseProperty* pData = BasePropertyManager::instance().findEntityData(QUuid(uuid));
if(pData)
{
if(itemIndex.isValid()) //已存在
{
QStandardItem *pItem = _pModel->itemFromIndex(itemIndex);
if(pItem)
{
if(pItem->text() != pData->tag()) //已改名
{
pItem->setText(pData->tag());
}
QString sStation = pItem->parent()->text();
QString sZone = pItem->parent()->parent()->text();
QString sGrid = pItem->parent()->parent()->parent()->text();
if(sStation == pData->station() && sZone == pData->zone() && sGrid == pData->grid()) //从属关系未变
{
}
else
{
QStandardItem* item = _pModel->takeItem(pItem->row()); //从原层级中取出,添加到新的层级
addItemToView(pData->grid(),pData->zone(),pData->station(),_pModel->invisibleRootItem(),item);
}
}
}
else
{
QStandardItem *pItem = new QStandardItem(pData->tag());
pItem->setData(uuid,Qt::UserRole);
addItemToView(pData->grid(),pData->zone(),pData->station(),_pModel->invisibleRootItem(),pItem);
}
}
}
void TopologyView::addItemToView(QString sGrid,QString sZone,QString sStation,QStandardItem *root,QStandardItem *pItem)
{
2025-10-30 17:20:10 +08:00
QModelIndex iG;
QModelIndex iZ;
QModelIndex iS;
iG = findIndex(_pModel, sGrid, Qt::DisplayRole, QModelIndex());
if (iG.isValid()) {
iZ = findIndex(_pModel, sZone, Qt::DisplayRole, iG); // 只在Grid下查找
if (iZ.isValid()) {
iS = findIndex(_pModel, sStation, Qt::DisplayRole, iZ); // 只在Zone下查找
}
}
2025-05-23 10:30:52 +08:00
if(iG.isValid()) //已创建
{
QStandardItem* itemGrid = _pModel->itemFromIndex(iG);
if(iZ.isValid())
{
QStandardItem* itemZone = _pModel->itemFromIndex(iZ);
if(iS.isValid())
{
QStandardItem* itemStation = _pModel->itemFromIndex(iS);
itemStation->appendRow(pItem);
}
else
{
QStandardItem* itemStation = new QStandardItem(sStation);
itemStation->appendRow(pItem);
itemZone->appendRow(itemStation);
}
}
else
{
QStandardItem* itemZone = new QStandardItem(sZone);
QStandardItem* itemStation = new QStandardItem(sStation);
itemStation->appendRow(pItem);
itemZone->appendRow(itemStation);
itemGrid->appendRow(itemZone);
}
}
else
{
QStandardItem* itemGrid = new QStandardItem(sGrid);
QStandardItem* itemZone = new QStandardItem(sZone);
QStandardItem* itemStation = new QStandardItem(sStation);
itemStation->appendRow(pItem);
itemZone->appendRow(itemStation);
itemGrid->appendRow(itemZone);
root->appendRow(itemGrid);
}
}