new-feature:1.添加带表达式内容的xml格式化。2.支持带特殊字符的读写,如& < > "。
This commit is contained in:
117
MainWidget.cpp
117
MainWidget.cpp
@@ -15,11 +15,12 @@
|
||||
|
||||
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.4");
|
||||
setWindowTitle(u8"OneLevelXmlOpr v1.3.5");
|
||||
setWindowIcon(QIcon("://resource/xml.ico"));
|
||||
|
||||
QScreen* primaryScreen = QGuiApplication::primaryScreen();
|
||||
@@ -50,22 +51,33 @@ MainWidget::MainWidget(QWidget* parent) : QWidget(parent), ui(new Ui::MainWidget
|
||||
ui->btnSearch->setFixedWidth(120);
|
||||
|
||||
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->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);
|
||||
@@ -81,7 +93,8 @@ MainWidget::MainWidget(QWidget* parent) : QWidget(parent), ui(new Ui::MainWidget
|
||||
}
|
||||
|
||||
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_);
|
||||
}
|
||||
@@ -211,8 +224,10 @@ void MainWidget::generate_table_widget()
|
||||
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();
|
||||
|
||||
@@ -244,7 +259,8 @@ void MainWidget::generate_table_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;
|
||||
@@ -426,7 +442,8 @@ 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()
|
||||
@@ -523,9 +540,9 @@ 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()));
|
||||
}
|
||||
else {
|
||||
search_key =
|
||||
QString::fromLocal8Bit(target->Attribute(keys_[0].c_str()));
|
||||
} else {
|
||||
search_key = pre_value;
|
||||
}
|
||||
|
||||
@@ -578,8 +595,10 @@ 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(); });
|
||||
}
|
||||
|
||||
@@ -599,8 +618,10 @@ 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);
|
||||
}
|
||||
@@ -705,7 +726,10 @@ void MainWidget::sort_by_repeat(std::vector<Element_t*>& vec)
|
||||
std::string d(str);
|
||||
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); });
|
||||
std::sort(turn_vec.begin(), turn_vec.end(),
|
||||
[&](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);
|
||||
@@ -777,7 +801,8 @@ 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;
|
||||
@@ -807,7 +832,8 @@ 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;
|
||||
@@ -822,7 +848,8 @@ void MainWidget::replace_str(const QString& pre, const QString& after, Element_t
|
||||
}
|
||||
}
|
||||
|
||||
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) {
|
||||
@@ -839,14 +866,17 @@ void MainWidget::replace_str(Element_t* ele, const QString& rg, const QString& a
|
||||
}
|
||||
}
|
||||
|
||||
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: {
|
||||
@@ -858,13 +888,18 @@ void MainWidget::get_related_elements(std::vector<OperElement*>& out, ReplaceAre
|
||||
}
|
||||
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;
|
||||
}
|
||||
@@ -878,7 +913,9 @@ 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"备份完成。");
|
||||
@@ -918,6 +955,20 @@ void MainWidget::unit_change()
|
||||
current_ = vec_;
|
||||
}
|
||||
|
||||
bool MainWidget::format_xml()
|
||||
{
|
||||
std::string xml_path = ui->edStatus->text().toStdString();
|
||||
if (xml_path.empty()) {
|
||||
return false;
|
||||
}
|
||||
if (!xml_.handle_save(xml_path)) {
|
||||
CUtil::msg(this, u8"格式化失败");
|
||||
return false;
|
||||
}
|
||||
CUtil::msg(this, u8"格式化结束");
|
||||
return true;
|
||||
}
|
||||
|
||||
std::string MainWidget::extract_prefix(const std::string& name)
|
||||
{
|
||||
auto pos = name.find('.');
|
||||
|
||||
Reference in New Issue
Block a user