#include "customHMIList.h" #include #include #include "tools.h" CustomHMIList::CustomHMIList(QWidget *parent) : QWidget(parent) ,_modelAll(nullptr) ,_tree(nullptr) { initial(); } CustomHMIList::~CustomHMIList() { } void CustomHMIList::initial() { QVBoxLayout* layOut = new QVBoxLayout(this); _tree = new QTreeView(this); layOut->addWidget(_tree); layOut->setContentsMargins(0,0,0,0); _modelAll = new QStandardItemModel(this); _tree->setModel(_modelAll); _tree->setHeaderHidden(true); connect(_modelAll, &QStandardItemModel::itemChanged,this, &CustomHMIList::onItemChanged); } void CustomHMIList::onUpdateItems(QList lst,bool refresh) { if(refresh){ QStandardItem *root = _modelAll->invisibleRootItem(); int rowCount = root->rowCount(); if (rowCount > 0) { _modelAll->removeRows(0, rowCount); } // 清空间隔节点缓存 m_mapBayItems.clear(); } // 先处理间隔节点(nCategory == 1) for(auto &info:lst){ auto curItem = info.item; if(curItem.nCategory == 1){ // 间隔信息 // 查找是否已存在该间隔节点 QStandardItem* pBayItem = findBayItem(curItem.sName); if(!pBayItem){ // 创建间隔节点 pBayItem = new QStandardItem(curItem.sName); pBayItem->setCheckable(true); // 启用勾选框 pBayItem->setCheckState(Qt::Unchecked); pBayItem->setData(curItem.nCategory, Qt::UserRole+1); // 存储类别 pBayItem->setData(curItem.nEquipType, Qt::UserRole+2); // 存储设备类型 pBayItem->setData(curItem.uid, Qt::UserRole+3); // 存储UUID // 存储到缓存中 m_mapBayItems[curItem.sName] = pBayItem; _modelAll->appendRow(pBayItem); } } } // 再处理设备节点(nCategory == 0) for(auto &info:lst){ auto curItem = info.item; if(curItem.nCategory == 0){ // 设备信息 // 获取设备所属的间隔名称 QString bayName = info.parent.sName; if(!bayName.isEmpty()){ // 查找对应的间隔节点 QStandardItem* pBayItem = findBayItem(bayName); if(pBayItem){ // 创建设备节点 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); // 不再为母线或独立设备添加子设备 // 所有设备都作为间隔的直接子节点 pBayItem->appendRow(pItem); } } } } // 展开所有节点 _tree->expandAll(); } void CustomHMIList::onSelectItems(QList lst) { 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); } } generatePreview(); } void CustomHMIList::generatePreview() { QList lst; QList lstItem = getTreeViewCheckedItems(_tree); for(auto& pItem:lstItem){ HierarchyItem 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){ HierarchyStructItem 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 previewHMI(lst); } void CustomHMIList::onMonitorCreated(QList lst) { } void CustomHMIList::onItemChanged(QStandardItem *item) { QSignalBlocker blocker(_modelAll); if (item->isCheckable()) { Qt::CheckState newState = item->checkState(); // 设置所有子项与父项相同的状态 setChildrenCheckState(item, newState); } _tree->repaint(); } void CustomHMIList::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 CustomHMIList::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 CustomHMIList::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); } } } QStandardItem* CustomHMIList::findBayItem(const QString& bayName) { // 先从缓存查找 if(m_mapBayItems.contains(bayName)){ return m_mapBayItems[bayName]; } // 遍历查找 for(int i = 0; i < _modelAll->rowCount(); ++i){ QStandardItem* pItem = _modelAll->item(i); if(pItem->text() == bayName && pItem->data(Qt::UserRole+1).toInt() == 1){ // 类别为间隔 return pItem; } } return nullptr; } QList CustomHMIList::getCheckedItems(QStandardItem* parentItem) { QList 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 CustomHMIList::getTreeViewCheckedItems(QTreeView* treeView) { QList checkedItems; QStandardItemModel* model = qobject_cast(treeView->model()); if (!model) return checkedItems; QStandardItem* rootItem = model->invisibleRootItem(); return getCheckedItems(rootItem); } void CustomHMIList::resetSelect() { traverseSelectStandardItemModel(_modelAll,Qt::Unchecked); }