func:替换添加所有页选项。

This commit is contained in:
taynpg 2024-05-21 15:27:54 +08:00
parent bd7f2681ab
commit 2a7af82b0b
3 changed files with 104 additions and 51 deletions

View File

@ -16,10 +16,9 @@ MainWidget::MainWidget(QWidget* parent) : QWidget(parent), ui(new Ui::MainWidget
{ {
ui->setupUi(this); ui->setupUi(this);
setWindowTitle(u8"OneLevelXmlOpr v1.2.9"); setWindowTitle(u8"OneLevelXmlOpr v1.2.10");
setWindowIcon(QIcon("://resource/xml.ico")); setWindowIcon(QIcon("://resource/xml.ico"));
QScreen* primaryScreen = QGuiApplication::primaryScreen(); QScreen* primaryScreen = QGuiApplication::primaryScreen();
QRect screenGeometry = primaryScreen->geometry(); QRect screenGeometry = primaryScreen->geometry();
setMinimumWidth(screenGeometry.width() * 0.5); setMinimumWidth(screenGeometry.width() * 0.5);
@ -53,7 +52,7 @@ MainWidget::MainWidget(QWidget* parent) : QWidget(parent), ui(new Ui::MainWidget
} }
read(file); read(file);
}); });
connect(ui->btnSearch, &QPushButton::clicked, this, [&]() { search(); }); connect(ui->btnSearch, &QPushButton::clicked, this, [&]() { search(ui->edSearchKey->text()); });
connect(ui->btnBackup, &QPushButton::clicked, this, [&]() { backup_file(); }); connect(ui->btnBackup, &QPushButton::clicked, this, [&]() { backup_file(); });
connect(ui->btnRead, &QPushButton::clicked, this, [&]() { read(ui->edStatus->text().trimmed()); }); 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(); });
@ -143,7 +142,7 @@ void MainWidget::keyPressEvent(QKeyEvent* event)
{ {
switch (event->key()) { switch (event->key()) {
case Qt::Key_Return: case Qt::Key_Return:
search(); search(ui->edSearchKey->text());
break; break;
default: default:
break; break;
@ -298,10 +297,10 @@ void MainWidget::read(const QString& file_path)
ui->btnBackup->setEnabled(true); ui->btnBackup->setEnabled(true);
} }
void MainWidget::search() void MainWidget::search(const QString& key)
{ {
QString key = ui->edSearchKey->text().trimmed(); QString bkey = key;
if (key.isEmpty()) { if (bkey.isEmpty()) {
current_ = vec_; current_ = vec_;
push_content(current_); push_content(current_);
return; return;
@ -311,7 +310,7 @@ void MainWidget::search()
} }
if (!ui->cbCaseSensitive->isChecked()) { if (!ui->cbCaseSensitive->isChecked()) {
key = key.toUpper(); bkey = bkey.toUpper();
} }
current_.clear(); current_.clear();
@ -322,7 +321,7 @@ void MainWidget::search()
if (!ui->cbCaseSensitive->isChecked()) { if (!ui->cbCaseSensitive->isChecked()) {
qdata = qdata.toUpper(); qdata = qdata.toUpper();
} }
if (!qdata.contains(key)) { if (!qdata.contains(bkey)) {
continue; continue;
} }
current_.push_back(item); current_.push_back(item);
@ -394,7 +393,7 @@ void MainWidget::copy_select_line()
} }
int df = it - vec_.begin() + 1; int df = it - vec_.begin() + 1;
vec_.insert(vec_.begin() + df, newer); vec_.insert(vec_.begin() + df, newer);
search(); search(ui->edSearchKey->text());
} }
// 返回 true 表示确认编辑了, false 表示取消编辑了。 // 返回 true 表示确认编辑了, false 表示取消编辑了。
@ -521,7 +520,7 @@ void MainWidget::del_select_line()
} }
} }
vec_.erase(it); vec_.erase(it);
search(); search(ui->edSearchKey->text());
} }
Element_t* MainWidget::get_current_select_key() Element_t* MainWidget::get_current_select_key()
@ -648,54 +647,49 @@ void MainWidget::replace_content(bool is_common)
CUtil::msg(this, u8"替换前数据为空。"); CUtil::msg(this, u8"替换前数据为空。");
return; return;
} }
auto handle = [&](const std::vector<OperElement*>& vec, bool is_search) {
for (auto& item : vec) {
if (is_common) {
replace_str(key, after, item->element_);
} else {
replace_str(item->element_, key, after);
}
if (!is_search) {
ele_update_gui(item->element_, item->row_);
}
delete item;
}
};
std::vector<OperElement*> vec;
if (ui->rbReplaceSelect->isChecked()) { if (ui->rbReplaceSelect->isChecked()) {
QModelIndexList indexList = tab_widget_->selectionModel()->selectedRows(); QModelIndexList indexList = tab_widget_->selectionModel()->selectedRows();
if (indexList.size() < 1) { if (indexList.size() < 1) {
CUtil::msg(this, u8"无选择数据"); CUtil::msg(this, u8"无选择数据");
return; return;
} }
QString ret; get_related_elements(vec, AREA_SELECT);
for (int i = 0; i < indexList.size(); ++i) { handle(vec, false);
Element_t* e = get_element_by_row(indexList[i].row()); } else if (ui->rbAllPage->isChecked()) {
if (e == nullptr) { get_related_elements(vec, AREA_ALL_PAGE);
continue; handle(vec, false);
} xml_.get_all_elements(vec_);
if (is_common) { current_.clear();
replace_str(key, after, e); current_ = vec_;
} else { search(ui->edRepAfter->text());
replace_str(e, key, after);
}
ele_update_gui(e, indexList[i].row());
}
} else if (ui->rbRepCurPage->isChecked()) { } else if (ui->rbRepCurPage->isChecked()) {
int row_count = tab_widget_->rowCount(); get_related_elements(vec, AREA_CUR_PAGE);
for (int i = 0; i < row_count; ++i) { handle(vec, false);
Element_t* e = get_element_by_row(i);
if (e == nullptr) {
continue;
}
if (is_common) {
replace_str(key, after, e);
} else {
replace_str(e, key, after);
}
ele_update_gui(e, i);
}
} else { } else {
if (!CUtil::affirm(this, u8"确认", u8"确认进行全局替换吗?")) { if (!CUtil::affirm(this, u8"确认", u8"确认进行全局替换吗?")) {
return; return;
} }
for (auto& data : vec_) { get_related_elements(vec, AREA_ALL);
if (is_common) { handle(vec, true);
replace_str(key, after, data);
} else {
replace_str(data, key, after);
}
}
xml_.get_all_elements(vec_); xml_.get_all_elements(vec_);
current_.clear(); current_.clear();
current_ = vec_; current_ = vec_;
search(); search(ui->edSearchKey->text());
} }
} }
@ -728,6 +722,40 @@ void MainWidget::replace_str(Element_t* ele, const QString& rg, const QString& a
} }
} }
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); });
break;
}
case AREA_CUR_PAGE: {
int rows = tab_widget_->rowCount();
for (int i = 0; i < rows; ++i) {
out.emplace_back(new OperElement(get_element_by_row(i), i));
}
break;
}
case AREA_ALL: {
out.resize(vec_.size());
std::transform(vec_.begin(), vec_.end(), out.begin(),
[](Element_t* ele) { return new OperElement(ele, 0); });
break;
}
default: {
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()));
}
break;
}
}
}
void MainWidget::backup_file() void MainWidget::backup_file()
{ {
if (tab_widget_ == nullptr) { if (tab_widget_ == nullptr) {
@ -742,8 +770,14 @@ void MainWidget::backup_file()
} }
} }
SElement_t::SElement_t(Element_t* e, const std::string& s) OperElement::OperElement(Element_t* ele, int row)
{
element_ = ele;
row_ = row;
}
SElement_t::SElement_t(Element_t* e, std::string& s)
{ {
ele = e; ele = e;
str = s; str = std::move(s);
} }

View File

@ -11,9 +11,17 @@
#include "src/attribute_edit.h" #include "src/attribute_edit.h"
struct SElement_t { struct SElement_t {
SElement_t(Element_t* e, const std::string& s); SElement_t(Element_t* e, std::string& s);
Element_t* ele{}; Element_t* ele;
std::string str{}; std::string str;
};
enum ReplaceArea { AREA_SELECT, AREA_ALL_PAGE, AREA_CUR_PAGE, AREA_ALL };
struct OperElement {
OperElement(Element_t* ele, int row);
Element_t* element_{};
int row_{0};
}; };
QT_BEGIN_NAMESPACE QT_BEGIN_NAMESPACE
@ -37,7 +45,7 @@ public:
private: private:
void read(const QString& file_path); void read(const QString& file_path);
void search(); void search(const QString& key);
void item_changed_handle(QTableWidgetItem* item); void item_changed_handle(QTableWidgetItem* item);
void save(); void save();
void copy_select_line(); void copy_select_line();
@ -60,6 +68,10 @@ protected:
void keyPressEvent(QKeyEvent* event); void keyPressEvent(QKeyEvent* event);
void replace_str(const QString& pre, const QString& after, Element_t* ele); void replace_str(const QString& pre, const QString& after, Element_t* ele);
void replace_str(Element_t* ele, const QString& rg, const QString& after); void replace_str(Element_t* ele, const QString& rg, const QString& after);
private:
void get_related_elements(std::vector<OperElement*>& out, ReplaceArea area);
private: private:
Element_t* get_element_by_key(const QString& key); Element_t* get_element_by_key(const QString& key);
Element_t* get_element_by_row(int row); Element_t* get_element_by_row(int row);

View File

@ -81,6 +81,13 @@
</property> </property>
</widget> </widget>
</item> </item>
<item>
<widget class="QRadioButton" name="rbAllPage">
<property name="text">
<string>所有页</string>
</property>
</widget>
</item>
<item> <item>
<widget class="QRadioButton" name="rbRepCurPage"> <widget class="QRadioButton" name="rbRepCurPage">
<property name="text"> <property name="text">