Răsfoiți Sursa

feat:增加历史记录功能,能打开上一次的位置;增加显示筛选数量和结果数量

ChenYL 2 ani în urmă
părinte
comite
b31d14c092
5 a modificat fișierele cu 97 adăugiri și 73 ștergeri
  1. 3 1
      .gitignore
  2. 9 7
      README.md
  3. 16 16
      src/DataFilter.py
  4. 4 4
      src/DataFilter.ui
  5. 65 45
      src/main.py

+ 3 - 1
.gitignore

@@ -158,4 +158,6 @@ cython_debug/
 #  and can be added to the global gitignore or merged into this file.  For a more nuclear
 #  option (not recommended) you can uncomment the following to ignore the entire idea folder.
 .idea/
-*.iml
+*.iml
+
+src/config.json

+ 9 - 7
README.md

@@ -1,13 +1,15 @@
 # 开发记录
 
 待办列表
-* 调整界面结构,增大结果区域
-* 增加正则匹配不限制位置
-* 增加历史记录功能,能打开上一次的位置
+* 打开时恢复至上次的状态
 * 增加操作日志
-* 增加显示筛选数量和结果数量
 
 开发进度
-* 2023-07-05 增加文件选择功能
-* 2023-07-06 增加正则匹配不限制位置
-* 2023-07-08 增加支持4级筛选
+* 2023-07-05
+  - 增加文件选择功能
+* 2023-07-06 
+  - 增加正则匹配不限制位置
+* 2023-07-08
+  - 增加支持4级筛选;调整界面结构,增大结果区域
+  - 增加历史记录功能,能打开上一次的位置
+  - 增加显示筛选数量和结果数量

+ 16 - 16
src/DataFilter.py

@@ -95,10 +95,10 @@ class Ui_Form(object):
 
         self.verticalLayout.addWidget(self.firstCategoryBox)
 
-        self.label_3 = QLabel(Form)
-        self.label_3.setObjectName(u"label_3")
+        self.result_label_1 = QLabel(Form)
+        self.result_label_1.setObjectName(u"result_label_1")
 
-        self.verticalLayout.addWidget(self.label_3)
+        self.verticalLayout.addWidget(self.result_label_1)
 
         self.firstResultBox = QTextBrowser(Form)
         self.firstResultBox.setObjectName(u"firstResultBox")
@@ -156,10 +156,10 @@ class Ui_Form(object):
 
         self.verticalLayout_4.addWidget(self.secondCategoryBox)
 
-        self.label_5 = QLabel(Form)
-        self.label_5.setObjectName(u"label_5")
+        self.result_label_2 = QLabel(Form)
+        self.result_label_2.setObjectName(u"result_label_2")
 
-        self.verticalLayout_4.addWidget(self.label_5)
+        self.verticalLayout_4.addWidget(self.result_label_2)
 
         self.secondResultBox = QTextBrowser(Form)
         self.secondResultBox.setObjectName(u"secondResultBox")
@@ -215,10 +215,10 @@ class Ui_Form(object):
 
         self.verticalLayout_7.addWidget(self.threeCategoryBox)
 
-        self.label_7 = QLabel(Form)
-        self.label_7.setObjectName(u"label_7")
+        self.result_label_3 = QLabel(Form)
+        self.result_label_3.setObjectName(u"result_label_3")
 
-        self.verticalLayout_7.addWidget(self.label_7)
+        self.verticalLayout_7.addWidget(self.result_label_3)
 
         self.threeResultBox = QTextBrowser(Form)
         self.threeResultBox.setObjectName(u"threeResultBox")
@@ -274,10 +274,10 @@ class Ui_Form(object):
 
         self.verticalLayout_10.addWidget(self.fourCategoryBox)
 
-        self.label_9 = QLabel(Form)
-        self.label_9.setObjectName(u"label_9")
+        self.result_label_4 = QLabel(Form)
+        self.result_label_4.setObjectName(u"result_label_4")
 
-        self.verticalLayout_10.addWidget(self.label_9)
+        self.verticalLayout_10.addWidget(self.result_label_4)
 
         self.fourResultBox = QTextBrowser(Form)
         self.fourResultBox.setObjectName(u"fourResultBox")
@@ -307,24 +307,24 @@ class Ui_Form(object):
         self.firstCharacterBtn.setText(QCoreApplication.translate("Form", u"\u63d2\u5165\u82f1\u6587", None))
         self.firstFilterBtn.setText(QCoreApplication.translate("Form", u"\u63d0\u53d6", None))
         self.label_2.setText(QCoreApplication.translate("Form", u"\u81ea\u5b9a\u4e49\u7c7b\u522b\u8bcd", None))
-        self.label_3.setText(QCoreApplication.translate("Form", u"\u63d0\u53d6\u7ed3\u679c", None))
+        self.result_label_1.setText(QCoreApplication.translate("Form", u"\u63d0\u53d6\u7ed3\u679c", None))
         self.secondDigitBtn.setText(QCoreApplication.translate("Form", u"\u63d2\u5165\u6570\u5b57", None))
         self.secondCategoryBtn.setText(QCoreApplication.translate("Form", u"\u63d2\u5165\u7c7b\u522b", None))
         self.secondCharacterBtn.setText(QCoreApplication.translate("Form", u"\u63d2\u5165\u82f1\u6587", None))
         self.secondFilterBtn.setText(QCoreApplication.translate("Form", u"\u63d0\u53d6", None))
         self.label_4.setText(QCoreApplication.translate("Form", u"\u81ea\u5b9a\u4e49\u7c7b\u522b\u8bcd", None))
-        self.label_5.setText(QCoreApplication.translate("Form", u"\u63d0\u53d6\u7ed3\u679c", None))
+        self.result_label_2.setText(QCoreApplication.translate("Form", u"\u63d0\u53d6\u7ed3\u679c", None))
         self.threeDigitBtn.setText(QCoreApplication.translate("Form", u"\u63d2\u5165\u6570\u5b57", None))
         self.threeCategoryBtn.setText(QCoreApplication.translate("Form", u"\u63d2\u5165\u7c7b\u522b", None))
         self.threeCharacterBtn.setText(QCoreApplication.translate("Form", u"\u63d2\u5165\u82f1\u6587", None))
         self.threeFilterBtn.setText(QCoreApplication.translate("Form", u"\u63d0\u53d6", None))
         self.label_6.setText(QCoreApplication.translate("Form", u"\u81ea\u5b9a\u4e49\u7c7b\u522b\u8bcd", None))
-        self.label_7.setText(QCoreApplication.translate("Form", u"\u63d0\u53d6\u7ed3\u679c", None))
+        self.result_label_3.setText(QCoreApplication.translate("Form", u"\u63d0\u53d6\u7ed3\u679c", None))
         self.fourDigitBtn.setText(QCoreApplication.translate("Form", u"\u63d2\u5165\u6570\u5b57", None))
         self.fourCategoryBtn.setText(QCoreApplication.translate("Form", u"\u63d2\u5165\u7c7b\u522b", None))
         self.fourCharacterBtn.setText(QCoreApplication.translate("Form", u"\u63d2\u5165\u82f1\u6587", None))
         self.fourFilterBtn.setText(QCoreApplication.translate("Form", u"\u63d0\u53d6", None))
         self.label_8.setText(QCoreApplication.translate("Form", u"\u81ea\u5b9a\u4e49\u7c7b\u522b\u8bcd", None))
-        self.label_9.setText(QCoreApplication.translate("Form", u"\u63d0\u53d6\u7ed3\u679c", None))
+        self.result_label_4.setText(QCoreApplication.translate("Form", u"\u63d0\u53d6\u7ed3\u679c", None))
     # retranslateUi
 

+ 4 - 4
src/DataFilter.ui

@@ -107,7 +107,7 @@
         </widget>
        </item>
        <item>
-        <widget class="QLabel" name="label_3">
+        <widget class="QLabel" name="result_label_1">
          <property name="text">
           <string>提取结果</string>
          </property>
@@ -180,7 +180,7 @@
         </widget>
        </item>
        <item>
-        <widget class="QLabel" name="label_5">
+        <widget class="QLabel" name="result_label_2">
          <property name="text">
           <string>提取结果</string>
          </property>
@@ -246,7 +246,7 @@
         </widget>
        </item>
        <item>
-        <widget class="QLabel" name="label_7">
+        <widget class="QLabel" name="result_label_3">
          <property name="text">
           <string>提取结果</string>
          </property>
@@ -312,7 +312,7 @@
         </widget>
        </item>
        <item>
-        <widget class="QLabel" name="label_9">
+        <widget class="QLabel" name="result_label_4">
          <property name="text">
           <string>提取结果</string>
          </property>

+ 65 - 45
src/main.py

@@ -1,4 +1,6 @@
 # -*- coding: utf-8 -*-
+import json
+import os.path
 import re
 import sys
 from functools import partial
@@ -12,17 +14,20 @@ category_pattern = re.compile(r'\[类别\]')
 digit_pattern = re.compile(r'\[数字\]')
 english_pattern = re.compile(r'\[字母\]')
 
+CHARACTER_FILTER_STR = "[字母]"
+DIGIT_FILTER_STR = "[数字]"
+CATEGORY_FILTER_STR = "[类别]"
 
-class MyMainForm(QWidget, Ui_Form):
+CONFIG_FILE_PATH = "./config.json"
+CONFIG_ITEM_LAST_SELECT_FILE_PATH = "lastSelectFilePath"
 
-  CHARACTER_FILTER_STR = "[字母]"
-  DIGIT_FILTER_STR = "[数字]"
-  CATEGORY_FILTER_STR = "[类别]"
+class MyMainForm(QWidget, Ui_Form):
 
   def __init__(self, parent=None):
     super(MyMainForm, self).__init__(parent)
     self.setupUi(self)
     self.bind()
+    self.loadConfig()
 
   def bind(self):
     self.toolDict = {
@@ -40,37 +45,46 @@ class MyMainForm(QWidget, Ui_Form):
       self.fourCharacterBtn.objectName(): self.fourKeyBox
     }
     self.resultDict = {
-      self.firstFilterBtn.objectName(): (self.firstKeyBox, self.firstCategoryBox, self.firstResultBox, None),
-      self.secondFilterBtn.objectName(): (self.secondKeyBox, self.secondCategoryBox, self.secondResultBox, self.firstResultBox),
-      self.threeFilterBtn.objectName(): (self.threeKeyBox, self.threeCategoryBox, self.threeResultBox, self.secondResultBox),
-      self.fourFilterBtn.objectName(): (self.fourKeyBox, self.fourCategoryBox, self.fourResultBox, self.threeResultBox)
+      self.firstFilterBtn.objectName(): (self.firstKeyBox, self.firstCategoryBox, self.firstResultBox, None, self.result_label_1),
+      self.secondFilterBtn.objectName(): (self.secondKeyBox, self.secondCategoryBox, self.secondResultBox, self.firstResultBox, self.result_label_2),
+      self.threeFilterBtn.objectName(): (self.threeKeyBox, self.threeCategoryBox, self.threeResultBox, self.secondResultBox, self.result_label_3),
+      self.fourFilterBtn.objectName(): (self.fourKeyBox, self.fourCategoryBox, self.fourResultBox, self.threeResultBox, self.result_label_4)
     }
 
     self.fileBtn.clicked.connect(self.selectFile)
 
-    self.firstCategoryBtn.clicked.connect(partial(self.add_filter_str, self.firstCategoryBtn, self.CATEGORY_FILTER_STR))
-    self.firstDigitBtn.clicked.connect(partial(self.add_filter_str, self.firstDigitBtn, self.DIGIT_FILTER_STR))
-    self.firstCharacterBtn.clicked.connect(partial(self.add_filter_str, self.firstCharacterBtn, self.CHARACTER_FILTER_STR))
+    self.firstCategoryBtn.clicked.connect(partial(self.add_filter_str, self.firstCategoryBtn, CATEGORY_FILTER_STR))
+    self.firstDigitBtn.clicked.connect(partial(self.add_filter_str, self.firstDigitBtn, DIGIT_FILTER_STR))
+    self.firstCharacterBtn.clicked.connect(partial(self.add_filter_str, self.firstCharacterBtn, CHARACTER_FILTER_STR))
     self.firstFilterBtn.clicked.connect(partial(self.submit, self.firstFilterBtn))
 
-    self.secondCategoryBtn.clicked.connect(partial(self.add_filter_str, self.secondCategoryBtn, self.CATEGORY_FILTER_STR))
-    self.secondDigitBtn.clicked.connect(partial(self.add_filter_str, self.secondDigitBtn, self.DIGIT_FILTER_STR))
-    self.secondCharacterBtn.clicked.connect(partial(self.add_filter_str, self.secondCharacterBtn, self.CHARACTER_FILTER_STR))
+    self.secondCategoryBtn.clicked.connect(partial(self.add_filter_str, self.secondCategoryBtn, CATEGORY_FILTER_STR))
+    self.secondDigitBtn.clicked.connect(partial(self.add_filter_str, self.secondDigitBtn, DIGIT_FILTER_STR))
+    self.secondCharacterBtn.clicked.connect(partial(self.add_filter_str, self.secondCharacterBtn, CHARACTER_FILTER_STR))
     self.secondFilterBtn.clicked.connect(partial(self.submit, self.secondFilterBtn))
 
-    self.threeCategoryBtn.clicked.connect(partial(self.add_filter_str, self.threeCategoryBtn, self.CATEGORY_FILTER_STR))
-    self.threeDigitBtn.clicked.connect(partial(self.add_filter_str, self.threeDigitBtn, self.DIGIT_FILTER_STR))
-    self.threeCharacterBtn.clicked.connect(partial(self.add_filter_str, self.threeCharacterBtn, self.CHARACTER_FILTER_STR))
+    self.threeCategoryBtn.clicked.connect(partial(self.add_filter_str, self.threeCategoryBtn, CATEGORY_FILTER_STR))
+    self.threeDigitBtn.clicked.connect(partial(self.add_filter_str, self.threeDigitBtn, DIGIT_FILTER_STR))
+    self.threeCharacterBtn.clicked.connect(partial(self.add_filter_str, self.threeCharacterBtn, CHARACTER_FILTER_STR))
     self.threeFilterBtn.clicked.connect(partial(self.submit, self.threeFilterBtn))
 
-    self.fourCategoryBtn.clicked.connect(partial(self.add_filter_str, self.fourCategoryBtn, self.CATEGORY_FILTER_STR))
-    self.fourDigitBtn.clicked.connect(partial(self.add_filter_str, self.fourDigitBtn, self.DIGIT_FILTER_STR))
-    self.fourCharacterBtn.clicked.connect(partial(self.add_filter_str, self.fourCharacterBtn, self.CHARACTER_FILTER_STR))
+    self.fourCategoryBtn.clicked.connect(partial(self.add_filter_str, self.fourCategoryBtn, CATEGORY_FILTER_STR))
+    self.fourDigitBtn.clicked.connect(partial(self.add_filter_str, self.fourDigitBtn, DIGIT_FILTER_STR))
+    self.fourCharacterBtn.clicked.connect(partial(self.add_filter_str, self.fourCharacterBtn, CHARACTER_FILTER_STR))
     self.fourFilterBtn.clicked.connect(partial(self.submit, self.fourFilterBtn))
 
+  def loadConfig(self):
+    if os.path.isfile(CONFIG_FILE_PATH):
+      with open(CONFIG_FILE_PATH, 'r', encoding='utf-8') as f:
+        config = json.loads(f.read())
+        self.filePathBox.setText(config[CONFIG_ITEM_LAST_SELECT_FILE_PATH])
+
+
   def selectFile(self):
-    file_path = QFileDialog.getOpenFileName(self, "选择文件")
-    self.filePathBox.setText(file_path[0])
+    file_path, file_type = QFileDialog.getOpenFileName(self, "选择文件")
+    with open(CONFIG_FILE_PATH, 'w', encoding='utf-8') as f:
+      f.write(json.dumps({CONFIG_ITEM_LAST_SELECT_FILE_PATH: file_path}))
+    self.filePathBox.setText(file_path)
 
   def add_filter_str(self, btn_widget: QPushButton, filter_text):
     key_box = self.toolDict[btn_widget.objectName()]
@@ -78,33 +92,14 @@ class MyMainForm(QWidget, Ui_Form):
 
   def submit(self, filter_btn: QPushButton):
 
-    key_box, category_box, result_box, parent_result_box = self.resultDict[filter_btn.objectName()]
+    key_box, category_box, result_box, parent_result_box, result_label = self.resultDict[filter_btn.objectName()]
 
     if not self.check(key_box, category_box, parent_result_box):
       return
 
-    key_text = key_box.text()
-
-    originKeyArray = None
-    if parent_result_box is None:
-      with open(self.filePathBox.text(), 'r', encoding='utf-8') as f:
-        originKeyArray = [content.replace("\n", "") for content in f.readlines()]
-    else:
-      originKeyArray = parent_result_box.toPlainText().split("\n")
-
-    resultKeyArray = []
-    if category_pattern.search(key_text) is not None:
-      categoryKeyArray = category_box.toPlainText().splitlines()
-      for categoryKey in categoryKeyArray:
-        resultKeyArray.extend(self.filter(originKeyArray, key_text, "类别", categoryKey))
-    elif digit_pattern.search(key_text) is not None:
-      resultKeyArray.extend(self.filter(originKeyArray, key_text, "数字", "0-9"))
-    elif english_pattern.search(key_text) is not None:
-      resultKeyArray.extend(self.filter(originKeyArray, key_text, "字母", "A-Za-z"))
-    else:
-      resultKeyArray.extend(self.filter(originKeyArray, key_text))
-
-    result_box.setText("\n".join(resultKeyArray))
+    before_filter_cnt, after_filter_cnt, filter_result_arr = self.deal(key_box, category_box, parent_result_box)
+    result_label.setText("提取结果:原始数据%s条,筛选后%s条" % (before_filter_cnt, after_filter_cnt))
+    result_box.setText("\n".join(filter_result_arr))
 
   def check(self, key_box: QLineEdit, category_box: QTextEdit, parent_result_box: QTextBrowser):
     key_text = key_box.text()
@@ -136,6 +131,31 @@ class MyMainForm(QWidget, Ui_Form):
 
     return True
 
+  def deal(self, key_box: QLineEdit, category_box: QTextEdit, parent_result_box: QTextBrowser):
+    key_text = key_box.text()
+
+    parent_key_arr = None
+    if parent_result_box is None:
+      with open(self.filePathBox.text(), 'r', encoding='utf-8') as f:
+        parent_key_arr = [content.replace("\n", "") for content in f.readlines()]
+    else:
+      parent_key_arr = parent_result_box.toPlainText().split("\n")
+
+    filter_result_arr = None
+    if category_pattern.search(key_text) is not None:
+      filter_result_arr = set()
+      categoryKeyArray = category_box.toPlainText().splitlines()
+      for categoryKey in categoryKeyArray:
+        filter_result_arr.update(self.filter(parent_key_arr, key_text, "类别", categoryKey))
+    elif digit_pattern.search(key_text) is not None:
+      filter_result_arr = self.filter(parent_key_arr, key_text, "数字", "0-9")
+    elif english_pattern.search(key_text) is not None:
+      filter_result_arr = self.filter(parent_key_arr, key_text, "字母", "A-Za-z")
+    else:
+      filter_result_arr = self.filter(parent_key_arr, key_text)
+
+    return len(parent_key_arr), len(filter_result_arr), filter_result_arr
+
   def filter(self, originArray, inputText, oldStr=None, newStr=None):
     resultArray = []
     key_pattern = None