综合平台编程器项目的远程存储
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

256 line
8.3 KiB

  1. #include "alarm_configuration_dialog.h"
  2. #include "domain/project_limits.h"
  3. #include "services/alarm_editor_service.h"
  4. #include "ui_alarm_configuration_dialog.h"
  5. #include <QHeaderView>
  6. #include <QMessageBox>
  7. #include <QTableWidgetItem>
  8. namespace {
  9. QString fromUtf8(const std::string &value)
  10. {
  11. return QString::fromUtf8(value.data(), static_cast<int>(value.size()));
  12. }
  13. std::string toUtf8(const QString &value)
  14. {
  15. const QByteArray bytes = value.toUtf8();
  16. return std::string(bytes.constData(), static_cast<std::size_t>(bytes.size()));
  17. }
  18. QString conditionText(AlarmCondition condition)
  19. {
  20. switch (condition)
  21. {
  22. case AlarmCondition::MOn:
  23. {
  24. return AlarmConfigurationDialog::tr("M 为 ON");
  25. }
  26. case AlarmCondition::MOff:
  27. {
  28. return AlarmConfigurationDialog::tr("M 为 OFF");
  29. }
  30. case AlarmCondition::DHigh:
  31. {
  32. return AlarmConfigurationDialog::tr("D 大于等于阈值");
  33. }
  34. case AlarmCondition::DLow:
  35. {
  36. return AlarmConfigurationDialog::tr("D 小于等于阈值");
  37. }
  38. default:
  39. {
  40. return {};
  41. }
  42. }
  43. }
  44. } // namespace
  45. AlarmConfigurationDialog::AlarmConfigurationDialog(
  46. AlarmEditorService &service,
  47. QWidget *parent)
  48. : QDialog(parent),
  49. ui_(std::make_unique<Ui::AlarmConfigurationDialog>()),
  50. service_(service)
  51. {
  52. ui_->setupUi(this);
  53. ui_->messageEdit->setMaxLength(
  54. static_cast<int>(ProjectLimits::kMaximumAlarmMessageCharacters));
  55. ui_->alarmTable->horizontalHeader()->setSectionResizeMode(
  56. 0, QHeaderView::ResizeToContents);
  57. ui_->alarmTable->horizontalHeader()->setSectionResizeMode(
  58. 1, QHeaderView::ResizeToContents);
  59. ui_->alarmTable->horizontalHeader()->setSectionResizeMode(
  60. 2, QHeaderView::ResizeToContents);
  61. ui_->alarmTable->horizontalHeader()->setSectionResizeMode(
  62. 3, QHeaderView::ResizeToContents);
  63. ui_->alarmTable->horizontalHeader()->setSectionResizeMode(
  64. 4, QHeaderView::Stretch);
  65. ui_->areaComboBox->setItemData(0, static_cast<int>(RegisterArea::M));
  66. ui_->areaComboBox->setItemData(1, static_cast<int>(RegisterArea::D));
  67. connect(ui_->areaComboBox,
  68. QOverload<int>::of(&QComboBox::currentIndexChanged),
  69. this,
  70. [this] { updateConditionOptions(); });
  71. connect(ui_->alarmTable, &QTableWidget::itemSelectionChanged,
  72. this, [this] { loadSelectedDefinition(); });
  73. connect(ui_->addButton, &QPushButton::clicked,
  74. this, [this] { addDefinition(); });
  75. connect(ui_->updateButton, &QPushButton::clicked,
  76. this, [this] { updateDefinition(); });
  77. connect(ui_->removeButton, &QPushButton::clicked,
  78. this, [this] { removeDefinition(); });
  79. connect(ui_->buttonBox, &QDialogButtonBox::rejected,
  80. this, &QDialog::reject);
  81. updateConditionOptions();
  82. reloadDefinitions();
  83. }
  84. AlarmConfigurationDialog::~AlarmConfigurationDialog() = default;
  85. void AlarmConfigurationDialog::reloadDefinitions(const std::string &selected_id)
  86. {
  87. const auto &definitions = service_.definitions();
  88. ui_->alarmTable->setRowCount(static_cast<int>(definitions.size()));
  89. int selected_row = -1;
  90. for (std::size_t index = 0; index < definitions.size(); ++index)
  91. {
  92. const AlarmDefinition &definition = definitions[index];
  93. const int row = static_cast<int>(index);
  94. auto *id_item = new QTableWidgetItem(fromUtf8(definition.id));
  95. id_item->setData(Qt::UserRole, fromUtf8(definition.id));
  96. ui_->alarmTable->setItem(row, 0, id_item);
  97. ui_->alarmTable->setItem(
  98. row, 1, new QTableWidgetItem(QString::fromStdString(
  99. definition.address.toString())));
  100. ui_->alarmTable->setItem(
  101. row, 2, new QTableWidgetItem(conditionText(definition.condition)));
  102. ui_->alarmTable->setItem(
  103. row,
  104. 3,
  105. new QTableWidgetItem(
  106. definition.address.area() == RegisterArea::M
  107. ? QStringLiteral("-")
  108. : QString::number(definition.threshold)));
  109. ui_->alarmTable->setItem(
  110. row, 4, new QTableWidgetItem(fromUtf8(definition.message)));
  111. if (definition.id == selected_id)
  112. {
  113. selected_row = row;
  114. }
  115. }
  116. if (selected_row >= 0)
  117. {
  118. ui_->alarmTable->selectRow(selected_row);
  119. }
  120. else
  121. {
  122. ui_->alarmTable->clearSelection();
  123. ui_->updateButton->setEnabled(false);
  124. ui_->removeButton->setEnabled(false);
  125. }
  126. }
  127. void AlarmConfigurationDialog::loadSelectedDefinition()
  128. {
  129. const AlarmDefinition *definition = service_.findDefinition(selectedId());
  130. const bool selected = definition != nullptr;
  131. ui_->updateButton->setEnabled(selected);
  132. ui_->removeButton->setEnabled(selected);
  133. if (!selected)
  134. {
  135. return;
  136. }
  137. ui_->areaComboBox->setCurrentIndex(
  138. definition->address.area() == RegisterArea::M ? 0 : 1);
  139. updateConditionOptions();
  140. ui_->conditionComboBox->setCurrentIndex(
  141. ui_->conditionComboBox->findData(
  142. static_cast<int>(definition->condition)));
  143. ui_->indexSpinBox->setValue(definition->address.index());
  144. ui_->thresholdSpinBox->setValue(definition->threshold);
  145. ui_->messageEdit->setText(fromUtf8(definition->message));
  146. }
  147. void AlarmConfigurationDialog::updateConditionOptions()
  148. {
  149. const RegisterArea area = static_cast<RegisterArea>(
  150. ui_->areaComboBox->currentData().toInt());
  151. ui_->conditionComboBox->clear();
  152. if (area == RegisterArea::M)
  153. {
  154. ui_->conditionComboBox->addItem(
  155. conditionText(AlarmCondition::MOn),
  156. static_cast<int>(AlarmCondition::MOn));
  157. ui_->conditionComboBox->addItem(
  158. conditionText(AlarmCondition::MOff),
  159. static_cast<int>(AlarmCondition::MOff));
  160. }
  161. else
  162. {
  163. ui_->conditionComboBox->addItem(
  164. conditionText(AlarmCondition::DHigh),
  165. static_cast<int>(AlarmCondition::DHigh));
  166. ui_->conditionComboBox->addItem(
  167. conditionText(AlarmCondition::DLow),
  168. static_cast<int>(AlarmCondition::DLow));
  169. }
  170. ui_->thresholdLabel->setEnabled(area == RegisterArea::D);
  171. ui_->thresholdSpinBox->setEnabled(area == RegisterArea::D);
  172. }
  173. void AlarmConfigurationDialog::addDefinition()
  174. {
  175. const AlarmEditorResult result = service_.addDefinition(definitionFromInputs());
  176. if (!result.succeeded)
  177. {
  178. QMessageBox::warning(this, tr("新增报警"), fromUtf8(result.message));
  179. return;
  180. }
  181. reloadDefinitions(result.id);
  182. }
  183. void AlarmConfigurationDialog::updateDefinition()
  184. {
  185. const std::string id = selectedId();
  186. if (id.empty())
  187. {
  188. return;
  189. }
  190. const AlarmEditorResult result = service_.updateDefinition(
  191. id, definitionFromInputs());
  192. if (!result.succeeded)
  193. {
  194. QMessageBox::warning(this, tr("更新报警"), fromUtf8(result.message));
  195. return;
  196. }
  197. reloadDefinitions(result.id);
  198. }
  199. void AlarmConfigurationDialog::removeDefinition()
  200. {
  201. const std::string id = selectedId();
  202. if (id.empty())
  203. {
  204. return;
  205. }
  206. if (QMessageBox::question(
  207. this, tr("删除报警"), tr("确定删除当前报警定义吗?"))
  208. != QMessageBox::Yes)
  209. {
  210. return;
  211. }
  212. const AlarmEditorResult result = service_.removeDefinition(id);
  213. if (!result.succeeded)
  214. {
  215. QMessageBox::warning(this, tr("删除报警"), fromUtf8(result.message));
  216. return;
  217. }
  218. reloadDefinitions();
  219. }
  220. AlarmDefinition AlarmConfigurationDialog::definitionFromInputs() const
  221. {
  222. AlarmDefinition definition;
  223. const RegisterArea area = static_cast<RegisterArea>(
  224. ui_->areaComboBox->currentData().toInt());
  225. definition.address = RegisterAddress{area, ui_->indexSpinBox->value()};
  226. definition.condition = static_cast<AlarmCondition>(
  227. ui_->conditionComboBox->currentData().toInt());
  228. definition.threshold = static_cast<std::int16_t>(
  229. ui_->thresholdSpinBox->value());
  230. definition.message = toUtf8(ui_->messageEdit->text().trimmed());
  231. return definition;
  232. }
  233. std::string AlarmConfigurationDialog::selectedId() const
  234. {
  235. const int row = ui_->alarmTable->currentRow();
  236. const QTableWidgetItem *item = row >= 0 ? ui_->alarmTable->item(row, 0) : nullptr;
  237. return item == nullptr ? std::string{} : toUtf8(item->data(Qt::UserRole).toString());
  238. }