|
- #include "alarm_configuration_dialog.h"
-
- #include "services/alarm_editor_service.h"
- #include "ui_alarm_configuration_dialog.h"
-
- #include <QHeaderView>
- #include <QMessageBox>
- #include <QTableWidgetItem>
-
- namespace {
-
- QString fromUtf8(const std::string &value)
- {
- return QString::fromUtf8(value.data(), static_cast<int>(value.size()));
- }
-
- std::string toUtf8(const QString &value)
- {
- const QByteArray bytes = value.toUtf8();
- return std::string(bytes.constData(), static_cast<std::size_t>(bytes.size()));
- }
-
- QString conditionText(AlarmCondition condition)
- {
- switch (condition)
- {
- case AlarmCondition::MOn:
- {
- return AlarmConfigurationDialog::tr("M 为 ON");
- }
- case AlarmCondition::DHigh:
- {
- return AlarmConfigurationDialog::tr("D 大于等于阈值");
- }
- case AlarmCondition::DLow:
- {
- return AlarmConfigurationDialog::tr("D 小于等于阈值");
- }
- default:
- {
- return {};
- }
- }
- }
-
- } // namespace
-
- AlarmConfigurationDialog::AlarmConfigurationDialog(
- AlarmEditorService &service,
- QWidget *parent)
- : QDialog(parent),
- ui_(std::make_unique<Ui::AlarmConfigurationDialog>()),
- service_(service)
- {
- ui_->setupUi(this);
- ui_->alarmTable->horizontalHeader()->setSectionResizeMode(
- 0, QHeaderView::ResizeToContents);
- ui_->alarmTable->horizontalHeader()->setSectionResizeMode(
- 1, QHeaderView::ResizeToContents);
- ui_->alarmTable->horizontalHeader()->setSectionResizeMode(
- 2, QHeaderView::ResizeToContents);
- ui_->alarmTable->horizontalHeader()->setSectionResizeMode(
- 3, QHeaderView::ResizeToContents);
- ui_->alarmTable->horizontalHeader()->setSectionResizeMode(
- 4, QHeaderView::Stretch);
- ui_->areaComboBox->setItemData(0, static_cast<int>(RegisterArea::M));
- ui_->areaComboBox->setItemData(1, static_cast<int>(RegisterArea::D));
- connect(ui_->areaComboBox,
- QOverload<int>::of(&QComboBox::currentIndexChanged),
- this,
- [this] { updateConditionOptions(); });
- connect(ui_->alarmTable, &QTableWidget::itemSelectionChanged,
- this, [this] { loadSelectedDefinition(); });
- connect(ui_->addButton, &QPushButton::clicked,
- this, [this] { addDefinition(); });
- connect(ui_->updateButton, &QPushButton::clicked,
- this, [this] { updateDefinition(); });
- connect(ui_->removeButton, &QPushButton::clicked,
- this, [this] { removeDefinition(); });
- connect(ui_->buttonBox, &QDialogButtonBox::rejected,
- this, &QDialog::reject);
- updateConditionOptions();
- reloadDefinitions();
- }
-
- AlarmConfigurationDialog::~AlarmConfigurationDialog() = default;
-
- void AlarmConfigurationDialog::reloadDefinitions(const std::string &selected_id)
- {
- const auto &definitions = service_.definitions();
- ui_->alarmTable->setRowCount(static_cast<int>(definitions.size()));
- int selected_row = -1;
- for (std::size_t index = 0; index < definitions.size(); ++index)
- {
- const AlarmDefinition &definition = definitions[index];
- const int row = static_cast<int>(index);
- auto *id_item = new QTableWidgetItem(fromUtf8(definition.id));
- id_item->setData(Qt::UserRole, fromUtf8(definition.id));
- ui_->alarmTable->setItem(row, 0, id_item);
- ui_->alarmTable->setItem(
- row, 1, new QTableWidgetItem(QString::fromStdString(
- definition.address.toString())));
- ui_->alarmTable->setItem(
- row, 2, new QTableWidgetItem(conditionText(definition.condition)));
- ui_->alarmTable->setItem(
- row,
- 3,
- new QTableWidgetItem(
- definition.condition == AlarmCondition::MOn
- ? QStringLiteral("-")
- : QString::number(definition.threshold)));
- ui_->alarmTable->setItem(
- row, 4, new QTableWidgetItem(fromUtf8(definition.message)));
- if (definition.id == selected_id)
- {
- selected_row = row;
- }
- }
- if (selected_row >= 0)
- {
- ui_->alarmTable->selectRow(selected_row);
- }
- else
- {
- ui_->alarmTable->clearSelection();
- ui_->updateButton->setEnabled(false);
- ui_->removeButton->setEnabled(false);
- }
- }
-
- void AlarmConfigurationDialog::loadSelectedDefinition()
- {
- const AlarmDefinition *definition = service_.findDefinition(selectedId());
- const bool selected = definition != nullptr;
- ui_->updateButton->setEnabled(selected);
- ui_->removeButton->setEnabled(selected);
- if (!selected)
- {
- return;
- }
- ui_->areaComboBox->setCurrentIndex(
- definition->address.area() == RegisterArea::M ? 0 : 1);
- updateConditionOptions();
- ui_->conditionComboBox->setCurrentIndex(
- ui_->conditionComboBox->findData(
- static_cast<int>(definition->condition)));
- ui_->indexSpinBox->setValue(definition->address.index());
- ui_->thresholdSpinBox->setValue(definition->threshold);
- ui_->messageEdit->setText(fromUtf8(definition->message));
- }
-
- void AlarmConfigurationDialog::updateConditionOptions()
- {
- const RegisterArea area = static_cast<RegisterArea>(
- ui_->areaComboBox->currentData().toInt());
- ui_->conditionComboBox->clear();
- if (area == RegisterArea::M)
- {
- ui_->conditionComboBox->addItem(
- conditionText(AlarmCondition::MOn),
- static_cast<int>(AlarmCondition::MOn));
- }
- else
- {
- ui_->conditionComboBox->addItem(
- conditionText(AlarmCondition::DHigh),
- static_cast<int>(AlarmCondition::DHigh));
- ui_->conditionComboBox->addItem(
- conditionText(AlarmCondition::DLow),
- static_cast<int>(AlarmCondition::DLow));
- }
- ui_->thresholdLabel->setEnabled(area == RegisterArea::D);
- ui_->thresholdSpinBox->setEnabled(area == RegisterArea::D);
- }
-
- void AlarmConfigurationDialog::addDefinition()
- {
- const AlarmEditorResult result = service_.addDefinition(definitionFromInputs());
- if (!result.succeeded)
- {
- QMessageBox::warning(this, tr("新增报警"), fromUtf8(result.message));
- return;
- }
- reloadDefinitions(result.id);
- }
-
- void AlarmConfigurationDialog::updateDefinition()
- {
- const std::string id = selectedId();
- if (id.empty())
- {
- return;
- }
- const AlarmEditorResult result = service_.updateDefinition(
- id, definitionFromInputs());
- if (!result.succeeded)
- {
- QMessageBox::warning(this, tr("更新报警"), fromUtf8(result.message));
- return;
- }
- reloadDefinitions(result.id);
- }
-
- void AlarmConfigurationDialog::removeDefinition()
- {
- const std::string id = selectedId();
- if (id.empty())
- {
- return;
- }
- if (QMessageBox::question(
- this, tr("删除报警"), tr("确定删除当前报警定义吗?"))
- != QMessageBox::Yes)
- {
- return;
- }
- const AlarmEditorResult result = service_.removeDefinition(id);
- if (!result.succeeded)
- {
- QMessageBox::warning(this, tr("删除报警"), fromUtf8(result.message));
- return;
- }
- reloadDefinitions();
- }
-
- AlarmDefinition AlarmConfigurationDialog::definitionFromInputs() const
- {
- AlarmDefinition definition;
- const RegisterArea area = static_cast<RegisterArea>(
- ui_->areaComboBox->currentData().toInt());
- definition.address = RegisterAddress{area, ui_->indexSpinBox->value()};
- definition.condition = static_cast<AlarmCondition>(
- ui_->conditionComboBox->currentData().toInt());
- definition.threshold = static_cast<std::int16_t>(
- ui_->thresholdSpinBox->value());
- definition.message = toUtf8(ui_->messageEdit->text().trimmed());
- return definition;
- }
-
- std::string AlarmConfigurationDialog::selectedId() const
- {
- const int row = ui_->alarmTable->currentRow();
- const QTableWidgetItem *item = row >= 0 ? ui_->alarmTable->item(row, 0) : nullptr;
- return item == nullptr ? std::string{} : toUtf8(item->data(Qt::UserRole).toString());
- }
|