EnergySpectrumAnalyer/src/MeasureDeviceParamsConfigView/DeviceParamsTableForm.cpp

382 lines
15 KiB
C++

#include "DeviceParamsTableForm.h"
#include "ui_DeviceParamsTableForm.h"
#include "GlobalDefine.h"
#include <QComboBox>
#include <QDialog>
#include <QCheckBox>
#include <QSpinBox>
#include <QFile>
#include <QJsonDocument>
#include <QJsonObject>
#include <QMessageBox>
DeviceParamsItemDelegate::DeviceParamsItemDelegate(QObject* parent)
: QStyledItemDelegate(parent)
{
}
QWidget* DeviceParamsItemDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const
{
QWidget* editor = nullptr;
int col = index.column();
switch (col) {
case 1: { // 多道分辨率
QStringList value_list { "256", "512", "1024", "2048", "4096", "8192", "16384" };
QComboBox *combobox = new QComboBox(parent);
for (int index = 0; index < value_list.size(); ++index) {
QString value = value_list.at(index);
combobox->addItem(value);
}
editor = combobox;
} break;
case 2: { // 硬件增益
QStringList value_list { "1", "2", "4", "8", "16" };
QComboBox *combobox = new QComboBox(parent);
for (int index = 0; index < value_list.size(); ++index) {
QString value = value_list.at(index);
combobox->addItem(value, index + 1);
}
editor = combobox;
} break;
case 3: { // 软件增益
QSpinBox* spinbox = new QSpinBox(parent);
spinbox->setRange(1, 2^32-1);
editor = spinbox;
} break;
case 4: { // 时间常数
QSpinBox* spinbox = new QSpinBox(parent);
spinbox->setRange(1, 100);
editor = spinbox;
} break;
case 5: { // 直流偏移
QSpinBox* spinbox = new QSpinBox(parent);
spinbox->setRange(-1000, 1000);
editor = spinbox;
} break;
case 6: { // 上升时间
QSpinBox* spinbox = new QSpinBox(parent);
spinbox->setRange(1, 255);
editor = spinbox;
} break;
case 7: { // 平顶时间
QSpinBox* spinbox = new QSpinBox(parent);
spinbox->setRange(1, 255);
editor = spinbox;
} break;
case 8: { // 最大能量范围
QSpinBox* spinbox = new QSpinBox(parent);
spinbox->setRange(0, 99999);
editor = spinbox;
} break;
default:
break;
}
return editor;
}
void DeviceParamsItemDelegate::setEditorData(QWidget* editor, const QModelIndex& index) const
{
QVariant value = index.data(Qt::EditRole);
int col = index.column();
switch (col) {
case 1:
case 2: {
QComboBox *combo = qobject_cast<QComboBox*>(editor);
if (combo) {
int idx = combo->findText(value.toString());
if (idx >= 0) combo->setCurrentIndex(idx);
}
} break;
case 3:
case 4:
case 5:
case 6:
case 7:
case 8: {
QSpinBox *spinbox = qobject_cast<QSpinBox*>(editor);
if (spinbox) {
spinbox->setValue(value.toUInt());
}
} break;
default:
break;
}
}
void DeviceParamsItemDelegate::setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const
{
int col = index.column();
switch (col) {
case 1:
case 2: {
QComboBox *combobox = qobject_cast<QComboBox*>(editor);
if (combobox) {
model->setData(index, combobox->currentText());
}
} break;
case 3:
case 4:
case 5:
case 6:
case 7:
case 8: {
QSpinBox *spinbox = qobject_cast<QSpinBox*>(editor);
if (spinbox) {
model->setData(index, spinbox->value());
}
} break;
default:
break;
}
}
void DeviceParamsItemDelegate::updateEditorGeometry(QWidget* editor, const QStyleOptionViewItem& option, const QModelIndex& index) const
{
editor->setGeometry(option.rect);
}
bool DeviceParamsItemDelegate::eventFilter(QObject* obj, QEvent* event)
{
return QStyledItemDelegate::eventFilter(obj, event);
}
DeviceParamsTableForm::DeviceParamsTableForm(QWidget* parent)
: QWidget(parent)
, ui(new Ui::DeviceParamsTableForm)
{
ui->setupUi(this);
ui->params_cfg_table->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
ui->params_cfg_table->horizontalHeader()->setSectionResizeMode(0, QHeaderView::ResizeToContents);
ui->params_cfg_table->horizontalHeader()->setDefaultAlignment(Qt::AlignLeft);
ui->params_cfg_table->setItemDelegate(new DeviceParamsItemDelegate(this));
for (int row = 0; row < 32; ++row) {
int channel_num = row + 1;
int board_id = (channel_num - 1) / 4;
int channel_id = (channel_num - 1) % 4;
QVariantMap channel_info;
channel_info["board_id"] = board_id;
channel_info["channel_id"] = channel_id;
ui->params_cfg_table->insertRow(row);
const QString& channel_text = QStringLiteral(u"通道%1").arg(channel_num);
ui->params_cfg_table->setItem(row, 0, new QTableWidgetItem(channel_text));
ui->params_cfg_table->item(row, 0)->setCheckState(Qt::Unchecked);
ui->params_cfg_table->item(row, 0)->setData(Qt::UserRole, channel_info);
ui->params_cfg_table->setItem(row, 1, new QTableWidgetItem());
ui->params_cfg_table->setItem(row, 2, new QTableWidgetItem());
ui->params_cfg_table->setItem(row, 3, new QTableWidgetItem());
ui->params_cfg_table->setItem(row, 4, new QTableWidgetItem());
ui->params_cfg_table->setItem(row, 5, new QTableWidgetItem());
ui->params_cfg_table->setItem(row, 6, new QTableWidgetItem());
ui->params_cfg_table->setItem(row, 7, new QTableWidgetItem());
ui->params_cfg_table->setItem(row, 8, new QTableWidgetItem());
ui->params_cfg_table->setItem(row, 9, new QTableWidgetItem());
}
connect(ui->btn_all_select, &QPushButton::clicked, this, &DeviceParamsTableForm::onAllSelectBtnClicked);
connect(ui->btn_reserve_select, &QPushButton::clicked, this, &DeviceParamsTableForm::onReserveSelectBtnClicked);
connect(ui->btn_channel_select, &QPushButton::clicked, this, &DeviceParamsTableForm::onCfgChannelSelectBtnClicked);
connect(ui->btn_update_to_other, &QPushButton::clicked, this, &DeviceParamsTableForm::onCurrentChannelToOther);
connect(ui->btn_save, &QPushButton::clicked, this, &DeviceParamsTableForm::onSaveSelectedChannelConfig);
}
DeviceParamsTableForm::~DeviceParamsTableForm()
{
delete ui;
}
void DeviceParamsTableForm::SetConfigFilename(const QString& filename)
{
_config_filename = filename;
if ( _config_filename.isEmpty() ) {
return;
}
QFile json_file(_config_filename);
if (!json_file.open(QIODevice::ReadOnly | QIODevice::Text)) {
return;
}
QByteArray json_data = json_file.readAll();
json_file.close();
QJsonDocument json_doc = QJsonDocument::fromJson(json_data);
if (json_doc.isNull()) {
return;
}
if (!json_doc.isObject())
return;
QVariantMap device_config_info = json_doc.object().toVariantMap();
if (!device_config_info.contains(QStringLiteral(u"ChannelConfig")))
return;
QVariantList channel_config_list = device_config_info[QStringLiteral(u"ChannelConfig")].toList();
if (channel_config_list.isEmpty())
return;
for (auto channel_config : channel_config_list) {
if (!channel_config.isValid())
continue;
QVariantMap channel_config_info = channel_config.toMap();
if (!channel_config_info.contains("BoardId") || !channel_config_info.contains("ChannelId"))
continue;
int board_id = channel_config_info["BoardId"].toInt();
int channel_id = channel_config_info["ChannelId"].toInt();
int row = board_id * 4 + channel_id;
if (row >= 32)
continue;
QVariantMap channel_info;
channel_info["board_id"] = board_id;
channel_info["channel_id"] = channel_id;
ui->params_cfg_table->item(row, 0)->setData(Qt::UserRole, channel_info);
if (channel_config_info.contains("AddrCount"))
ui->params_cfg_table->item(row, 1)->setText(channel_config_info["AddrCount"].toString());
if (channel_config_info.contains("DeviceGain"))
ui->params_cfg_table->item(row, 2)->setText(channel_config_info["DeviceGain"].toString());
if (channel_config_info.contains("DeviceGainSelectIndex"))
ui->params_cfg_table->item(row, 2)->setData(Qt::UserRole, channel_config_info["DeviceGainSelectIndex"]);
if (channel_config_info.contains("SoftGain"))
ui->params_cfg_table->item(row, 3)->setText(channel_config_info["SoftGain"].toString());
if (channel_config_info.contains("TimeConst"))
ui->params_cfg_table->item(row, 4)->setText(channel_config_info["TimeConst"].toString());
if (channel_config_info.contains("DcOffset"))
ui->params_cfg_table->item(row, 5)->setText(channel_config_info["DcOffset"].toString());
if (channel_config_info.contains("RiseTime"))
ui->params_cfg_table->item(row, 6)->setText(channel_config_info["RiseTime"].toString());
if (channel_config_info.contains("FlatTime"))
ui->params_cfg_table->item(row, 7)->setText(channel_config_info["FlatTime"].toString());
if (channel_config_info.contains("MaxEnergy"))
ui->params_cfg_table->item(row, 8)->setText(channel_config_info["MaxEnergy"].toString());
}
}
void DeviceParamsTableForm::onAllSelectBtnClicked()
{
for (int row = 0; row < 32; ++row) {
if (ui->params_cfg_table->isRowHidden(row))
continue;
ui->params_cfg_table->item(row, 0)->setCheckState(Qt::Checked);
}
}
void DeviceParamsTableForm::onReserveSelectBtnClicked()
{
for (int row = 0; row < 32; ++row) {
if (ui->params_cfg_table->isRowHidden(row))
continue;
auto item = ui->params_cfg_table->item(row, 0);
Qt::CheckState check_state = item->checkState() == Qt::Checked ? Qt::Unchecked : Qt::Checked;
item->setCheckState(check_state);
}
}
void DeviceParamsTableForm::onCfgChannelSelectBtnClicked()
{
QDialog cfg_channel_select_dlg(this, Qt::Dialog | Qt::WindowCloseButtonHint);
cfg_channel_select_dlg.setWindowTitle(QString(QStringLiteral(u"设备测量参数配置通道选择")));
cfg_channel_select_dlg.setWindowModality(Qt::WindowModal);
cfg_channel_select_dlg.setModal(true);
QVBoxLayout* layout = new QVBoxLayout(&cfg_channel_select_dlg);
// 自动计算多列排布
int num_columns = std::sqrt(32);
if (num_columns == 0)
num_columns = 1;
QVBoxLayout* checkbox_layout = new QVBoxLayout();
QHBoxLayout* checkbox_column_layout = new QHBoxLayout();
for (int row = 0; row < 32; ++row) {
QCheckBox* check_box = new QCheckBox(QStringLiteral(u"通道%1").arg(row + 1));
check_box->setChecked(!ui->params_cfg_table->isRowHidden(row));
checkbox_column_layout->addWidget(check_box);
connect(check_box, &QCheckBox::stateChanged, [this, row](int state) {
ui->params_cfg_table->setRowHidden(row, state == Qt::Unchecked);
});
if (checkbox_column_layout->count() >= num_columns) {
checkbox_layout->addLayout(checkbox_column_layout);
checkbox_column_layout = new QHBoxLayout();
}
}
if (checkbox_column_layout->count() < num_columns) {
checkbox_column_layout->addStretch();
}
checkbox_layout->addLayout(checkbox_column_layout);
// 全选和反选
QHBoxLayout* button_layout = new QHBoxLayout();
QPushButton* btn_all_select = new QPushButton(QString(QStringLiteral(u"全选")));
connect(btn_all_select, &QPushButton::clicked, [this]() {
for (int row = 0; row < 32; ++row) {
ui->params_cfg_table->setRowHidden(row, true);
}
});
button_layout->addWidget(btn_all_select);
QPushButton* btn_reserve_select = new QPushButton(QString(QStringLiteral(u"反选")));
connect(btn_reserve_select, &QPushButton::clicked, [this]() {
for (int row = 0; row < 32; ++row) {
ui->params_cfg_table->setRowHidden(row, !ui->params_cfg_table->isRowHidden(row));
}
});
button_layout->addWidget(btn_reserve_select);
layout->addLayout(button_layout);
layout->addLayout(checkbox_layout);
cfg_channel_select_dlg.exec();
}
void DeviceParamsTableForm::onCurrentChannelToOther()
{
int current_row = ui->params_cfg_table->currentRow();
for (int row = 0; row < 32; ++row) {
if (ui->params_cfg_table->isRowHidden(row))
continue;
for (int col = 1; col < ui->params_cfg_table->columnCount(); ++col) {
const QString& current_value = ui->params_cfg_table->item(current_row, col)->text();
ui->params_cfg_table->item(row, col)->setText(current_value);
}
}
}
void DeviceParamsTableForm::onSaveSelectedChannelConfig()
{
QVariantList device_channel_params_list;
for (int row = 0; row < 32; ++row) {
if (ui->params_cfg_table->item(row, 0)->checkState() == Qt::Checked) {
QVariantMap channel_info = ui->params_cfg_table->item(row, 0)->data(Qt::UserRole).toMap();
int board_id = channel_info["board_id"].toInt();
int channel_id = channel_info["channel_id"].toInt();
int addr_count = ui->params_cfg_table->item(row, 1)->text().toInt();
int device_gain = ui->params_cfg_table->item(row, 2)->text().toInt();
int device_gain_index = ui->params_cfg_table->item(row, 2)->data(Qt::UserRole).toInt();
uint soft_gain = ui->params_cfg_table->item(row, 3)->text().toUInt();
int time_const = ui->params_cfg_table->item(row, 4)->text().toInt();
int dc_offset = ui->params_cfg_table->item(row, 5)->text().toInt();
int rise_time = ui->params_cfg_table->item(row, 6)->text().toInt();
int flat_time = ui->params_cfg_table->item(row, 7)->text().toInt();
int max_energy = ui->params_cfg_table->item(row, 8)->text().toInt();
QVariantMap device_channel_params;
device_channel_params["BoardId"] = board_id;
device_channel_params["ChannelId"] = channel_id;
device_channel_params["AddrCount"] = addr_count;
device_channel_params["DeviceGain"] = device_gain;
device_channel_params["DeviceGainSelectIndex"] = device_gain_index;
device_channel_params["SoftGain"] = soft_gain;
device_channel_params["TimeConst"] = time_const;
device_channel_params["DcOffset"] = dc_offset;
device_channel_params["RiseTime"] = rise_time;
device_channel_params["FlatTime"] = flat_time;
device_channel_params["MaxEnergy"] = max_energy;
device_channel_params_list.append(device_channel_params);
}
}
if (device_channel_params_list.isEmpty()) {
QMessageBox::information(this, QStringLiteral(u"提示"), QStringLiteral(u"请选择需要保存的测量设备通道配置参数!"));
return;
}
QVariantMap device_config_info;
device_config_info["ChannelConfig"] = device_channel_params_list;
QJsonDocument json_doc = QJsonDocument::fromVariant(device_config_info);
QFile json_file(this->_config_filename);
if (!json_file.open(QIODevice::WriteOnly | QIODevice::Text)) {
return;
}
json_file.write(json_doc.toJson());
json_file.close();
LOG_INFO(QStringLiteral(u"测量设备参数配置保存完成."));
emit deviceConfigParamsSaved(this->_config_filename);
}