From 726c42f5372ba34f58f0eb8447e11bb9235bfdb5 Mon Sep 17 00:00:00 2001 From: taynpg Date: Thu, 16 May 2024 12:18:20 +0800 Subject: [PATCH] =?UTF-8?q?func=EF=BC=9A=E6=B7=BB=E5=8A=A0key=E5=AD=97?= =?UTF-8?q?=E6=AE=B5=E9=87=8D=E5=A4=8D=E6=A3=80=E6=B5=8B=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- MainWidget.cpp | 68 +++++++++++++++++++++++------------------- MainWidget.h | 2 +- src/attribute_edit.cpp | 6 ++-- src/xml_opr.cpp | 24 +++++++++++++++ src/xml_opr.h | 10 ++++--- 5 files changed, 71 insertions(+), 39 deletions(-) diff --git a/MainWidget.cpp b/MainWidget.cpp index 7452585..adfa186 100644 --- a/MainWidget.cpp +++ b/MainWidget.cpp @@ -11,7 +11,7 @@ MainWidget::MainWidget(QWidget* parent) : QWidget(parent), ui(new Ui::MainWidget { ui->setupUi(this); - setWindowTitle(u8"OneLevelXmlOpr v1.2.2"); + setWindowTitle(u8"OneLevelXmlOpr v1.2.3"); setWindowIcon(QIcon("://resource/xml.ico")); setMinimumWidth(900); @@ -38,7 +38,10 @@ MainWidget::MainWidget(QWidget* parent) : QWidget(parent), ui(new Ui::MainWidget connect(ui->btnDelSelectLine, &QPushButton::clicked, this, [&]() { del_select_line(); }); connect(ui->btnExit, &QPushButton::clicked, this, [&]() { QApplication::exit(0); }); connect(ui->btnReset, &QPushButton::clicked, this, &MainWidget::reset); - connect(ui->btnEditProperty, &QPushButton::clicked, this, &MainWidget::edit_property); + connect(ui->btnEditProperty, &QPushButton::clicked, this, [&]() { + Element_t* ele = get_current_select_key(); + edit_property(ele); + }); connect(ui->btnPagePre, &QPushButton::clicked, this, [&]() { unsigned int cur = ui->edCurPage->text().toUInt(); push_content(current_, cur - 1); @@ -75,24 +78,6 @@ void MainWidget::copy_key() CUtil::msg(this, u8"已复制"); } -void MainWidget::edit_property() -{ - Element_t* target = get_current_select_key(); - if (target == nullptr) { - return; - } - Property_t property; - xml_.get_key_value(target, property); - attri_edit_->set_attribute(property, false); - attri_edit_->exec(); - - if (!attri_edit_->is_ok_) { - return; - } - attri_edit_->get_attribute(property); - xml_.key_value_to_element(target, property); -} - void MainWidget::closeEvent(QCloseEvent* event) { QSettings settings; @@ -104,8 +89,7 @@ void MainWidget::closeEvent(QCloseEvent* event) void MainWidget::keyPressEvent(QKeyEvent* event) { - switch (event->key()) - { + switch (event->key()) { case Qt::Key_Return: search(); break; @@ -325,18 +309,10 @@ void MainWidget::copy_select_line() } Element_t* newer = xml_.copy_element(target); - Property_t property; - xml_.get_key_value(newer, property); - - attri_edit_->set_attribute(property); - attri_edit_->exec(); - - if (!attri_edit_->is_ok_) { + if (!edit_property(newer)) { return; } - attri_edit_->get_attribute(property); - xml_.key_value_to_element(newer, property); xml_.insert_brother_node(target, newer); tab_widget_->insertRow(cur_item->row() + 1); @@ -353,6 +329,36 @@ void MainWidget::copy_select_line() search(); } +// 返回 true 表示确认编辑了, false 表示取消编辑了。 +bool MainWidget::edit_property(Element_t* target) +{ + if (target == nullptr) { + return false; + } + + Property_t property; + xml_.get_key_value(target, property); + + attri_edit_->set_attribute(property); + attri_edit_->exec(); + + if (!attri_edit_->is_ok_) { + return false; + } + + attri_edit_->get_attribute(property); + while (xml_.check_key_exists(property)) { + CUtil::msg(attri_edit_, u8"不能有相同的key,请检查。"); + attri_edit_->exec(); + attri_edit_->get_attribute(property); + if (!attri_edit_->is_ok_) { + return false; + } + } + xml_.key_value_to_element(target, property); + return true; +} + void MainWidget::insert_one_line(Element_t* ele, int row) { if (ele == nullptr) { diff --git a/MainWidget.h b/MainWidget.h index 9748b7f..eb9540b 100644 --- a/MainWidget.h +++ b/MainWidget.h @@ -38,7 +38,7 @@ private: void reset(); void judge_btn_page(); void copy_key(); - void edit_property(); + bool edit_property(Element_t* target); protected: void closeEvent(QCloseEvent* event); diff --git a/src/attribute_edit.cpp b/src/attribute_edit.cpp index 43cb103..682f1ae 100644 --- a/src/attribute_edit.cpp +++ b/src/attribute_edit.cpp @@ -6,7 +6,7 @@ CAttributeEdit::CAttributeEdit(QWidget* parent) : QDialog(parent), ui(new Ui::CA ui->setupUi(this); setWindowTitle(u8"属性编辑"); - setMinimumSize(400, 600); + setMinimumSize(600, 760); connect(ui->btnOk, &QPushButton::clicked, this, &CAttributeEdit::handle_ok); connect(ui->btnCancel, &QPushButton::clicked, this, [&]() { @@ -86,8 +86,8 @@ void CAttributeEdit::init_table() table_->setHorizontalHeaderLabels(list); table_->setSelectionBehavior(QAbstractItemView::SelectRows); table_->setSelectionMode(QAbstractItemView::SelectionMode::SingleSelection); - table_->setColumnWidth(0, 80); - table_->setColumnWidth(1, 200); + table_->setColumnWidth(0, 100); + table_->setColumnWidth(1, 250); QHBoxLayout* ly = new QHBoxLayout(); ly->addWidget(table_); ui->widget->setLayout(ly); diff --git a/src/xml_opr.cpp b/src/xml_opr.cpp index 75ead07..6bde3ce 100644 --- a/src/xml_opr.cpp +++ b/src/xml_opr.cpp @@ -21,6 +21,14 @@ bool CXmlOpr::parse_xml(std::vector& vec) { std::string next_node{}; std::string node_path = opr_base_.node_path; + keys_.clear(); + auto keys = splitString(opr_base_.purpose, ","); + for (const auto& item : keys) { + if (item.empty()) { + continue; + } + keys_.push_back(item); + } auto nodes = splitString(opr_base_.node_path, "/"); for (const auto& item : nodes) { @@ -71,6 +79,22 @@ void CXmlOpr::del_element(Element_t* ele) parent_node_->DeleteChild(ele); } +bool CXmlOpr::check_key_exists(const Property_t& property) +{ + if (keys_.size() < 1 || property.size() < 1) { + return false; + } + Element_t* purpose_node = parent_node_->FirstChildElement(opr_base_.the_node.c_str()); + while (purpose_node) { + const char* value = purpose_node->Attribute(keys_[0].c_str()); + if (property[0].value == std::string(value)) { + return true; + } + purpose_node = purpose_node->NextSiblingElement(); + } + return false; +} + bool CXmlOpr::save() { auto ret = doc_.SaveFile(xml_path_.c_str()); diff --git a/src/xml_opr.h b/src/xml_opr.h index d7515e1..9f50501 100644 --- a/src/xml_opr.h +++ b/src/xml_opr.h @@ -28,6 +28,7 @@ public: void insert_brother_node(Element_t* brother, Element_t* newer); Element_t* copy_element(Element_t* ele); void del_element(Element_t* ele); + bool check_key_exists(const Property_t& property); bool save(); public: @@ -35,10 +36,11 @@ public: void key_value_to_element(Element_t* ele, const Property_t& vec); private: - tinyxml2::XMLDocument doc_{}; - OprBase opr_base_{}; - std::string xml_path_{}; - Element_t* parent_node_{}; + tinyxml2::XMLDocument doc_{}; + OprBase opr_base_{}; + std::string xml_path_{}; + Element_t* parent_node_{}; + std::vector keys_{}; }; #endif \ No newline at end of file