diff --git a/.clang-format b/.clang-format index fff25a0..c13b964 100644 --- a/.clang-format +++ b/.clang-format @@ -31,7 +31,7 @@ TabWidth: 4 # 构造函数的初始化列表要么都在同一行,要么都各自一行 ConstructorInitializerAllOnOneLineOrOnePerLine: true # 每行字符的限制,0表示没有限制 -ColumnLimit: 100 +ColumnLimit: 110 # 允许短的块放在同一行 AllowShortBlocksOnASingleLine: false # 是否允许短函数在一行 diff --git a/MainWidget.cpp b/MainWidget.cpp index 81749a7..9e54a2a 100644 --- a/MainWidget.cpp +++ b/MainWidget.cpp @@ -54,12 +54,8 @@ MainWidget::MainWidget(QWidget* parent) : QWidget(parent), ui(new Ui::MainWidget connect(ui->btnImport, &QPushButton::clicked, this, [&]() { CDataEdit edit; edit.is_import_ = true; + edit.set_xml_opr(&xml_); edit.exec(); - if (edit.is_import_sucess_) { - CUtil::msg(this, u8"导入成功"); - } else { - CUtil::msg(this, u8"导入失败"); - } }); connect(ui->btnPageNext, &QPushButton::clicked, this, [&]() { unsigned int cur = ui->edCurPage->text().toUInt(); diff --git a/MainWidget.ui b/MainWidget.ui index 36d0cdf..0129f40 100644 --- a/MainWidget.ui +++ b/MainWidget.ui @@ -6,7 +6,7 @@ 0 0 - 985 + 1211 805 @@ -37,13 +37,6 @@ - - - - 备份快照 - - - @@ -74,6 +67,13 @@ + + + + 备份快照 + + + diff --git a/src/data_edit.cpp b/src/data_edit.cpp index c054344..8505098 100644 --- a/src/data_edit.cpp +++ b/src/data_edit.cpp @@ -3,6 +3,8 @@ #include #include #include +#include +#include #include "../public_def.h" CDataEdit::CDataEdit(QWidget* parent) : QDialog(parent), ui(new Ui::CDataEdit) @@ -10,7 +12,7 @@ CDataEdit::CDataEdit(QWidget* parent) : QDialog(parent), ui(new Ui::CDataEdit) ui->setupUi(this); // setMinimumWidth(600); - setWindowTitle(u8"源属性编辑"); + setWindowTitle(u8"源编辑"); connect(ui->btnExit, &QPushButton::clicked, this, [&]() { close(); }); connect(ui->btnAdd, &QPushButton::clicked, this, [&]() { import_data(); }); @@ -30,6 +32,11 @@ void CDataEdit::set_data(const QString& data) data_ = data; } +void CDataEdit::set_xml_opr(CXmlOpr* xml_opr) +{ + xml_opr_ = xml_opr; +} + void CDataEdit::import_data() { QString data = ui->plainTextEdit->toPlainText(); @@ -37,18 +44,24 @@ void CDataEdit::import_data() CUtil::msg(this, u8"内容为空"); return; } + + if (xml_opr_ == nullptr) { + CUtil::msg(this, u8"xml_opr无实例。"); + return; + } + + std::vector valid_data{}; QStringList list = data.trimmed().split("\n"); for (int i = 0; i < list.size(); ++i) { const QString& item = list[i]; if (item.trimmed().isEmpty()) { continue; } - tinyxml2::XMLDocument doc; - doc.Parse(item.toStdString().c_str()); - if (doc.Error()) { - CUtil::msg(this, u8"不是合法的xml语句。"); + if (xml_opr_->check_same_struct (item.toStdString())) { + CUtil::msg(this, u8"不是合法的xml语句或者与现有结构不一致。"); return; } + valid_data.push_back(item.toStdString()); } } diff --git a/src/data_edit.h b/src/data_edit.h index 385d1ea..6bdb012 100644 --- a/src/data_edit.h +++ b/src/data_edit.h @@ -3,6 +3,8 @@ #include +#include "xml_opr.h" + namespace Ui { class CDataEdit; } @@ -18,6 +20,7 @@ public: public: void set_data(const QString& data); void import_data(); + void set_xml_opr(CXmlOpr* xml_opr); protected: void showEvent(QShowEvent* event) override; @@ -30,6 +33,7 @@ public: private: Ui::CDataEdit* ui; QString data_{}; + CXmlOpr* xml_opr_{}; }; #endif // DATA_EDIT_H diff --git a/src/xml_opr.cpp b/src/xml_opr.cpp index b7c1f74..b3770a3 100644 --- a/src/xml_opr.cpp +++ b/src/xml_opr.cpp @@ -101,6 +101,61 @@ Element_t* CXmlOpr::copy_element(Element_t* ele) return ret; } +bool CXmlOpr::check_valid_xml_data(const std::string& data) +{ + tinyxml2::XMLDocument doc; + doc.Parse(data.c_str()); + if (doc.Error()) { + return false; + } + return true; +} + +bool CXmlOpr::check_same_struct(const std::string& data) +{ + auto* own_ele = parent_node_->FirstChildElement(opr_base_.the_node.c_str()); + if (own_ele == nullptr) { + return true; + } + tinyxml2::XMLDocument doc; + doc.Parse(data.c_str()); + if (doc.Error()) { + return false; + } + const auto* import_ele = doc.FirstChildElement(opr_base_.the_node.c_str()); + if (import_ele == nullptr) { + return false; + } + const auto* attribute = own_ele->FirstAttribute(); + int own_cnt = 0; + while (attribute) { + ++own_cnt; + if (import_ele->FindAttribute(attribute->Name()) == nullptr) { + return true; + } + attribute = attribute->Next(); + } + + const auto* attr_import = import_ele->FirstAttribute(); + int import_cnt = 0; + while (attr_import) { + ++import_cnt; + attr_import = attr_import->Next(); + } + + if (import_cnt != own_cnt) { + return false; + } + + return true; +} + +bool CXmlOpr::import_newer_data(const std::vector& vec) +{ + + return false; +} + void CXmlOpr::del_element(Element_t* ele) { parent_node_->DeleteChild(ele); diff --git a/src/xml_opr.h b/src/xml_opr.h index 61dca23..04b890d 100644 --- a/src/xml_opr.h +++ b/src/xml_opr.h @@ -21,15 +21,23 @@ public: ~CXmlOpr(); public: - bool open(const std::string& xml_path); - void set_baseinfo(const OprBase& base); - bool parse_xml(std::vector& vec); - void copy_and_del(std::vector& vec, std::vector& out); - void insert_brother_node(Element_t* brother, Element_t* newer); + bool open(const std::string& xml_path); + void set_baseinfo(const OprBase& base); + bool parse_xml(std::vector& vec); + void copy_and_del(std::vector& vec, std::vector& out); + void insert_brother_node(Element_t* brother, Element_t* newer); + void del_element(Element_t* ele); + bool check_key_exists(const Property_t& property); + bool save(); + +public: Element_t* copy_element(Element_t* ele); - void del_element(Element_t* ele); - bool check_key_exists(const Property_t& property); - bool save(); + // 检查 xml 格式合法性 + bool check_valid_xml_data(const std::string& data); + // 不检查 xml 格式合法性,请自行调用 check_valid_xml_data + bool check_same_struct(const std::string& data); + // 不检查 xml 格式合法性,请自行调用 check_valid_xml_data + bool import_newer_data(const std::vector& vec); public: void get_key_value(Element_t* ele, Property_t& vec);