#include "dataSourceDlg.h" #include "ui_dataSourceDlg.h" #include #include "structDataSource.h" #include "instance/extraPropertyManager.h" #include "global.h" DataSourceDlg::DataSourceDlg(QWidget *parent) : QDialog(parent) , ui(new Ui::dataSourceDlg) ,_treeModel(nullptr) ,m_currentCategoryItem(nullptr) ,_pExtraProManager(nullptr) { ui->setupUi(this); this->setWindowFlags(Qt::FramelessWindowHint | windowFlags()); initial(); } DataSourceDlg::~DataSourceDlg() { delete ui; } void DataSourceDlg::initial() { connect(ui->btn_ok,&QPushButton::clicked,this,&DataSourceDlg::onOkClicked); connect(ui->btn_cancel,&QPushButton::clicked,this,&DataSourceDlg::onCancelClicked); m_dataSource = new StructDataSource(this); _treeModel = new QStandardItemModel(this); _treeModel->setHorizontalHeaderLabels(QStringList() << "属性层级结构"); ui->treeView->setModel(_treeModel); } void DataSourceDlg::loadData() { if(_pExtraProManager) m_dataSource->loadExtrapro(_pExtraProManager->geAlltProperty()); } void DataSourceDlg::showDlg() { if(_pExtraProManager) { show(); clearItems(); auto& mapExtra = m_dataSource->allProperties; QStandardItem* root = _treeModel->invisibleRootItem(); for(auto& pro:mapExtra){ QStandardItem* propertyItem = new QStandardItem(); addItemToView(pro,"name",root,propertyItem); } ui->treeView->expandAll(); } } void DataSourceDlg::addItemToView(const ExtraProperty& property, const QString& displayMode, // "name" 或 "tag" QStandardItem* root, QStandardItem* pItem) { // 设置叶子节点的显示文本(使用name或tag) if (displayMode == "name") { pItem->setText(property.name.isEmpty() ? "未命名属性" : property.name); } else { pItem->setText(property.tag.isEmpty() ? "unknown_property" : property.tag); } // 在叶子节点存储完整的属性信息 QVariantMap propertyData; propertyData["property"] = QVariant::fromValue(property); propertyData["displayMode"] = displayMode; propertyData["code"] = property.code; pItem->setData(propertyData, Qt::UserRole + 1); QVector levels = { {(displayMode == "name") ? property.grid_name : property.grid_tag, property.grid_name, property.grid_tag, true, (displayMode == "name") ? "未命名电网" : "unknown_grid"}, {(displayMode == "name") ? property.zone_name : property.zone_tag, property.zone_name, property.zone_tag, true, (displayMode == "name") ? "未命名区域" : "unknown_zone"}, {(displayMode == "name") ? property.station_name : property.station_tag, property.station_name, property.station_tag, true, (displayMode == "name") ? "未命名站点" : "unknown_station"}, {property.currentLevel, property.currentLevel, property.currentLevel, false, (displayMode == "name") ? "通用层级" : "common_level"}, {(displayMode == "name") ? property.bay_name : property.bay_tag, property.bay_name, property.bay_tag, false, (displayMode == "name") ? "间隔" : "bay"}, {property.component_name.isEmpty() ? (displayMode == "name") ? "未命名设备" : property.component_uuid.toString() : (displayMode == "name") ? property.component_name : property.component_uuid.toString(), property.component_name, property.component_uuid.toString(), true, (displayMode == "name") ? "未命名设备" : "unknown_component"} }; } void DataSourceDlg::onOkClicked() { hide(); } void DataSourceDlg::onCancelClicked() { hide(); } void DataSourceDlg::onTreeSelectionChanged(const QModelIndex& current, const QModelIndex& previous) { Q_UNUSED(previous); if (!current.isValid()) { clearPropertyList(); return; } QStandardItem* item = _treeModel->itemFromIndex(current); if (!item) { clearPropertyList(); return; } QVariantMap itemData = item->data(Qt::UserRole + 1).toMap(); QString levelType = itemData.value("levelType", "").toString(); if (levelType == "category") { // 点击分类节点,从category节点获取属性 loadCategoryProperties(item); }else{ clearPropertyList(); } } void DataSourceDlg::clearPropertyList() { ui->listWidget->clear(); m_currentCategoryItem = nullptr; } void DataSourceDlg::loadCategoryProperties(QStandardItem* categoryItem) { m_currentCategoryItem = categoryItem; // 从category节点获取属性列表 QVariantMap categoryData = categoryItem->data(Qt::UserRole + 1).toMap(); QVector properties = categoryData["properties"].value>(); if (properties.isEmpty()) { // 如果没有属性,从DataManager重新获取 properties = getCategoryPropertiesFromDataManager(categoryData); } if (properties.isEmpty()) { clearItems(); return; } updatePropertyList(properties); } QVector DataSourceDlg::getCategoryPropertiesFromDataManager(const QVariantMap& categoryData) { QString groupTag = categoryData.value("groupTag").toString(); QString modelName = categoryData.value("modelName").toString(); QString paraType = categoryData.value("paraType").toString(); QString sourceType = categoryData.value("sourceType").toString(); QString componentUuid = categoryData.value("component_uuid").toString(); QVector result; for (auto it = m_dataSource->allProperties.begin(); it != m_dataSource->allProperties.end(); ++it) { const ExtraProperty& prop = it.value(); bool match = (prop.group_tag == groupTag) && (prop.sourceType == sourceType) && (prop.type_tag == paraType) && (prop.component_uuid.toString() == componentUuid); if (sourceType == "property") { match = match && (prop.sourceConfig.value("modelName").toString() == modelName); } if (match) { result.append(prop); } } return result; } void DataSourceDlg::updatePropertyList(QVector vec) { for(auto& pro:vec){ QListWidgetItem* pItem = new QListWidgetItem(pro.code); pItem->setData(Qt::UserRole,pro.connect_para); ui->listWidget->addItem(pItem); } } bool DataSourceDlg::expandToPropertyByCode(const QString& propertyCode, const QString& displayMode) { // 在树中搜索对应编码的节点 QStandardItemModel* model = qobject_cast(ui->treeView->model()); if (!model) { return false; } QModelIndexList matches = model->match( model->index(0, 0), Qt::UserRole + 1, // 搜索用户数据 QVariant(), // 任意值 -1, // 搜索所有 Qt::MatchRecursive | Qt::MatchContains ); for (const QModelIndex& index : matches) { QStandardItem* item = model->itemFromIndex(index); if (item) { QVariantMap itemData = item->data(Qt::UserRole + 1).toMap(); // 检查是否是属性节点 if (itemData.contains("property")) { ExtraProperty property = itemData["property"].value(); if (property.code == propertyCode) { // 构建从根节点到该节点的路径 QStringList nodeNames; QModelIndex currentIndex = index; while (currentIndex.isValid()) { QStandardItem* currentItem = model->itemFromIndex(currentIndex); if (currentItem) { nodeNames.prepend(currentItem->text()); } currentIndex = currentIndex.parent(); } // 去掉根节点 if (!nodeNames.isEmpty()) { nodeNames.removeFirst(); } // 展开到该节点 QStandardItem* rootItem = model->invisibleRootItem(); return expandToNodeByNames(rootItem, nodeNames); } } } } return false; } bool DataSourceDlg::expandToNodeByNames(QStandardItem* rootItem, const QStringList& nodeNames) { if (!rootItem || nodeNames.isEmpty()) { return false; } QStandardItem* currentItem = rootItem; QTreeView* treeView = ui->treeView; for (int i = 0; i < nodeNames.size(); ++i) { const QString& targetName = nodeNames[i]; bool found = false; // 在当前节点的子节点中查找 for (int row = 0; row < currentItem->rowCount(); ++row) { QStandardItem* childItem = currentItem->child(row, 0); if (childItem && childItem->text() == targetName) { // 展开当前节点 QModelIndex currentIndex = currentItem->index(); treeView->expand(currentIndex); // 如果是最后一层,选中该节点 if (i == nodeNames.size() - 1) { QModelIndex childIndex = childItem->index(); treeView->scrollTo(childIndex); treeView->selectionModel()->select( childIndex, QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows ); return true; } // 继续查找下一层 currentItem = childItem; found = true; break; } } if (!found) { // 没有找到对应的节点 qWarning() << "Node not found at level" << i << ":" << targetName; // 展开到已找到的层级 if (currentItem != rootItem) { QModelIndex currentIndex = currentItem->index(); treeView->expand(currentIndex); treeView->scrollTo(currentIndex); } return false; } } return true; } void DataSourceDlg::clearItems() { if(_treeModel){ QStandardItem *root = _treeModel->invisibleRootItem(); //先清空model int rowCount = root->rowCount(); if (rowCount > 0) { _treeModel->removeRows(0, rowCount); } } }