DiagramDesigner/diagramCavas/source/structDataCauseEditDlg.cpp

126 lines
3.6 KiB
C++
Raw Normal View History

2026-01-16 18:40:46 +08:00
#include "structDataCauseEditDlg.h"
#include <QVBoxLayout>
#include <QDialogButtonBox>
#include <QLabel>
StructDataCauseEditDlg::StructDataCauseEditDlg(const QMap<QString, double>& initialData,QWidget* parent)
: QDialog(parent) {
setupUI();
setData(initialData);
}
void StructDataCauseEditDlg::setData(const QMap<QString, double>& data) {
m_data = data;
updateUIFromData();
}
QMap<QString, double> StructDataCauseEditDlg::getData() const {
return m_data;
}
void StructDataCauseEditDlg::setupUI() {
setWindowTitle("编辑 Cause 数据");
setMinimumSize(350, 200);
QVBoxLayout* mainLayout = new QVBoxLayout(this);
// 标题
QLabel* titleLabel = new QLabel("遥测原因", this);
titleLabel->setStyleSheet("font-weight: bold; font-size: 12pt;");
mainLayout->addWidget(titleLabel);
// 分隔线
QFrame* line = new QFrame(this);
line->setFrameShape(QFrame::HLine);
line->setFrameShadow(QFrame::Sunken);
mainLayout->addWidget(line);
// 可用的键列表
m_availableKeys = {"upup", "up", "down", "downdown"};
// 为每个键创建编辑器
for (const QString& key : m_availableKeys) {
QHBoxLayout* rowLayout = new QHBoxLayout;
// 复选框
QCheckBox* checkBox = new QCheckBox(key, this);
checkBox->setStyleSheet("QCheckBox { font-weight: bold; }");
// 数值输入框
QDoubleSpinBox* spinBox = new QDoubleSpinBox(this);
spinBox->setRange(0.0, 100.0);
//spinBox->setSuffix("%");
spinBox->setDecimals(1);
spinBox->setSingleStep(0.5);
spinBox->setButtonSymbols(QAbstractSpinBox::NoButtons);
// 连接信号
connect(checkBox, &QCheckBox::toggled, this, [this, key, spinBox, checkBox](bool checked) {
spinBox->setEnabled(checked);
if (checked) {
spinBox->setFocus();
spinBox->selectAll();
} else {
spinBox->setValue(0.0);
}
});
rowLayout->addWidget(checkBox);
rowLayout->addWidget(spinBox);
rowLayout->addStretch();
mainLayout->addLayout(rowLayout);
// 存储控件指针
m_checkBoxes[key] = checkBox;
m_spinBoxes[key] = spinBox;
}
// 分隔线
QFrame* line2 = new QFrame(this);
line2->setFrameShape(QFrame::HLine);
line2->setFrameShadow(QFrame::Sunken);
mainLayout->addWidget(line2);
// 按钮
m_buttonBox = new QDialogButtonBox(
QDialogButtonBox::Ok | QDialogButtonBox::Cancel, this);
mainLayout->addWidget(m_buttonBox);
connect(m_buttonBox, &QDialogButtonBox::accepted, this, [this]() {
updateTotal();
accept();
});
connect(m_buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
setLayout(mainLayout);
// 初始更新
updateTotal();
}
void StructDataCauseEditDlg::updateUIFromData() {
for(auto iter = m_data.begin();iter != m_data.end();++iter) {
if (m_checkBoxes.contains(iter.key()) && m_spinBoxes.contains(iter.key())) {
bool hasValue = m_data.contains(iter.key()) && m_data[iter.key()] > 0;
m_checkBoxes[iter.key()]->setChecked(hasValue);
m_spinBoxes[iter.key()]->setValue(hasValue ? m_data[iter.key()] : 0.0);
m_spinBoxes[iter.key()]->setEnabled(hasValue);
}
}
}
void StructDataCauseEditDlg::updateTotal() {
m_data.clear();
for (const QString& key : m_availableKeys) {
if (m_checkBoxes[key]->isChecked()) {
double value = m_spinBoxes[key]->value();
m_data[key] = value;
}
}
}