DiagramDesigner/source/monitorItemsDlg.cpp

238 lines
8.0 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "monitorItemsDlg.h"
#include "ui_monitorItemsDlg.h"
#include "dataBase.h"
#include "tools.h"
#include <QToolTip>
MonitorItemsDlg::MonitorItemsDlg(QWidget *parent)
: QDialog(parent)
, ui(new Ui::monitorItemsDlg)
,_modelAll(nullptr)
,_modelSelect(nullptr)
{
ui->setupUi(this);
initial();
}
MonitorItemsDlg::~MonitorItemsDlg()
{
delete ui;
}
void MonitorItemsDlg::initial()
{
_modelAll = new QStandardItemModel(this);
ui->treeView_all->setModel(_modelAll);
ui->treeView_all->setHeaderHidden(true);
_modelSelect = new QStandardItemModel(this);
ui->treeView_select->setModel(_modelSelect);
ui->treeView_select->setHeaderHidden(true);
ui->le_name->setPlaceholderText("输入名称");
connect(ui->btn_generate,&QPushButton::clicked,this,&MonitorItemsDlg::onGenerateClicked);
connect(_modelAll, &QStandardItemModel::itemChanged,this, &MonitorItemsDlg::onItemChanged);
}
void MonitorItemsDlg::onUpdateItems(QList<monitorRelationItem> lst,bool refresh)
{
if(refresh){
QStandardItem *root = _modelAll->invisibleRootItem();
int rowCount = root->rowCount();
if (rowCount > 0) {
_modelAll->removeRows(0, rowCount);
}
}
for(auto &info:lst){
auto curItem = info.item;
if(curItem.nCategory == 0){
if(curItem.nEquipType == 1 || curItem.nEquipType == 15 || curItem.nEquipType == 16){ //母线与变压器等间隔外设备并列第一层
QStandardItem *pItem = new QStandardItem(curItem.sName);
pItem->setCheckable(true); // 启用勾选框
pItem->setCheckState(Qt::Unchecked);
pItem->setData(curItem.nCategory,Qt::UserRole+1);
pItem->setData(curItem.nEquipType,Qt::UserRole+2);
pItem->setData(curItem.uid,Qt::UserRole+3);
for(auto& subInfo:info.subList){ //母线挂接间隔,变压器挂接设备
QStandardItem *pSub = new QStandardItem(subInfo.sName);
pSub->setCheckable(true); // 启用勾选框
pSub->setCheckState(Qt::Unchecked);
pSub->setData(subInfo.nCategory,Qt::UserRole+1);
pSub->setData(subInfo.nEquipType,Qt::UserRole+2);
pSub->setData(subInfo.uid,Qt::UserRole+3);
pItem->appendRow(pSub);
}
_modelAll->appendRow(pItem);
}
else{ //其他设备挂接在母线下的间隔中
if(!info.parent.sName.isEmpty()){ //有父,在间隔内
QStandardItem *pItem = new QStandardItem(curItem.sName);
pItem->setCheckable(true); // 启用勾选框
pItem->setCheckState(Qt::Unchecked);
pItem->setData(curItem.nCategory,Qt::UserRole+1);
pItem->setData(curItem.nEquipType,Qt::UserRole+2);
pItem->setData(curItem.uid,Qt::UserRole+3);
auto pParent = findStandardItemAtLevel(_modelAll,1,info.parent.sName,nullptr,0); //查找父间隔所在item
if(pParent){
pParent->appendRow(pItem);
}
}
}
}
}
ui->treeView_all->expandAll();
}
void MonitorItemsDlg::onSelectItems(QList<monitorRelationItem> lst)
{
ui->stackedWidget->setCurrentIndex(0);
resetSelect();
for(auto& info:lst){
QModelIndex itemIndex = findIndex(_modelAll,info.item.uid,Qt::UserRole+3);
if(itemIndex.isValid())
{
QStandardItem *pItem = _modelAll->itemFromIndex(itemIndex);
pItem->setCheckState(Qt::Checked);
}
}
}
void MonitorItemsDlg::onGenerateClicked()
{
QString sName = ui->le_name->text();
if (sName.isEmpty()) {
ui->le_name->setFocus();
return;
}
QList<monitorRelationItem> lst;
QList<QStandardItem*> lstItem = getTreeViewCheckedItems(ui->treeView_all);
for(auto& pItem:lstItem){
monitorRelationItem info;
auto pParent = pItem->parent();
if(pParent){
info.parent.nCategory = pParent->data(Qt::UserRole+1).toInt();
info.parent.nEquipType = pParent->data(Qt::UserRole+2).toInt();
info.parent.uid = pParent->data(Qt::UserRole+3).toUuid();
info.parent.sName = pParent->text();
}
info.item.nCategory = pItem->data(Qt::UserRole+1).toInt();
info.item.nEquipType = pItem->data(Qt::UserRole+2).toInt();
info.item.uid = pItem->data(Qt::UserRole+3).toUuid();
info.item.sName = pItem->text();
auto lstChild = getAllChildren(pItem);
for(auto &child:lstChild){
monitorRelationSturctItem stru;
stru.nCategory = child->data(Qt::UserRole+1).toInt();
stru.nEquipType = child->data(Qt::UserRole+2).toInt();
stru.uid = child->data(Qt::UserRole+3).toUuid();
stru.sName = child->text();
info.subList.append(stru);
}
lst.append(info);
}
emit generateMonitor(sName,lst);
}
void MonitorItemsDlg::onMonitorCreated(QList<monitorRelationItem> lst)
{
/*ui->stackedWidget->setCurrentIndex(1);
ui->listWidget_select->clear();
for(auto& pair:lst){
QListWidgetItem *item = new QListWidgetItem(pair.first);
item->setData(Qt::UserRole,pair.second);
ui->listWidget_select->addItem(item);
}*/
}
void MonitorItemsDlg::onItemChanged(QStandardItem *item)
{
QSignalBlocker blocker(_modelAll);
if (item->isCheckable()) {
Qt::CheckState newState = item->checkState();
// 设置所有子项与父项相同的状态
setChildrenCheckState(item, newState);
}
ui->treeView_all->repaint();
}
void MonitorItemsDlg::setChildrenCheckState(QStandardItem *parent, Qt::CheckState state) {
for (int row = 0; row < parent->rowCount(); ++row) {
QStandardItem *child = parent->child(row, 0); // 第一列
if (child && child->isCheckable()) {
child->setCheckState(state);
setChildrenCheckState(child, state);
}
}
}
void MonitorItemsDlg::traverseSelectStandardItemModel(QStandardItemModel *model,Qt::CheckState check) {
if (!model) return;
// 遍历所有顶层项
for (int row = 0; row < model->rowCount(); ++row) {
for (int col = 0; col < model->columnCount(); ++col) {
QStandardItem *item = model->item(row, col);
item->setCheckState(check);
traverseSelectStandardItem(item, 0,check);
}
}
}
void MonitorItemsDlg::traverseSelectStandardItem(QStandardItem *item, int depth,Qt::CheckState check) {
if (!item) return;
// 遍历子项
for (int row = 0; row < item->rowCount(); ++row) {
for (int col = 0; col < item->columnCount(); ++col) {
QStandardItem *child = item->child(row, col);
child->setCheckState(check);
traverseSelectStandardItem(child, depth + 1,check);
}
}
}
QList<QStandardItem*> MonitorItemsDlg::getCheckedItems(QStandardItem* parentItem) {
QList<QStandardItem*> checkedItems;
if (!parentItem) return checkedItems;
for (int i = 0; i < parentItem->rowCount(); ++i) {
QStandardItem* childItem = parentItem->child(i);
if (childItem->checkState() == Qt::Checked) {
checkedItems.append(childItem);
}
// 递归检查子项
checkedItems.append(getCheckedItems(childItem));
}
return checkedItems;
}
// 主函数获取treeView中所有checked项
QList<QStandardItem*> MonitorItemsDlg::getTreeViewCheckedItems(QTreeView* treeView) {
QList<QStandardItem*> checkedItems;
QStandardItemModel* model = qobject_cast<QStandardItemModel*>(treeView->model());
if (!model) return checkedItems;
QStandardItem* rootItem = model->invisibleRootItem();
return getCheckedItems(rootItem);
}
void MonitorItemsDlg::resetSelect()
{
traverseSelectStandardItemModel(_modelAll,Qt::Unchecked);
}