mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-08-30 23:00:21 -04:00
Work on dialog to display category notes
This commit is contained in:
parent
71cafdbc2f
commit
0620eb2230
@ -2464,6 +2464,18 @@ class Cache:
|
||||
vm = table.id_map
|
||||
return {vm.get(fid):v for fid,v in lm.items() if v}
|
||||
|
||||
@read_api
|
||||
def link_for(self, field, item_id):
|
||||
'''
|
||||
Return the link, if any, for the specified item or None if no link is found
|
||||
'''
|
||||
f = self.fields.get(field)
|
||||
if f is not None:
|
||||
table = f.table
|
||||
lm = getattr(table, 'link_map', None)
|
||||
if lm is not None:
|
||||
return lm.get(item_id)
|
||||
|
||||
@read_api
|
||||
def get_all_link_maps_for_book(self, book_id):
|
||||
'''
|
||||
|
@ -89,6 +89,18 @@ def author_search_href(which, title=None, author=None):
|
||||
return func(key, title=title, author=author), tt
|
||||
|
||||
|
||||
def render_author_link(default_author_link, author, book_title=None, author_sort=None):
|
||||
book_title = book_title or ''
|
||||
if default_author_link.startswith('search-'):
|
||||
which_src = default_author_link.partition('-')[2]
|
||||
link, lt = author_search_href(which_src, title=book_title, author=author)
|
||||
else:
|
||||
formatter = EvalFormatter()
|
||||
vals = {'author': qquote(author), 'title': qquote(book_title), 'author_sort': qquote(author_sort or author)}
|
||||
link = lt = formatter.safe_format(default_author_link, vals, '', vals)
|
||||
return link, lt
|
||||
|
||||
|
||||
def mi_to_html(
|
||||
mi,
|
||||
field_list=None, default_author_link=None, use_roman_numbers=True,
|
||||
@ -270,21 +282,11 @@ def mi_to_html(
|
||||
ans.append((field, row % (_('Ids')+title_sep, links)))
|
||||
elif field == 'authors':
|
||||
authors = []
|
||||
formatter = EvalFormatter()
|
||||
for aut in mi.authors:
|
||||
link = ''
|
||||
if show_links:
|
||||
if default_author_link:
|
||||
if default_author_link.startswith('search-'):
|
||||
which_src = default_author_link.partition('-')[2]
|
||||
link, lt = author_search_href(which_src, title=mi.title, author=aut)
|
||||
else:
|
||||
vals = {'author': qquote(aut), 'title': qquote(mi.title)}
|
||||
try:
|
||||
vals['author_sort'] = qquote(mi.author_sort_map[aut])
|
||||
except KeyError:
|
||||
vals['author_sort'] = qquote(aut)
|
||||
link = lt = formatter.safe_format(default_author_link, vals, '', vals)
|
||||
link, lt = render_author_link(default_author_link, aut, mi.title, mi.author_sort_map.get(aut) or aut)
|
||||
else:
|
||||
aut = p(aut)
|
||||
if link:
|
||||
|
74
src/calibre/gui2/dialogs/show_category_note.py
Normal file
74
src/calibre/gui2/dialogs/show_category_note.py
Normal file
@ -0,0 +1,74 @@
|
||||
#!/usr/bin/env python
|
||||
# License: GPLv3 Copyright: 2023, Kovid Goyal <kovid at kovidgoyal.net>
|
||||
|
||||
import os
|
||||
from qt.core import (
|
||||
QByteArray, QDialogButtonBox, QIcon, QSize, Qt, QTextDocument, QVBoxLayout,
|
||||
)
|
||||
|
||||
from calibre.db.constants import RESOURCE_URL_SCHEME
|
||||
from calibre.ebooks.metadata.book.render import render_author_link
|
||||
from calibre.gui2 import Application, default_author_link
|
||||
from calibre.gui2.book_details import resolved_css
|
||||
from calibre.gui2.widgets2 import Dialog, HTMLDisplay
|
||||
|
||||
|
||||
class Display(HTMLDisplay):
|
||||
notes_resource_scheme = RESOURCE_URL_SCHEME
|
||||
|
||||
def __init__(self, parent=None):
|
||||
super().__init__(parent)
|
||||
self.document().setDefaultStyleSheet(resolved_css() + '\n\nli { margin-top: 0.5ex; margin-bottom: 0.5ex; }')
|
||||
self.setFocusPolicy(Qt.FocusPolicy.NoFocus)
|
||||
|
||||
def loadResource(self, rtype, qurl):
|
||||
if qurl.scheme() == RESOURCE_URL_SCHEME and int(rtype) == int(QTextDocument.ResourceType.ImageResource):
|
||||
db = self.parent().db
|
||||
resource = db.get_notes_resource(f'{qurl.host()}:{qurl.path()[1:]}')
|
||||
if resource is not None:
|
||||
return QByteArray(resource['data'])
|
||||
return
|
||||
return super().loadResource(rtype, qurl)
|
||||
|
||||
|
||||
class ShowNoteDialog(Dialog):
|
||||
|
||||
def __init__(self, field, item_id, db, parent=None):
|
||||
self.db = db.new_api
|
||||
self.item_val = self.db.get_item_name(field, item_id)
|
||||
self.has_links = self.db.has_link_map(field)
|
||||
self.item_link = (self.db.link_for(field, item_id) or '') if self.has_links else ''
|
||||
self.author_search_link = self.author_search_tooltip = ''
|
||||
if field == 'authors':
|
||||
lk = default_author_link()
|
||||
if lk != 'calibre':
|
||||
self.author_search_link, self.author_search_tooltip = render_author_link(lk, self.item_val)
|
||||
self.field, self.item_id = field, item_id
|
||||
super().__init__(self.item_val, 'show-notes-for-category', parent=parent)
|
||||
self.setWindowIcon(QIcon.ic('tag.png'))
|
||||
self.display.setHtml(self.db.notes_for(self.field, self.item_id))
|
||||
|
||||
def setup_ui(self):
|
||||
self.l = l = QVBoxLayout(self)
|
||||
|
||||
self.display = d = Display(self)
|
||||
l.addWidget(d)
|
||||
|
||||
self.bb.clear()
|
||||
self.bb.addButton(QDialogButtonBox.StandardButton.Close)
|
||||
l.addWidget(self.bb)
|
||||
|
||||
def sizeHint(self):
|
||||
return QSize(800, 620)
|
||||
|
||||
|
||||
def develop_show_note():
|
||||
from calibre.library import db as dbc
|
||||
app = Application([])
|
||||
d = ShowNoteDialog('authors', 1, dbc(os.path.expanduser('~/test library')))
|
||||
d.exec()
|
||||
del d, app
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
develop_show_note()
|
Loading…
x
Reference in New Issue
Block a user