diff --git a/vnpy/trader/ui/editor.py b/vnpy/trader/ui/editor.py index 112030c3..6f4d9122 100644 --- a/vnpy/trader/ui/editor.py +++ b/vnpy/trader/ui/editor.py @@ -45,8 +45,8 @@ class CodeEditor(QtWidgets.QMainWindow): bar = self.menuBar() file_menu = bar.addMenu("文件") - 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.new_file, "Ctrl+N") + 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, @@ -58,6 +58,9 @@ class CodeEditor(QtWidgets.QMainWindow): self.add_menu_action(file_menu, "退出", self.close) 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.paste, "Ctrl+P") 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.replace, "Ctrl+H") - help_menu = bar.addMenu("帮助") - def add_menu_action( self, menu: QtWidgets.QMenu, @@ -175,7 +176,7 @@ class CodeEditor(QtWidgets.QMainWindow): def open_file(self): """""" file_path, _ = QtWidgets.QFileDialog.getOpenFileName( - self, "打开", "", "Python(*.py)") + self, "打开文件", "", "Python(*.py)") if file_path: self.open_editor(file_path) @@ -225,11 +226,19 @@ class CodeEditor(QtWidgets.QMainWindow): 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): """""" - self.get_active_editor.cut() + self.get_active_editor().cut() def find(self): """""" @@ -284,17 +293,17 @@ class CodeEditor(QtWidgets.QMainWindow): 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__() - self.editor = editor - self.replace = replace - - self.text = "" - self.case_sensitive = False - self.whole_word = False - self.selection = False + self.editor: Qsci.QsciScintilla = editor + self.show_replace: bool = show_replace + self.new_task: bool = True self.init_ui() @@ -303,18 +312,28 @@ class FindDialog(QtWidgets.QDialog): find_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.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.stateChanged.connect(self.reset_task) + self.selection_check = QtWidgets.QCheckBox("选中区域") + self.selection_check.stateChanged.connect(self.reset_task) find_button = QtWidgets.QPushButton("查找") find_button.clicked.connect(self.find_text) - replace_button = QtWidgets.QPushButton("替换") - replace_button.clicked.connect(self.replace_text) + self.replace_button = QtWidgets.QPushButton("替换") + self.replace_button.clicked.connect(self.replace_text) + self.replace_button.setEnabled(False) check_hbox = QtWidgets.QHBoxLayout() check_hbox.addWidget(self.case_check) @@ -326,7 +345,7 @@ class FindDialog(QtWidgets.QDialog): button_hbox = QtWidgets.QHBoxLayout() button_hbox.addWidget(find_button) - button_hbox.addWidget(replace_button) + button_hbox.addWidget(self.replace_button) form = QtWidgets.QFormLayout() form.addRow(find_label, self.find_line) @@ -336,59 +355,66 @@ class FindDialog(QtWidgets.QDialog): self.setLayout(form) - if self.replace: + if self.show_replace: self.setWindowTitle("替换") else: self.setWindowTitle("查找") + replace_label.setVisible(False) self.replace_line.setVisible(False) - replace_button.setVisible(False) + self.replace_button.setVisible(False) def find_text(self): """""" - new_text = self.find_line.text() - new_case_sensitive = self.case_check.isChecked() - new_whole_word = self.whole_check.isChecked() - new_selection = self.selection_check.isChecked() + if not self.new_task: + result = self.editor.findNext() - if ( - new_text == self.text - and new_case_sensitive == self.case_sensitive - and new_whole_word == self.whole_word - and new_selection == self.selection - ): - self.editor.findNext() - return + if result: + self.new_task = False + self.replace_button.setEnabled(True) + return + else: + self.new_task = True - self.text = new_text - self.case_sensitive = new_case_sensitive - self.whole_word = new_whole_word - self.selection = new_selection + self.editor.cancelFind() - if not self.selection: - self.editor.findFirst( - self.text, + if not self.selection_check.isChecked(): + result = self.editor.findFirst( + self.find_line.text(), False, - self.case_sensitive, - self.whole_word, + self.case_check.isChecked(), + self.whole_check.isChecked(), False, - show=True + line=1, + index=1 ) + else: - self.editor.findFirstInSelection( - self.text, + result = self.editor.findFirstInSelection( + self.find_line.text(), False, - self.case_sensitive, - self.whole_word, - False, - show=True + self.case_check.isChecked(), + self.whole_check.isChecked(), + False ) + if result: + self.new_task = False + self.replace_button.setEnabled(True) + else: + self.new_task = True + def replace_text(self): """""" new_text = self.replace_line.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__":