综合平台编程器项目的远程存储
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.
 
 
 
 

156 lines
4.9 KiB

  1. #include "register_comment_dialog.h"
  2. #include "services/register_comment_service.h"
  3. #include "ui_register_comment_dialog.h"
  4. #include <QHeaderView>
  5. #include <QMessageBox>
  6. #include <QTableWidgetItem>
  7. #include <optional>
  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. } // namespace
  19. RegisterCommentDialog::RegisterCommentDialog(
  20. RegisterCommentService &service,
  21. QWidget *parent)
  22. : QDialog(parent),
  23. ui_(std::make_unique<Ui::RegisterCommentDialog>()),
  24. service_(service)
  25. {
  26. ui_->setupUi(this);
  27. ui_->commentTable->horizontalHeader()->setSectionResizeMode(
  28. 0, QHeaderView::ResizeToContents);
  29. ui_->commentTable->horizontalHeader()->setSectionResizeMode(
  30. 1, QHeaderView::Stretch);
  31. ui_->areaComboBox->setItemData(0, static_cast<int>(RegisterArea::M));
  32. ui_->areaComboBox->setItemData(1, static_cast<int>(RegisterArea::D));
  33. connect(ui_->commentTable, &QTableWidget::itemSelectionChanged,
  34. this, [this] { loadSelectedComment(); });
  35. connect(ui_->saveButton, &QPushButton::clicked,
  36. this, [this] { saveComment(); });
  37. connect(ui_->removeButton, &QPushButton::clicked,
  38. this, [this] { removeComment(); });
  39. connect(ui_->buttonBox, &QDialogButtonBox::rejected,
  40. this, &QDialog::reject);
  41. reloadComments();
  42. }
  43. RegisterCommentDialog::~RegisterCommentDialog() = default;
  44. void RegisterCommentDialog::reloadComments(
  45. const RegisterAddress *selected_address)
  46. {
  47. const auto &comments = service_.comments();
  48. ui_->commentTable->setRowCount(static_cast<int>(comments.size()));
  49. int selected_row = -1;
  50. for (std::size_t index = 0; index < comments.size(); ++index)
  51. {
  52. const RegisterComment &comment = comments[index];
  53. const int row = static_cast<int>(index);
  54. auto *address_item = new QTableWidgetItem(
  55. QString::fromStdString(comment.address.toString()));
  56. address_item->setData(Qt::UserRole, static_cast<int>(comment.address.area()));
  57. address_item->setData(Qt::UserRole + 1, comment.address.index());
  58. ui_->commentTable->setItem(row, 0, address_item);
  59. ui_->commentTable->setItem(row, 1, new QTableWidgetItem(fromUtf8(comment.text)));
  60. if (selected_address != nullptr && comment.address == *selected_address)
  61. {
  62. selected_row = row;
  63. }
  64. }
  65. if (selected_row >= 0)
  66. {
  67. ui_->commentTable->selectRow(selected_row);
  68. }
  69. else
  70. {
  71. ui_->commentTable->clearSelection();
  72. ui_->removeButton->setEnabled(false);
  73. }
  74. }
  75. void RegisterCommentDialog::loadSelectedComment()
  76. {
  77. const std::optional<RegisterAddress> address = selectedAddress();
  78. const RegisterComment *comment = address.has_value()
  79. ? service_.findComment(*address) : nullptr;
  80. ui_->removeButton->setEnabled(comment != nullptr);
  81. if (comment == nullptr)
  82. {
  83. return;
  84. }
  85. ui_->areaComboBox->setCurrentIndex(
  86. comment->address.area() == RegisterArea::M ? 0 : 1);
  87. ui_->indexSpinBox->setValue(comment->address.index());
  88. ui_->commentEdit->setText(fromUtf8(comment->text));
  89. }
  90. void RegisterCommentDialog::saveComment()
  91. {
  92. const RegisterAddress address = addressFromInputs();
  93. const RegisterCommentResult result = service_.setComment(
  94. address, toUtf8(ui_->commentEdit->text()));
  95. if (!result.succeeded)
  96. {
  97. QMessageBox::warning(this, tr("保存注释"), fromUtf8(result.message));
  98. return;
  99. }
  100. reloadComments(&address);
  101. }
  102. void RegisterCommentDialog::removeComment()
  103. {
  104. const std::optional<RegisterAddress> address = selectedAddress();
  105. if (!address.has_value())
  106. {
  107. return;
  108. }
  109. if (QMessageBox::question(
  110. this, tr("删除注释"), tr("确定删除当前软元件注释吗?"))
  111. != QMessageBox::Yes)
  112. {
  113. return;
  114. }
  115. const RegisterCommentResult result = service_.removeComment(*address);
  116. if (!result.succeeded)
  117. {
  118. QMessageBox::warning(this, tr("删除注释"), fromUtf8(result.message));
  119. return;
  120. }
  121. reloadComments();
  122. }
  123. RegisterAddress RegisterCommentDialog::addressFromInputs() const
  124. {
  125. const RegisterArea area = static_cast<RegisterArea>(
  126. ui_->areaComboBox->currentData().toInt());
  127. return RegisterAddress{area, ui_->indexSpinBox->value()};
  128. }
  129. std::optional<RegisterAddress> RegisterCommentDialog::selectedAddress() const
  130. {
  131. const int row = ui_->commentTable->currentRow();
  132. const QTableWidgetItem *item = row >= 0 ? ui_->commentTable->item(row, 0) : nullptr;
  133. if (item == nullptr)
  134. {
  135. return std::nullopt;
  136. }
  137. return RegisterAddress{
  138. static_cast<RegisterArea>(item->data(Qt::UserRole).toInt()),
  139. item->data(Qt::UserRole + 1).toInt()};
  140. }