Compare commits
2 Commits
4db3a6b202
...
c2c3f6f2ca
Author | SHA1 | Date | |
---|---|---|---|
c2c3f6f2ca | |||
2451ae76be |
@ -11,7 +11,7 @@ ReflowComments: true
|
||||
SpacesBeforeTrailingComments: 3
|
||||
TabWidth: 4
|
||||
ConstructorInitializerAllOnOneLineOrOnePerLine: true
|
||||
ColumnLimit: 80
|
||||
ColumnLimit: 130
|
||||
AllowShortBlocksOnASingleLine: Never
|
||||
AllowShortFunctionsOnASingleLine: None
|
||||
AllowShortEnumsOnASingleLine: false
|
||||
|
@ -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)
|
||||
|
187
MainWidget.cpp
187
MainWidget.cpp
@ -16,12 +16,11 @@
|
||||
|
||||
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);
|
||||
|
||||
setWindowTitle(u8"OneLevelXmlOpr v1.3.8");
|
||||
setWindowTitle(u8"OneLevelXmlOpr v1.3.9");
|
||||
setWindowIcon(QIcon("://resource/xml.ico"));
|
||||
|
||||
QScreen* primaryScreen = QGuiApplication::primaryScreen();
|
||||
@ -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_);
|
||||
}
|
||||
@ -108,8 +96,9 @@ MainWidget::MainWidget(QWidget* parent)
|
||||
});
|
||||
|
||||
connect(ui->btnSet, &QPushButton::clicked, this, [&]() {
|
||||
QString cur = ui->cbConfig->currentText();
|
||||
group_set_->exec();
|
||||
base_init();
|
||||
base_init(cur);
|
||||
});
|
||||
connect(ui->btnHis, &QPushButton::clicked, this, [&]() {
|
||||
CUIHistory his(this, his_);
|
||||
@ -151,7 +140,7 @@ MainWidget::MainWidget(QWidget* parent)
|
||||
restoreGeometry(settings.value("geometry").toByteArray());
|
||||
settings.endGroup();
|
||||
|
||||
// QFile qss_file("://qss/lightblue.css");
|
||||
//QFile qss_file("://qss/lightblue.css");
|
||||
// QFile qss_file("://qss/flatgray.css");
|
||||
// if (qss_file.open(QFile::ReadOnly)) {
|
||||
// qApp->setStyleSheet(qss_file.readAll());
|
||||
@ -159,7 +148,7 @@ MainWidget::MainWidget(QWidget* parent)
|
||||
|
||||
init_menu();
|
||||
// 基本处理
|
||||
base_init();
|
||||
base_init("");
|
||||
}
|
||||
|
||||
void MainWidget::copy_key()
|
||||
@ -224,14 +213,15 @@ void MainWidget::show_custom_menu()
|
||||
|
||||
void MainWidget::generate_table_widget()
|
||||
{
|
||||
tab_widget_ = new QTableWidget();
|
||||
clear_tab_widget();
|
||||
if (tab_widget_ == nullptr) {
|
||||
tab_widget_ = new QTableWidget();
|
||||
}
|
||||
metrics_ = std::make_shared<QFontMetrics>(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();
|
||||
|
||||
@ -258,13 +248,14 @@ void MainWidget::generate_table_widget()
|
||||
tab_widget_->setColumnWidth(i, col_with_[i]);
|
||||
}
|
||||
|
||||
QHBoxLayout* lay = new QHBoxLayout();
|
||||
lay->addWidget(tab_widget_);
|
||||
ui->widget->setLayout(lay);
|
||||
if (lay_ == nullptr) {
|
||||
lay_ = new QHBoxLayout();
|
||||
}
|
||||
lay_->addWidget(tab_widget_);
|
||||
ui->widget->setLayout(lay_);
|
||||
}
|
||||
|
||||
void MainWidget::push_content(const std::vector<tinyxml2::XMLElement*>& eles,
|
||||
std::size_t page, bool auto_jump_pre)
|
||||
void MainWidget::push_content(const std::vector<tinyxml2::XMLElement*>& eles, std::size_t page, bool auto_jump_pre)
|
||||
{
|
||||
if (tab_widget_ == nullptr || page == 0) {
|
||||
return;
|
||||
@ -331,22 +322,44 @@ void MainWidget::judge_btn_page()
|
||||
}
|
||||
}
|
||||
|
||||
void MainWidget::clear_data()
|
||||
{
|
||||
}
|
||||
|
||||
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();
|
||||
if (!ini_oper_->get_item(cur_config_)) {
|
||||
CUtil::msg(this, u8"获取配置失败。");
|
||||
clear_tab_widget();
|
||||
alreay_open_ = false;
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!xml_.open(file_path.toStdString())) {
|
||||
CUtil::msg(this, u8"打开xml失败。");
|
||||
clear_tab_widget();
|
||||
alreay_open_ = false;
|
||||
return false;
|
||||
}
|
||||
xml_.set_baseinfo(cur_config_);
|
||||
if (!xml_.parse_xml(vec_)) {
|
||||
CUtil::msg(this, u8"解析xml失败。");
|
||||
clear_tab_widget();
|
||||
alreay_open_ = false;
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -369,6 +382,7 @@ bool MainWidget::read(const QString& file_path)
|
||||
}
|
||||
u_ = false;
|
||||
ui->btnResort->setEnabled(false);
|
||||
ui->cbUnit->setEnabled(true);
|
||||
} else {
|
||||
ui->cbUnit->setEnabled(false);
|
||||
}
|
||||
@ -384,21 +398,20 @@ bool MainWidget::read(const QString& file_path)
|
||||
current_ = vec_;
|
||||
|
||||
ui->edStatus->setText(file_path);
|
||||
ui->btnRead->setEnabled(false);
|
||||
// ui->btnRead->setEnabled(false);
|
||||
ui->btnSave->setEnabled(true);
|
||||
ui->btnSelectFile->setEnabled(false);
|
||||
// ui->btnSelectFile->setEnabled(false);
|
||||
ui->btnImport->setEnabled(true);
|
||||
ui->btnExport->setEnabled(true);
|
||||
ui->btnBackup->setEnabled(true);
|
||||
ui->btnBackup->setEnabled(true);
|
||||
ui->btnResort->setEnabled(true);
|
||||
|
||||
ui->cbConfig->setEnabled(false);
|
||||
ui->btnSet->setEnabled(false);
|
||||
ui->btnHis->setEnabled(false);
|
||||
// ui->cbConfig->setEnabled(false);
|
||||
// ui->btnSet->setEnabled(false);
|
||||
// ui->btnHis->setEnabled(false);
|
||||
|
||||
his_->push(ui->edStatus->text().toStdString());
|
||||
|
||||
alreay_open_ = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -452,8 +465,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 +562,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 +616,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 +637,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 +744,7 @@ void MainWidget::sort_by_repeat(std::vector<Element_t*>& 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 +816,7 @@ void MainWidget::replace_content(bool is_common)
|
||||
|
||||
std::vector<OperElement*> 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 +846,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 +861,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 +878,14 @@ void MainWidget::replace_str(Element_t* ele, const QString& rg,
|
||||
}
|
||||
}
|
||||
|
||||
void MainWidget::get_related_elements(std::vector<OperElement*>& out,
|
||||
ReplaceArea area)
|
||||
void MainWidget::get_related_elements(std::vector<OperElement*>& 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 +897,13 @@ void MainWidget::get_related_elements(std::vector<OperElement*>& 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,23 +917,25 @@ 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"备份完成。");
|
||||
}
|
||||
}
|
||||
|
||||
void MainWidget::base_init()
|
||||
void MainWidget::base_init(const QString& cur_config)
|
||||
{
|
||||
StrVec_t vec;
|
||||
ini_oper_->get_all_node(vec);
|
||||
|
||||
QStringList list;
|
||||
bool have = false;
|
||||
for (const auto& data : vec) {
|
||||
list.append(QString::fromStdString(data));
|
||||
if (QString::fromStdString(data) == cur_config) {
|
||||
have = true;
|
||||
}
|
||||
}
|
||||
ui->cbConfig->clear();
|
||||
|
||||
@ -948,7 +944,11 @@ void MainWidget::base_init()
|
||||
}
|
||||
|
||||
ui->cbConfig->addItems(list);
|
||||
ui->cbConfig->setCurrentIndex(0);
|
||||
if (have) {
|
||||
ui->cbConfig->setCurrentIndex(list.indexOf(cur_config));
|
||||
} else {
|
||||
ui->cbConfig->setCurrentIndex(0);
|
||||
}
|
||||
}
|
||||
|
||||
void MainWidget::unit_change()
|
||||
@ -956,6 +956,9 @@ void MainWidget::unit_change()
|
||||
if (u_) {
|
||||
return;
|
||||
}
|
||||
if (unit_change_auto_) {
|
||||
return;
|
||||
}
|
||||
std::string unit = ui->cbUnit->currentText().toStdString();
|
||||
if (!xml_.get_all_elements(vec_, unit)) {
|
||||
CUtil::msg(this, u8"获取单元信息失败:" + QString::fromStdString(unit));
|
||||
@ -979,9 +982,7 @@ bool MainWidget::format_xml()
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!CUtil::affirm(
|
||||
this, u8"确认",
|
||||
u8"重排版内容将会覆盖源文件,请确认是否需要备份,继续?")) {
|
||||
if (!CUtil::affirm(this, u8"确认", u8"重排版内容将会覆盖源文件,请确认是否需要备份,继续?")) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -993,6 +994,18 @@ bool MainWidget::format_xml()
|
||||
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)
|
||||
{
|
||||
auto pos = name.find('.');
|
||||
|
@ -7,6 +7,7 @@
|
||||
#include <QTableWidget>
|
||||
#include <QWidget>
|
||||
#include <unordered_map>
|
||||
#include <QHBoxLayout>
|
||||
|
||||
#include "conf_setting.h"
|
||||
#include "src/attribute_edit.h"
|
||||
@ -57,6 +58,7 @@ public:
|
||||
|
||||
private:
|
||||
bool read(const QString& file_path);
|
||||
void clear_data();
|
||||
void search(const QString& key);
|
||||
void item_changed_handle(QTableWidgetItem* item);
|
||||
void save();
|
||||
@ -74,9 +76,10 @@ private:
|
||||
void copy_multi_data();
|
||||
void replace_content(bool is_common = true);
|
||||
void backup_file();
|
||||
void base_init();
|
||||
void base_init(const QString& cur_config);
|
||||
void unit_change();
|
||||
bool format_xml();
|
||||
void clear_tab_widget();
|
||||
|
||||
private:
|
||||
std::string extract_prefix(const std::string& name);
|
||||
@ -126,5 +129,8 @@ private:
|
||||
CHistory* his_{};
|
||||
OneGroupIni cur_config_{};
|
||||
bool u_{false};
|
||||
bool alreay_open_{false};
|
||||
QHBoxLayout* lay_{};
|
||||
bool unit_change_auto_{true};
|
||||
};
|
||||
#endif // MAINWIDGET_H
|
||||
|
@ -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()
|
||||
@ -27,19 +23,21 @@ CGroupSetting::~CGroupSetting()
|
||||
|
||||
void CGroupSetting::showEvent(QShowEvent* event)
|
||||
{
|
||||
update_ini();
|
||||
update_ini("");
|
||||
QDialog::showEvent(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;
|
||||
}
|
||||
|
||||
// 验证重名
|
||||
|
||||
// 添加
|
||||
@ -57,12 +55,15 @@ void CGroupSetting::add_item()
|
||||
CUtil::msg(this, u8"失败");
|
||||
return;
|
||||
}
|
||||
update_ini();
|
||||
update_ini(QString::fromStdString(gp.name));
|
||||
CUtil::msg(this, u8"成功");
|
||||
}
|
||||
|
||||
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"失败");
|
||||
@ -70,7 +71,7 @@ void CGroupSetting::del_item()
|
||||
}
|
||||
|
||||
// 删除成功之后要更新 cb
|
||||
update_ini();
|
||||
update_ini("");
|
||||
|
||||
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;
|
||||
ini_opr_->get_all_node(vec);
|
||||
|
||||
QStringList list;
|
||||
bool has_cur = false;
|
||||
for (const auto& data : vec) {
|
||||
list.append(QString::fromStdString(data));
|
||||
if (data == cur_key.toStdString()) {
|
||||
has_cur = true;
|
||||
}
|
||||
}
|
||||
|
||||
auto_add_ = true;
|
||||
ui->cbConfig->clear();
|
||||
if (!list.empty()) {
|
||||
ui->cbConfig->addItems(list);
|
||||
ui->cbConfig->setCurrentIndex(0);
|
||||
if (has_cur) {
|
||||
ui->cbConfig->setCurrentIndex(list.indexOf(cur_key));
|
||||
} else {
|
||||
ui->cbConfig->setCurrentIndex(0);
|
||||
}
|
||||
OneGroupIni gp;
|
||||
gp.name = ui->cbConfig->currentText().toStdString();
|
||||
if (!ini_opr_->get_item(gp)) {
|
||||
|
@ -25,7 +25,7 @@ private:
|
||||
void del_item();
|
||||
void set_ui(const OneGroupIni& gp);
|
||||
void change_ini();
|
||||
void update_ini();
|
||||
void update_ini(const QString& cur_key);
|
||||
|
||||
private:
|
||||
bool auto_add_{false};
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -15,8 +15,7 @@ CXmlOpr::~CXmlOpr() = default;
|
||||
bool CXmlOpr::open(const std::string& xml_path)
|
||||
{
|
||||
#ifdef _WIN32
|
||||
if (doc_.LoadFile(CUtil::utf8_to_gbk(xml_path).c_str()) !=
|
||||
tinyxml2::XML_SUCCESS) {
|
||||
if (doc_.LoadFile(CUtil::utf8_to_gbk(xml_path).c_str()) != tinyxml2::XML_SUCCESS) {
|
||||
return false;
|
||||
}
|
||||
xml_path_ = CUtil::utf8_to_gbk(xml_path);
|
||||
@ -29,8 +28,7 @@ bool CXmlOpr::open(const std::string& xml_path)
|
||||
return true;
|
||||
}
|
||||
|
||||
bool CXmlOpr::backup_file(const std::string& desti_folder,
|
||||
const std::string& time)
|
||||
bool CXmlOpr::backup_file(const std::string& desti_folder, const std::string& time)
|
||||
{
|
||||
if (!fs::exists(xml_path_)) {
|
||||
return false;
|
||||
@ -39,8 +37,7 @@ bool CXmlOpr::backup_file(const std::string& desti_folder,
|
||||
fs::create_directories(desti_folder);
|
||||
}
|
||||
fs::path file_path = fs::path(xml_path_);
|
||||
fs::path des = fs::path(desti_folder)
|
||||
.append(file_path.stem().string() + "_" + time + ".xml");
|
||||
fs::path des = fs::path(desti_folder).append(file_path.stem().string() + "_" + time + ".xml");
|
||||
return fs::copy_file(xml_path_, des);
|
||||
}
|
||||
|
||||
@ -49,8 +46,7 @@ void CXmlOpr::set_baseinfo(const OneGroupIni& base)
|
||||
opr_base_ = base;
|
||||
}
|
||||
|
||||
bool CXmlOpr::get_all_elements(std::vector<Element_t*>& vec,
|
||||
const std::string& unit)
|
||||
bool CXmlOpr::get_all_elements(std::vector<Element_t*>& vec, const std::string& unit)
|
||||
{
|
||||
vec.clear();
|
||||
|
||||
@ -68,8 +64,7 @@ bool CXmlOpr::get_all_elements(std::vector<Element_t*>& vec,
|
||||
}
|
||||
}
|
||||
|
||||
auto purpose_node =
|
||||
parent_node2_->FirstChildElement(opr_base_.item_key.c_str());
|
||||
auto purpose_node = parent_node2_->FirstChildElement(opr_base_.item_key.c_str());
|
||||
while (purpose_node) {
|
||||
vec.push_back(purpose_node);
|
||||
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,
|
||||
std::vector<Element_t*>& out)
|
||||
void CXmlOpr::copy_and_del(std::vector<Element_t*>& vec, std::vector<Element_t*>& out)
|
||||
{
|
||||
out.clear();
|
||||
// 先找到最后一个节点
|
||||
Element_t* last_node =
|
||||
parent_node2_->LastChildElement(opr_base_.item_key.c_str());
|
||||
Element_t* last_node = parent_node2_->LastChildElement(opr_base_.item_key.c_str());
|
||||
Element_t* last_node_bk = last_node;
|
||||
if (last_node == nullptr) {
|
||||
return;
|
||||
@ -149,8 +142,7 @@ void CXmlOpr::copy_and_del(std::vector<Element_t*>& vec,
|
||||
last_node = n;
|
||||
}
|
||||
// 删除原有的节点
|
||||
Element_t* fnode =
|
||||
parent_node2_->FirstChildElement(opr_base_.item_key.c_str());
|
||||
Element_t* fnode = parent_node2_->FirstChildElement(opr_base_.item_key.c_str());
|
||||
Element_t* fnext = fnode->NextSiblingElement();
|
||||
while (fnode != last_node_bk) {
|
||||
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)
|
||||
{
|
||||
auto* own_ele =
|
||||
parent_node2_->FirstChildElement(opr_base_.item_key.c_str());
|
||||
auto* own_ele = parent_node2_->FirstChildElement(opr_base_.item_key.c_str());
|
||||
if (own_ele == nullptr) {
|
||||
return true;
|
||||
}
|
||||
@ -234,12 +225,10 @@ bool CXmlOpr::check_same_struct(const std::string& data)
|
||||
|
||||
// Warning: 不检查 xml 格式合法性,请自行调用 check_valid_xml_data
|
||||
// 且导入前每条数据请自行使用 check_same_struct 检测。
|
||||
bool CXmlOpr::import_newer_data(const std::vector<std::string>& vec,
|
||||
std::size_t& success_count)
|
||||
bool CXmlOpr::import_newer_data(const std::vector<std::string>& vec, std::size_t& success_count)
|
||||
{
|
||||
success_count = 0;
|
||||
auto* last_item =
|
||||
parent_node2_->LastChildElement(opr_base_.item_key.c_str());
|
||||
auto* last_item = parent_node2_->LastChildElement(opr_base_.item_key.c_str());
|
||||
if (last_item == nullptr) {
|
||||
return false;
|
||||
}
|
||||
@ -262,8 +251,7 @@ bool CXmlOpr::import_newer_data(const std::vector<std::string>& vec,
|
||||
}
|
||||
return true;
|
||||
}
|
||||
std::string CXmlOpr::handle_space(const std::string& content,
|
||||
const std::vector<std::string>& keychars)
|
||||
std::string CXmlOpr::handle_space(const std::string& content, const std::vector<std::string>& keychars)
|
||||
{
|
||||
std::string result;
|
||||
size_t pos = 0;
|
||||
@ -276,10 +264,8 @@ std::string CXmlOpr::handle_space(const std::string& content,
|
||||
}
|
||||
std::string op(keychars[i]);
|
||||
for (const char& c : op) {
|
||||
if (c == '|' || c == '\\' || c == '(' || c == ')' || c == '[' ||
|
||||
c == ']' || c == '{' || c == '}' || c == '^' || c == '$' ||
|
||||
c == '.' || c == '*' || c == '+' || c == '?' || c == '|' ||
|
||||
c == '\\') {
|
||||
if (c == '|' || c == '\\' || c == '(' || c == ')' || c == '[' || c == ']' || c == '{' || c == '}' || c == '^' ||
|
||||
c == '$' || c == '.' || c == '*' || c == '+' || c == '?' || c == '|' || c == '\\') {
|
||||
oss << '\\'; // 添加转义字符
|
||||
}
|
||||
oss << c;
|
||||
@ -308,10 +294,8 @@ std::string CXmlOpr::handle_space(const std::string& content,
|
||||
}
|
||||
|
||||
// Extract the quoted content
|
||||
std::string quoted_content =
|
||||
content.substr(start_quote + 1, end_quote - start_quote - 1);
|
||||
std::string processed_content =
|
||||
std::regex_replace(quoted_content, operators_regex, " $& ");
|
||||
std::string quoted_content = content.substr(start_quote + 1, end_quote - start_quote - 1);
|
||||
std::string processed_content = std::regex_replace(quoted_content, operators_regex, " $& ");
|
||||
|
||||
// Process quoted content to replace multiple spaces with a single space
|
||||
std::istringstream iss(processed_content);
|
||||
@ -335,8 +319,7 @@ std::string CXmlOpr::handle_space(const std::string& content,
|
||||
|
||||
return result;
|
||||
}
|
||||
bool CXmlOpr::handle_transfer(const std::string& path,
|
||||
const std::vector<std::string>& keychars)
|
||||
bool CXmlOpr::handle_transfer(const std::string& path, const std::vector<std::string>& keychars)
|
||||
{
|
||||
std::ifstream file(path);
|
||||
if (!file.is_open()) {
|
||||
@ -414,8 +397,7 @@ bool CXmlOpr::check_key_exists(const std::string& key)
|
||||
if (keys_.size() < 1 || key.empty()) {
|
||||
return false;
|
||||
}
|
||||
Element_t* purpose_node =
|
||||
parent_node2_->FirstChildElement(opr_base_.item_key.c_str());
|
||||
Element_t* purpose_node = parent_node2_->FirstChildElement(opr_base_.item_key.c_str());
|
||||
while (purpose_node) {
|
||||
const char* value = purpose_node->Attribute(keys_[0].c_str());
|
||||
if (key == std::string(value)) {
|
||||
@ -435,8 +417,17 @@ bool CXmlOpr::save()
|
||||
return true;
|
||||
}
|
||||
|
||||
bool CXmlOpr::handle_save(const std::string& path,
|
||||
const std::vector<std::string>& keychars)
|
||||
void CXmlOpr::clear_data()
|
||||
{
|
||||
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)) {
|
||||
return false;
|
||||
|
@ -28,15 +28,14 @@ public:
|
||||
void set_baseinfo(const OneGroupIni& base);
|
||||
bool parse_xml(std::vector<Element_t*>& vec);
|
||||
bool get_all_unit(std::vector<std::string>& units);
|
||||
bool get_all_elements(std::vector<Element_t*>& vec,
|
||||
const std::string& unit = "");
|
||||
void copy_and_del(std::vector<Element_t*>& vec,
|
||||
std::vector<Element_t*>& out);
|
||||
bool get_all_elements(std::vector<Element_t*>& vec, const std::string& unit = "");
|
||||
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 del_element(Element_t* ele);
|
||||
bool check_key_exists(const Property_t& property);
|
||||
bool check_key_exists(const std::string& key);
|
||||
bool save();
|
||||
void clear_data();
|
||||
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);
|
||||
|
||||
@ -47,8 +46,7 @@ public:
|
||||
// 不检查 xml 格式合法性,请自行调用 check_valid_xml_data
|
||||
bool check_same_struct(const std::string& data);
|
||||
// 不检查 xml 格式合法性,请自行调用 check_valid_xml_data
|
||||
bool import_newer_data(const std::vector<std::string>& vec,
|
||||
std::size_t& success_count);
|
||||
bool import_newer_data(const std::vector<std::string>& vec, std::size_t& success_count);
|
||||
// 处理转义
|
||||
bool handle_transfer(const std::string& path, const std::vector<std::string>& keychars);
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user