2024-05-18 02:12:19 +08:00
|
|
|
#include "data_edit.h"
|
|
|
|
#include "ui_data_edit.h"
|
|
|
|
#include <QScreen>
|
|
|
|
#include <QClipboard>
|
|
|
|
#include <tinyxml2.h>
|
2024-05-19 00:48:03 +08:00
|
|
|
#include <vector>
|
|
|
|
#include <string>
|
2024-05-18 02:12:19 +08:00
|
|
|
#include "../public_def.h"
|
|
|
|
|
|
|
|
CDataEdit::CDataEdit(QWidget* parent) : QDialog(parent), ui(new Ui::CDataEdit)
|
|
|
|
{
|
|
|
|
ui->setupUi(this);
|
|
|
|
|
|
|
|
// setMinimumWidth(600);
|
2024-05-19 00:48:03 +08:00
|
|
|
setWindowTitle(u8"源编辑");
|
2024-05-18 02:12:19 +08:00
|
|
|
|
|
|
|
connect(ui->btnExit, &QPushButton::clicked, this, [&]() { close(); });
|
|
|
|
connect(ui->btnAdd, &QPushButton::clicked, this, [&]() { import_data(); });
|
|
|
|
connect(ui->btnCopy, &QPushButton::clicked, this, [&]() {
|
|
|
|
QClipboard* clip = QApplication::clipboard();
|
|
|
|
clip->setText(ui->plainTextEdit->toPlainText());
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
CDataEdit::~CDataEdit()
|
|
|
|
{
|
|
|
|
delete ui;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CDataEdit::set_data(const QString& data)
|
|
|
|
{
|
|
|
|
data_ = data;
|
|
|
|
}
|
|
|
|
|
2024-05-19 00:48:03 +08:00
|
|
|
void CDataEdit::set_xml_opr(CXmlOpr* xml_opr)
|
|
|
|
{
|
|
|
|
xml_opr_ = xml_opr;
|
|
|
|
}
|
|
|
|
|
2024-05-18 02:12:19 +08:00
|
|
|
void CDataEdit::import_data()
|
|
|
|
{
|
|
|
|
QString data = ui->plainTextEdit->toPlainText();
|
|
|
|
if (data.trimmed().isEmpty()) {
|
|
|
|
CUtil::msg(this, u8"内容为空");
|
|
|
|
return;
|
|
|
|
}
|
2024-05-19 00:48:03 +08:00
|
|
|
|
|
|
|
if (xml_opr_ == nullptr) {
|
|
|
|
CUtil::msg(this, u8"xml_opr无实例。");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::string> valid_data{};
|
2024-05-19 16:50:22 +08:00
|
|
|
QStringList list = data.trimmed().split("\n");
|
2024-05-18 02:12:19 +08:00
|
|
|
for (int i = 0; i < list.size(); ++i) {
|
|
|
|
const QString& item = list[i];
|
|
|
|
if (item.trimmed().isEmpty()) {
|
|
|
|
continue;
|
|
|
|
}
|
2024-05-19 21:02:50 +08:00
|
|
|
if (!xml_opr_->check_same_struct(item.toStdString())) {
|
2024-05-19 00:48:03 +08:00
|
|
|
CUtil::msg(this, u8"不是合法的xml语句或者与现有结构不一致。");
|
2024-05-18 02:12:19 +08:00
|
|
|
return;
|
|
|
|
}
|
2024-05-19 00:48:03 +08:00
|
|
|
valid_data.push_back(item.toStdString());
|
2024-05-18 02:12:19 +08:00
|
|
|
}
|
2024-05-19 16:50:22 +08:00
|
|
|
auto task_count = valid_data.size();
|
|
|
|
std::size_t success_count{};
|
|
|
|
if (!xml_opr_->import_newer_data(valid_data, success_count)) {
|
|
|
|
CUtil::msg(this, u8"导入失败。");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
QString info = QString(u8"需要导入 %1 条数据,成功导入 %2 条。").arg(task_count).arg(success_count);
|
|
|
|
CUtil::msg(this, info);
|
2024-05-19 21:02:50 +08:00
|
|
|
close();
|
2024-05-18 02:12:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CDataEdit::showEvent(QShowEvent* event)
|
|
|
|
{
|
|
|
|
show_before();
|
|
|
|
QDialog::showEvent(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CDataEdit::show_before()
|
|
|
|
{
|
|
|
|
QScreen* primaryScreen = QGuiApplication::primaryScreen();
|
|
|
|
QRect screenGeometry = primaryScreen->geometry();
|
|
|
|
setMinimumWidth(screenGeometry.width() * 0.8);
|
|
|
|
if (is_import_) {
|
|
|
|
ui->btnCopy->setVisible(false);
|
|
|
|
} else {
|
|
|
|
ui->btnAdd->setVisible(false);
|
|
|
|
ui->plainTextEdit->clear();
|
|
|
|
ui->plainTextEdit->appendPlainText(data_);
|
|
|
|
}
|
|
|
|
}
|