From 693a371e0d48812b4f20af019e389a4a42c006a1 Mon Sep 17 00:00:00 2001 From: taynpg Date: Sun, 15 Sep 2024 15:08:32 +0800 Subject: [PATCH] =?UTF-8?q?change=EF=BC=9A=E6=A0=BC=E5=BC=8F=E5=8C=96xml?= =?UTF-8?q?=E5=8D=95=E5=85=83=E8=AE=BE=E5=AE=9A=E4=B8=80=E4=B8=AA=E7=AA=97?= =?UTF-8?q?=E5=8F=A3=EF=BC=8C=E5=8F=AF=E4=BB=A5=E9=80=89=E6=8B=A9=E6=80=A7?= =?UTF-8?q?=E6=A0=BC=E5=BC=8F=E5=8C=96=E5=93=AA=E4=BA=9B=E5=85=B7=E4=BD=93?= =?UTF-8?q?=E5=86=85=E5=AE=B9=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CMakeLists.txt | 2 + MainWidget.cpp | 18 +++- form/qformatset.cpp | 87 +++++++++++++++++ form/qformatset.h | 32 +++++++ form/qformatset.ui | 202 +++++++++++++++++++++++++++++++++++++++ src/QCustomQLineEdit.cpp | 4 +- src/xml_opr.cpp | 42 ++++++-- src/xml_opr.h | 6 +- 8 files changed, 376 insertions(+), 17 deletions(-) create mode 100644 form/qformatset.cpp create mode 100644 form/qformatset.h create mode 100644 form/qformatset.ui diff --git a/CMakeLists.txt b/CMakeLists.txt index 57f605a..71fd772 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -31,6 +31,7 @@ find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Widgets) include_directories(3rd) include_directories(src) +include_directories(form) set(PROJECT_SOURCES main.cpp MainWidget.cpp @@ -42,6 +43,7 @@ set(PROJECT_SOURCES src/attribute_edit.cpp flatgray.qrc src/data_edit.h src/data_edit.cpp src/data_edit.ui src/QCustomQLineEdit.h src/QCustomQLineEdit.cpp + form/qformatset.h form/qformatset.cpp form/qformatset.ui ) if (MSVC) diff --git a/MainWidget.cpp b/MainWidget.cpp index 9de37bd..234eca1 100644 --- a/MainWidget.cpp +++ b/MainWidget.cpp @@ -11,6 +11,7 @@ #include "./ui_MainWidget.h" #include "public_def.h" +#include "qformatset.h" #include "src/data_edit.h" constexpr std::size_t g_OnePage = 100; @@ -20,7 +21,7 @@ MainWidget::MainWidget(QWidget* parent) { ui->setupUi(this); - setWindowTitle(u8"OneLevelXmlOpr v1.3.6"); + setWindowTitle(u8"OneLevelXmlOpr v1.3.7"); setWindowIcon(QIcon("://resource/xml.ico")); QScreen* primaryScreen = QGuiApplication::primaryScreen(); @@ -966,16 +967,25 @@ void MainWidget::unit_change() bool MainWidget::format_xml() { - std::string xml_path = ui->edStatus->text().toStdString(); + QFormatSet set; + set.exec(); + + if (!set.isok_) { + return false; + } + + std::string xml_path = set.xml_path_; if (xml_path.empty()) { return false; } - if (!CUtil::affirm(this, u8"确认", u8"重排版内容将会覆盖源文件,请确认是否需要备份,继续?")) { + if (!CUtil::affirm( + this, u8"确认", + u8"重排版内容将会覆盖源文件,请确认是否需要备份,继续?")) { return false; } - if (!xml_.handle_save(xml_path)) { + if (!xml_.handle_save(xml_path, set.values_)) { CUtil::msg(this, u8"重排版内容失败"); return false; } diff --git a/form/qformatset.cpp b/form/qformatset.cpp new file mode 100644 index 0000000..555a46b --- /dev/null +++ b/form/qformatset.cpp @@ -0,0 +1,87 @@ +#include "qformatset.h" + +#include "../public_def.h" +#include "ui_qformatset.h" + +QFormatSet::QFormatSet(QWidget* parent) + : QDialog(parent), ui(new Ui::QFormatSet) +{ + ui->setupUi(this); + connect(ui->btnOk, &QPushButton::clicked, this, [&]() { handle(); }); + connect(ui->btnCancel, &QPushButton::clicked, this, [&]() { + isok_ = false; + close(); + }); + connect(ui->cbAll, &QCheckBox::toggled, this, [&]() { check_select(); }); + connect(ui->btnSelect, &QPushButton::clicked, this, [&]() { + QString file = CUtil::select_file(this, u8"请选择xml文件", + u8"XML(*.xml);;所有文件 (*)"); + if (file.isEmpty()) { + return; + } + ui->edXmlPath->setText(file); + }); +} + +QFormatSet::~QFormatSet() +{ + delete ui; +} + +void QFormatSet::handle() +{ + values_.clear(); + if (ui->cbAdd->isChecked()) { + values_.push_back("+"); + } + if (ui->cbAnd->isChecked()) { + values_.push_back("&&"); + } + if (ui->cbOr->isChecked()) { + values_.push_back("||"); + } + if (ui->cbMul->isChecked()) { + values_.push_back("*"); + } + if (ui->cbSub->isChecked()) { + values_.push_back("-"); + } + if (ui->cbDiv->isChecked()) { + values_.push_back("/"); + } + if (ui->cbLess->isChecked()) { + values_.push_back("<"); + } + if (ui->cbMore->isChecked()) { + values_.push_back(">"); + } + if (ui->cbEq->isChecked()) { + values_.push_back("=="); + } + if (ui->cbNotEq->isChecked()) { + values_.push_back("!="); + } + xml_path_ = ui->edXmlPath->text().toStdString(); + isok_ = true; + close(); +} + +void QFormatSet::check_select() +{ + auto set_check = [&](QCheckBox* ed, bool check) { + if (ed) { + ed->setChecked(check); + } + }; + bool check = ui->cbAll->isChecked(); + set_check(ui->cbAdd, check); + set_check(ui->cbSub, check); + set_check(ui->cbMul, check); + set_check(ui->cbDiv, check); + set_check(ui->cbLess, check); + set_check(ui->cbMore, check); + set_check(ui->cbOr, check); + set_check(ui->cbAnd, check); + set_check(ui->cbEq, check); + set_check(ui->cbNotEq, check); +} diff --git a/form/qformatset.h b/form/qformatset.h new file mode 100644 index 0000000..4278ad7 --- /dev/null +++ b/form/qformatset.h @@ -0,0 +1,32 @@ +#ifndef QFORMATSET_H +#define QFORMATSET_H + +#include +#include + +namespace Ui { +class QFormatSet; +} + +class QFormatSet : public QDialog +{ + Q_OBJECT + +public: + explicit QFormatSet(QWidget *parent = nullptr); + ~QFormatSet(); + +private: + void handle(); + void check_select(); + +private: + Ui::QFormatSet *ui; + +public: + std::vector values_{}; + std::string xml_path_{}; + bool isok_{false}; +}; + +#endif // QFORMATSET_H diff --git a/form/qformatset.ui b/form/qformatset.ui new file mode 100644 index 0000000..3c72fbe --- /dev/null +++ b/form/qformatset.ui @@ -0,0 +1,202 @@ + + + QFormatSet + + + + 0 + 0 + 529 + 200 + + + + 重排版 + + + + + + 操作 + + + + + + + + + + + 选择 + + + + + + + + + + + + 设置值内容指定字符前后空格 + + + + + + + + 与(双&&) + + + + + + + 或(||) + + + + + + + 小于(<) + + + + + + + 大于(>) + + + + + + + Qt::Orientation::Horizontal + + + + 40 + 20 + + + + + + + + + + + + 加(+) + + + + + + + 减(-) + + + + + + + 乘(*) + + + + + + + 除(/) + + + + + + + 等(==) + + + + + + + 不等(!=) + + + + + + + Qt::Orientation::Horizontal + + + + 40 + 20 + + + + + + + + + + + + + + + 全选 + + + + + + + Qt::Orientation::Horizontal + + + + 40 + 20 + + + + + + + + 确认 + + + + + + + 取消 + + + + + + + + + + QCustomQLineEdit + QLineEdit +
QCustomQLineEdit.h
+
+
+ + +
diff --git a/src/QCustomQLineEdit.cpp b/src/QCustomQLineEdit.cpp index 7b4cab4..3b1d45f 100644 --- a/src/QCustomQLineEdit.cpp +++ b/src/QCustomQLineEdit.cpp @@ -10,7 +10,7 @@ void QCustomQLineEdit::dragEnterEvent(QDragEnterEvent* event) if (event->mimeData()->hasUrls()) { event->acceptProposedAction(); } - QLineEdit::dragEnterEvent(event); + //QLineEdit::dragEnterEvent(event); } void QCustomQLineEdit::dragMoveEvent(QDragMoveEvent* event) @@ -28,7 +28,7 @@ void QCustomQLineEdit::dropEvent(QDropEvent* event) event->accept(); } } - QLineEdit::dropEvent(event); + //QLineEdit::dropEvent(event); } QCustomQLineEdit::~QCustomQLineEdit() diff --git a/src/xml_opr.cpp b/src/xml_opr.cpp index 5b2d217..566e68c 100644 --- a/src/xml_opr.cpp +++ b/src/xml_opr.cpp @@ -262,14 +262,33 @@ bool CXmlOpr::import_newer_data(const std::vector& vec, } return true; } -std::string CXmlOpr::handle_space(const std::string& content) +std::string CXmlOpr::handle_space(const std::string& content, + const std::vector& keychars) { std::string result; size_t pos = 0; size_t len = content.length(); + std::ostringstream oss; + for (auto i = size_t(0); i < keychars.size(); ++i) { + if (i > 0) { + oss << "|"; + } + std::string op(keychars[i]); + for (const char& c : op) { + if (c == '|' || c == '\\' || c == '(' || c == ')' || c == '[' || + c == ']' || c == '{' || c == '}' || c == '^' || c == '$' || + c == '.' || c == '*' || c == '+' || c == '?' || c == '|' || + c == '\\') { + oss << '\\'; // 添加转义字符 + } + oss << c; + } + } + // Define a regular expression for the operators - std::regex operators_regex(R"(&&|!=|==|<|>|\|\||\+|-|\*|/)"); + // std::regex operators_regex(R"(&&|!=|==|<|>|\|\||\+|-|\*|/)"); + std::regex operators_regex(oss.str()); while (pos < len) { size_t start_quote = content.find('"', pos); @@ -316,7 +335,8 @@ std::string CXmlOpr::handle_space(const std::string& content) return result; } -bool CXmlOpr::handle_transfer(const std::string& path) +bool CXmlOpr::handle_transfer(const std::string& path, + const std::vector& keychars) { std::ifstream file(path); if (!file.is_open()) { @@ -361,13 +381,18 @@ bool CXmlOpr::handle_transfer(const std::string& path) pos += 1; // Move past the replaced character } - // 处理空格格式化 - std::string sec_handle = handle_space(result); std::ofstream ofile(path); if (!ofile.is_open()) { return false; } - ofile << sec_handle; + + if (keychars.size() < 1) { + ofile << result; + } else { + // 处理空格格式化 + std::string sec_handle = handle_space(result, keychars); + ofile << sec_handle; + } return true; } @@ -410,7 +435,8 @@ bool CXmlOpr::save() return true; } -bool CXmlOpr::handle_save(const std::string& path) +bool CXmlOpr::handle_save(const std::string& path, + const std::vector& keychars) { if (!open(path)) { return false; @@ -419,7 +445,7 @@ bool CXmlOpr::handle_save(const std::string& path) if (ret != tinyxml2::XML_SUCCESS) { return false; } - if (!handle_transfer(xml_path_)) { + if (!handle_transfer(xml_path_, keychars)) { return false; } return true; diff --git a/src/xml_opr.h b/src/xml_opr.h index dfe3e6a..bbf3d34 100644 --- a/src/xml_opr.h +++ b/src/xml_opr.h @@ -37,8 +37,8 @@ public: bool check_key_exists(const Property_t& property); bool check_key_exists(const std::string& key); bool save(); - bool handle_save(const std::string& path); - std::string handle_space(const std::string& content); + bool handle_save(const std::string& path, const std::vector& keychars); + std::string handle_space(const std::string& content, const std::vector& keychars); public: Element_t* copy_element(Element_t* ele); @@ -50,7 +50,7 @@ public: bool import_newer_data(const std::vector& vec, std::size_t& success_count); // 处理转义 - bool handle_transfer(const std::string& path); + bool handle_transfer(const std::string& path, const std::vector& keychars); public: void get_attributes(Element_t* ele, Property_t& vec);