mirror of
https://github.com/kovidgoyal/calibre.git
synced 2026-01-03 02:30:21 -05:00
Fix tooltips for non-string objects throwing exceptions when custom tooltip templates are used. This happens because idx.data(Qt.ItemDataRole.ToolTipRole) can return a bool, QDateTime, float, or int instead of the string representation for these objects.
pep8 will "fix" the Qt imports list.
This commit is contained in:
parent
8c22882248
commit
6ec119a537
@ -15,7 +15,10 @@ import traceback
|
||||
from collections import defaultdict, namedtuple
|
||||
from itertools import groupby
|
||||
|
||||
from qt.core import QAbstractTableModel, QApplication, QColor, QFont, QFontMetrics, QIcon, QImage, QModelIndex, QPainter, QPixmap, Qt, pyqtSignal
|
||||
from qt.core import (
|
||||
QAbstractTableModel, QApplication, QColor, QDateTime,
|
||||
QFont, QFontMetrics, QIcon, QImage, QLocale, QModelIndex, QPainter,
|
||||
QPixmap, Qt, pyqtSignal)
|
||||
|
||||
from calibre import fit_image, human_readable, isbytestring, prepare_string_for_xml, strftime
|
||||
from calibre.constants import DEBUG, config_dir, dark_link_color, filesystem_encoding
|
||||
@ -1008,14 +1011,29 @@ class BooksModel(QAbstractTableModel): # {{{
|
||||
def f(idx):
|
||||
try:
|
||||
template = self.db.new_api.pref('column_tooltip_templates', {}).get(key, '')
|
||||
# Simulate what Qt does to get the text for non-string data
|
||||
# types so that idx.data(Qt.ItemDataRole.ToolTipRole)
|
||||
# always returns a string.
|
||||
orig_obj = orig_tt_func(idx)
|
||||
match orig_obj:
|
||||
case None:
|
||||
v = ''
|
||||
case str():
|
||||
v = orig_obj
|
||||
case bool():
|
||||
v = str(orig_obj)
|
||||
case int() | float() | QDateTime():
|
||||
v = QLocale().toString(orig_obj)
|
||||
case _:
|
||||
v = f'unsupported type {type(orig_obj)}'
|
||||
if template:
|
||||
global_vars = {'column_lookup_name': key, 'original_text': orig_tt_func(idx)}
|
||||
global_vars = {'column_lookup_name': key, 'original_text': v}
|
||||
mi = self.db.new_api.get_proxy_metadata(self.db.data.index_to_id(idx))
|
||||
return self.formatter.safe_format(
|
||||
template, {}, _('tooltip template error'), mi, global_vars=global_vars)
|
||||
return v
|
||||
except Exception as e:
|
||||
return str(e)
|
||||
return orig_tt_func(idx)
|
||||
return f
|
||||
|
||||
for f, allow_half in iteritems(rating_fields):
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user