189 lines
5.6 KiB
C++
189 lines
5.6 KiB
C++
#include <QStandardItemModel>
|
|
#include <QMenu>
|
|
#include <QUuid>
|
|
#include "diagramView.h"
|
|
#include "ui_diagramView.h"
|
|
#include "tools.h"
|
|
#include "topologyManager.h"
|
|
#include "dataBase.h"
|
|
|
|
DiagramView::DiagramView(QWidget *parent)
|
|
: QDialog(parent)
|
|
, ui(new Ui::diagramView)
|
|
,_pModel(nullptr)
|
|
{
|
|
ui->setupUi(this);
|
|
_pModel = new QStandardItemModel(this);
|
|
ui->treeView->setContextMenuPolicy(Qt::CustomContextMenu);
|
|
_count = 0;
|
|
}
|
|
|
|
DiagramView::~DiagramView()
|
|
{
|
|
delete ui;
|
|
}
|
|
|
|
void DiagramView::initial()
|
|
{
|
|
connect(ui->treeView, &QTreeView::customContextMenuRequested, this, &DiagramView::onIndexRbtnClicked);
|
|
connect(ui->treeView, &QTreeView::clicked, this, &DiagramView::onItemClicked);
|
|
connect(_pModel, &QStandardItemModel::itemChanged, this, &DiagramView::onItemChanged);
|
|
ui->treeView->setHeaderHidden(true);
|
|
// 设置模型的列数
|
|
_pModel->setColumnCount(1);
|
|
|
|
//QList<QStandardItem*> pageList;
|
|
QList<pageInfo> lst = DataBase::GetInstance()->getAllPage();
|
|
for(auto info:lst)
|
|
{
|
|
QStandardItem* pItem = new QStandardItem(info.name);
|
|
pItem->setData(info.id,Qt::UserRole);
|
|
//pageList.append(pItem);
|
|
_pModel->appendRow(pItem);
|
|
}
|
|
//_pModel->appendRow(pageList);
|
|
|
|
// 创建树视图
|
|
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()) {
|
|
// 如果点击的是空白区域,创建新组态图菜单
|
|
|
|
QAction *addAction = new QAction("添加组态图", this);
|
|
|
|
connect(addAction,&QAction::triggered,this,[&](){
|
|
QVariant id = QUuid::createUuid();
|
|
|
|
QString sName = generateName();
|
|
QStandardItem *item = new QStandardItem(sName);
|
|
item->setData(id,Qt::UserRole+1);
|
|
_pModel->appendRow(item);
|
|
|
|
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();
|
|
|
|
QString sName = generateName();
|
|
QStandardItem *item = new QStandardItem(sName);
|
|
item->setData(id,Qt::UserRole+1);
|
|
item->appendRow(item);
|
|
|
|
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);
|
|
QString str = item->text(); //名称可能修改
|
|
DiagramInfo info;
|
|
info.id = item->data(Qt::UserRole).toInt();
|
|
info.sName = str;
|
|
emit diagramChange(info);
|
|
}
|
|
|
|
void DiagramView::onItemClicked(const QModelIndex &index)
|
|
{
|
|
QStandardItem* item = _pModel->itemFromIndex(index);
|
|
QStandardItem* parent = item->parent();
|
|
if(item)
|
|
{
|
|
DiagramInfo info;
|
|
info.id = item->data(Qt::UserRole).toInt();
|
|
info.sName = item->text();
|
|
if(parent)
|
|
info.parentId = parent->data(Qt::UserRole).toInt();
|
|
emit diagramSelected(info);
|
|
}
|
|
}
|
|
|
|
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;
|
|
}
|
|
}
|