opti:优化替换范围,添加正则替换功能。
This commit is contained in:
		
							parent
							
								
									43c2d8680d
								
							
						
					
					
						commit
						aa6481cea4
					
				
							
								
								
									
										4
									
								
								.vscode/settings.json
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										4
									
								
								.vscode/settings.json
									
									
									
									
										vendored
									
									
								
							@ -100,6 +100,8 @@
 | 
				
			|||||||
        "qmenu": "cpp",
 | 
					        "qmenu": "cpp",
 | 
				
			||||||
        "qmessagebox": "cpp",
 | 
					        "qmessagebox": "cpp",
 | 
				
			||||||
        "qaction": "cpp",
 | 
					        "qaction": "cpp",
 | 
				
			||||||
        "codecvt": "cpp"
 | 
					        "codecvt": "cpp",
 | 
				
			||||||
 | 
					        "qregexp": "cpp",
 | 
				
			||||||
 | 
					        "qregularexpression": "cpp"
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@ -4,6 +4,7 @@
 | 
				
			|||||||
#include <QFile>
 | 
					#include <QFile>
 | 
				
			||||||
#include <QKeyEvent>
 | 
					#include <QKeyEvent>
 | 
				
			||||||
#include <QDateTime>
 | 
					#include <QDateTime>
 | 
				
			||||||
 | 
					#include <QRegularExpression>
 | 
				
			||||||
#include <filesystem>
 | 
					#include <filesystem>
 | 
				
			||||||
#include "src/data_edit.h"
 | 
					#include "src/data_edit.h"
 | 
				
			||||||
#include "./ui_MainWidget.h"
 | 
					#include "./ui_MainWidget.h"
 | 
				
			||||||
@ -52,7 +53,8 @@ MainWidget::MainWidget(QWidget* parent) : QWidget(parent), ui(new Ui::MainWidget
 | 
				
			|||||||
    connect(ui->btnSave, &QPushButton::clicked, this, [&]() { save(); });
 | 
					    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->btnReset, &QPushButton::clicked, this, &MainWidget::reset);
 | 
				
			||||||
    connect(ui->btnReplace, &QPushButton::clicked, this, &MainWidget::replace_content);
 | 
					    connect(ui->btnReplace, &QPushButton::clicked, this, [&]() { replace_content(true); });
 | 
				
			||||||
 | 
					    connect(ui->btnRxReplace, &QPushButton::clicked, this, [&]() { replace_content(false); });
 | 
				
			||||||
    connect(ui->btnExport, &QPushButton::clicked, this, &MainWidget::copy_multi_data);
 | 
					    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();
 | 
				
			||||||
@ -81,6 +83,9 @@ MainWidget::MainWidget(QWidget* parent) : QWidget(parent), ui(new Ui::MainWidget
 | 
				
			|||||||
        push_content(current_, cur);
 | 
					        push_content(current_, cur);
 | 
				
			||||||
    });
 | 
					    });
 | 
				
			||||||
    connect(ui->btnResort, &QPushButton::clicked, this, [&]() {
 | 
					    connect(ui->btnResort, &QPushButton::clicked, this, [&]() {
 | 
				
			||||||
 | 
					        if (tab_widget_ == nullptr) {
 | 
				
			||||||
 | 
					            return;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
        sort_by_repeat(vec_);
 | 
					        sort_by_repeat(vec_);
 | 
				
			||||||
        std::vector<Element_t*> nvec{};
 | 
					        std::vector<Element_t*> nvec{};
 | 
				
			||||||
        xml_.copy_and_del(vec_, nvec);
 | 
					        xml_.copy_and_del(vec_, nvec);
 | 
				
			||||||
@ -631,7 +636,7 @@ void MainWidget::copy_multi_data()
 | 
				
			|||||||
    edit.exec();
 | 
					    edit.exec();
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void MainWidget::replace_content()
 | 
					void MainWidget::replace_content(bool is_common)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    if (tab_widget_ == nullptr) {
 | 
					    if (tab_widget_ == nullptr) {
 | 
				
			||||||
        return;
 | 
					        return;
 | 
				
			||||||
@ -654,7 +659,11 @@ void MainWidget::replace_content()
 | 
				
			|||||||
            if (e == nullptr) {
 | 
					            if (e == nullptr) {
 | 
				
			||||||
                continue;
 | 
					                continue;
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
 | 
					            if (is_common) {
 | 
				
			||||||
                replace_str(key, after, e);
 | 
					                replace_str(key, after, e);
 | 
				
			||||||
 | 
					            } else {
 | 
				
			||||||
 | 
					                replace_str(e, key, after);
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
            ele_update_gui(e, indexList[i].row());
 | 
					            ele_update_gui(e, indexList[i].row());
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    } else if (ui->rbRepCurPage->isChecked()) {
 | 
					    } else if (ui->rbRepCurPage->isChecked()) {
 | 
				
			||||||
@ -664,24 +673,24 @@ void MainWidget::replace_content()
 | 
				
			|||||||
            if (e == nullptr) {
 | 
					            if (e == nullptr) {
 | 
				
			||||||
                continue;
 | 
					                continue;
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
 | 
					            if (is_common) {
 | 
				
			||||||
                replace_str(key, after, e);
 | 
					                replace_str(key, after, e);
 | 
				
			||||||
 | 
					            } else {
 | 
				
			||||||
 | 
					                replace_str(e, key, after);
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
            ele_update_gui(e, i);
 | 
					            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_) {
 | 
					        for (auto& data : vec_) {
 | 
				
			||||||
            auto* element = data->FirstAttribute();
 | 
					            if (is_common) {
 | 
				
			||||||
            while (element) {
 | 
					                replace_str(key, after, data);
 | 
				
			||||||
                QString content(element->Value());
 | 
					            } else {
 | 
				
			||||||
                content.replace(key, after);
 | 
					                replace_str(data, key, after);
 | 
				
			||||||
                data->SetAttribute(element->Name(), content.toStdString().c_str());
 | 
					 | 
				
			||||||
                element = element->Next();
 | 
					 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					 | 
				
			||||||
        xml_.get_all_elements(vec_);
 | 
					        xml_.get_all_elements(vec_);
 | 
				
			||||||
        current_.clear();
 | 
					        current_.clear();
 | 
				
			||||||
        current_ = vec_;
 | 
					        current_ = vec_;
 | 
				
			||||||
@ -694,14 +703,27 @@ void MainWidget::replace_str(const QString& pre, const QString& after, Element_t
 | 
				
			|||||||
    if (ele == nullptr) {
 | 
					    if (ele == nullptr) {
 | 
				
			||||||
        return;
 | 
					        return;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    auto* element = ele->FirstAttribute();
 | 
					    for (auto i = 0; i < keys_.size(); ++i) {
 | 
				
			||||||
    while (element) {
 | 
					        auto*   value = ele->Attribute(keys_[i].c_str());
 | 
				
			||||||
        QString content(element->Value());
 | 
					        QString content(value);
 | 
				
			||||||
        if (content.contains(pre)) {
 | 
					        if (content.contains(pre)) {
 | 
				
			||||||
            content.replace(pre, after);
 | 
					            content.replace(pre, after);
 | 
				
			||||||
            ele->SetAttribute(element->Name(), content.toStdString().c_str());
 | 
					            ele->SetAttribute(keys_[i].c_str(), content.toStdString().c_str());
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        element = element->Next();
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					void MainWidget::replace_str(Element_t* ele, const QString& rg, const QString& after)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    QRegularExpression rx(rg);
 | 
				
			||||||
 | 
					    if (ele == nullptr) {
 | 
				
			||||||
 | 
					        return;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    for (auto i = 0; i < keys_.size(); ++i) {
 | 
				
			||||||
 | 
					        auto*   value = ele->Attribute(keys_[i].c_str());
 | 
				
			||||||
 | 
					        QString content(value);
 | 
				
			||||||
 | 
					        content.replace(rx, after);
 | 
				
			||||||
 | 
					        ele->SetAttribute(keys_[i].c_str(), content.toStdString().c_str());
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -46,13 +46,14 @@ private:
 | 
				
			|||||||
    void show_custom_menu();
 | 
					    void show_custom_menu();
 | 
				
			||||||
    void sort_by_repeat(std::vector<Element_t*>& vec);
 | 
					    void sort_by_repeat(std::vector<Element_t*>& vec);
 | 
				
			||||||
    void copy_multi_data();
 | 
					    void copy_multi_data();
 | 
				
			||||||
    void replace_content();
 | 
					    void replace_content(bool is_common = true);
 | 
				
			||||||
    void backup_file();
 | 
					    void backup_file();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
protected:
 | 
					protected:
 | 
				
			||||||
    void closeEvent(QCloseEvent* event);
 | 
					    void closeEvent(QCloseEvent* event);
 | 
				
			||||||
    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);
 | 
				
			||||||
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);
 | 
				
			||||||
 | 
				
			|||||||
@ -37,6 +37,20 @@
 | 
				
			|||||||
        </property>
 | 
					        </property>
 | 
				
			||||||
       </widget>
 | 
					       </widget>
 | 
				
			||||||
      </item>
 | 
					      </item>
 | 
				
			||||||
 | 
					      <item>
 | 
				
			||||||
 | 
					       <widget class="QPushButton" name="btnBackup">
 | 
				
			||||||
 | 
					        <property name="text">
 | 
				
			||||||
 | 
					         <string>备份</string>
 | 
				
			||||||
 | 
					        </property>
 | 
				
			||||||
 | 
					       </widget>
 | 
				
			||||||
 | 
					      </item>
 | 
				
			||||||
 | 
					      <item>
 | 
				
			||||||
 | 
					       <widget class="QPushButton" name="btnResort">
 | 
				
			||||||
 | 
					        <property name="text">
 | 
				
			||||||
 | 
					         <string>重排序</string>
 | 
				
			||||||
 | 
					        </property>
 | 
				
			||||||
 | 
					       </widget>
 | 
				
			||||||
 | 
					      </item>
 | 
				
			||||||
      <item>
 | 
					      <item>
 | 
				
			||||||
       <widget class="QPushButton" name="btnSave">
 | 
					       <widget class="QPushButton" name="btnSave">
 | 
				
			||||||
        <property name="text">
 | 
					        <property name="text">
 | 
				
			||||||
@ -56,21 +70,14 @@
 | 
				
			|||||||
      <item>
 | 
					      <item>
 | 
				
			||||||
       <widget class="QPushButton" name="btnExport">
 | 
					       <widget class="QPushButton" name="btnExport">
 | 
				
			||||||
        <property name="text">
 | 
					        <property name="text">
 | 
				
			||||||
         <string>导出选择行</string>
 | 
					         <string>导出</string>
 | 
				
			||||||
        </property>
 | 
					        </property>
 | 
				
			||||||
       </widget>
 | 
					       </widget>
 | 
				
			||||||
      </item>
 | 
					      </item>
 | 
				
			||||||
      <item>
 | 
					      <item>
 | 
				
			||||||
       <widget class="QPushButton" name="btnImport">
 | 
					       <widget class="QPushButton" name="btnImport">
 | 
				
			||||||
        <property name="text">
 | 
					        <property name="text">
 | 
				
			||||||
         <string>导入行</string>
 | 
					         <string>导入</string>
 | 
				
			||||||
        </property>
 | 
					 | 
				
			||||||
       </widget>
 | 
					 | 
				
			||||||
      </item>
 | 
					 | 
				
			||||||
      <item>
 | 
					 | 
				
			||||||
       <widget class="QPushButton" name="btnBackup">
 | 
					 | 
				
			||||||
        <property name="text">
 | 
					 | 
				
			||||||
         <string>备份快照</string>
 | 
					 | 
				
			||||||
        </property>
 | 
					        </property>
 | 
				
			||||||
       </widget>
 | 
					       </widget>
 | 
				
			||||||
      </item>
 | 
					      </item>
 | 
				
			||||||
@ -108,7 +115,14 @@
 | 
				
			|||||||
      <item>
 | 
					      <item>
 | 
				
			||||||
       <widget class="QPushButton" name="btnReplace">
 | 
					       <widget class="QPushButton" name="btnReplace">
 | 
				
			||||||
        <property name="text">
 | 
					        <property name="text">
 | 
				
			||||||
         <string>替换为</string>
 | 
					         <string>一般替换</string>
 | 
				
			||||||
 | 
					        </property>
 | 
				
			||||||
 | 
					       </widget>
 | 
				
			||||||
 | 
					      </item>
 | 
				
			||||||
 | 
					      <item>
 | 
				
			||||||
 | 
					       <widget class="QPushButton" name="btnRxReplace">
 | 
				
			||||||
 | 
					        <property name="text">
 | 
				
			||||||
 | 
					         <string>正则替换</string>
 | 
				
			||||||
        </property>
 | 
					        </property>
 | 
				
			||||||
       </widget>
 | 
					       </widget>
 | 
				
			||||||
      </item>
 | 
					      </item>
 | 
				
			||||||
@ -125,7 +139,7 @@
 | 
				
			|||||||
      <item>
 | 
					      <item>
 | 
				
			||||||
       <widget class="QCheckBox" name="cbCaseSensitive">
 | 
					       <widget class="QCheckBox" name="cbCaseSensitive">
 | 
				
			||||||
        <property name="text">
 | 
					        <property name="text">
 | 
				
			||||||
         <string>搜索区分大小写</string>
 | 
					         <string>区分大小写</string>
 | 
				
			||||||
        </property>
 | 
					        </property>
 | 
				
			||||||
       </widget>
 | 
					       </widget>
 | 
				
			||||||
      </item>
 | 
					      </item>
 | 
				
			||||||
@ -153,13 +167,6 @@
 | 
				
			|||||||
        </property>
 | 
					        </property>
 | 
				
			||||||
       </widget>
 | 
					       </widget>
 | 
				
			||||||
      </item>
 | 
					      </item>
 | 
				
			||||||
      <item>
 | 
					 | 
				
			||||||
       <widget class="QPushButton" name="btnResort">
 | 
					 | 
				
			||||||
        <property name="text">
 | 
					 | 
				
			||||||
         <string>重新排序</string>
 | 
					 | 
				
			||||||
        </property>
 | 
					 | 
				
			||||||
       </widget>
 | 
					 | 
				
			||||||
      </item>
 | 
					 | 
				
			||||||
     </layout>
 | 
					     </layout>
 | 
				
			||||||
    </widget>
 | 
					    </widget>
 | 
				
			||||||
   </item>
 | 
					   </item>
 | 
				
			||||||
@ -282,7 +289,6 @@
 | 
				
			|||||||
  <tabstop>btnSave</tabstop>
 | 
					  <tabstop>btnSave</tabstop>
 | 
				
			||||||
  <tabstop>btnExport</tabstop>
 | 
					  <tabstop>btnExport</tabstop>
 | 
				
			||||||
  <tabstop>btnImport</tabstop>
 | 
					  <tabstop>btnImport</tabstop>
 | 
				
			||||||
  <tabstop>btnBackup</tabstop>
 | 
					 | 
				
			||||||
  <tabstop>edRepPre</tabstop>
 | 
					  <tabstop>edRepPre</tabstop>
 | 
				
			||||||
  <tabstop>edRepAfter</tabstop>
 | 
					  <tabstop>edRepAfter</tabstop>
 | 
				
			||||||
  <tabstop>btnReplace</tabstop>
 | 
					  <tabstop>btnReplace</tabstop>
 | 
				
			||||||
@ -290,7 +296,6 @@
 | 
				
			|||||||
  <tabstop>edSearchKey</tabstop>
 | 
					  <tabstop>edSearchKey</tabstop>
 | 
				
			||||||
  <tabstop>btnSearch</tabstop>
 | 
					  <tabstop>btnSearch</tabstop>
 | 
				
			||||||
  <tabstop>btnReset</tabstop>
 | 
					  <tabstop>btnReset</tabstop>
 | 
				
			||||||
  <tabstop>btnResort</tabstop>
 | 
					 | 
				
			||||||
  <tabstop>btnExit</tabstop>
 | 
					  <tabstop>btnExit</tabstop>
 | 
				
			||||||
  <tabstop>edCurPage</tabstop>
 | 
					  <tabstop>edCurPage</tabstop>
 | 
				
			||||||
  <tabstop>edAllPage</tabstop>
 | 
					  <tabstop>edAllPage</tabstop>
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user