func:添加key字段重复检测。

This commit is contained in:
taynpg 2024-05-16 12:18:20 +08:00
parent 32aa6dd101
commit 726c42f537
5 changed files with 71 additions and 39 deletions

View File

@ -11,7 +11,7 @@ MainWidget::MainWidget(QWidget* parent) : QWidget(parent), ui(new Ui::MainWidget
{ {
ui->setupUi(this); ui->setupUi(this);
setWindowTitle(u8"OneLevelXmlOpr v1.2.2"); setWindowTitle(u8"OneLevelXmlOpr v1.2.3");
setWindowIcon(QIcon("://resource/xml.ico")); setWindowIcon(QIcon("://resource/xml.ico"));
setMinimumWidth(900); 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->btnDelSelectLine, &QPushButton::clicked, this, [&]() { del_select_line(); });
connect(ui->btnExit, &QPushButton::clicked, this, [&]() { QApplication::exit(0); }); connect(ui->btnExit, &QPushButton::clicked, this, [&]() { QApplication::exit(0); });
connect(ui->btnReset, &QPushButton::clicked, this, &MainWidget::reset); 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, [&]() { connect(ui->btnPagePre, &QPushButton::clicked, this, [&]() {
unsigned int cur = ui->edCurPage->text().toUInt(); unsigned int cur = ui->edCurPage->text().toUInt();
push_content(current_, cur - 1); push_content(current_, cur - 1);
@ -75,24 +78,6 @@ void MainWidget::copy_key()
CUtil::msg(this, u8"已复制"); 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) void MainWidget::closeEvent(QCloseEvent* event)
{ {
QSettings settings; QSettings settings;
@ -104,8 +89,7 @@ void MainWidget::closeEvent(QCloseEvent* event)
void MainWidget::keyPressEvent(QKeyEvent* event) void MainWidget::keyPressEvent(QKeyEvent* event)
{ {
switch (event->key()) switch (event->key()) {
{
case Qt::Key_Return: case Qt::Key_Return:
search(); search();
break; break;
@ -325,18 +309,10 @@ void MainWidget::copy_select_line()
} }
Element_t* newer = xml_.copy_element(target); Element_t* newer = xml_.copy_element(target);
Property_t property; if (!edit_property(newer)) {
xml_.get_key_value(newer, property);
attri_edit_->set_attribute(property);
attri_edit_->exec();
if (!attri_edit_->is_ok_) {
return; return;
} }
attri_edit_->get_attribute(property);
xml_.key_value_to_element(newer, property);
xml_.insert_brother_node(target, newer); xml_.insert_brother_node(target, newer);
tab_widget_->insertRow(cur_item->row() + 1); tab_widget_->insertRow(cur_item->row() + 1);
@ -353,6 +329,36 @@ void MainWidget::copy_select_line()
search(); 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) void MainWidget::insert_one_line(Element_t* ele, int row)
{ {
if (ele == nullptr) { if (ele == nullptr) {

View File

@ -38,7 +38,7 @@ private:
void reset(); void reset();
void judge_btn_page(); void judge_btn_page();
void copy_key(); void copy_key();
void edit_property(); bool edit_property(Element_t* target);
protected: protected:
void closeEvent(QCloseEvent* event); void closeEvent(QCloseEvent* event);

View File

@ -6,7 +6,7 @@ CAttributeEdit::CAttributeEdit(QWidget* parent) : QDialog(parent), ui(new Ui::CA
ui->setupUi(this); ui->setupUi(this);
setWindowTitle(u8"属性编辑"); setWindowTitle(u8"属性编辑");
setMinimumSize(400, 600); setMinimumSize(600, 760);
connect(ui->btnOk, &QPushButton::clicked, this, &CAttributeEdit::handle_ok); connect(ui->btnOk, &QPushButton::clicked, this, &CAttributeEdit::handle_ok);
connect(ui->btnCancel, &QPushButton::clicked, this, [&]() { connect(ui->btnCancel, &QPushButton::clicked, this, [&]() {
@ -86,8 +86,8 @@ void CAttributeEdit::init_table()
table_->setHorizontalHeaderLabels(list); table_->setHorizontalHeaderLabels(list);
table_->setSelectionBehavior(QAbstractItemView::SelectRows); table_->setSelectionBehavior(QAbstractItemView::SelectRows);
table_->setSelectionMode(QAbstractItemView::SelectionMode::SingleSelection); table_->setSelectionMode(QAbstractItemView::SelectionMode::SingleSelection);
table_->setColumnWidth(0, 80); table_->setColumnWidth(0, 100);
table_->setColumnWidth(1, 200); table_->setColumnWidth(1, 250);
QHBoxLayout* ly = new QHBoxLayout(); QHBoxLayout* ly = new QHBoxLayout();
ly->addWidget(table_); ly->addWidget(table_);
ui->widget->setLayout(ly); ui->widget->setLayout(ly);

View File

@ -21,6 +21,14 @@ bool CXmlOpr::parse_xml(std::vector<tinyxml2::XMLElement*>& vec)
{ {
std::string next_node{}; std::string next_node{};
std::string node_path = opr_base_.node_path; 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, "/"); auto nodes = splitString(opr_base_.node_path, "/");
for (const auto& item : nodes) { for (const auto& item : nodes) {
@ -71,6 +79,22 @@ void CXmlOpr::del_element(Element_t* ele)
parent_node_->DeleteChild(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() bool CXmlOpr::save()
{ {
auto ret = doc_.SaveFile(xml_path_.c_str()); auto ret = doc_.SaveFile(xml_path_.c_str());

View File

@ -28,6 +28,7 @@ public:
void insert_brother_node(Element_t* brother, Element_t* newer); void insert_brother_node(Element_t* brother, Element_t* newer);
Element_t* copy_element(Element_t* ele); Element_t* copy_element(Element_t* ele);
void del_element(Element_t* ele); void del_element(Element_t* ele);
bool check_key_exists(const Property_t& property);
bool save(); bool save();
public: public:
@ -39,6 +40,7 @@ private:
OprBase opr_base_{}; OprBase opr_base_{};
std::string xml_path_{}; std::string xml_path_{};
Element_t* parent_node_{}; Element_t* parent_node_{};
std::vector<std::string> keys_{};
}; };
#endif #endif