geopro/src/app/ExportDatasetDialog.cpp

113 lines
4.2 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "ExportDatasetDialog.hpp"
#include <utility>
#include <QComboBox>
#include <QFormLayout>
#include <QHBoxLayout>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <QLabel>
#include <QMessageBox>
#include <QPushButton>
#include <QVBoxLayout>
#include "Theme.hpp"
#include "api/NavLoads.hpp"
#include "api/NavRequest.hpp"
#include "repo/IAsyncProjectRepository.hpp"
namespace geopro::app {
ExportDatasetDialog::ExportDatasetDialog(geopro::data::IAsyncProjectRepository& repo,
QString dsObjectId, QString tmTypeBaseConfId,
QWidget* parent)
: QDialog(parent), repo_(repo), dsObjectId_(std::move(dsObjectId)),
tmTypeBaseConfId_(std::move(tmTypeBaseConfId)) {
setModal(true);
setWindowTitle(QStringLiteral("导出数据集"));
setMinimumWidth(geopro::app::scaledPx(400));
auto* lay = new QVBoxLayout(this);
lay->setContentsMargins(geopro::app::space::kLg, geopro::app::space::kMd,
geopro::app::space::kLg, geopro::app::space::kMd);
lay->setSpacing(geopro::app::space::kMd);
auto* fl = new QFormLayout();
templateCombo_ = new QComboBox(this);
fl->addRow(QStringLiteral("导出模板"), templateCombo_);
lay->addLayout(fl);
status_ = new QLabel(QStringLiteral("加载模板…"), this);
geopro::app::applyTokenizedStyleSheet(status_, QStringLiteral("color:{{text/disabled}};"));
lay->addWidget(status_);
auto* btnRow = new QHBoxLayout();
btnRow->addStretch();
auto* cancel = new QPushButton(QStringLiteral("取消"), this);
okBtn_ = new QPushButton(QStringLiteral("导出"), this);
okBtn_->setDefault(true);
okBtn_->setEnabled(false);
btnRow->addWidget(cancel);
btnRow->addWidget(okBtn_);
lay->addLayout(btnRow);
QObject::connect(cancel, &QPushButton::clicked, this, &QDialog::reject);
QObject::connect(okBtn_, &QPushButton::clicked, this, &ExportDatasetDialog::onConfirm);
loadTemplates();
}
ExportDatasetDialog::~ExportDatasetDialog() {
// 析构时取消在途请求,避免回调命中已销毁的 this。
if (tplReq_) tplReq_->abort();
if (expReq_) expReq_->abort();
}
void ExportDatasetDialog::loadTemplates() {
if (tplReq_) tplReq_->abort();
tplReq_ = repo_.queryExportTemplatesAsync(tmTypeBaseConfId_.toStdString());
QObject::connect(tplReq_, &geopro::data::NavRequest::done, this, [this](const QVariant& v) {
templates_ = qvariant_cast<std::vector<geopro::data::ExportTemplate>>(v);
if (templates_.empty()) {
status_->setText(QStringLiteral("无可用导出模板"));
return;
}
status_->setVisible(false);
for (const auto& t : templates_)
templateCombo_->addItem(QString::fromStdString(t.name), QString::fromStdString(t.id));
okBtn_->setEnabled(true);
});
QObject::connect(tplReq_, &geopro::data::NavRequest::failed, this, [this](const QString& msg) {
status_->setText(QStringLiteral("加载模板失败:%1").arg(msg));
});
}
void ExportDatasetDialog::onConfirm() {
const QString templateId = templateCombo_->currentData().toString();
if (templateId.isEmpty()) {
QMessageBox::warning(this, QStringLiteral("校验"), QStringLiteral("请选择导出模板"));
return;
}
// 模板导出 bodyspec §E.2{ dsObjectIdList:[该ds], templateId }。
QJsonObject body{{QStringLiteral("dsObjectIdList"), QJsonArray{dsObjectId_}},
{QStringLiteral("templateId"), templateId}};
okBtn_->setEnabled(false);
status_->setText(QStringLiteral("导出中…"));
status_->setVisible(true);
if (expReq_) expReq_->abort();
expReq_ = repo_.exportDatasetAsync(QJsonDocument(body).toJson(QJsonDocument::Compact).toStdString());
QObject::connect(expReq_, &geopro::data::NavRequest::done, this, [this](const QVariant&) {
emit exported();
accept();
});
QObject::connect(expReq_, &geopro::data::NavRequest::failed, this, [this](const QString& msg) {
status_->setText(QStringLiteral("导出失败:%1").arg(msg));
okBtn_->setEnabled(true);
});
}
} // namespace geopro::app