127 lines
3.7 KiB
C++
127 lines
3.7 KiB
C++
#include "monitorPagesDlg.h"
|
|
#include "ui_monitorPagesDlg.h"
|
|
#include <QMenu>
|
|
#include "dataBase.h"
|
|
#include "tools.h"
|
|
|
|
|
|
|
|
MonitorPagesDlg::MonitorPagesDlg(QWidget *parent)
|
|
: QDialog(parent)
|
|
, ui(new Ui::monitorPagesDlg)
|
|
,_pModel(nullptr)
|
|
{
|
|
ui->setupUi(this);
|
|
initial();
|
|
}
|
|
|
|
MonitorPagesDlg::~MonitorPagesDlg()
|
|
{
|
|
delete ui;
|
|
}
|
|
|
|
void MonitorPagesDlg::initial()
|
|
{
|
|
_pModel = new QStandardItemModel(this);
|
|
ui->treeView->setModel(_pModel);
|
|
ui->treeView->setHeaderHidden(true);
|
|
connect(ui->treeView, &QTreeView::clicked, this, &MonitorPagesDlg::onItemClicked);
|
|
}
|
|
|
|
void MonitorPagesDlg::onMonitorCreated(QString sProj,QPair<QString,QUuid> pair,int nMode)
|
|
{
|
|
QStandardItem* topLevelItem = findTopLevelItem(sProj);
|
|
if (topLevelItem) {
|
|
// 如果存在,直接在该项下插入子项
|
|
QStandardItem* childItem = new QStandardItem(pair.first);
|
|
childItem->setData(pair.second);
|
|
topLevelItem->appendRow(childItem);
|
|
} else {
|
|
// 如果不存在,创建新的顶层项并插入子项
|
|
QStandardItem* newTopLevelItem = new QStandardItem(sProj);
|
|
QStandardItem* childItem = new QStandardItem(pair.first);
|
|
childItem->setData(pair.second);
|
|
newTopLevelItem->appendRow(childItem);
|
|
_pModel->appendRow(newTopLevelItem);
|
|
}
|
|
ui->treeView->expandAll();
|
|
}
|
|
|
|
void MonitorPagesDlg::onItemClicked(const QModelIndex &index)
|
|
{
|
|
QStandardItem* item = _pModel->itemFromIndex(index);
|
|
int nLevel = getLevel(item);
|
|
if(nLevel == 0) //顶层不响应
|
|
return;
|
|
QStandardItem* parent = item->parent();
|
|
if(item)
|
|
{
|
|
DiagramInfo info;
|
|
info.id = item->data(Qt::UserRole+1).toUuid();
|
|
info.sName = item->text();
|
|
info.sBasePageName = parent->text();
|
|
emit monitorSelected(info);
|
|
}
|
|
}
|
|
|
|
void MonitorPagesDlg::onIndexRbtnClicked(const QPoint &pos)
|
|
{
|
|
// 获取当前点击的位置对应的索引
|
|
QMenu menu;
|
|
QModelIndex index = ui->treeView->indexAt(pos);
|
|
if (!index.isValid()) {
|
|
return;
|
|
}
|
|
else{ //目标组态图下添加子组态图
|
|
QStandardItem* item = _pModel->itemFromIndex(index);
|
|
if(item){
|
|
int nLevel = getLevel(item);
|
|
if(nLevel == 0){
|
|
QAction *allSaveAction = new QAction("全部保存", this);
|
|
|
|
connect(allSaveAction,&QAction::triggered,this,[&](){
|
|
|
|
QList<QPair<QString,QUuid>> lstInfo;
|
|
auto lstItem = getAllChildren(item);
|
|
for(auto& subItem:lstItem){
|
|
QString sName = subItem->text();
|
|
QUuid uid = subItem->data().toUuid();
|
|
lstInfo.append(qMakePair(sName,uid));
|
|
}
|
|
emit prepareSaveMonitor(lstInfo);
|
|
});
|
|
|
|
menu.addAction(allSaveAction);
|
|
}
|
|
else if(nLevel == 1){
|
|
QAction *saveAction = new QAction("保存", this);
|
|
|
|
connect(saveAction,&QAction::triggered,this,[&](){
|
|
QList<QPair<QString,QUuid>> lstInfo;
|
|
QString sName = item->text();
|
|
QUuid uid = item->data().toUuid();
|
|
lstInfo.append(qMakePair(sName,uid));
|
|
|
|
emit prepareSaveMonitor(lstInfo);
|
|
});
|
|
|
|
menu.addAction(saveAction);
|
|
}
|
|
}
|
|
}
|
|
|
|
// 在点击位置显示菜单
|
|
menu.exec(ui->treeView->mapToGlobal(pos));
|
|
}
|
|
|
|
QStandardItem* MonitorPagesDlg::findTopLevelItem(const QString& name)
|
|
{
|
|
for (int i = 0; i < _pModel->rowCount(); ++i) {
|
|
QStandardItem* item = _pModel->item(i);
|
|
if (item && item->text() == name) {
|
|
return item;
|
|
}
|
|
}
|
|
return nullptr;
|
|
}
|