From 2451ae76be8d9182dae7ea8d814a78a1435d2194 Mon Sep 17 00:00:00 2001 From: taynpg Date: Thu, 3 Apr 2025 13:39:36 +0800 Subject: [PATCH] =?UTF-8?q?fix=EF=BC=9A=E4=BF=AE=E6=AD=A3=E9=85=8D?= =?UTF-8?q?=E7=BD=AE=E5=88=A0=E9=99=A4=E4=B8=8D=E5=AE=8C=E5=85=A8=E9=97=AE?= =?UTF-8?q?=E9=A2=98=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .clang-format | 2 +- CMakeLists.txt | 2 +- MainWidget.cpp | 102 +++++++++++++++-------------------------------- conf_setting.cpp | 19 ++++----- src/config.cpp | 10 +---- 5 files changed, 46 insertions(+), 89 deletions(-) diff --git a/.clang-format b/.clang-format index da60dac..bc56c6e 100644 --- a/.clang-format +++ b/.clang-format @@ -11,7 +11,7 @@ ReflowComments: true SpacesBeforeTrailingComments: 3 TabWidth: 4 ConstructorInitializerAllOnOneLineOrOnePerLine: true -ColumnLimit: 80 +ColumnLimit: 130 AllowShortBlocksOnASingleLine: Never AllowShortFunctionsOnASingleLine: None AllowShortEnumsOnASingleLine: false diff --git a/CMakeLists.txt b/CMakeLists.txt index c0fd1c0..9adb92f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -52,7 +52,7 @@ if (MSVC) # if(${QT_VERSION_MAJOR} LESS 6) # add_compile_options(/source-charset:utf-8) # endif() -#add_compile_options(/source-charset:utf-8) +add_compile_options(/utf-8) add_compile_options(/EHsc) add_compile_options(/wd4267) add_compile_options(-D_CRT_SECURE_NO_WARNINGS) diff --git a/MainWidget.cpp b/MainWidget.cpp index 2f757b2..233c260 100644 --- a/MainWidget.cpp +++ b/MainWidget.cpp @@ -16,8 +16,7 @@ constexpr std::size_t g_OnePage = 100; namespace fs = std::filesystem; -MainWidget::MainWidget(QWidget* parent) - : QWidget(parent), ui(new Ui::MainWidget) +MainWidget::MainWidget(QWidget* parent) : QWidget(parent), ui(new Ui::MainWidget) { ui->setupUi(this); @@ -54,33 +53,23 @@ MainWidget::MainWidget(QWidget* parent) ui->btnResort->setEnabled(false); connect(ui->btnSelectFile, &QPushButton::clicked, this, [&]() { - QString file = CUtil::select_file(this, u8"请选择xml文件", - u8"XML(*.xml);;所有文件 (*)"); + QString file = CUtil::select_file(this, u8"请选择xml文件", u8"XML(*.xml);;所有文件 (*)"); if (file.isEmpty()) { return; } ui->edStatus->setText(file); }); - connect(ui->btnSearch, &QPushButton::clicked, this, - [&]() { search(ui->edSearchKey->text()); }); - connect(ui->btnBackup, &QPushButton::clicked, this, - [&]() { backup_file(); }); - connect(ui->btnFormat, &QPushButton::clicked, this, - [&]() { format_xml(); }); - connect(ui->btnRead, &QPushButton::clicked, this, - [&]() { read(ui->edStatus->text().trimmed()); }); + connect(ui->btnSearch, &QPushButton::clicked, this, [&]() { search(ui->edSearchKey->text()); }); + connect(ui->btnBackup, &QPushButton::clicked, this, [&]() { backup_file(); }); + connect(ui->btnFormat, &QPushButton::clicked, this, [&]() { format_xml(); }); + connect(ui->btnRead, &QPushButton::clicked, this, [&]() { read(ui->edStatus->text().trimmed()); }); connect(ui->btnSave, &QPushButton::clicked, this, [&]() { save(); }); - 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->btnReplace, &QPushButton::clicked, this, - [&]() { replace_content(true); }); - connect(ui->btnRxReplace, &QPushButton::clicked, this, - [&]() { replace_content(false); }); - connect(ui->cbUnit, &QComboBox::currentTextChanged, this, - [&](const QString& content) { unit_change(); }); - connect(ui->btnExport, &QPushButton::clicked, this, - &MainWidget::copy_multi_data); + connect(ui->btnReplace, &QPushButton::clicked, this, [&]() { replace_content(true); }); + connect(ui->btnRxReplace, &QPushButton::clicked, this, [&]() { replace_content(false); }); + connect(ui->cbUnit, &QComboBox::currentTextChanged, this, [&](const QString& content) { unit_change(); }); + connect(ui->btnExport, &QPushButton::clicked, this, &MainWidget::copy_multi_data); connect(ui->btnPagePre, &QPushButton::clicked, this, [&]() { unsigned int cur = ui->edCurPage->text().toUInt(); push_content(current_, cur - 1); @@ -96,8 +85,7 @@ MainWidget::MainWidget(QWidget* parent) } if (cur_config_.is_same) { - xml_.get_all_elements(vec_, - ui->cbUnit->currentText().toStdString()); + xml_.get_all_elements(vec_, ui->cbUnit->currentText().toStdString()); } else { xml_.get_all_elements(vec_); } @@ -228,10 +216,8 @@ void MainWidget::generate_table_widget() metrics_ = std::make_shared(tab_widget_->font()); tab_widget_->setContextMenuPolicy(Qt::CustomContextMenu); - connect(tab_widget_, &QTableWidget::itemChanged, this, - [&](QTableWidgetItem* item) { item_changed_handle(item); }); - connect(tab_widget_, &QTableWidget::customContextMenuRequested, this, - &MainWidget::show_custom_menu); + connect(tab_widget_, &QTableWidget::itemChanged, this, [&](QTableWidgetItem* item) { item_changed_handle(item); }); + connect(tab_widget_, &QTableWidget::customContextMenuRequested, this, &MainWidget::show_custom_menu); auto keys = CUtil::splitString(cur_config_.propertis, ","); keys_.clear(); @@ -263,8 +249,7 @@ void MainWidget::generate_table_widget() ui->widget->setLayout(lay); } -void MainWidget::push_content(const std::vector& eles, - std::size_t page, bool auto_jump_pre) +void MainWidget::push_content(const std::vector& eles, std::size_t page, bool auto_jump_pre) { if (tab_widget_ == nullptr || page == 0) { return; @@ -452,8 +437,7 @@ void MainWidget::item_changed_handle(QTableWidgetItem* item) if (result == nullptr) { return; } - result->SetAttribute(keys_[col].c_str(), - item->text().toStdString().c_str()); + result->SetAttribute(keys_[col].c_str(), item->text().toStdString().c_str()); } void MainWidget::save() @@ -550,8 +534,7 @@ void MainWidget::ele_update_gui(Element_t* target, const QString& pre_value) QString search_key; if (pre_value.isEmpty()) { - search_key = - QString::fromLocal8Bit(target->Attribute(keys_[0].c_str())); + search_key = QString::fromLocal8Bit(target->Attribute(keys_[0].c_str())); } else { search_key = pre_value; } @@ -605,10 +588,8 @@ void MainWidget::init_menu() } edit_property(target, cur_item->row(), false); }); - connect(ac_copy_curline_, &QAction::triggered, this, - [&]() { copy_select_line(); }); - connect(ac_del_curline_, &QAction::triggered, this, - [&]() { del_select_line(); }); + connect(ac_copy_curline_, &QAction::triggered, this, [&]() { copy_select_line(); }); + connect(ac_del_curline_, &QAction::triggered, this, [&]() { del_select_line(); }); connect(ac_copy_key_, &QAction::triggered, this, [&]() { copy_key(); }); } @@ -628,10 +609,8 @@ void MainWidget::insert_one_line(Element_t* ele, int row) } QString sda(data); int dwidth = metrics_->horizontalAdvance(sda) + blank_with_; - int need_set_width = - dwidth > allow_max_with_ ? allow_max_with_ : dwidth; - col_with_[i] = - col_with_[i] < need_set_width ? need_set_width : col_with_[i]; + int need_set_width = dwidth > allow_max_with_ ? allow_max_with_ : dwidth; + col_with_[i] = col_with_[i] < need_set_width ? need_set_width : col_with_[i]; wgItem->setText(sda); tab_widget_->setItem(row, i, wgItem); } @@ -737,9 +716,7 @@ void MainWidget::sort_by_repeat(std::vector& vec) turn_vec.emplace_back(item, d); } std::sort(turn_vec.begin(), turn_vec.end(), - [&](const SElement_t& se1, const SElement_t& se2) { - return compare_by_prefix(se1, se2); - }); + [&](const SElement_t& se1, const SElement_t& se2) { return compare_by_prefix(se1, se2); }); vec.clear(); for (const auto& item : turn_vec) { vec.push_back(item.ele); @@ -811,8 +788,7 @@ void MainWidget::replace_content(bool is_common) std::vector vec; if (ui->rbReplaceSelect->isChecked()) { - QModelIndexList indexList = - tab_widget_->selectionModel()->selectedRows(); + QModelIndexList indexList = tab_widget_->selectionModel()->selectedRows(); if (indexList.size() < 1) { CUtil::msg(this, u8"无选择数据"); return; @@ -842,8 +818,7 @@ void MainWidget::replace_content(bool is_common) } } -void MainWidget::replace_str(const QString& pre, const QString& after, - Element_t* ele) +void MainWidget::replace_str(const QString& pre, const QString& after, Element_t* ele) { if (ele == nullptr) { return; @@ -858,8 +833,7 @@ void MainWidget::replace_str(const QString& pre, const QString& after, } } -void MainWidget::replace_str(Element_t* ele, const QString& rg, - const QString& after) +void MainWidget::replace_str(Element_t* ele, const QString& rg, const QString& after) { QRegularExpression rx(rg); if (ele == nullptr) { @@ -876,17 +850,14 @@ void MainWidget::replace_str(Element_t* ele, const QString& rg, } } -void MainWidget::get_related_elements(std::vector& out, - ReplaceArea area) +void MainWidget::get_related_elements(std::vector& out, ReplaceArea area) { assert(tab_widget_); out.clear(); switch (area) { case AREA_ALL_PAGE: { out.resize(current_.size()); - std::transform( - current_.begin(), current_.end(), out.begin(), - [](Element_t* ele) { return new OperElement(ele, 0); }); + std::transform(current_.begin(), current_.end(), out.begin(), [](Element_t* ele) { return new OperElement(ele, 0); }); break; } case AREA_CUR_PAGE: { @@ -898,18 +869,13 @@ void MainWidget::get_related_elements(std::vector& out, } case AREA_ALL: { out.resize(vec_.size()); - std::transform( - vec_.begin(), vec_.end(), out.begin(), - [](Element_t* ele) { return new OperElement(ele, 0); }); + std::transform(vec_.begin(), vec_.end(), out.begin(), [](Element_t* ele) { return new OperElement(ele, 0); }); break; } default: { - QModelIndexList indexList = - tab_widget_->selectionModel()->selectedRows(); + QModelIndexList indexList = tab_widget_->selectionModel()->selectedRows(); for (int i = 0; i < indexList.size(); ++i) { - out.emplace_back( - new OperElement(get_element_by_row(indexList[i].row()), - indexList[i].row())); + out.emplace_back(new OperElement(get_element_by_row(indexList[i].row()), indexList[i].row())); } break; } @@ -923,9 +889,7 @@ void MainWidget::backup_file() } QString time = QDateTime::currentDateTime().toString("yyyy-MMdd-hhmmss"); - if (!xml_.backup_file( - fs::path(exe_path_).parent_path().append("backup").string(), - time.toStdString())) { + if (!xml_.backup_file(fs::path(exe_path_).parent_path().append("backup").string(), time.toStdString())) { CUtil::msg(this, u8"备份失败。"); } else { CUtil::msg(this, u8"备份完成。"); @@ -979,9 +943,7 @@ bool MainWidget::format_xml() return false; } - if (!CUtil::affirm( - this, u8"确认", - u8"重排版内容将会覆盖源文件,请确认是否需要备份,继续?")) { + if (!CUtil::affirm(this, u8"确认", u8"重排版内容将会覆盖源文件,请确认是否需要备份,继续?")) { return false; } diff --git a/conf_setting.cpp b/conf_setting.cpp index 11c620a..7997b95 100644 --- a/conf_setting.cpp +++ b/conf_setting.cpp @@ -3,21 +3,17 @@ #include "public_def.h" #include "ui_conf_setting.h" -CGroupSetting::CGroupSetting(QWidget* parent, CGroupIni* oper) - : QDialog(parent), ui(new Ui::CGroupSetting) +CGroupSetting::CGroupSetting(QWidget* parent, CGroupIni* oper) : QDialog(parent), ui(new Ui::CGroupSetting) { ini_opr_ = oper; ui->setupUi(this); setWindowTitle(u8"配置编辑"); - connect(ui->btnAddConfig, &QPushButton::clicked, this, - [&]() { add_item(); }); - connect(ui->btnDelConfig, &QPushButton::clicked, this, - [&]() { del_item(); }); + connect(ui->btnAddConfig, &QPushButton::clicked, this, [&]() { add_item(); }); + connect(ui->btnDelConfig, &QPushButton::clicked, this, [&]() { del_item(); }); connect(ui->btnExit, &QPushButton::clicked, this, [&]() { close(); }); - connect(ui->cbConfig, &QComboBox::currentTextChanged, this, - [&](const QString& content) { change_ini(); }); + connect(ui->cbConfig, &QComboBox::currentTextChanged, this, [&](const QString& content) { change_ini(); }); } CGroupSetting::~CGroupSetting() @@ -33,13 +29,15 @@ void CGroupSetting::showEvent(QShowEvent* event) void CGroupSetting::add_item() { + if (!CUtil::affirm(this, u8"确认", u8"确认添加/更新吗?")) { + return; + } QString config_name = ui->edConfigName->text(); config_name = config_name.trimmed(); if (config_name.isEmpty()) { CUtil::msg(this, u8"配置名为空"); return; } - // 验证重名 // 添加 @@ -63,6 +61,9 @@ void CGroupSetting::add_item() void CGroupSetting::del_item() { + if (!CUtil::affirm(this, u8"确认", u8"确认删除吗?")) { + return; + } std::string key = ui->cbConfig->currentText().toStdString(); if (!ini_opr_->del_item(key)) { CUtil::msg(this, u8"失败"); diff --git a/src/config.cpp b/src/config.cpp index 17b99c3..76803d0 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -52,8 +52,7 @@ void CGroupIni::get_all_node(StrVec_t& vec) bool CGroupIni::add_item(const OneGroupIni& group) { ini_.SetValue(group.name.c_str(), "MainNodes", group.main_nodes.c_str()); - ini_.SetValue(group.name.c_str(), "RelativeNodes", - group.relative_nodes.c_str()); + ini_.SetValue(group.name.c_str(), "RelativeNodes", group.relative_nodes.c_str()); ini_.SetValue(group.name.c_str(), "ItemKey", group.item_key.c_str()); ini_.SetValue(group.name.c_str(), "Properties", group.propertis.c_str()); ini_.SetLongValue(group.name.c_str(), "MaxColLen", group.max_col_len); @@ -97,12 +96,7 @@ bool CGroupIni::get_item(OneGroupIni& group) bool CGroupIni::del_item(const std::string& key) { - ini_.Delete(key.c_str(), "MainNodes", true); - ini_.Delete(key.c_str(), "RelativeNodes", true); - ini_.Delete(key.c_str(), "ItemKey", true); - ini_.Delete(key.c_str(), "Properties", true); - ini_.Delete(key.c_str(), "MaxColLen", true); - ini_.Delete(key.c_str(), "MaxBlankAdd", true); + ini_.Delete(key.c_str(), nullptr); if (ini_.SaveFile(work_file_.c_str()) != SI_OK) { return false; }