From d120efdcedcce1ee6d58b54b22ba9405416f88f4 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sun, 27 Aug 2023 20:09:52 +0530 Subject: [PATCH] Implement link insertion --- src/calibre/gui2/comments_editor.py | 3 +- .../gui2/dialogs/edit_category_notes.py | 65 ++++++++++++++++++- 2 files changed, 66 insertions(+), 2 deletions(-) diff --git a/src/calibre/gui2/comments_editor.py b/src/calibre/gui2/comments_editor.py index 710bf3a0a0..bffd614504 100644 --- a/src/calibre/gui2/comments_editor.py +++ b/src/calibre/gui2/comments_editor.py @@ -268,6 +268,7 @@ def fix_html(original_html, original_txt, remove_comments=True, callback=None): class EditorWidget(QTextEdit, LineEditECM): # {{{ data_changed = pyqtSignal() + insert_images_separately = False @property def readonly(self): @@ -343,7 +344,7 @@ class EditorWidget(QTextEdit, LineEditECM): # {{{ r('color', 'format-text-color', _('Foreground color')) r('background', 'format-fill-color', _('Background color')) - r('insert_link', 'insert-link', _('Insert link or image'), + r('insert_link', 'insert-link', _('Insert link') if self.insert_images_separately else _('Insert link or image'), shortcut=QKeySequence('Ctrl+l', QKeySequence.SequenceFormat.PortableText)) r('insert_hr', 'format-text-hr', _('Insert separator'),) r('clear', 'trash', _('Clear')) diff --git a/src/calibre/gui2/dialogs/edit_category_notes.py b/src/calibre/gui2/dialogs/edit_category_notes.py index a13003562c..10f420c6fd 100644 --- a/src/calibre/gui2/dialogs/edit_category_notes.py +++ b/src/calibre/gui2/dialogs/edit_category_notes.py @@ -1,15 +1,59 @@ #!/usr/bin/env python # License: GPLv3 Copyright: 2023, Kovid Goyal -from qt.core import QIcon, QSize, QVBoxLayout, QWidget, pyqtSlot +import os +from qt.core import ( + QDialog, QFormLayout, QIcon, QLineEdit, QSize, Qt, QVBoxLayout, QWidget, pyqtSlot, +) +from calibre.gui2 import Application from calibre.gui2.comments_editor import Editor, EditorWidget from calibre.gui2.widgets2 import Dialog +class AskLink(Dialog): # {{{ + + def __init__(self, initial_name='', parent=None): + super().__init__(_('Create link'), 'create-link-for-notes', parent=parent) + self.setWindowIcon(QIcon.ic('insert-link.png')) + if initial_name: + self.name_edit.setText(initial_name) + + def setup_ui(self): + self.v = v = QVBoxLayout(self) + self.f = f = QFormLayout() + f.setFieldGrowthPolicy(QFormLayout.FieldGrowthPolicy.ExpandingFieldsGrow) + v.addLayout(f) + v.addWidget(self.bb) + + self.url_edit = u = QLineEdit(self) + u.setPlaceholderText(_('The URL for this link')) + u.setMinimumWidth(400) + f.addRow(_('&URL:'), u) + + self.name_edit = n = QLineEdit(self) + n.setPlaceholderText(_('The name (optional) for this link')) + f.addRow(_('&Name:'), n) + + self.url_edit.setFocus(Qt.FocusReason.OtherFocusReason) + + @property + def link_name(self): + return self.name_edit.text().strip() + + @property + def url(self): + return self.url_edit.text().strip() +# }}} + + class NoteEditorWidget(EditorWidget): load_resource = None + insert_images_separately = True + + def __init__(self, *args, **kw): + super().__init__(*args, **kw) @pyqtSlot(int, 'QUrl', result='QVariant') def loadResource(self, rtype, qurl): @@ -20,9 +64,17 @@ class NoteEditorWidget(EditorWidget): self.searchable_text = '' self.referenced_resources = set() + def ask_link(self): + c = self.textCursor() + selected_text = c.selection().toPlainText().replace('\n', ' ') + d = AskLink(selected_text, parent=self) + if d.exec() == QDialog.DialogCode.Accepted: + return d.url, d.link_name, False + return '', '', False class NoteEditor(Editor): + editor_class = NoteEditorWidget def get_doc(self): @@ -70,6 +122,17 @@ class EditNoteDialog(Dialog): l.addWidget(self.edit_note_widget) l.addWidget(self.bb) + def sizeHint(self): + return QSize(800, 620) + def accept(self): if self.edit_note_widget.commit(): super().accept() + + +if __name__ == '__main__': + from calibre.library import db as dbc + app = Application([]) + d = EditNoteDialog('authors', 1, dbc(os.path.expanduser('~/test library'))) + d.exec() + del d, app