#include "register_comment_dialog.h" #include "services/register_comment_service.h" #include "ui_register_comment_dialog.h" #include #include #include #include namespace { QString fromUtf8(const std::string &value) { return QString::fromUtf8(value.data(), static_cast(value.size())); } std::string toUtf8(const QString &value) { const QByteArray bytes = value.toUtf8(); return std::string(bytes.constData(), static_cast(bytes.size())); } } // namespace RegisterCommentDialog::RegisterCommentDialog( RegisterCommentService &service, QWidget *parent) : QDialog(parent), ui_(std::make_unique()), service_(service) { ui_->setupUi(this); ui_->commentTable->horizontalHeader()->setSectionResizeMode( 0, QHeaderView::ResizeToContents); ui_->commentTable->horizontalHeader()->setSectionResizeMode( 1, QHeaderView::Stretch); ui_->areaComboBox->setItemData(0, static_cast(RegisterArea::M)); ui_->areaComboBox->setItemData(1, static_cast(RegisterArea::D)); connect(ui_->commentTable, &QTableWidget::itemSelectionChanged, this, [this] { loadSelectedComment(); }); connect(ui_->saveButton, &QPushButton::clicked, this, [this] { saveComment(); }); connect(ui_->removeButton, &QPushButton::clicked, this, [this] { removeComment(); }); connect(ui_->buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject); reloadComments(); } RegisterCommentDialog::~RegisterCommentDialog() = default; void RegisterCommentDialog::reloadComments( const RegisterAddress *selected_address) { const auto &comments = service_.comments(); ui_->commentTable->setRowCount(static_cast(comments.size())); int selected_row = -1; for (std::size_t index = 0; index < comments.size(); ++index) { const RegisterComment &comment = comments[index]; const int row = static_cast(index); auto *address_item = new QTableWidgetItem( QString::fromStdString(comment.address.toString())); address_item->setData(Qt::UserRole, static_cast(comment.address.area())); address_item->setData(Qt::UserRole + 1, comment.address.index()); ui_->commentTable->setItem(row, 0, address_item); ui_->commentTable->setItem(row, 1, new QTableWidgetItem(fromUtf8(comment.text))); if (selected_address != nullptr && comment.address == *selected_address) { selected_row = row; } } if (selected_row >= 0) { ui_->commentTable->selectRow(selected_row); } else { ui_->commentTable->clearSelection(); ui_->removeButton->setEnabled(false); } } void RegisterCommentDialog::loadSelectedComment() { const std::optional address = selectedAddress(); const RegisterComment *comment = address.has_value() ? service_.findComment(*address) : nullptr; ui_->removeButton->setEnabled(comment != nullptr); if (comment == nullptr) { return; } ui_->areaComboBox->setCurrentIndex( comment->address.area() == RegisterArea::M ? 0 : 1); ui_->indexSpinBox->setValue(comment->address.index()); ui_->commentEdit->setText(fromUtf8(comment->text)); } void RegisterCommentDialog::saveComment() { const RegisterAddress address = addressFromInputs(); const RegisterCommentResult result = service_.setComment( address, toUtf8(ui_->commentEdit->text())); if (!result.succeeded) { QMessageBox::warning(this, tr("保存注释"), fromUtf8(result.message)); return; } reloadComments(&address); } void RegisterCommentDialog::removeComment() { const std::optional address = selectedAddress(); if (!address.has_value()) { return; } if (QMessageBox::question( this, tr("删除注释"), tr("确定删除当前软元件注释吗?")) != QMessageBox::Yes) { return; } const RegisterCommentResult result = service_.removeComment(*address); if (!result.succeeded) { QMessageBox::warning(this, tr("删除注释"), fromUtf8(result.message)); return; } reloadComments(); } RegisterAddress RegisterCommentDialog::addressFromInputs() const { const RegisterArea area = static_cast( ui_->areaComboBox->currentData().toInt()); return RegisterAddress{area, ui_->indexSpinBox->value()}; } std::optional RegisterCommentDialog::selectedAddress() const { const int row = ui_->commentTable->currentRow(); const QTableWidgetItem *item = row >= 0 ? ui_->commentTable->item(row, 0) : nullptr; if (item == nullptr) { return std::nullopt; } return RegisterAddress{ static_cast(item->data(Qt::UserRole).toInt()), item->data(Qt::UserRole + 1).toInt()}; }