From 4c31beff3e9ad46f6e0373cd12f75ef6005eec98 Mon Sep 17 00:00:00 2001 From: Charles Haley Date: Thu, 29 May 2025 15:33:11 +0100 Subject: [PATCH] Add context menu action to copy the source code for the currently selected template to the editor. --- src/calibre/gui2/dialogs/template_dialog.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/calibre/gui2/dialogs/template_dialog.py b/src/calibre/gui2/dialogs/template_dialog.py index 05b96a946f..bc15ec77b8 100644 --- a/src/calibre/gui2/dialogs/template_dialog.py +++ b/src/calibre/gui2/dialogs/template_dialog.py @@ -612,6 +612,8 @@ class TemplateDialog(QDialog, Ui_TemplateDialog): self.documentation.setOpenLinks(False) self.documentation.anchorClicked.connect(self.url_clicked) self.source_code.setReadOnly(True) + self.source_code.setContextMenuPolicy(Qt.ContextMenuPolicy.CustomContextMenu) + self.source_code.customContextMenuRequested.connect(self.show_code_context_menu) self.doc_button.clicked.connect(self.open_documentation_viewer) self.general_info_button.clicked.connect(self.open_general_info_dialog) @@ -822,6 +824,24 @@ class TemplateDialog(QDialog, Ui_TemplateDialog): self.template_value.cellWidget(0, 2).setText( _("*** Breakpoints are enabled. Waiting for the 'Go' button to be pressed")) + def show_code_context_menu(self, point): + m = self.source_code.createStandardContextMenu() + name = self.current_function_name + if (name and self.all_functions[name].object_type in + (StoredObjectType.StoredPythonTemplate, StoredObjectType.StoredGPMTemplate)): + m.addSeparator() + ca = m.addAction(_('Copy stored template source to editor')) + ca.triggered.connect(self.copy_source_code_to_editor) + m.exec(self.source_code.mapToGlobal(point)) + + def copy_source_code_to_editor(self): + if self.textbox.toPlainText(): + r = question_dialog(self, _('Discard existing text?'), + _('The editor contains text. Do you want to overwrite that text?')) + if not r: + return + self.textbox.setPlainText(self.source_code.toPlainText()) + def show_context_menu(self, point): m = self.textbox.createStandardContextMenu() m.addSeparator()