Compare commits

..

2 Commits

9 changed files with 168 additions and 157 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,12 +16,11 @@
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);
setWindowTitle(u8"OneLevelXmlOpr v1.3.8"); setWindowTitle(u8"OneLevelXmlOpr v1.3.9");
setWindowIcon(QIcon("://resource/xml.ico")); setWindowIcon(QIcon("://resource/xml.ico"));
QScreen* primaryScreen = QGuiApplication::primaryScreen(); QScreen* primaryScreen = QGuiApplication::primaryScreen();
@ -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_);
} }
@ -108,8 +96,9 @@ MainWidget::MainWidget(QWidget* parent)
}); });
connect(ui->btnSet, &QPushButton::clicked, this, [&]() { connect(ui->btnSet, &QPushButton::clicked, this, [&]() {
QString cur = ui->cbConfig->currentText();
group_set_->exec(); group_set_->exec();
base_init(); base_init(cur);
}); });
connect(ui->btnHis, &QPushButton::clicked, this, [&]() { connect(ui->btnHis, &QPushButton::clicked, this, [&]() {
CUIHistory his(this, his_); CUIHistory his(this, his_);
@ -159,7 +148,7 @@ MainWidget::MainWidget(QWidget* parent)
init_menu(); init_menu();
// 基本处理 // 基本处理
base_init(); base_init("");
} }
void MainWidget::copy_key() void MainWidget::copy_key()
@ -224,14 +213,15 @@ void MainWidget::show_custom_menu()
void MainWidget::generate_table_widget() void MainWidget::generate_table_widget()
{ {
clear_tab_widget();
if (tab_widget_ == nullptr) {
tab_widget_ = new QTableWidget(); tab_widget_ = new QTableWidget();
}
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();
@ -258,13 +248,14 @@ void MainWidget::generate_table_widget()
tab_widget_->setColumnWidth(i, col_with_[i]); tab_widget_->setColumnWidth(i, col_with_[i]);
} }
QHBoxLayout* lay = new QHBoxLayout(); if (lay_ == nullptr) {
lay->addWidget(tab_widget_); lay_ = new QHBoxLayout();
ui->widget->setLayout(lay); }
lay_->addWidget(tab_widget_);
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;
@ -331,22 +322,44 @@ void MainWidget::judge_btn_page()
} }
} }
void MainWidget::clear_data()
{
}
bool MainWidget::read(const QString& file_path) bool MainWidget::read(const QString& file_path)
{ {
unit_change_auto_ = true;
ui->cbUnit->clear();
unit_change_auto_ = false;
if (alreay_open_) {
if (!CUtil::affirm(this, u8"确认", u8"当前已有打开文件在使用,请确认已保存相关内容再继续。")) {
return false;
} else {
xml_.clear_data();
current_.clear();
vec_.clear();
}
}
// 获取配置 // 获取配置
cur_config_.name = ui->cbConfig->currentText().toStdString(); cur_config_.name = ui->cbConfig->currentText().toStdString();
if (!ini_oper_->get_item(cur_config_)) { if (!ini_oper_->get_item(cur_config_)) {
CUtil::msg(this, u8"获取配置失败。"); CUtil::msg(this, u8"获取配置失败。");
clear_tab_widget();
alreay_open_ = false;
return false; return false;
} }
if (!xml_.open(file_path.toStdString())) { if (!xml_.open(file_path.toStdString())) {
CUtil::msg(this, u8"打开xml失败。"); CUtil::msg(this, u8"打开xml失败。");
clear_tab_widget();
alreay_open_ = false;
return false; return false;
} }
xml_.set_baseinfo(cur_config_); xml_.set_baseinfo(cur_config_);
if (!xml_.parse_xml(vec_)) { if (!xml_.parse_xml(vec_)) {
CUtil::msg(this, u8"解析xml失败。"); CUtil::msg(this, u8"解析xml失败。");
clear_tab_widget();
alreay_open_ = false;
return false; return false;
} }
@ -369,6 +382,7 @@ bool MainWidget::read(const QString& file_path)
} }
u_ = false; u_ = false;
ui->btnResort->setEnabled(false); ui->btnResort->setEnabled(false);
ui->cbUnit->setEnabled(true);
} else { } else {
ui->cbUnit->setEnabled(false); ui->cbUnit->setEnabled(false);
} }
@ -384,21 +398,20 @@ bool MainWidget::read(const QString& file_path)
current_ = vec_; current_ = vec_;
ui->edStatus->setText(file_path); ui->edStatus->setText(file_path);
ui->btnRead->setEnabled(false); // ui->btnRead->setEnabled(false);
ui->btnSave->setEnabled(true); ui->btnSave->setEnabled(true);
ui->btnSelectFile->setEnabled(false); // ui->btnSelectFile->setEnabled(false);
ui->btnImport->setEnabled(true); ui->btnImport->setEnabled(true);
ui->btnExport->setEnabled(true); ui->btnExport->setEnabled(true);
ui->btnBackup->setEnabled(true); ui->btnBackup->setEnabled(true);
ui->btnBackup->setEnabled(true);
ui->btnResort->setEnabled(true); ui->btnResort->setEnabled(true);
ui->cbConfig->setEnabled(false); // ui->cbConfig->setEnabled(false);
ui->btnSet->setEnabled(false); // ui->btnSet->setEnabled(false);
ui->btnHis->setEnabled(false); // ui->btnHis->setEnabled(false);
his_->push(ui->edStatus->text().toStdString()); his_->push(ui->edStatus->text().toStdString());
alreay_open_ = true;
return true; return true;
} }
@ -452,8 +465,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 +562,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 +616,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 +637,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 +744,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 +816,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 +846,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 +861,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 +878,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 +897,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,23 +917,25 @@ 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"备份完成。");
} }
} }
void MainWidget::base_init() void MainWidget::base_init(const QString& cur_config)
{ {
StrVec_t vec; StrVec_t vec;
ini_oper_->get_all_node(vec); ini_oper_->get_all_node(vec);
QStringList list; QStringList list;
bool have = false;
for (const auto& data : vec) { for (const auto& data : vec) {
list.append(QString::fromStdString(data)); list.append(QString::fromStdString(data));
if (QString::fromStdString(data) == cur_config) {
have = true;
}
} }
ui->cbConfig->clear(); ui->cbConfig->clear();
@ -948,14 +944,21 @@ void MainWidget::base_init()
} }
ui->cbConfig->addItems(list); ui->cbConfig->addItems(list);
if (have) {
ui->cbConfig->setCurrentIndex(list.indexOf(cur_config));
} else {
ui->cbConfig->setCurrentIndex(0); ui->cbConfig->setCurrentIndex(0);
} }
}
void MainWidget::unit_change() void MainWidget::unit_change()
{ {
if (u_) { if (u_) {
return; return;
} }
if (unit_change_auto_) {
return;
}
std::string unit = ui->cbUnit->currentText().toStdString(); std::string unit = ui->cbUnit->currentText().toStdString();
if (!xml_.get_all_elements(vec_, unit)) { if (!xml_.get_all_elements(vec_, unit)) {
CUtil::msg(this, u8"获取单元信息失败:" + QString::fromStdString(unit)); CUtil::msg(this, u8"获取单元信息失败:" + QString::fromStdString(unit));
@ -979,9 +982,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;
} }
@ -993,6 +994,18 @@ bool MainWidget::format_xml()
return true; return true;
} }
void MainWidget::clear_tab_widget()
{
if (tab_widget_) {
tab_widget_->setRowCount(0);
if (lay_) {
lay_->removeWidget(tab_widget_);
}
delete tab_widget_;
tab_widget_ = new QTableWidget();
}
}
std::string MainWidget::extract_prefix(const std::string& name) std::string MainWidget::extract_prefix(const std::string& name)
{ {
auto pos = name.find('.'); auto pos = name.find('.');

View File

@ -7,6 +7,7 @@
#include <QTableWidget> #include <QTableWidget>
#include <QWidget> #include <QWidget>
#include <unordered_map> #include <unordered_map>
#include <QHBoxLayout>
#include "conf_setting.h" #include "conf_setting.h"
#include "src/attribute_edit.h" #include "src/attribute_edit.h"
@ -57,6 +58,7 @@ public:
private: private:
bool read(const QString& file_path); bool read(const QString& file_path);
void clear_data();
void search(const QString& key); void search(const QString& key);
void item_changed_handle(QTableWidgetItem* item); void item_changed_handle(QTableWidgetItem* item);
void save(); void save();
@ -74,9 +76,10 @@ private:
void copy_multi_data(); void copy_multi_data();
void replace_content(bool is_common = true); void replace_content(bool is_common = true);
void backup_file(); void backup_file();
void base_init(); void base_init(const QString& cur_config);
void unit_change(); void unit_change();
bool format_xml(); bool format_xml();
void clear_tab_widget();
private: private:
std::string extract_prefix(const std::string& name); std::string extract_prefix(const std::string& name);
@ -126,5 +129,8 @@ private:
CHistory* his_{}; CHistory* his_{};
OneGroupIni cur_config_{}; OneGroupIni cur_config_{};
bool u_{false}; bool u_{false};
bool alreay_open_{false};
QHBoxLayout* lay_{};
bool unit_change_auto_{true};
}; };
#endif // MAINWIDGET_H #endif // MAINWIDGET_H

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()
@ -27,19 +23,21 @@ CGroupSetting::~CGroupSetting()
void CGroupSetting::showEvent(QShowEvent* event) void CGroupSetting::showEvent(QShowEvent* event)
{ {
update_ini(); update_ini("");
QDialog::showEvent(event); QDialog::showEvent(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;
} }
// 验证重名 // 验证重名
// 添加 // 添加
@ -57,12 +55,15 @@ void CGroupSetting::add_item()
CUtil::msg(this, u8"失败"); CUtil::msg(this, u8"失败");
return; return;
} }
update_ini(); update_ini(QString::fromStdString(gp.name));
CUtil::msg(this, u8"成功"); CUtil::msg(this, u8"成功");
} }
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"失败");
@ -70,7 +71,7 @@ void CGroupSetting::del_item()
} }
// 删除成功之后要更新 cb // 删除成功之后要更新 cb
update_ini(); update_ini("");
CUtil::msg(this, u8"成功"); CUtil::msg(this, u8"成功");
} }
@ -107,21 +108,29 @@ void CGroupSetting::change_ini()
} }
} }
void CGroupSetting::update_ini() void CGroupSetting::update_ini(const QString& cur_key)
{ {
StrVec_t vec; StrVec_t vec;
ini_opr_->get_all_node(vec); ini_opr_->get_all_node(vec);
QStringList list; QStringList list;
bool has_cur = false;
for (const auto& data : vec) { for (const auto& data : vec) {
list.append(QString::fromStdString(data)); list.append(QString::fromStdString(data));
if (data == cur_key.toStdString()) {
has_cur = true;
}
} }
auto_add_ = true; auto_add_ = true;
ui->cbConfig->clear(); ui->cbConfig->clear();
if (!list.empty()) { if (!list.empty()) {
ui->cbConfig->addItems(list); ui->cbConfig->addItems(list);
if (has_cur) {
ui->cbConfig->setCurrentIndex(list.indexOf(cur_key));
} else {
ui->cbConfig->setCurrentIndex(0); ui->cbConfig->setCurrentIndex(0);
}
OneGroupIni gp; OneGroupIni gp;
gp.name = ui->cbConfig->currentText().toStdString(); gp.name = ui->cbConfig->currentText().toStdString();
if (!ini_opr_->get_item(gp)) { if (!ini_opr_->get_item(gp)) {

View File

@ -25,7 +25,7 @@ private:
void del_item(); void del_item();
void set_ui(const OneGroupIni& gp); void set_ui(const OneGroupIni& gp);
void change_ini(); void change_ini();
void update_ini(); void update_ini(const QString& cur_key);
private: private:
bool auto_add_{false}; bool auto_add_{false};

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;
} }

View File

@ -15,8 +15,7 @@ CXmlOpr::~CXmlOpr() = default;
bool CXmlOpr::open(const std::string& xml_path) bool CXmlOpr::open(const std::string& xml_path)
{ {
#ifdef _WIN32 #ifdef _WIN32
if (doc_.LoadFile(CUtil::utf8_to_gbk(xml_path).c_str()) != if (doc_.LoadFile(CUtil::utf8_to_gbk(xml_path).c_str()) != tinyxml2::XML_SUCCESS) {
tinyxml2::XML_SUCCESS) {
return false; return false;
} }
xml_path_ = CUtil::utf8_to_gbk(xml_path); xml_path_ = CUtil::utf8_to_gbk(xml_path);
@ -29,8 +28,7 @@ bool CXmlOpr::open(const std::string& xml_path)
return true; return true;
} }
bool CXmlOpr::backup_file(const std::string& desti_folder, bool CXmlOpr::backup_file(const std::string& desti_folder, const std::string& time)
const std::string& time)
{ {
if (!fs::exists(xml_path_)) { if (!fs::exists(xml_path_)) {
return false; return false;
@ -39,8 +37,7 @@ bool CXmlOpr::backup_file(const std::string& desti_folder,
fs::create_directories(desti_folder); fs::create_directories(desti_folder);
} }
fs::path file_path = fs::path(xml_path_); fs::path file_path = fs::path(xml_path_);
fs::path des = fs::path(desti_folder) fs::path des = fs::path(desti_folder).append(file_path.stem().string() + "_" + time + ".xml");
.append(file_path.stem().string() + "_" + time + ".xml");
return fs::copy_file(xml_path_, des); return fs::copy_file(xml_path_, des);
} }
@ -49,8 +46,7 @@ void CXmlOpr::set_baseinfo(const OneGroupIni& base)
opr_base_ = base; opr_base_ = base;
} }
bool CXmlOpr::get_all_elements(std::vector<Element_t*>& vec, bool CXmlOpr::get_all_elements(std::vector<Element_t*>& vec, const std::string& unit)
const std::string& unit)
{ {
vec.clear(); vec.clear();
@ -68,8 +64,7 @@ bool CXmlOpr::get_all_elements(std::vector<Element_t*>& vec,
} }
} }
auto purpose_node = auto purpose_node = parent_node2_->FirstChildElement(opr_base_.item_key.c_str());
parent_node2_->FirstChildElement(opr_base_.item_key.c_str());
while (purpose_node) { while (purpose_node) {
vec.push_back(purpose_node); vec.push_back(purpose_node);
purpose_node = purpose_node->NextSiblingElement(); purpose_node = purpose_node->NextSiblingElement();
@ -131,13 +126,11 @@ bool CXmlOpr::get_all_unit(std::vector<std::string>& units)
} }
// 排序后(仅指针排序)的节点进行复制(让实际内容有序),删除原始节点 // 排序后(仅指针排序)的节点进行复制(让实际内容有序),删除原始节点
void CXmlOpr::copy_and_del(std::vector<Element_t*>& vec, void CXmlOpr::copy_and_del(std::vector<Element_t*>& vec, std::vector<Element_t*>& out)
std::vector<Element_t*>& out)
{ {
out.clear(); out.clear();
// 先找到最后一个节点 // 先找到最后一个节点
Element_t* last_node = Element_t* last_node = parent_node2_->LastChildElement(opr_base_.item_key.c_str());
parent_node2_->LastChildElement(opr_base_.item_key.c_str());
Element_t* last_node_bk = last_node; Element_t* last_node_bk = last_node;
if (last_node == nullptr) { if (last_node == nullptr) {
return; return;
@ -149,8 +142,7 @@ void CXmlOpr::copy_and_del(std::vector<Element_t*>& vec,
last_node = n; last_node = n;
} }
// 删除原有的节点 // 删除原有的节点
Element_t* fnode = Element_t* fnode = parent_node2_->FirstChildElement(opr_base_.item_key.c_str());
parent_node2_->FirstChildElement(opr_base_.item_key.c_str());
Element_t* fnext = fnode->NextSiblingElement(); Element_t* fnext = fnode->NextSiblingElement();
while (fnode != last_node_bk) { while (fnode != last_node_bk) {
parent_node2_->DeleteChild(fnode); parent_node2_->DeleteChild(fnode);
@ -194,8 +186,7 @@ bool CXmlOpr::check_valid_xml_data(const std::string& data)
bool CXmlOpr::check_same_struct(const std::string& data) bool CXmlOpr::check_same_struct(const std::string& data)
{ {
auto* own_ele = auto* own_ele = parent_node2_->FirstChildElement(opr_base_.item_key.c_str());
parent_node2_->FirstChildElement(opr_base_.item_key.c_str());
if (own_ele == nullptr) { if (own_ele == nullptr) {
return true; return true;
} }
@ -234,12 +225,10 @@ bool CXmlOpr::check_same_struct(const std::string& data)
// Warning: 不检查 xml 格式合法性,请自行调用 check_valid_xml_data // Warning: 不检查 xml 格式合法性,请自行调用 check_valid_xml_data
// 且导入前每条数据请自行使用 check_same_struct 检测。 // 且导入前每条数据请自行使用 check_same_struct 检测。
bool CXmlOpr::import_newer_data(const std::vector<std::string>& vec, bool CXmlOpr::import_newer_data(const std::vector<std::string>& vec, std::size_t& success_count)
std::size_t& success_count)
{ {
success_count = 0; success_count = 0;
auto* last_item = auto* last_item = parent_node2_->LastChildElement(opr_base_.item_key.c_str());
parent_node2_->LastChildElement(opr_base_.item_key.c_str());
if (last_item == nullptr) { if (last_item == nullptr) {
return false; return false;
} }
@ -262,8 +251,7 @@ bool CXmlOpr::import_newer_data(const std::vector<std::string>& vec,
} }
return true; return true;
} }
std::string CXmlOpr::handle_space(const std::string& content, std::string CXmlOpr::handle_space(const std::string& content, const std::vector<std::string>& keychars)
const std::vector<std::string>& keychars)
{ {
std::string result; std::string result;
size_t pos = 0; size_t pos = 0;
@ -276,10 +264,8 @@ std::string CXmlOpr::handle_space(const std::string& content,
} }
std::string op(keychars[i]); std::string op(keychars[i]);
for (const char& c : op) { for (const char& c : op) {
if (c == '|' || c == '\\' || c == '(' || c == ')' || c == '[' || if (c == '|' || c == '\\' || c == '(' || c == ')' || c == '[' || c == ']' || c == '{' || c == '}' || c == '^' ||
c == ']' || c == '{' || c == '}' || c == '^' || c == '$' || c == '$' || c == '.' || c == '*' || c == '+' || c == '?' || c == '|' || c == '\\') {
c == '.' || c == '*' || c == '+' || c == '?' || c == '|' ||
c == '\\') {
oss << '\\'; // 添加转义字符 oss << '\\'; // 添加转义字符
} }
oss << c; oss << c;
@ -308,10 +294,8 @@ std::string CXmlOpr::handle_space(const std::string& content,
} }
// Extract the quoted content // Extract the quoted content
std::string quoted_content = std::string quoted_content = content.substr(start_quote + 1, end_quote - start_quote - 1);
content.substr(start_quote + 1, end_quote - start_quote - 1); std::string processed_content = std::regex_replace(quoted_content, operators_regex, " $& ");
std::string processed_content =
std::regex_replace(quoted_content, operators_regex, " $& ");
// Process quoted content to replace multiple spaces with a single space // Process quoted content to replace multiple spaces with a single space
std::istringstream iss(processed_content); std::istringstream iss(processed_content);
@ -335,8 +319,7 @@ std::string CXmlOpr::handle_space(const std::string& content,
return result; return result;
} }
bool CXmlOpr::handle_transfer(const std::string& path, bool CXmlOpr::handle_transfer(const std::string& path, const std::vector<std::string>& keychars)
const std::vector<std::string>& keychars)
{ {
std::ifstream file(path); std::ifstream file(path);
if (!file.is_open()) { if (!file.is_open()) {
@ -414,8 +397,7 @@ bool CXmlOpr::check_key_exists(const std::string& key)
if (keys_.size() < 1 || key.empty()) { if (keys_.size() < 1 || key.empty()) {
return false; return false;
} }
Element_t* purpose_node = Element_t* purpose_node = parent_node2_->FirstChildElement(opr_base_.item_key.c_str());
parent_node2_->FirstChildElement(opr_base_.item_key.c_str());
while (purpose_node) { while (purpose_node) {
const char* value = purpose_node->Attribute(keys_[0].c_str()); const char* value = purpose_node->Attribute(keys_[0].c_str());
if (key == std::string(value)) { if (key == std::string(value)) {
@ -435,8 +417,17 @@ bool CXmlOpr::save()
return true; return true;
} }
bool CXmlOpr::handle_save(const std::string& path, void CXmlOpr::clear_data()
const std::vector<std::string>& keychars) {
xml_path_.clear();
parent_node_ = nullptr;
parent_node2_ = nullptr;
keys_.clear();
units_.clear();
doc_.Clear();
}
bool CXmlOpr::handle_save(const std::string& path, const std::vector<std::string>& keychars)
{ {
if (!open(path)) { if (!open(path)) {
return false; return false;

View File

@ -28,15 +28,14 @@ public:
void set_baseinfo(const OneGroupIni& base); void set_baseinfo(const OneGroupIni& base);
bool parse_xml(std::vector<Element_t*>& vec); bool parse_xml(std::vector<Element_t*>& vec);
bool get_all_unit(std::vector<std::string>& units); bool get_all_unit(std::vector<std::string>& units);
bool get_all_elements(std::vector<Element_t*>& vec, bool get_all_elements(std::vector<Element_t*>& vec, const std::string& unit = "");
const std::string& unit = ""); void copy_and_del(std::vector<Element_t*>& vec, std::vector<Element_t*>& out);
void copy_and_del(std::vector<Element_t*>& vec,
std::vector<Element_t*>& out);
void insert_brother_node(Element_t* brother, Element_t* newer); void insert_brother_node(Element_t* brother, Element_t* newer);
void del_element(Element_t* ele); void del_element(Element_t* ele);
bool check_key_exists(const Property_t& property); bool check_key_exists(const Property_t& property);
bool check_key_exists(const std::string& key); bool check_key_exists(const std::string& key);
bool save(); bool save();
void clear_data();
bool handle_save(const std::string& path, const std::vector<std::string>& keychars); bool handle_save(const std::string& path, const std::vector<std::string>& keychars);
std::string handle_space(const std::string& content, const std::vector<std::string>& keychars); std::string handle_space(const std::string& content, const std::vector<std::string>& keychars);
@ -47,8 +46,7 @@ public:
// 不检查 xml 格式合法性,请自行调用 check_valid_xml_data // 不检查 xml 格式合法性,请自行调用 check_valid_xml_data
bool check_same_struct(const std::string& data); bool check_same_struct(const std::string& data);
// 不检查 xml 格式合法性,请自行调用 check_valid_xml_data // 不检查 xml 格式合法性,请自行调用 check_valid_xml_data
bool import_newer_data(const std::vector<std::string>& vec, bool import_newer_data(const std::vector<std::string>& vec, std::size_t& success_count);
std::size_t& success_count);
// 处理转义 // 处理转义
bool handle_transfer(const std::string& path, const std::vector<std::string>& keychars); bool handle_transfer(const std::string& path, const std::vector<std::string>& keychars);