Implement link insertion

This commit is contained in:
Kovid Goyal 2023-08-27 20:09:52 +05:30
parent a52f03fa5c
commit d120efdced
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 66 additions and 2 deletions

View File

@ -268,6 +268,7 @@ def fix_html(original_html, original_txt, remove_comments=True, callback=None):
class EditorWidget(QTextEdit, LineEditECM): # {{{ class EditorWidget(QTextEdit, LineEditECM): # {{{
data_changed = pyqtSignal() data_changed = pyqtSignal()
insert_images_separately = False
@property @property
def readonly(self): def readonly(self):
@ -343,7 +344,7 @@ class EditorWidget(QTextEdit, LineEditECM): # {{{
r('color', 'format-text-color', _('Foreground color')) r('color', 'format-text-color', _('Foreground color'))
r('background', 'format-fill-color', _('Background 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)) shortcut=QKeySequence('Ctrl+l', QKeySequence.SequenceFormat.PortableText))
r('insert_hr', 'format-text-hr', _('Insert separator'),) r('insert_hr', 'format-text-hr', _('Insert separator'),)
r('clear', 'trash', _('Clear')) r('clear', 'trash', _('Clear'))

View File

@ -1,15 +1,59 @@
#!/usr/bin/env python #!/usr/bin/env python
# License: GPLv3 Copyright: 2023, Kovid Goyal <kovid at kovidgoyal.net> # License: GPLv3 Copyright: 2023, Kovid Goyal <kovid at kovidgoyal.net>
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.comments_editor import Editor, EditorWidget
from calibre.gui2.widgets2 import Dialog 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): class NoteEditorWidget(EditorWidget):
load_resource = None load_resource = None
insert_images_separately = True
def __init__(self, *args, **kw):
super().__init__(*args, **kw)
@pyqtSlot(int, 'QUrl', result='QVariant') @pyqtSlot(int, 'QUrl', result='QVariant')
def loadResource(self, rtype, qurl): def loadResource(self, rtype, qurl):
@ -20,9 +64,17 @@ class NoteEditorWidget(EditorWidget):
self.searchable_text = '' self.searchable_text = ''
self.referenced_resources = set() 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): class NoteEditor(Editor):
editor_class = NoteEditorWidget editor_class = NoteEditorWidget
def get_doc(self): def get_doc(self):
@ -70,6 +122,17 @@ class EditNoteDialog(Dialog):
l.addWidget(self.edit_note_widget) l.addWidget(self.edit_note_widget)
l.addWidget(self.bb) l.addWidget(self.bb)
def sizeHint(self):
return QSize(800, 620)
def accept(self): def accept(self):
if self.edit_note_widget.commit(): if self.edit_note_widget.commit():
super().accept() 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