Annotations browser: Add a color swatch in the results list to make seeing the type of highlight easier. See #2109496 (Custom organization of annotations and bookmarks)

This commit is contained in:
Kovid Goyal 2025-08-21 08:05:08 +05:30
parent 041a64958d
commit 5ab3d72ed7
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 13 additions and 1 deletions

View File

@ -210,6 +210,7 @@ class AnnotsResultsDelegate(ResultsDelegate):
add_ellipsis = False
emphasize_text = False
has_icons = True
def result_data(self, result):
if not isinstance(result, dict):
@ -397,6 +398,8 @@ class ResultsList(QTreeWidget):
self.currentItemChanged.connect(self.current_item_changed)
self.number_of_results = 0
self.item_map = []
self.icon_size = 12
self.setIconSize(QSize(self.icon_size, self.icon_size))
def show_context_menu(self, pos):
item = self.itemAt(pos)
@ -438,6 +441,9 @@ class ResultsList(QTreeWidget):
self.open_annotation.emit(r['book_id'], r['format'], r['annotation'])
def set_results(self, results, emphasize_text):
from calibre.gui2.viewer.highlights import decoration_for_style
is_dark = is_dark_theme()
dpr = self.devicePixelRatioF()
self.clear()
self.delegate.emphasize_text = emphasize_text
self.number_of_results = 0
@ -463,6 +469,11 @@ class ResultsList(QTreeWidget):
item.setData(0, Qt.ItemDataRole.UserRole, result)
item.setData(0, Qt.ItemDataRole.UserRole + 1, self.number_of_results)
self.number_of_results += 1
a = result.get('annotation')
if a and (s := a.get('style')):
dec = decoration_for_style(self.palette(), s, self.icon_size, dpr, is_dark)
if dec:
item.setData(0, Qt.ItemDataRole.DecorationRole, dec)
if self.item_map:
self.setCurrentItem(self.item_map[0])

View File

@ -14,6 +14,7 @@ class ResultsDelegate(QStyledItemDelegate): # {{{
add_ellipsis = True
emphasize_text = True
has_icons = False
def result_data(self, result):
if not hasattr(result, 'is_hidden'):
@ -40,7 +41,7 @@ class ResultsDelegate(QStyledItemDelegate): # {{{
else:
emphasis_font = font
flags = Qt.AlignmentFlag.AlignTop | Qt.TextFlag.TextSingleLine | Qt.TextFlag.TextIncludeTrailingSpaces
rect = option.rect.adjusted(option.decorationSize.width() + 4 if is_hidden else 0, 0, 0, 0)
rect = option.rect.adjusted(option.decorationSize.width() + (6 if self.has_icons else 4) if self.has_icons or is_hidden else 0, 0, 0, 0)
painter.setClipRect(rect)
before = re.sub(r'\s+', ' ', result_before)
if show_leading_dot: