Skip to content

Commit

Permalink
Get tag browser value icons working in content server
Browse files Browse the repository at this point in the history
  • Loading branch information
kovidgoyal committed Jan 16, 2025
1 parent a0a7ef0 commit 39e17d5
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 42 deletions.
2 changes: 1 addition & 1 deletion src/calibre/db/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
DATA_FILE_PATTERN = f'{DATA_DIR_NAME}/**/*'
BOOK_ID_PATH_TEMPLATE = ' ({})'
RESOURCE_URL_SCHEME = 'calres'

TEMPLATE_ICON_INDICATOR = ' template ' # Item values cannot start or end with space

@dataclass
class TrashEntry:
Expand Down
2 changes: 1 addition & 1 deletion src/calibre/gui2/tag_browser/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

from calibre.constants import config_dir
from calibre.db.categories import Tag, category_display_order
from calibre.db.constants import TEMPLATE_ICON_INDICATOR
from calibre.ebooks.metadata import rating_to_stars
from calibre.gui2 import config, error_dialog, file_icon_provider, gprefs, question_dialog
from calibre.gui2.dialogs.confirm_delete import confirm
Expand All @@ -30,7 +31,6 @@
'mark_minus': 3, 'mark_minusminus': 4}
DRAG_IMAGE_ROLE = Qt.ItemDataRole.UserRole + 1000
COUNT_ROLE = DRAG_IMAGE_ROLE + 1
TEMPLATE_ICON_INDICATOR = ' template ' # Item values cannot start or end with space

_bf = None

Expand Down
84 changes: 44 additions & 40 deletions src/calibre/srv/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

from calibre.constants import config_dir
from calibre.db.categories import Tag, category_display_order
from calibre.db.constants import DATA_FILE_PATTERN
from calibre.db.constants import DATA_FILE_PATTERN, TEMPLATE_ICON_INDICATOR
from calibre.ebooks.metadata.sources.identify import urls_from_identifiers
from calibre.library.comments import comments_to_html, markdown
from calibre.library.field_metadata import category_icon_map
Expand Down Expand Up @@ -157,48 +157,48 @@ def category_item_as_json(x, clear_rating=False):
return ans


# don't want to import from the GUI so use the value of TEMPLATE_ICON_INDICATOR
TEMPLATE_ICON_INDICATOR = ' template '

def get_gpref(name: str, defval = None):
gprefs = getattr(get_gpref, 'gprefs', None)
if gprefs is None:
from calibre.utils.config import JSONConfig
gprefs = get_gpref.gprefs = JSONConfig('gui')
return gprefs.get(name, defval)


def get_icon_for_node(node, parent, node_to_tag_map, tag_map, eval_formatter):
try:
category = node['category']
if category in ('search', 'formats') or category.startswith('@'):
node['value_icon'] = None
return

def name_for_icon(node):
return node.get('original_name', node.get('name'))

from calibre.gui2 import gprefs
value_icons = gprefs['tags_browser_value_icons']
val_icon,for_children = value_icons.get(category, {}).get(name_for_icon(node), (None, False))
if val_icon is None:
# No specific icon. Walk up the hierarchy checking parents.
par = parent
while True:
pid = str(par['id'])
if not pid.startswith('n'):
# 'Real' nodes (tag nodes) start with 'n'
break
pt = node_to_tag_map[pid]
pd = tag_map[id(pt)][1]
val_icon,for_children = value_icons.get(pd['category'], {}).get(name_for_icon(pd), (None, False))
if val_icon is not None and for_children:
break
par = pd
if val_icon is None and TEMPLATE_ICON_INDICATOR in value_icons.get(category, {}):
t = eval_formatter.safe_format(value_icons[category][TEMPLATE_ICON_INDICATOR][0],
{'category': category, 'value': name_for_icon(node)},
'VALUE_ICON_TEMPLATE_ERROR', None)
if t:
# Use linux path separator
val_icon = 'template_icons/' + t
category = node['category']
if category in ('search', 'formats') or category.startswith('@'):
return

def name_for_icon(node):
return node.get('original_name', node.get('name'))

value_icons = get_gpref('tags_browser_value_icons')
val_icon, for_children = value_icons.get(category, {}).get(name_for_icon(node), (None, False))
if val_icon is None:
# No specific icon. Walk up the hierarchy checking parents.
par = parent
while True:
pid = str(par['id'])
if not pid.startswith('n'):
# 'Real' nodes (tag nodes) start with 'n'
break
pt = node_to_tag_map[pid]
pd = tag_map[id(pt)][1]
val_icon,for_children = value_icons.get(pd['category'], {}).get(name_for_icon(pd), (None, False))
if val_icon is not None and for_children:
break
par = pd
if val_icon is None and TEMPLATE_ICON_INDICATOR in value_icons.get(category, {}):
t = eval_formatter.safe_format(
value_icons[category][TEMPLATE_ICON_INDICATOR][0], {'category': category, 'value': name_for_icon(node)},
'VALUE_ICON_TEMPLATE_ERROR', None)
if t:
# Use POSIX path separator
val_icon = 'template_icons/' + t
if val_icon:
node['value_icon'] = val_icon
except:
import traceback
traceback.print_exc()


CategoriesSettings = namedtuple(
Expand Down Expand Up @@ -466,7 +466,11 @@ def create_tag_node(tag, parent):
node_id, node_data = node_data
node = {'id':node_id, 'children':[]}
parent['children'].append(node)
get_icon_for_node(node_data, parent, node_to_tag_map, tag_map, eval_formatter)
try:
get_icon_for_node(node_data, parent, node_to_tag_map, tag_map, eval_formatter)
except Exception:
import traceback
traceback.print_exc()
return node, node_data

for idx, tag in enumerate(category_items):
Expand Down
3 changes: 3 additions & 0 deletions src/pyj/book_list/search.pyj
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ def icon_for_node(node):
fallback = interface_data.icon_map[node.data.category]
else:
ans = interface_data.icon_map[node.data.category] or 'column.png'
if node.data.value_icon:
fallback = ans
ans = '_' + node.data.value_icon
if fallback:
fallback = absolute_path(interface_data.icon_path + '/' + fallback)
return absolute_path(interface_data.icon_path + '/' + ans), fallback
Expand Down

0 comments on commit 39e17d5

Please sign in to comment.