func:替换添加所有页选项。
This commit is contained in:
parent
bd7f2681ab
commit
2a7af82b0b
128
MainWidget.cpp
128
MainWidget.cpp
@ -16,10 +16,9 @@ MainWidget::MainWidget(QWidget* parent) : QWidget(parent), ui(new Ui::MainWidget
|
||||
{
|
||||
ui->setupUi(this);
|
||||
|
||||
setWindowTitle(u8"OneLevelXmlOpr v1.2.9");
|
||||
setWindowTitle(u8"OneLevelXmlOpr v1.2.10");
|
||||
setWindowIcon(QIcon("://resource/xml.ico"));
|
||||
|
||||
|
||||
QScreen* primaryScreen = QGuiApplication::primaryScreen();
|
||||
QRect screenGeometry = primaryScreen->geometry();
|
||||
setMinimumWidth(screenGeometry.width() * 0.5);
|
||||
@ -53,7 +52,7 @@ MainWidget::MainWidget(QWidget* parent) : QWidget(parent), ui(new Ui::MainWidget
|
||||
}
|
||||
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->btnRead, &QPushButton::clicked, this, [&]() { read(ui->edStatus->text().trimmed()); });
|
||||
connect(ui->btnSave, &QPushButton::clicked, this, [&]() { save(); });
|
||||
@ -143,7 +142,7 @@ void MainWidget::keyPressEvent(QKeyEvent* event)
|
||||
{
|
||||
switch (event->key()) {
|
||||
case Qt::Key_Return:
|
||||
search();
|
||||
search(ui->edSearchKey->text());
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
@ -298,10 +297,10 @@ void MainWidget::read(const QString& file_path)
|
||||
ui->btnBackup->setEnabled(true);
|
||||
}
|
||||
|
||||
void MainWidget::search()
|
||||
void MainWidget::search(const QString& key)
|
||||
{
|
||||
QString key = ui->edSearchKey->text().trimmed();
|
||||
if (key.isEmpty()) {
|
||||
QString bkey = key;
|
||||
if (bkey.isEmpty()) {
|
||||
current_ = vec_;
|
||||
push_content(current_);
|
||||
return;
|
||||
@ -311,7 +310,7 @@ void MainWidget::search()
|
||||
}
|
||||
|
||||
if (!ui->cbCaseSensitive->isChecked()) {
|
||||
key = key.toUpper();
|
||||
bkey = bkey.toUpper();
|
||||
}
|
||||
|
||||
current_.clear();
|
||||
@ -322,7 +321,7 @@ void MainWidget::search()
|
||||
if (!ui->cbCaseSensitive->isChecked()) {
|
||||
qdata = qdata.toUpper();
|
||||
}
|
||||
if (!qdata.contains(key)) {
|
||||
if (!qdata.contains(bkey)) {
|
||||
continue;
|
||||
}
|
||||
current_.push_back(item);
|
||||
@ -394,7 +393,7 @@ void MainWidget::copy_select_line()
|
||||
}
|
||||
int df = it - vec_.begin() + 1;
|
||||
vec_.insert(vec_.begin() + df, newer);
|
||||
search();
|
||||
search(ui->edSearchKey->text());
|
||||
}
|
||||
|
||||
// 返回 true 表示确认编辑了, false 表示取消编辑了。
|
||||
@ -521,7 +520,7 @@ void MainWidget::del_select_line()
|
||||
}
|
||||
}
|
||||
vec_.erase(it);
|
||||
search();
|
||||
search(ui->edSearchKey->text());
|
||||
}
|
||||
|
||||
Element_t* MainWidget::get_current_select_key()
|
||||
@ -648,54 +647,49 @@ void MainWidget::replace_content(bool is_common)
|
||||
CUtil::msg(this, u8"替换前数据为空。");
|
||||
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()) {
|
||||
QModelIndexList indexList = tab_widget_->selectionModel()->selectedRows();
|
||||
if (indexList.size() < 1) {
|
||||
CUtil::msg(this, u8"无选择数据");
|
||||
return;
|
||||
}
|
||||
QString ret;
|
||||
for (int i = 0; i < indexList.size(); ++i) {
|
||||
Element_t* e = get_element_by_row(indexList[i].row());
|
||||
if (e == nullptr) {
|
||||
continue;
|
||||
}
|
||||
if (is_common) {
|
||||
replace_str(key, after, e);
|
||||
} else {
|
||||
replace_str(e, key, after);
|
||||
}
|
||||
ele_update_gui(e, indexList[i].row());
|
||||
}
|
||||
get_related_elements(vec, AREA_SELECT);
|
||||
handle(vec, false);
|
||||
} else if (ui->rbAllPage->isChecked()) {
|
||||
get_related_elements(vec, AREA_ALL_PAGE);
|
||||
handle(vec, false);
|
||||
xml_.get_all_elements(vec_);
|
||||
current_.clear();
|
||||
current_ = vec_;
|
||||
search(ui->edRepAfter->text());
|
||||
} else if (ui->rbRepCurPage->isChecked()) {
|
||||
int row_count = tab_widget_->rowCount();
|
||||
for (int i = 0; i < row_count; ++i) {
|
||||
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);
|
||||
}
|
||||
get_related_elements(vec, AREA_CUR_PAGE);
|
||||
handle(vec, false);
|
||||
} else {
|
||||
if (!CUtil::affirm(this, u8"确认", u8"确认进行全局替换吗?")) {
|
||||
return;
|
||||
}
|
||||
for (auto& data : vec_) {
|
||||
if (is_common) {
|
||||
replace_str(key, after, data);
|
||||
} else {
|
||||
replace_str(data, key, after);
|
||||
}
|
||||
}
|
||||
get_related_elements(vec, AREA_ALL);
|
||||
handle(vec, true);
|
||||
xml_.get_all_elements(vec_);
|
||||
current_.clear();
|
||||
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()
|
||||
{
|
||||
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;
|
||||
str = s;
|
||||
str = std::move(s);
|
||||
}
|
||||
|
20
MainWidget.h
20
MainWidget.h
@ -11,9 +11,17 @@
|
||||
#include "src/attribute_edit.h"
|
||||
|
||||
struct SElement_t {
|
||||
SElement_t(Element_t* e, const std::string& s);
|
||||
Element_t* ele{};
|
||||
std::string str{};
|
||||
SElement_t(Element_t* e, std::string& s);
|
||||
Element_t* ele;
|
||||
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
|
||||
@ -37,7 +45,7 @@ public:
|
||||
|
||||
private:
|
||||
void read(const QString& file_path);
|
||||
void search();
|
||||
void search(const QString& key);
|
||||
void item_changed_handle(QTableWidgetItem* item);
|
||||
void save();
|
||||
void copy_select_line();
|
||||
@ -60,6 +68,10 @@ protected:
|
||||
void keyPressEvent(QKeyEvent* event);
|
||||
void replace_str(const QString& pre, const QString& after, Element_t* ele);
|
||||
void replace_str(Element_t* ele, const QString& rg, const QString& after);
|
||||
|
||||
private:
|
||||
void get_related_elements(std::vector<OperElement*>& out, ReplaceArea area);
|
||||
|
||||
private:
|
||||
Element_t* get_element_by_key(const QString& key);
|
||||
Element_t* get_element_by_row(int row);
|
||||
|
@ -81,6 +81,13 @@
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QRadioButton" name="rbAllPage">
|
||||
<property name="text">
|
||||
<string>所有页</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QRadioButton" name="rbRepCurPage">
|
||||
<property name="text">
|
||||
|
Loading…
x
Reference in New Issue
Block a user