fix:修正配置删除不完全问题。

This commit is contained in:
taynpg 2025-04-03 13:39:36 +08:00
parent 4db3a6b202
commit 2451ae76be
5 changed files with 46 additions and 89 deletions

View File

@ -11,7 +11,7 @@ ReflowComments: true
SpacesBeforeTrailingComments: 3 SpacesBeforeTrailingComments: 3
TabWidth: 4 TabWidth: 4
ConstructorInitializerAllOnOneLineOrOnePerLine: true ConstructorInitializerAllOnOneLineOrOnePerLine: true
ColumnLimit: 80 ColumnLimit: 130
AllowShortBlocksOnASingleLine: Never AllowShortBlocksOnASingleLine: Never
AllowShortFunctionsOnASingleLine: None AllowShortFunctionsOnASingleLine: None
AllowShortEnumsOnASingleLine: false AllowShortEnumsOnASingleLine: false

View File

@ -52,7 +52,7 @@ if (MSVC)
# if(${QT_VERSION_MAJOR} LESS 6) # if(${QT_VERSION_MAJOR} LESS 6)
# add_compile_options(/source-charset:utf-8) # add_compile_options(/source-charset:utf-8)
# endif() # endif()
#add_compile_options(/source-charset:utf-8) add_compile_options(/utf-8)
add_compile_options(/EHsc) add_compile_options(/EHsc)
add_compile_options(/wd4267) add_compile_options(/wd4267)
add_compile_options(-D_CRT_SECURE_NO_WARNINGS) add_compile_options(-D_CRT_SECURE_NO_WARNINGS)

View File

@ -16,8 +16,7 @@
constexpr std::size_t g_OnePage = 100; constexpr std::size_t g_OnePage = 100;
namespace fs = std::filesystem; namespace fs = std::filesystem;
MainWidget::MainWidget(QWidget* parent) MainWidget::MainWidget(QWidget* parent) : QWidget(parent), ui(new Ui::MainWidget)
: QWidget(parent), ui(new Ui::MainWidget)
{ {
ui->setupUi(this); ui->setupUi(this);
@ -54,33 +53,23 @@ MainWidget::MainWidget(QWidget* parent)
ui->btnResort->setEnabled(false); ui->btnResort->setEnabled(false);
connect(ui->btnSelectFile, &QPushButton::clicked, this, [&]() { connect(ui->btnSelectFile, &QPushButton::clicked, this, [&]() {
QString file = CUtil::select_file(this, u8"请选择xml文件", QString file = CUtil::select_file(this, u8"请选择xml文件", u8"XML(*.xml);;所有文件 (*)");
u8"XML(*.xml);;所有文件 (*)");
if (file.isEmpty()) { if (file.isEmpty()) {
return; return;
} }
ui->edStatus->setText(file); ui->edStatus->setText(file);
}); });
connect(ui->btnSearch, &QPushButton::clicked, this, connect(ui->btnSearch, &QPushButton::clicked, this, [&]() { search(ui->edSearchKey->text()); });
[&]() { search(ui->edSearchKey->text()); }); connect(ui->btnBackup, &QPushButton::clicked, this, [&]() { backup_file(); });
connect(ui->btnBackup, &QPushButton::clicked, this, connect(ui->btnFormat, &QPushButton::clicked, this, [&]() { format_xml(); });
[&]() { backup_file(); }); connect(ui->btnRead, &QPushButton::clicked, this, [&]() { read(ui->edStatus->text().trimmed()); });
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->btnSave, &QPushButton::clicked, this, [&]() { save(); });
connect(ui->btnExit, &QPushButton::clicked, this, connect(ui->btnExit, &QPushButton::clicked, this, [&]() { QApplication::exit(0); });
[&]() { QApplication::exit(0); });
connect(ui->btnReset, &QPushButton::clicked, this, &MainWidget::reset); connect(ui->btnReset, &QPushButton::clicked, this, &MainWidget::reset);
connect(ui->btnReplace, &QPushButton::clicked, this, connect(ui->btnReplace, &QPushButton::clicked, this, [&]() { replace_content(true); });
[&]() { replace_content(true); }); connect(ui->btnRxReplace, &QPushButton::clicked, this, [&]() { replace_content(false); });
connect(ui->btnRxReplace, &QPushButton::clicked, this, connect(ui->cbUnit, &QComboBox::currentTextChanged, this, [&](const QString& content) { unit_change(); });
[&]() { replace_content(false); }); connect(ui->btnExport, &QPushButton::clicked, this, &MainWidget::copy_multi_data);
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, [&]() { 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);
@ -96,8 +85,7 @@ MainWidget::MainWidget(QWidget* parent)
} }
if (cur_config_.is_same) { if (cur_config_.is_same) {
xml_.get_all_elements(vec_, xml_.get_all_elements(vec_, ui->cbUnit->currentText().toStdString());
ui->cbUnit->currentText().toStdString());
} else { } else {
xml_.get_all_elements(vec_); xml_.get_all_elements(vec_);
} }
@ -228,10 +216,8 @@ void MainWidget::generate_table_widget()
metrics_ = std::make_shared<QFontMetrics>(tab_widget_->font()); metrics_ = std::make_shared<QFontMetrics>(tab_widget_->font());
tab_widget_->setContextMenuPolicy(Qt::CustomContextMenu); tab_widget_->setContextMenuPolicy(Qt::CustomContextMenu);
connect(tab_widget_, &QTableWidget::itemChanged, this, connect(tab_widget_, &QTableWidget::itemChanged, this, [&](QTableWidgetItem* item) { item_changed_handle(item); });
[&](QTableWidgetItem* item) { item_changed_handle(item); }); connect(tab_widget_, &QTableWidget::customContextMenuRequested, this, &MainWidget::show_custom_menu);
connect(tab_widget_, &QTableWidget::customContextMenuRequested, this,
&MainWidget::show_custom_menu);
auto keys = CUtil::splitString(cur_config_.propertis, ","); auto keys = CUtil::splitString(cur_config_.propertis, ",");
keys_.clear(); keys_.clear();
@ -263,8 +249,7 @@ void MainWidget::generate_table_widget()
ui->widget->setLayout(lay); ui->widget->setLayout(lay);
} }
void MainWidget::push_content(const std::vector<tinyxml2::XMLElement*>& eles, void MainWidget::push_content(const std::vector<tinyxml2::XMLElement*>& eles, std::size_t page, bool auto_jump_pre)
std::size_t page, bool auto_jump_pre)
{ {
if (tab_widget_ == nullptr || page == 0) { if (tab_widget_ == nullptr || page == 0) {
return; return;
@ -452,8 +437,7 @@ void MainWidget::item_changed_handle(QTableWidgetItem* item)
if (result == nullptr) { if (result == nullptr) {
return; return;
} }
result->SetAttribute(keys_[col].c_str(), result->SetAttribute(keys_[col].c_str(), item->text().toStdString().c_str());
item->text().toStdString().c_str());
} }
void MainWidget::save() void MainWidget::save()
@ -550,8 +534,7 @@ void MainWidget::ele_update_gui(Element_t* target, const QString& pre_value)
QString search_key; QString search_key;
if (pre_value.isEmpty()) { if (pre_value.isEmpty()) {
search_key = search_key = QString::fromLocal8Bit(target->Attribute(keys_[0].c_str()));
QString::fromLocal8Bit(target->Attribute(keys_[0].c_str()));
} else { } else {
search_key = pre_value; search_key = pre_value;
} }
@ -605,10 +588,8 @@ void MainWidget::init_menu()
} }
edit_property(target, cur_item->row(), false); edit_property(target, cur_item->row(), false);
}); });
connect(ac_copy_curline_, &QAction::triggered, this, connect(ac_copy_curline_, &QAction::triggered, this, [&]() { copy_select_line(); });
[&]() { copy_select_line(); }); connect(ac_del_curline_, &QAction::triggered, this, [&]() { del_select_line(); });
connect(ac_del_curline_, &QAction::triggered, this,
[&]() { del_select_line(); });
connect(ac_copy_key_, &QAction::triggered, this, [&]() { copy_key(); }); 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); QString sda(data);
int dwidth = metrics_->horizontalAdvance(sda) + blank_with_; int dwidth = metrics_->horizontalAdvance(sda) + blank_with_;
int need_set_width = int need_set_width = dwidth > allow_max_with_ ? allow_max_with_ : dwidth;
dwidth > allow_max_with_ ? allow_max_with_ : dwidth; col_with_[i] = col_with_[i] < need_set_width ? need_set_width : col_with_[i];
col_with_[i] =
col_with_[i] < need_set_width ? need_set_width : col_with_[i];
wgItem->setText(sda); wgItem->setText(sda);
tab_widget_->setItem(row, i, wgItem); tab_widget_->setItem(row, i, wgItem);
} }
@ -737,9 +716,7 @@ void MainWidget::sort_by_repeat(std::vector<Element_t*>& vec)
turn_vec.emplace_back(item, d); turn_vec.emplace_back(item, d);
} }
std::sort(turn_vec.begin(), turn_vec.end(), std::sort(turn_vec.begin(), turn_vec.end(),
[&](const SElement_t& se1, const SElement_t& se2) { [&](const SElement_t& se1, const SElement_t& se2) { return compare_by_prefix(se1, se2); });
return compare_by_prefix(se1, se2);
});
vec.clear(); vec.clear();
for (const auto& item : turn_vec) { for (const auto& item : turn_vec) {
vec.push_back(item.ele); vec.push_back(item.ele);
@ -811,8 +788,7 @@ void MainWidget::replace_content(bool is_common)
std::vector<OperElement*> vec; std::vector<OperElement*> vec;
if (ui->rbReplaceSelect->isChecked()) { if (ui->rbReplaceSelect->isChecked()) {
QModelIndexList indexList = QModelIndexList indexList = tab_widget_->selectionModel()->selectedRows();
tab_widget_->selectionModel()->selectedRows();
if (indexList.size() < 1) { if (indexList.size() < 1) {
CUtil::msg(this, u8"无选择数据"); CUtil::msg(this, u8"无选择数据");
return; return;
@ -842,8 +818,7 @@ void MainWidget::replace_content(bool is_common)
} }
} }
void MainWidget::replace_str(const QString& pre, const QString& after, void MainWidget::replace_str(const QString& pre, const QString& after, Element_t* ele)
Element_t* ele)
{ {
if (ele == nullptr) { if (ele == nullptr) {
return; 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, void MainWidget::replace_str(Element_t* ele, const QString& rg, const QString& after)
const QString& after)
{ {
QRegularExpression rx(rg); QRegularExpression rx(rg);
if (ele == nullptr) { if (ele == nullptr) {
@ -876,17 +850,14 @@ void MainWidget::replace_str(Element_t* ele, const QString& rg,
} }
} }
void MainWidget::get_related_elements(std::vector<OperElement*>& out, void MainWidget::get_related_elements(std::vector<OperElement*>& out, ReplaceArea area)
ReplaceArea area)
{ {
assert(tab_widget_); assert(tab_widget_);
out.clear(); out.clear();
switch (area) { switch (area) {
case AREA_ALL_PAGE: { case AREA_ALL_PAGE: {
out.resize(current_.size()); out.resize(current_.size());
std::transform( std::transform(current_.begin(), current_.end(), out.begin(), [](Element_t* ele) { return new OperElement(ele, 0); });
current_.begin(), current_.end(), out.begin(),
[](Element_t* ele) { return new OperElement(ele, 0); });
break; break;
} }
case AREA_CUR_PAGE: { case AREA_CUR_PAGE: {
@ -898,18 +869,13 @@ void MainWidget::get_related_elements(std::vector<OperElement*>& out,
} }
case AREA_ALL: { case AREA_ALL: {
out.resize(vec_.size()); out.resize(vec_.size());
std::transform( std::transform(vec_.begin(), vec_.end(), out.begin(), [](Element_t* ele) { return new OperElement(ele, 0); });
vec_.begin(), vec_.end(), out.begin(),
[](Element_t* ele) { return new OperElement(ele, 0); });
break; break;
} }
default: { default: {
QModelIndexList indexList = QModelIndexList indexList = tab_widget_->selectionModel()->selectedRows();
tab_widget_->selectionModel()->selectedRows();
for (int i = 0; i < indexList.size(); ++i) { for (int i = 0; i < indexList.size(); ++i) {
out.emplace_back( out.emplace_back(new OperElement(get_element_by_row(indexList[i].row()), indexList[i].row()));
new OperElement(get_element_by_row(indexList[i].row()),
indexList[i].row()));
} }
break; break;
} }
@ -923,9 +889,7 @@ void MainWidget::backup_file()
} }
QString time = QDateTime::currentDateTime().toString("yyyy-MMdd-hhmmss"); QString time = QDateTime::currentDateTime().toString("yyyy-MMdd-hhmmss");
if (!xml_.backup_file( if (!xml_.backup_file(fs::path(exe_path_).parent_path().append("backup").string(), time.toStdString())) {
fs::path(exe_path_).parent_path().append("backup").string(),
time.toStdString())) {
CUtil::msg(this, u8"备份失败。"); CUtil::msg(this, u8"备份失败。");
} else { } else {
CUtil::msg(this, u8"备份完成。"); CUtil::msg(this, u8"备份完成。");
@ -979,9 +943,7 @@ bool MainWidget::format_xml()
return false; return false;
} }
if (!CUtil::affirm( if (!CUtil::affirm(this, u8"确认", u8"重排版内容将会覆盖源文件,请确认是否需要备份,继续?")) {
this, u8"确认",
u8"重排版内容将会覆盖源文件,请确认是否需要备份,继续?")) {
return false; return false;
} }

View File

@ -3,21 +3,17 @@
#include "public_def.h" #include "public_def.h"
#include "ui_conf_setting.h" #include "ui_conf_setting.h"
CGroupSetting::CGroupSetting(QWidget* parent, CGroupIni* oper) CGroupSetting::CGroupSetting(QWidget* parent, CGroupIni* oper) : QDialog(parent), ui(new Ui::CGroupSetting)
: QDialog(parent), ui(new Ui::CGroupSetting)
{ {
ini_opr_ = oper; ini_opr_ = oper;
ui->setupUi(this); ui->setupUi(this);
setWindowTitle(u8"配置编辑"); setWindowTitle(u8"配置编辑");
connect(ui->btnAddConfig, &QPushButton::clicked, this, connect(ui->btnAddConfig, &QPushButton::clicked, this, [&]() { add_item(); });
[&]() { add_item(); }); connect(ui->btnDelConfig, &QPushButton::clicked, this, [&]() { del_item(); });
connect(ui->btnDelConfig, &QPushButton::clicked, this,
[&]() { del_item(); });
connect(ui->btnExit, &QPushButton::clicked, this, [&]() { close(); }); connect(ui->btnExit, &QPushButton::clicked, this, [&]() { close(); });
connect(ui->cbConfig, &QComboBox::currentTextChanged, this, connect(ui->cbConfig, &QComboBox::currentTextChanged, this, [&](const QString& content) { change_ini(); });
[&](const QString& content) { change_ini(); });
} }
CGroupSetting::~CGroupSetting() CGroupSetting::~CGroupSetting()
@ -33,13 +29,15 @@ void CGroupSetting::showEvent(QShowEvent* event)
void CGroupSetting::add_item() void CGroupSetting::add_item()
{ {
if (!CUtil::affirm(this, u8"确认", u8"确认添加/更新吗?")) {
return;
}
QString config_name = ui->edConfigName->text(); QString config_name = ui->edConfigName->text();
config_name = config_name.trimmed(); config_name = config_name.trimmed();
if (config_name.isEmpty()) { if (config_name.isEmpty()) {
CUtil::msg(this, u8"配置名为空"); CUtil::msg(this, u8"配置名为空");
return; return;
} }
// 验证重名 // 验证重名
// 添加 // 添加
@ -63,6 +61,9 @@ void CGroupSetting::add_item()
void CGroupSetting::del_item() void CGroupSetting::del_item()
{ {
if (!CUtil::affirm(this, u8"确认", u8"确认删除吗?")) {
return;
}
std::string key = ui->cbConfig->currentText().toStdString(); std::string key = ui->cbConfig->currentText().toStdString();
if (!ini_opr_->del_item(key)) { if (!ini_opr_->del_item(key)) {
CUtil::msg(this, u8"失败"); CUtil::msg(this, u8"失败");

View File

@ -52,8 +52,7 @@ void CGroupIni::get_all_node(StrVec_t& vec)
bool CGroupIni::add_item(const OneGroupIni& group) 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(), "MainNodes", group.main_nodes.c_str());
ini_.SetValue(group.name.c_str(), "RelativeNodes", ini_.SetValue(group.name.c_str(), "RelativeNodes", group.relative_nodes.c_str());
group.relative_nodes.c_str());
ini_.SetValue(group.name.c_str(), "ItemKey", group.item_key.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_.SetValue(group.name.c_str(), "Properties", group.propertis.c_str());
ini_.SetLongValue(group.name.c_str(), "MaxColLen", group.max_col_len); 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) bool CGroupIni::del_item(const std::string& key)
{ {
ini_.Delete(key.c_str(), "MainNodes", true); ini_.Delete(key.c_str(), nullptr);
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);
if (ini_.SaveFile(work_file_.c_str()) != SI_OK) { if (ini_.SaveFile(work_file_.c_str()) != SI_OK) {
return false; return false;
} }