2025-05-09 19:36:32 +08:00
|
|
|
#include <QStandardItemModel>
|
|
|
|
|
#include <QMenu>
|
|
|
|
|
#include <QUuid>
|
2025-05-16 19:20:46 +08:00
|
|
|
#include "diagramView.h"
|
|
|
|
|
#include "ui_diagramView.h"
|
|
|
|
|
#include "tools.h"
|
|
|
|
|
#include "topologyManager.h"
|
2025-10-24 21:11:07 +08:00
|
|
|
#include "projectManager.h"
|
2025-05-16 19:20:46 +08:00
|
|
|
#include "dataBase.h"
|
2025-05-09 19:36:32 +08:00
|
|
|
|
|
|
|
|
DiagramView::DiagramView(QWidget *parent)
|
|
|
|
|
: QDialog(parent)
|
|
|
|
|
, ui(new Ui::diagramView)
|
|
|
|
|
,_pModel(nullptr)
|
|
|
|
|
{
|
|
|
|
|
ui->setupUi(this);
|
|
|
|
|
_pModel = new QStandardItemModel(this);
|
|
|
|
|
ui->treeView->setContextMenuPolicy(Qt::CustomContextMenu);
|
2025-10-24 21:11:07 +08:00
|
|
|
ui->treeWidget->setContextMenuPolicy(Qt::CustomContextMenu);
|
2025-05-16 19:20:46 +08:00
|
|
|
_count = 0;
|
2025-05-09 19:36:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DiagramView::~DiagramView()
|
|
|
|
|
{
|
|
|
|
|
delete ui;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DiagramView::initial()
|
|
|
|
|
{
|
|
|
|
|
connect(ui->treeView, &QTreeView::customContextMenuRequested, this, &DiagramView::onIndexRbtnClicked);
|
2025-10-24 21:11:07 +08:00
|
|
|
connect(ui->treeWidget, &QTreeView::customContextMenuRequested, this, &DiagramView::onEditorRbtnClicked);
|
2025-05-09 19:36:32 +08:00
|
|
|
connect(ui->treeView, &QTreeView::clicked, this, &DiagramView::onItemClicked);
|
|
|
|
|
connect(_pModel, &QStandardItemModel::itemChanged, this, &DiagramView::onItemChanged);
|
2025-10-24 21:11:07 +08:00
|
|
|
connect(ui->btn_new, &QPushButton::clicked, this, &DiagramView::onNewEditorClicked);
|
|
|
|
|
connect(ui->btn_open, &QPushButton::clicked, this, &DiagramView::onOpenEditorClicked);
|
|
|
|
|
connect(ui->btn_save, &QPushButton::clicked, this, &DiagramView::onSaveEditorClicked);
|
|
|
|
|
connect(&ProjectManager::instance(),&ProjectManager::createNewEditor,this,&DiagramView::onNewEditorCreated);
|
|
|
|
|
connect(&ProjectManager::instance(),&ProjectManager::editorSaved,this,&DiagramView::onEditorSaved);
|
2025-05-09 19:36:32 +08:00
|
|
|
ui->treeView->setHeaderHidden(true);
|
2025-10-24 21:11:07 +08:00
|
|
|
ui->treeWidget->setHeaderHidden(true);
|
|
|
|
|
ui->treeWidget->setColumnCount(2);
|
2025-05-09 19:36:32 +08:00
|
|
|
// 设置模型的列数
|
|
|
|
|
_pModel->setColumnCount(1);
|
|
|
|
|
|
2025-07-18 18:26:13 +08:00
|
|
|
//QList<QStandardItem*> pageList;
|
2025-05-16 19:20:46 +08:00
|
|
|
QList<pageInfo> lst = DataBase::GetInstance()->getAllPage();
|
|
|
|
|
for(auto info:lst)
|
|
|
|
|
{
|
|
|
|
|
QStandardItem* pItem = new QStandardItem(info.name);
|
|
|
|
|
pItem->setData(info.id,Qt::UserRole);
|
2025-07-18 18:26:13 +08:00
|
|
|
//pageList.append(pItem);
|
|
|
|
|
_pModel->appendRow(pItem);
|
2025-05-16 19:20:46 +08:00
|
|
|
}
|
2025-07-18 18:26:13 +08:00
|
|
|
//_pModel->appendRow(pageList);
|
2025-05-16 19:20:46 +08:00
|
|
|
|
2025-05-09 19:36:32 +08:00
|
|
|
// 创建树视图
|
|
|
|
|
ui->treeView->setModel(_pModel);
|
|
|
|
|
|
|
|
|
|
// 展开所有节点
|
|
|
|
|
ui->treeView->expandAll();
|
|
|
|
|
// 显示树视图
|
|
|
|
|
ui->treeView->setWindowTitle(QObject::tr("组态图结构"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DiagramView::loadTopologyFromDB()
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DiagramView::onIndexRbtnClicked(const QPoint &pos)
|
|
|
|
|
{
|
|
|
|
|
// 获取当前点击的位置对应的索引
|
|
|
|
|
QMenu menu;
|
|
|
|
|
QModelIndex index = ui->treeView->indexAt(pos);
|
|
|
|
|
if (!index.isValid()) {
|
|
|
|
|
// 如果点击的是空白区域,创建新组态图菜单
|
2025-05-16 19:20:46 +08:00
|
|
|
|
2025-05-09 19:36:32 +08:00
|
|
|
QAction *addAction = new QAction("添加组态图", this);
|
|
|
|
|
|
|
|
|
|
connect(addAction,&QAction::triggered,this,[&](){
|
|
|
|
|
QVariant id = QUuid::createUuid();
|
2025-05-16 19:20:46 +08:00
|
|
|
|
|
|
|
|
QString sName = generateName();
|
|
|
|
|
QStandardItem *item = new QStandardItem(sName);
|
|
|
|
|
item->setData(id,Qt::UserRole+1);
|
|
|
|
|
_pModel->appendRow(item);
|
2025-05-09 19:36:32 +08:00
|
|
|
|
|
|
|
|
DiagramInfo info;
|
|
|
|
|
//todo:具体信息
|
|
|
|
|
info.id = id; //新建的组态图随机赋予临时id
|
|
|
|
|
info.sName = sName;
|
|
|
|
|
info.sTag = sName;
|
|
|
|
|
emit diagramCreate(info);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
menu.addAction(addAction);
|
|
|
|
|
}
|
|
|
|
|
else{ //目标组态图下添加子组态图
|
|
|
|
|
QStandardItem* item = _pModel->itemFromIndex(index);
|
|
|
|
|
QVariant parentId = item->data(Qt::UserRole+1);
|
|
|
|
|
|
|
|
|
|
if(item)
|
|
|
|
|
{
|
|
|
|
|
QAction *addAction = new QAction("添加子组态图", this);
|
|
|
|
|
|
|
|
|
|
connect(addAction,&QAction::triggered,this,[&](){
|
|
|
|
|
QVariant id = QUuid::createUuid();
|
2025-05-16 19:20:46 +08:00
|
|
|
|
|
|
|
|
QString sName = generateName();
|
|
|
|
|
QStandardItem *item = new QStandardItem(sName);
|
|
|
|
|
item->setData(id,Qt::UserRole+1);
|
|
|
|
|
item->appendRow(item);
|
2025-05-09 19:36:32 +08:00
|
|
|
|
|
|
|
|
DiagramInfo info;
|
|
|
|
|
//todo:具体信息
|
|
|
|
|
info.id = id; //新建的随机赋予临时id
|
|
|
|
|
info.sName = sName;
|
|
|
|
|
info.sTag = sName;
|
|
|
|
|
info.parentId = parentId;
|
|
|
|
|
emit diagramCreate(info);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
menu.addAction(addAction);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
QAction *delAction = new QAction("删除", this);
|
|
|
|
|
connect(delAction,&QAction::triggered,this,[&](){
|
|
|
|
|
QModelIndex currentIndex = ui->treeView->currentIndex();
|
|
|
|
|
if(currentIndex.isValid())
|
|
|
|
|
{
|
|
|
|
|
QStandardItem* pItem = _pModel->itemFromIndex(currentIndex);
|
|
|
|
|
QVariant id = pItem->data(Qt::UserRole+1);
|
|
|
|
|
QString sName = pItem->text();
|
|
|
|
|
if(pItem)
|
|
|
|
|
{
|
|
|
|
|
QString str = item->text();
|
|
|
|
|
DiagramInfo info;
|
|
|
|
|
//todo:具体信息
|
|
|
|
|
info.id = id; //新建的随机赋予临时id
|
|
|
|
|
info.sName = sName;
|
|
|
|
|
info.sTag = sName;
|
|
|
|
|
info.parentId = parentId;
|
|
|
|
|
emit diagramDelete(info);
|
|
|
|
|
|
|
|
|
|
QStandardItem* pParent = item->parent();
|
|
|
|
|
if(pParent)
|
|
|
|
|
{
|
|
|
|
|
pParent->removeRow(currentIndex.row());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
menu.addAction(delAction);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 在点击位置显示菜单
|
|
|
|
|
menu.exec(ui->treeView->mapToGlobal(pos));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DiagramView::onItemChanged(QStandardItem *item)
|
|
|
|
|
{
|
|
|
|
|
int nLevel = getLevel(item);
|
2025-05-16 19:20:46 +08:00
|
|
|
QString str = item->text(); //名称可能修改
|
2025-05-09 19:36:32 +08:00
|
|
|
DiagramInfo info;
|
2025-05-16 19:20:46 +08:00
|
|
|
info.id = item->data(Qt::UserRole).toInt();
|
|
|
|
|
info.sName = str;
|
2025-05-09 19:36:32 +08:00
|
|
|
emit diagramChange(info);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DiagramView::onItemClicked(const QModelIndex &index)
|
|
|
|
|
{
|
|
|
|
|
QStandardItem* item = _pModel->itemFromIndex(index);
|
2025-05-16 19:20:46 +08:00
|
|
|
QStandardItem* parent = item->parent();
|
2025-05-09 19:36:32 +08:00
|
|
|
if(item)
|
|
|
|
|
{
|
|
|
|
|
DiagramInfo info;
|
2025-05-16 19:20:46 +08:00
|
|
|
info.id = item->data(Qt::UserRole).toInt();
|
|
|
|
|
info.sName = item->text();
|
|
|
|
|
if(parent)
|
|
|
|
|
info.parentId = parent->data(Qt::UserRole).toInt();
|
2025-05-09 19:36:32 +08:00
|
|
|
emit diagramSelected(info);
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-05-16 19:20:46 +08:00
|
|
|
|
2025-10-24 21:11:07 +08:00
|
|
|
void DiagramView::onEditorRbtnClicked(const QPoint &pos)
|
|
|
|
|
{
|
|
|
|
|
QTreeWidgetItem *item = ui->treeWidget->itemAt(pos);
|
|
|
|
|
if (!item) return;
|
|
|
|
|
|
|
|
|
|
int level = getItemLevel(item);
|
|
|
|
|
|
|
|
|
|
QMenu menu(this);
|
|
|
|
|
|
|
|
|
|
switch (level) {
|
|
|
|
|
case 1:{ // 顶级节点
|
|
|
|
|
QAction *actSaveTop = menu.addAction("保存");
|
|
|
|
|
QAction *actGenTop = menu.addAction("生成");
|
|
|
|
|
menu.addSeparator();
|
|
|
|
|
QAction *actDelTop = menu.addAction("删除");
|
|
|
|
|
|
|
|
|
|
QAction *act = menu.exec(ui->treeWidget->mapToGlobal(pos));
|
|
|
|
|
if(act == actSaveTop){
|
|
|
|
|
ProjectManager::instance().saveEditor(item->text(0));
|
|
|
|
|
}
|
|
|
|
|
else if(act == actGenTop){
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
else if(act == actDelTop){
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case 2:{ // 二级节点
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case 3:{ // 三级节点
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DiagramView::onNewEditorCreated(const QString& str,QUuid id)
|
|
|
|
|
{
|
|
|
|
|
ui->treeWidget->clear();
|
|
|
|
|
QTreeWidgetItem *topLevelItem = new QTreeWidgetItem(ui->treeWidget);
|
|
|
|
|
topLevelItem->setText(0, str);
|
|
|
|
|
topLevelItem->setData(0,Qt::UserRole,id);
|
|
|
|
|
topLevelItem->setText(1,"未保存");
|
|
|
|
|
topLevelItem->setForeground(1,Qt::yellow);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DiagramView::onEditorSaved(const QString& str,QUuid uid)
|
|
|
|
|
{
|
|
|
|
|
QTreeWidgetItemIterator it(ui->treeWidget);
|
|
|
|
|
while (*it) {
|
|
|
|
|
QTreeWidgetItem* item = *it;
|
|
|
|
|
if(item->data(0,Qt::UserRole).toUuid() == uid)
|
|
|
|
|
{
|
|
|
|
|
item->setText(1,"已保存");
|
|
|
|
|
item->setForeground(1,Qt::green);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
++it;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DiagramView::onNewEditorClicked()
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DiagramView::onOpenEditorClicked()
|
|
|
|
|
{
|
|
|
|
|
emit loadProject();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DiagramView::onSaveEditorClicked()
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-16 19:20:46 +08:00
|
|
|
QString DiagramView::generateName()
|
|
|
|
|
{
|
|
|
|
|
QString sName = QString::fromWCharArray(L"组态图_")+QString::number(_count);
|
|
|
|
|
QModelIndex Idx = findIndex(_pModel,sName);
|
|
|
|
|
if(Idx.isValid()) //已存在
|
|
|
|
|
{
|
|
|
|
|
_count += 1;
|
|
|
|
|
return generateName();
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
return sName;
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-10-24 21:11:07 +08:00
|
|
|
|
|
|
|
|
int DiagramView::getItemLevel(QTreeWidgetItem *item) {
|
|
|
|
|
int level = 0;
|
|
|
|
|
while (item->parent()) {
|
|
|
|
|
level++;
|
|
|
|
|
item = item->parent();
|
|
|
|
|
}
|
|
|
|
|
return level + 1; // 顶级节点为1级
|
|
|
|
|
}
|