[Mod] improve find/replace function

This commit is contained in:
vn.py 2019-11-01 09:19:17 +08:00
parent 31c7921c57
commit 21f525ac96

View File

@ -45,8 +45,8 @@ class CodeEditor(QtWidgets.QMainWindow):
bar = self.menuBar() bar = self.menuBar()
file_menu = bar.addMenu("文件") file_menu = bar.addMenu("文件")
self.add_menu_action(file_menu, "新建", self.new_file, "Ctrl+N") self.add_menu_action(file_menu, "新建文件", self.new_file, "Ctrl+N")
self.add_menu_action(file_menu, "打开", self.open_file, "Ctrl+O") self.add_menu_action(file_menu, "打开文件", self.open_file, "Ctrl+O")
self.add_menu_action(file_menu, "保存", self.save_file, "Ctrl+S") self.add_menu_action(file_menu, "保存", self.save_file, "Ctrl+S")
self.add_menu_action( self.add_menu_action(
file_menu, file_menu,
@ -58,6 +58,9 @@ class CodeEditor(QtWidgets.QMainWindow):
self.add_menu_action(file_menu, "退出", self.close) self.add_menu_action(file_menu, "退出", self.close)
edit_menu = bar.addMenu("编辑") edit_menu = bar.addMenu("编辑")
self.add_menu_action(edit_menu, "撤销", self.undo, "Ctrl+Z")
self.add_menu_action(edit_menu, "恢复", self.redo, "Ctrl+Y")
edit_menu.addSeparator()
self.add_menu_action(edit_menu, "复制", self.copy, "Ctrl+C") self.add_menu_action(edit_menu, "复制", self.copy, "Ctrl+C")
self.add_menu_action(edit_menu, "粘贴", self.paste, "Ctrl+P") self.add_menu_action(edit_menu, "粘贴", self.paste, "Ctrl+P")
self.add_menu_action(edit_menu, "剪切", self.cut, "Ctrl+X") self.add_menu_action(edit_menu, "剪切", self.cut, "Ctrl+X")
@ -65,8 +68,6 @@ class CodeEditor(QtWidgets.QMainWindow):
self.add_menu_action(edit_menu, "查找", self.find, "Ctrl+F") self.add_menu_action(edit_menu, "查找", self.find, "Ctrl+F")
self.add_menu_action(edit_menu, "替换", self.replace, "Ctrl+H") self.add_menu_action(edit_menu, "替换", self.replace, "Ctrl+H")
help_menu = bar.addMenu("帮助")
def add_menu_action( def add_menu_action(
self, self,
menu: QtWidgets.QMenu, menu: QtWidgets.QMenu,
@ -175,7 +176,7 @@ class CodeEditor(QtWidgets.QMainWindow):
def open_file(self): def open_file(self):
"""""" """"""
file_path, _ = QtWidgets.QFileDialog.getOpenFileName( file_path, _ = QtWidgets.QFileDialog.getOpenFileName(
self, "打开", "", "Python(*.py)") self, "打开文件", "", "Python(*.py)")
if file_path: if file_path:
self.open_editor(file_path) self.open_editor(file_path)
@ -225,11 +226,19 @@ class CodeEditor(QtWidgets.QMainWindow):
def paste(self): def paste(self):
"""""" """"""
self.get_active_editor.paste() self.get_active_editor().paste()
def undo(self):
""""""
self.get_active_editor().undo()
def redo(self):
""""""
self.get_active_editor().redo()
def cut(self): def cut(self):
"""""" """"""
self.get_active_editor.cut() self.get_active_editor().cut()
def find(self): def find(self):
"""""" """"""
@ -284,17 +293,17 @@ class CodeEditor(QtWidgets.QMainWindow):
class FindDialog(QtWidgets.QDialog): class FindDialog(QtWidgets.QDialog):
"""""" """"""
def __init__(self, editor: Qsci.QsciScintilla, replace: bool = False): def __init__(
self,
editor: Qsci.QsciScintilla,
show_replace: bool = False
):
"""""" """"""
super().__init__() super().__init__()
self.editor = editor self.editor: Qsci.QsciScintilla = editor
self.replace = replace self.show_replace: bool = show_replace
self.new_task: bool = True
self.text = ""
self.case_sensitive = False
self.whole_word = False
self.selection = False
self.init_ui() self.init_ui()
@ -303,18 +312,28 @@ class FindDialog(QtWidgets.QDialog):
find_label = QtWidgets.QLabel("查找") find_label = QtWidgets.QLabel("查找")
replace_label = QtWidgets.QLabel("替换") replace_label = QtWidgets.QLabel("替换")
self.find_line = QtWidgets.QLineEdit() selected_text = self.editor.selectedText()
self.find_line = QtWidgets.QLineEdit(selected_text)
self.find_line.textChanged.connect(self.reset_task)
self.replace_line = QtWidgets.QLineEdit() self.replace_line = QtWidgets.QLineEdit()
self.case_check = QtWidgets.QCheckBox("大小写") self.case_check = QtWidgets.QCheckBox("大小写")
self.case_check.setChecked(True)
self.case_check.stateChanged.connect(self.reset_task)
self.whole_check = QtWidgets.QCheckBox("全词匹配") self.whole_check = QtWidgets.QCheckBox("全词匹配")
self.whole_check.stateChanged.connect(self.reset_task)
self.selection_check = QtWidgets.QCheckBox("选中区域") self.selection_check = QtWidgets.QCheckBox("选中区域")
self.selection_check.stateChanged.connect(self.reset_task)
find_button = QtWidgets.QPushButton("查找") find_button = QtWidgets.QPushButton("查找")
find_button.clicked.connect(self.find_text) find_button.clicked.connect(self.find_text)
replace_button = QtWidgets.QPushButton("替换") self.replace_button = QtWidgets.QPushButton("替换")
replace_button.clicked.connect(self.replace_text) self.replace_button.clicked.connect(self.replace_text)
self.replace_button.setEnabled(False)
check_hbox = QtWidgets.QHBoxLayout() check_hbox = QtWidgets.QHBoxLayout()
check_hbox.addWidget(self.case_check) check_hbox.addWidget(self.case_check)
@ -326,7 +345,7 @@ class FindDialog(QtWidgets.QDialog):
button_hbox = QtWidgets.QHBoxLayout() button_hbox = QtWidgets.QHBoxLayout()
button_hbox.addWidget(find_button) button_hbox.addWidget(find_button)
button_hbox.addWidget(replace_button) button_hbox.addWidget(self.replace_button)
form = QtWidgets.QFormLayout() form = QtWidgets.QFormLayout()
form.addRow(find_label, self.find_line) form.addRow(find_label, self.find_line)
@ -336,59 +355,66 @@ class FindDialog(QtWidgets.QDialog):
self.setLayout(form) self.setLayout(form)
if self.replace: if self.show_replace:
self.setWindowTitle("替换") self.setWindowTitle("替换")
else: else:
self.setWindowTitle("查找") self.setWindowTitle("查找")
replace_label.setVisible(False) replace_label.setVisible(False)
self.replace_line.setVisible(False) self.replace_line.setVisible(False)
replace_button.setVisible(False) self.replace_button.setVisible(False)
def find_text(self): def find_text(self):
"""""" """"""
new_text = self.find_line.text() if not self.new_task:
new_case_sensitive = self.case_check.isChecked() result = self.editor.findNext()
new_whole_word = self.whole_check.isChecked()
new_selection = self.selection_check.isChecked()
if ( if result:
new_text == self.text self.new_task = False
and new_case_sensitive == self.case_sensitive self.replace_button.setEnabled(True)
and new_whole_word == self.whole_word return
and new_selection == self.selection else:
): self.new_task = True
self.editor.findNext()
return
self.text = new_text self.editor.cancelFind()
self.case_sensitive = new_case_sensitive
self.whole_word = new_whole_word
self.selection = new_selection
if not self.selection: if not self.selection_check.isChecked():
self.editor.findFirst( result = self.editor.findFirst(
self.text, self.find_line.text(),
False, False,
self.case_sensitive, self.case_check.isChecked(),
self.whole_word, self.whole_check.isChecked(),
False, False,
show=True line=1,
index=1
) )
else: else:
self.editor.findFirstInSelection( result = self.editor.findFirstInSelection(
self.text, self.find_line.text(),
False, False,
self.case_sensitive, self.case_check.isChecked(),
self.whole_word, self.whole_check.isChecked(),
False, False
show=True
) )
if result:
self.new_task = False
self.replace_button.setEnabled(True)
else:
self.new_task = True
def replace_text(self): def replace_text(self):
"""""" """"""
new_text = self.replace_line.text() new_text = self.replace_line.text()
self.editor.replace(new_text) self.editor.replace(new_text)
self.editor.findNext()
def reset_task(self):
""""""
self.new_task = True
self.replace_button.setEnabled(False)
if __name__ == "__main__": if __name__ == "__main__":