922 lines
31 KiB
C++
922 lines
31 KiB
C++
#include "structDataPreviewDlg.h"
|
||
#include "ui_structDataPreviewDlg.h"
|
||
#include <QStandardItemModel>
|
||
#include <QMenuBar>
|
||
#include <QMessageBox>
|
||
#include <QStatusBar>
|
||
#include <QCompleter>
|
||
#include <QStringListModel>
|
||
#include "titleBar.h"
|
||
#include "instance/extraPropertyManager.h"
|
||
#include "structDataSource.h"
|
||
#include "structDataMeasurementModel.h"
|
||
#include "structDataPropertyModel.h"
|
||
#include "structDataPropertyDelegate.h"
|
||
#include "structDataMeasurementDelegate.h"
|
||
#include "dataBase.h"
|
||
#include "global.h"
|
||
|
||
StructDataPreviewDlg::StructDataPreviewDlg(QWidget *parent)
|
||
: QDialog(parent)
|
||
, ui(new Ui::structDataPreviewDlg)
|
||
,_pExtraProManager(nullptr)
|
||
,_treeModel(nullptr)
|
||
,m_currentCategoryItem(nullptr)
|
||
,m_measurementTableModel(nullptr)
|
||
,m_propertyTableModel(nullptr)
|
||
,m_propertyDelegate(nullptr)
|
||
,m_measurementDelegate(nullptr)
|
||
,m_statusBar(nullptr)
|
||
{
|
||
ui->setupUi(this);
|
||
this->setWindowFlags(Qt::FramelessWindowHint | windowFlags());
|
||
m_statusBar = new QStatusBar(this);
|
||
ui->mainLayout->addWidget(m_statusBar);
|
||
m_statusBar->setMaximumHeight(24);
|
||
initial();
|
||
}
|
||
|
||
StructDataPreviewDlg::~StructDataPreviewDlg()
|
||
{
|
||
delete ui;
|
||
}
|
||
|
||
void StructDataPreviewDlg::loadData()
|
||
{
|
||
if(_pExtraProManager)
|
||
m_dataSource->loadExtrapro(_pExtraProManager->geAlltProperty());
|
||
m_dataSource->loadMeasurementData(DataBase::GetInstance()->getAllMeasurements());
|
||
m_dataSource->loadPropertyData(DataManager::instance().modelData());
|
||
addLog(QString("载入属性:%1").arg(m_dataSource->getAllProperties().size()));
|
||
}
|
||
|
||
void StructDataPreviewDlg::initial()
|
||
{
|
||
// 创建标题栏
|
||
m_titleBar = new TitleBar(this);
|
||
m_titleBar->setTitle("结构数据展示");
|
||
ui->mainLayout->insertWidget(0,m_titleBar);
|
||
|
||
QMenuBar *menuBar = new QMenuBar(this);
|
||
QMenu *fileMenu = menuBar->addMenu("文件");
|
||
QAction *saveAction = fileMenu->addAction("保存");
|
||
menuBar->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
|
||
menuBar->setFixedHeight(33);
|
||
|
||
QString style = R"(
|
||
QMenuBar {
|
||
background: #f1f5f9;
|
||
border-bottom: 1px solid #cbd5e1;
|
||
color: #334155;
|
||
font-weight: 500;
|
||
padding: 2px 0;
|
||
}
|
||
|
||
QMenuBar::item {
|
||
padding: 6px 12px;
|
||
border-radius: 3px;
|
||
margin: 0 1px;
|
||
}
|
||
|
||
QMenuBar::item:hover {
|
||
background: #e2e8f0;
|
||
border: 1px solid #cbd5e1;
|
||
}
|
||
|
||
QMenuBar::item:pressed {
|
||
background: #cbd5e0;
|
||
}
|
||
|
||
QMenu {
|
||
background: #ffffff;
|
||
border: 1px solid #cbd5e1;
|
||
border-radius: 4px;
|
||
color: #475569;
|
||
padding: 4px 0;
|
||
}
|
||
|
||
QMenu::item {
|
||
padding: 2px 20px 2px 6px;
|
||
margin: 1px 2px;
|
||
min-height: 20px;
|
||
}
|
||
|
||
QMenu::item:selected {
|
||
background: #3b82f6;
|
||
color: white;
|
||
border-radius: 2px;
|
||
}
|
||
|
||
QMenu::separator {
|
||
height: 1px;
|
||
background: #e2e8f0;
|
||
margin: 3px 8px;
|
||
}
|
||
)";
|
||
|
||
setStyleSheet(style);
|
||
|
||
ui->mainLayout->insertWidget(1,menuBar);
|
||
ui->mainLayout->setStretchFactor(menuBar, 0);
|
||
ui->splitter_2->setStretchFactor(0,1);
|
||
ui->splitter_2->setStretchFactor(1,4);
|
||
|
||
ui->splitter->setStretchFactor(0,5);
|
||
ui->splitter->setStretchFactor(1,1);
|
||
|
||
connect(saveAction,&QAction::triggered,this,&StructDataPreviewDlg::onSaveClicked);
|
||
|
||
connect(m_titleBar, &TitleBar::maximizeClicked, this, &StructDataPreviewDlg::toggleMaximize);
|
||
connect(m_titleBar, &TitleBar::closeClicked, this, &StructDataPreviewDlg::onExitClicked);
|
||
|
||
_treeModel = new QStandardItemModel(this);
|
||
_treeModel->setHorizontalHeaderLabels(QStringList() << "属性层级结构");
|
||
ui->tree_level->setModel(_treeModel);
|
||
|
||
connect(ui->btn_gird,&QPushButton::clicked,this,[=](){
|
||
onLevelButtonClicked(0);
|
||
});
|
||
connect(ui->btn_zone,&QPushButton::clicked,this,[=](){
|
||
onLevelButtonClicked(1);
|
||
});
|
||
connect(ui->btn_station,&QPushButton::clicked,this,[=](){
|
||
onLevelButtonClicked(2);
|
||
});
|
||
connect(ui->btn_level,&QPushButton::clicked,this,[=](){
|
||
onLevelButtonClicked(3);
|
||
});
|
||
connect(ui->btn_bay,&QPushButton::clicked,this,[=](){
|
||
onLevelButtonClicked(4);
|
||
});
|
||
connect(ui->btn_device,&QPushButton::clicked,this,[=](){
|
||
onLevelButtonClicked(5);
|
||
});
|
||
connect(ui->btn_group,&QPushButton::clicked,this,[=](){
|
||
onLevelButtonClicked(6);
|
||
});
|
||
m_dataSource = new StructDataSource(this);
|
||
|
||
connect(ui->tree_level->selectionModel(), &QItemSelectionModel::currentChanged,
|
||
this, &StructDataPreviewDlg::onTreeSelectionChanged);
|
||
|
||
// 连接数据更新信号
|
||
//connect(m_dataSource, &StructDataSource::propertyUpdated,this, &StructDataPreviewDlg::onPropertyUpdated);
|
||
|
||
m_propertyDelegate = new StructDataPropertyDelegate(this);
|
||
m_measurementDelegate = new StructDataMeasurementDelegate(this);
|
||
|
||
_recommandCompleter = new QCompleter(this);
|
||
_recommandCompleter->setCaseSensitivity(Qt::CaseInsensitive);
|
||
_recommandCompleter->setFilterMode(Qt::MatchContains);
|
||
_recommandCompleter->setCompletionMode(QCompleter::PopupCompletion);
|
||
_strLstModel = new QStringListModel(this);
|
||
|
||
_recommandCompleter->setModel(_strLstModel);
|
||
|
||
m_propertyDelegate->setConnectParaCompleter(_recommandCompleter);
|
||
m_measurementDelegate->setConnectParaCompleter(_recommandCompleter);
|
||
}
|
||
|
||
|
||
void StructDataPreviewDlg::clearItems()
|
||
{
|
||
if(_treeModel){
|
||
QStandardItem *root = _treeModel->invisibleRootItem(); //先清空model
|
||
int rowCount = root->rowCount();
|
||
if (rowCount > 0) {
|
||
_treeModel->removeRows(0, rowCount);
|
||
}
|
||
}
|
||
}
|
||
|
||
QString StructDataPreviewDlg::getLevelType(int index) {
|
||
switch (index) {
|
||
case 0: return "电网";
|
||
case 1: return "区域";
|
||
case 2: return "站点";
|
||
case 3: return "电压等级";
|
||
case 4: return "间隔";
|
||
case 5: return "设备";
|
||
case 6: return "分组";
|
||
default: return "未知层级";
|
||
}
|
||
}
|
||
|
||
void StructDataPreviewDlg::expandToLevel(QTreeView* treeView, int targetLevel)
|
||
{
|
||
if (!treeView || !treeView->model()) {
|
||
return;
|
||
}
|
||
|
||
QAbstractItemModel* model = treeView->model();
|
||
|
||
// 先收缩所有
|
||
treeView->collapseAll();
|
||
|
||
// 递归展开到指定层级
|
||
expandToLevelRecursive(treeView, model, QModelIndex(), 0, targetLevel);
|
||
}
|
||
|
||
void StructDataPreviewDlg::expandToLevelRecursive(QTreeView* treeView,
|
||
QAbstractItemModel* model,
|
||
const QModelIndex& parent,
|
||
int currentDepth,
|
||
int targetLevel)
|
||
{
|
||
if (!model || !treeView) {
|
||
return;
|
||
}
|
||
|
||
int rowCount = model->rowCount(parent);
|
||
|
||
for (int row = 0; row < rowCount; ++row) {
|
||
QModelIndex index = model->index(row, 0, parent);
|
||
|
||
// 获取节点的实际层级
|
||
int nodeLevel = getNodeLevel(model, index);
|
||
|
||
if (nodeLevel <= targetLevel) {
|
||
// 展开当前节点
|
||
treeView->expand(index);
|
||
} else {
|
||
// 收缩当前节点
|
||
treeView->collapse(index);
|
||
}
|
||
|
||
// 递归处理子节点
|
||
if (model->hasChildren(index)) {
|
||
expandToLevelRecursive(treeView, model, index, currentDepth + 1, targetLevel);
|
||
}
|
||
}
|
||
}
|
||
|
||
int StructDataPreviewDlg::getNodeLevel(QAbstractItemModel* model, const QModelIndex& index)
|
||
{
|
||
if (!model || !index.isValid()) {
|
||
return -1;
|
||
}
|
||
|
||
// 从UserRole+1中获取层级信息
|
||
QVariant data = model->data(index, Qt::UserRole + 1);
|
||
if (data.isValid()) {
|
||
QVariantMap nodeData = data.toMap();
|
||
|
||
// 先检查是否是属性节点
|
||
if (nodeData.contains("property")) {
|
||
return 7;
|
||
}
|
||
|
||
// 检查层级索引
|
||
if (nodeData.contains("levelIndex")) {
|
||
int levelIndex = nodeData["levelIndex"].toInt();
|
||
return qMin(levelIndex, 7);
|
||
}
|
||
}
|
||
|
||
// 如果没有存储层级信息,通过深度判断
|
||
return getDepthFromRoot(model, index);
|
||
}
|
||
|
||
QStandardItem* StructDataPreviewDlg::processGroupLevel(QStandardItem* componentItem,
|
||
const ExtraProperty& property) {
|
||
QString groupDisplayText = (property.group_name.isEmpty() ?
|
||
property.group_tag : property.group_name);
|
||
|
||
if (groupDisplayText.isEmpty()) {
|
||
groupDisplayText = "未命名分组";
|
||
}
|
||
|
||
// 查找group节点
|
||
for (int row = 0; row < componentItem->rowCount(); ++row) {
|
||
QStandardItem* child = componentItem->child(row, 0);
|
||
if (child) {
|
||
QVariantMap childData = child->data(Qt::UserRole + 1).toMap();
|
||
QString levelType = childData.value("levelType").toString();
|
||
|
||
if (levelType == "group") {
|
||
// 检查是否是同一个group
|
||
QString existingGroupTag = childData.value("groupTag", "").toString();
|
||
QString existingModelName = childData.value("modelName", "").toString();
|
||
QString propertyModelName = property.sourceConfig.value("modelName").toString();
|
||
|
||
if (existingGroupTag == property.group_tag) {
|
||
if (property.sourceType == "property") {
|
||
// 对于property类型,还需要检查modelName
|
||
if (existingModelName == propertyModelName) {
|
||
return child;
|
||
}
|
||
} else {
|
||
// 对于measurement类型,只需要groupTag匹配
|
||
return child;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
// 创建新的group节点
|
||
QStandardItem* groupItem = new QStandardItem(groupDisplayText);
|
||
QVariantMap groupData;
|
||
groupData["levelIndex"] = 6; // 第7层
|
||
groupData["levelType"] = "group";
|
||
groupData["groupName"] = property.group_name;
|
||
groupData["groupTag"] = property.group_tag;
|
||
groupData["sourceType"] = property.sourceType;
|
||
|
||
if (property.sourceType == "property") {
|
||
groupData["modelName"] = property.sourceConfig.value("modelName");
|
||
}
|
||
|
||
groupData["properties"] = QVariant::fromValue(QVector<ExtraProperty>());
|
||
groupItem->setData(groupData, Qt::UserRole + 1);
|
||
|
||
// 设置工具提示
|
||
QString tooltip = QString("分组: %1\n类型: %2")
|
||
.arg(groupDisplayText)
|
||
.arg(property.sourceType == "property" ? "属性" : "量测");
|
||
groupItem->setToolTip(tooltip);
|
||
|
||
componentItem->appendRow(groupItem);
|
||
return groupItem;
|
||
}
|
||
|
||
// 处理category层级
|
||
void StructDataPreviewDlg::processCategoryLevel(QStandardItem* groupItem,
|
||
const ExtraProperty& property) {
|
||
QString categoryName = property.type_name;
|
||
QString paraType = property.type_tag;
|
||
|
||
if (categoryName.isEmpty() || paraType.isEmpty()) {
|
||
qWarning() << "Invalid category or paraType for property:" << property.code;
|
||
return;
|
||
}
|
||
|
||
// 查找category节点
|
||
for (int row = 0; row < groupItem->rowCount(); ++row) {
|
||
QStandardItem* child = groupItem->child(row, 0);
|
||
if (child) {
|
||
QVariantMap childData = child->data(Qt::UserRole + 1).toMap();
|
||
QString levelType = childData.value("levelType").toString();
|
||
QString childParaType = childData.value("paraType", "").toString();
|
||
|
||
if (levelType == "category" && childParaType == paraType) {
|
||
// 找到对应的category节点,更新属性列表
|
||
updateCategoryProperties(child, property);
|
||
return;
|
||
}
|
||
}
|
||
}
|
||
|
||
// 创建新的category节点
|
||
QStandardItem* categoryItem = new QStandardItem(categoryName);
|
||
QVariantMap categoryData;
|
||
categoryData["levelType"] = "category";
|
||
categoryData["paraType"] = paraType;
|
||
categoryData["sourceType"] = property.sourceType;
|
||
categoryData["groupTag"] = property.group_tag;
|
||
categoryData["groupName"] = property.group_name;
|
||
|
||
if (property.sourceType == "property") {
|
||
categoryData["modelName"] = property.sourceConfig.value("modelName");
|
||
}
|
||
|
||
// 初始化属性列表
|
||
QVector<ExtraProperty> properties;
|
||
properties.append(property);
|
||
categoryData["properties"] = QVariant::fromValue(properties);
|
||
categoryData["propertyCount"] = 1;
|
||
|
||
categoryItem->setData(categoryData, Qt::UserRole + 1);
|
||
|
||
// 设置显示文本(包含数量)
|
||
categoryItem->setText(QString("%1 (1)").arg(categoryName));
|
||
|
||
// 设置工具提示
|
||
QString tooltip = QString("分类: %1\n参量类型: %2\n属性数量: 1")
|
||
.arg(categoryName)
|
||
.arg(categoryName);
|
||
categoryItem->setToolTip(tooltip);
|
||
|
||
groupItem->appendRow(categoryItem);
|
||
}
|
||
|
||
// 更新category节点的属性列表
|
||
void StructDataPreviewDlg::updateCategoryProperties(QStandardItem* categoryItem,
|
||
const ExtraProperty& property) {
|
||
QVariantMap categoryData = categoryItem->data(Qt::UserRole + 1).toMap();
|
||
QVector<ExtraProperty> properties = categoryData["properties"].value<QVector<ExtraProperty>>();
|
||
|
||
// 检查属性是否已存在
|
||
bool exists = false;
|
||
for (const auto& prop : properties) {
|
||
if (prop.code == property.code) {
|
||
exists = true;
|
||
break;
|
||
}
|
||
}
|
||
|
||
if (!exists) {
|
||
properties.append(property);
|
||
categoryData["properties"] = QVariant::fromValue(properties);
|
||
|
||
int newCount = properties.size();
|
||
categoryData["propertyCount"] = newCount;
|
||
|
||
// 更新显示文本
|
||
QString baseName = property.type_name;
|
||
categoryItem->setText(QString("%1 (%2)").arg(baseName).arg(newCount));
|
||
|
||
// 更新工具提示
|
||
QString tooltip = QString("属性数量: %1")
|
||
.arg(newCount);
|
||
categoryItem->setToolTip(tooltip);
|
||
|
||
categoryItem->setData(categoryData, Qt::UserRole + 1);
|
||
}
|
||
}
|
||
|
||
void StructDataPreviewDlg::loadCategoryProperties(QStandardItem* categoryItem) {
|
||
m_currentCategoryItem = categoryItem;
|
||
|
||
// 保存当前修改
|
||
saveCurrentIfModified();
|
||
|
||
// 从category节点获取属性列表
|
||
QVariantMap categoryData = categoryItem->data(Qt::UserRole + 1).toMap();
|
||
QVector<ExtraProperty> properties = categoryData["properties"].value<QVector<ExtraProperty>>();
|
||
|
||
if (properties.isEmpty()) {
|
||
// 如果没有属性,从DataManager重新获取
|
||
properties = getCategoryPropertiesFromDataManager(categoryData);
|
||
}
|
||
|
||
if (properties.isEmpty()) {
|
||
clearTableView();
|
||
QString categoryName = categoryItem->text();
|
||
ui->tableTitle->setText(QString("分类: %1").arg(categoryName));
|
||
return;
|
||
}
|
||
|
||
// 重置修改状态
|
||
m_currentModified = false;
|
||
//updateWindowTitle();
|
||
|
||
// 获取group和category信息
|
||
QString categoryName = categoryItem->text();
|
||
QString groupName = categoryItem->parent() ? categoryItem->parent()->text() : "";
|
||
QString sourceType = categoryData.value("sourceType").toString();
|
||
|
||
// 根据sourceType显示表格
|
||
if (sourceType == "property") {
|
||
setupPropertyTable(properties, categoryName, groupName);
|
||
} else if (sourceType == "measurement") {
|
||
setupMeasurementTable(properties, categoryName, groupName);
|
||
}
|
||
|
||
ui->tableView->setVisible(true);
|
||
}
|
||
|
||
QVector<ExtraProperty> StructDataPreviewDlg::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<ExtraProperty> 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 StructDataPreviewDlg::setupPropertyTable(const QVector<ExtraProperty>& properties,
|
||
const QString& categoryName,
|
||
const QString& groupName) {
|
||
if (!m_propertyTableModel) {
|
||
m_propertyTableModel = new StructDataPropertyModel(m_dataSource, this);
|
||
connect(m_propertyTableModel, &StructDataPropertyModel::propertyModified,
|
||
this, &StructDataPreviewDlg::onPropertyModified);
|
||
}
|
||
|
||
m_propertyTableModel->setProperties(properties);
|
||
ui->tableView->setModel(m_propertyTableModel);
|
||
|
||
// 使用Property代理
|
||
ui->tableView->setItemDelegate(m_propertyDelegate);
|
||
|
||
setupPropertyColumns();
|
||
updateTableTitle("属性", categoryName, groupName, properties.size());
|
||
}
|
||
|
||
void StructDataPreviewDlg::setupMeasurementTable(const QVector<ExtraProperty>& properties,
|
||
const QString& categoryName,
|
||
const QString& groupName) {
|
||
if (!m_measurementTableModel) {
|
||
m_measurementTableModel = new StructDataMeasurementModel(m_dataSource, this);
|
||
connect(m_measurementTableModel, &StructDataMeasurementModel::propertyModified,
|
||
this, &StructDataPreviewDlg::onPropertyModified);
|
||
}
|
||
|
||
m_measurementTableModel->setProperties(properties);
|
||
ui->tableView->setModel(m_measurementTableModel);
|
||
|
||
if (!m_measurementDelegate) {
|
||
m_measurementDelegate = new StructDataMeasurementDelegate(this);
|
||
}
|
||
ui->tableView->setItemDelegate(m_measurementDelegate);
|
||
|
||
setupMeasurementColumns();
|
||
|
||
// 去除数量显示
|
||
QString cleanCategoryName = categoryName;
|
||
int bracketPos = cleanCategoryName.indexOf(" (");
|
||
if (bracketPos != -1) {
|
||
cleanCategoryName = cleanCategoryName.left(bracketPos);
|
||
}
|
||
|
||
ui->tableTitle->setText(QString("分组: %1 - 分类: %2").arg(groupName).arg(cleanCategoryName));
|
||
}
|
||
|
||
void StructDataPreviewDlg::updateCategoryAfterPropertyModified(QStandardItem* categoryItem, const ExtraProperty& updatedProp) {
|
||
QVariantMap categoryData = categoryItem->data(Qt::UserRole + 1).toMap();
|
||
QVector<ExtraProperty> properties = categoryData["properties"].value<QVector<ExtraProperty>>();
|
||
|
||
// 更新属性列表
|
||
bool found = false;
|
||
for (auto& prop : properties) {
|
||
if (prop.code == updatedProp.code) {
|
||
prop = updatedProp;
|
||
found = true;
|
||
break;
|
||
}
|
||
}
|
||
|
||
if (found) {
|
||
categoryData["properties"] = QVariant::fromValue(properties);
|
||
categoryItem->setData(categoryData, Qt::UserRole + 1);
|
||
}
|
||
}
|
||
|
||
void StructDataPreviewDlg::saveCurrentIfModified()
|
||
{
|
||
if (m_currentModified && m_currentCategoryItem) {
|
||
int result = QMessageBox::question(this, "切换分类",
|
||
"当前分类有未保存的修改,是否保存?",
|
||
QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel);
|
||
|
||
if (result == QMessageBox::Save) {
|
||
saveAll();
|
||
} else if (result == QMessageBox::Cancel) {
|
||
ui->tree_level->selectionModel()->select(ui->tree_level->selectionModel()->currentIndex(),
|
||
QItemSelectionModel::Select);
|
||
}
|
||
}
|
||
}
|
||
|
||
void StructDataPreviewDlg::saveAll() {
|
||
m_dataSource->saveAll();
|
||
m_currentModified = false;
|
||
m_statusBar->showMessage("保存成功", 2000);
|
||
}
|
||
|
||
void StructDataPreviewDlg::clearTableView() {
|
||
ui->tableTitle->setText("未选择分类");
|
||
ui->tableView->setModel(nullptr);
|
||
ui->tableView->setVisible(false);
|
||
m_currentCategoryItem = nullptr;
|
||
}
|
||
|
||
void StructDataPreviewDlg::setupPropertyColumns() {
|
||
ui->tableView->setColumnWidth(StructDataPropertyModel::ColName, 120);
|
||
ui->tableView->setColumnWidth(StructDataPropertyModel::ColTag, 100);
|
||
ui->tableView->setColumnWidth(StructDataPropertyModel::ColCode, 200);
|
||
ui->tableView->setColumnWidth(StructDataPropertyModel::ColSourceType, 60);
|
||
ui->tableView->setColumnWidth(StructDataPropertyModel::ColConnectPara, 400);
|
||
ui->tableView->setColumnWidth(StructDataPropertyModel::ColModelName, 100);
|
||
ui->tableView->setColumnWidth(StructDataPropertyModel::ColDataType, 80);
|
||
ui->tableView->setColumnWidth(StructDataPropertyModel::ColDefaultValue, 100);
|
||
ui->tableView->setColumnWidth(StructDataPropertyModel::ColLengthPrecision, 80);
|
||
}
|
||
|
||
void StructDataPreviewDlg::setupMeasurementColumns() {
|
||
ui->tableView->setColumnWidth(StructDataMeasurementModel::ColName, 120);
|
||
ui->tableView->setColumnWidth(StructDataMeasurementModel::ColTag, 100);
|
||
ui->tableView->setColumnWidth(StructDataMeasurementModel::ColCode, 200);
|
||
ui->tableView->setColumnWidth(StructDataMeasurementModel::ColSourceType, 60);
|
||
ui->tableView->setColumnWidth(StructDataMeasurementModel::ColConnectPara, 400);
|
||
ui->tableView->setColumnWidth(StructDataMeasurementModel::ColEquipment, 100);
|
||
ui->tableView->setColumnWidth(StructDataMeasurementModel::ColChannel, 60);
|
||
ui->tableView->setColumnWidth(StructDataMeasurementModel::ColType, 60);
|
||
ui->tableView->setColumnWidth(StructDataMeasurementModel::ColSize, 60);
|
||
ui->tableView->setColumnWidth(StructDataMeasurementModel::ColSource, 60);
|
||
ui->tableView->setColumnWidth(StructDataMeasurementModel::ColStation, 80);
|
||
ui->tableView->setColumnWidth(StructDataMeasurementModel::ColPacket, 50);
|
||
ui->tableView->setColumnWidth(StructDataMeasurementModel::ColOffset, 60);
|
||
ui->tableView->setColumnWidth(StructDataMeasurementModel::ColEnable, 50);
|
||
ui->tableView->setColumnWidth(StructDataMeasurementModel::ColCause, 120);
|
||
}
|
||
|
||
void StructDataPreviewDlg::updateTableTitle(const QString& dataType, const QString& categoryName,
|
||
const QString& groupName, int count) {
|
||
ui->tableTitle->setText(QString("%1分组: %2 - %3").arg(dataType).arg(groupName).arg(categoryName));
|
||
}
|
||
|
||
int StructDataPreviewDlg::getDepthFromRoot(QAbstractItemModel* model, const QModelIndex& index) {
|
||
int depth = 0;
|
||
QModelIndex parent = index.parent();
|
||
|
||
while (parent.isValid()) {
|
||
depth++;
|
||
parent = parent.parent();
|
||
}
|
||
|
||
return depth;
|
||
}
|
||
|
||
void StructDataPreviewDlg::onExitClicked()
|
||
{
|
||
hide();
|
||
}
|
||
|
||
void StructDataPreviewDlg::onSaveClicked()
|
||
{
|
||
|
||
}
|
||
|
||
void StructDataPreviewDlg::onLevelButtonClicked(int nLevel)
|
||
{
|
||
// 展开到指定层级
|
||
expandToLevel(ui->tree_level,nLevel);
|
||
}
|
||
|
||
void StructDataPreviewDlg::onTreeSelectionChanged(const QModelIndex& current, const QModelIndex& previous) {
|
||
Q_UNUSED(previous);
|
||
|
||
if (!current.isValid()) {
|
||
clearTableView();
|
||
return;
|
||
}
|
||
|
||
QStandardItem* item = _treeModel->itemFromIndex(current);
|
||
if (!item) {
|
||
clearTableView();
|
||
return;
|
||
}
|
||
|
||
QVariantMap itemData = item->data(Qt::UserRole + 1).toMap();
|
||
QString levelType = itemData.value("levelType", "").toString();
|
||
|
||
if (levelType == "category") {
|
||
// 点击分类节点,从category节点获取属性
|
||
loadCategoryProperties(item);
|
||
}else{
|
||
clearTableView();
|
||
}
|
||
}
|
||
|
||
void StructDataPreviewDlg::onPropertyModified(int row, const ExtraProperty& prop) {
|
||
m_currentModified = true;
|
||
//updateWindowTitle();
|
||
|
||
// 更新树中category的属性数量
|
||
if (m_currentCategoryItem) {
|
||
updateCategoryAfterPropertyModified(m_currentCategoryItem, prop);
|
||
}
|
||
|
||
m_statusBar->showMessage(QString("属性'%1'已修改").arg(prop.name), 2000);
|
||
}
|
||
|
||
void StructDataPreviewDlg::showEvent(QShowEvent *event)
|
||
{
|
||
QDialog::showEvent(event);
|
||
if (m_titleBar) {
|
||
m_titleBar->updateMaximizeButton();
|
||
}
|
||
}
|
||
|
||
void StructDataPreviewDlg::showMaximized()
|
||
{
|
||
if (!isMaximized()) {
|
||
m_normalGeometry = geometry(); // 保存当前位置和大小
|
||
}
|
||
|
||
QDialog::showMaximized();
|
||
}
|
||
|
||
void StructDataPreviewDlg::showNormal()
|
||
{
|
||
QDialog::showNormal();
|
||
}
|
||
|
||
void StructDataPreviewDlg::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->tree_level->expandAll();
|
||
}
|
||
}
|
||
|
||
QVector<ExtraProperty> StructDataPreviewDlg::getGroupProperties(QStandardItem* groupItem) {
|
||
if (!groupItem) return QVector<ExtraProperty>();
|
||
|
||
QVariantMap data = groupItem->data(Qt::UserRole + 1).toMap();
|
||
if (data.contains("properties")) {
|
||
return data["properties"].value<QVector<ExtraProperty>>();
|
||
}
|
||
|
||
return QVector<ExtraProperty>();
|
||
}
|
||
|
||
// 获取group节点的sourceType
|
||
QString getGroupSourceType(QStandardItem* groupItem) {
|
||
if (!groupItem) return "";
|
||
|
||
QVariantMap data = groupItem->data(Qt::UserRole + 1).toMap();
|
||
return data.value("sourceType", "").toString();
|
||
}
|
||
|
||
void StructDataPreviewDlg::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<ExtraPropertyLevelInfo> 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"}
|
||
};
|
||
|
||
QStandardItem* currentParent = root;
|
||
|
||
for (size_t i = 0; i < levels.size(); ++i) {
|
||
const auto& level = levels[i];
|
||
bool isRequired = level.isRequired;
|
||
bool isEmpty = (displayMode == "name") ?
|
||
(level.nameValue.isEmpty() && level.displayText.isEmpty()) :
|
||
(level.tagValue.isEmpty() && level.displayText.isEmpty());
|
||
|
||
if (!isRequired && isEmpty) {
|
||
continue;
|
||
}
|
||
|
||
// 确定显示文本
|
||
QString displayText = level.displayText;
|
||
bool isMissing = false;
|
||
|
||
if (displayText.isEmpty()) {
|
||
if (isRequired) {
|
||
displayText = level.placeholder;
|
||
isMissing = true;
|
||
} else {
|
||
// 可选层级为空,跳过
|
||
continue;
|
||
}
|
||
}
|
||
|
||
// 查找当前层级是否已存在
|
||
QStandardItem* foundChild = nullptr;
|
||
|
||
for (int row = 0; row < currentParent->rowCount(); ++row) {
|
||
QStandardItem* child = currentParent->child(row, 0);
|
||
if (child && child->text() == displayText) {
|
||
// 检查是否为同一层级
|
||
QVariantMap childData = child->data(Qt::UserRole + 1).toMap();
|
||
int childLevelIndex = childData["levelIndex"].toInt();
|
||
|
||
if (childLevelIndex == static_cast<int>(i)) {
|
||
foundChild = child;
|
||
break;
|
||
}
|
||
}
|
||
}
|
||
|
||
if (foundChild) {
|
||
currentParent = foundChild;
|
||
} else {
|
||
QStandardItem* newNode = new QStandardItem(displayText);
|
||
QVariantMap levelData;
|
||
levelData["levelIndex"] = static_cast<int>(i);
|
||
levelData["levelType"] = getLevelType(i);
|
||
|
||
newNode->setData(levelData, Qt::UserRole + 1);
|
||
currentParent->appendRow(newNode);
|
||
currentParent = newNode;
|
||
}
|
||
}
|
||
|
||
// 现在currentParent是component节点
|
||
// 处理group层级(第7层)
|
||
QStandardItem* groupItem = processGroupLevel(currentParent, property);
|
||
|
||
// 处理category层级(在group下)
|
||
processCategoryLevel(groupItem, property);
|
||
|
||
}
|
||
|
||
void StructDataPreviewDlg::updateRecommandLst(QStringList lst)
|
||
{
|
||
for(auto& newName:lst){
|
||
if(!newName.isEmpty()){
|
||
if(!_curRecommandLst.contains(newName)){
|
||
_curRecommandLst.append(newName);
|
||
}
|
||
}
|
||
}
|
||
_strLstModel->setStringList(_curRecommandLst);
|
||
if (_recommandCompleter->popup()->isVisible()) {
|
||
_recommandCompleter->popup()->hide();
|
||
}
|
||
_recommandCompleter->complete(); // 重新显示补全列表
|
||
}
|
||
|
||
void StructDataPreviewDlg::addLog(const QString &message)
|
||
{
|
||
QString time = QDateTime::currentDateTime().toString("hh:mm:ss");
|
||
ui->listWidget->addItem(QString("[%1] %2").arg(time).arg(message));
|
||
ui->listWidget->scrollToBottom();
|
||
}
|
||
|
||
void StructDataPreviewDlg::resizeEvent(QResizeEvent *event)
|
||
{
|
||
QDialog::resizeEvent(event);
|
||
if (m_titleBar) {
|
||
m_titleBar->updateMaximizeButton();
|
||
}
|
||
}
|
||
|
||
void StructDataPreviewDlg::toggleMaximize()
|
||
{
|
||
if (isMaximized()) {
|
||
showNormal();
|
||
} else {
|
||
showMaximized();
|
||
}
|
||
|
||
// 更新标题栏按钮
|
||
if (m_titleBar) {
|
||
m_titleBar->updateMaximizeButton();
|
||
}
|
||
}
|