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-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-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("层级"));
|
|
|
|
|
|
|
|
|
|
// 创建根节点
|
|
|
|
|
QStandardItem *rootItem = new QStandardItem("电网");
|
|
|
|
|
rootItem->setFlags(rootItem->flags() & ~Qt::ItemIsEditable);
|
|
|
|
|
_pModel->appendRow(rootItem);
|
|
|
|
|
|
|
|
|
|
// 创建树视图
|
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);
|
|
|
|
|
}
|
|
|
|
|
else if(nLevel == 3) //station
|
|
|
|
|
{
|
2025-05-09 19:36:32 +08:00
|
|
|
/*QAction *addAction = new QAction("添加组态图", this);
|
2025-04-30 16:29:17 +08:00
|
|
|
QString sParentid = item->data(Qt::UserRole+1).toString();
|
|
|
|
|
|
|
|
|
|
connect(addAction,&QAction::triggered,this,[&](){
|
|
|
|
|
QString uuid = QUuid::createUuid().toString();
|
|
|
|
|
QStandardItem *diagramItem = new QStandardItem(QString("组态图_")+QString::number(nCount));
|
|
|
|
|
diagramItem->setData(uuid,Qt::UserRole+1);
|
|
|
|
|
diagramItem->setData(int(EntityType::ConfigurationDiagram),Qt::UserRole+2);
|
|
|
|
|
item->appendRow(diagramItem);
|
|
|
|
|
|
|
|
|
|
EntityInfo info;
|
|
|
|
|
info.eType = EntityType::ConfigurationDiagram;
|
|
|
|
|
info.sName = diagramItem->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
|
|
|
});
|
|
|
|
|
|
2025-05-09 19:36:32 +08:00
|
|
|
menu.addAction(addAction);*/
|
2025-04-30 16:29:17 +08:00
|
|
|
}
|
|
|
|
|
else if(nLevel == 4) //组态图
|
|
|
|
|
{
|
2025-05-09 19:36:32 +08:00
|
|
|
/*QAction *addAction = new QAction("添加子组态图", this);
|
2025-04-30 16:29:17 +08:00
|
|
|
QString sText = item->text();
|
|
|
|
|
QString sParentid = item->data(Qt::UserRole+1).toString();
|
|
|
|
|
connect(addAction,&QAction::triggered,this,[&](){
|
|
|
|
|
QString uuid = QUuid::createUuid().toString();
|
|
|
|
|
QStandardItem *diagramItem = new QStandardItem(sText+QString("_")+QString::number(nCount));
|
|
|
|
|
diagramItem->setData(uuid,Qt::UserRole+1);
|
|
|
|
|
diagramItem->setData(int(EntityType::ConfigurationDiagram),Qt::UserRole+2);
|
|
|
|
|
item->appendRow(diagramItem);
|
|
|
|
|
|
|
|
|
|
EntityInfo info;
|
|
|
|
|
info.eType = EntityType::ConfigurationDiagram;
|
|
|
|
|
info.sName = diagramItem->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
|
|
|
});
|
|
|
|
|
|
2025-05-09 19:36:32 +08:00
|
|
|
menu.addAction(addAction);*/
|
2025-04-30 16:29:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|