From 5ab3d72ed717916e3d7f0834bf9aaec31f8eb902 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Thu, 21 Aug 2025 08:05:08 +0530 Subject: [PATCH] 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) --- src/calibre/gui2/library/annotations.py | 11 +++++++++++ src/calibre/gui2/viewer/widgets.py | 3 ++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/calibre/gui2/library/annotations.py b/src/calibre/gui2/library/annotations.py index 88a25f9c2e..20d9dfc5a6 100644 --- a/src/calibre/gui2/library/annotations.py +++ b/src/calibre/gui2/library/annotations.py @@ -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]) diff --git a/src/calibre/gui2/viewer/widgets.py b/src/calibre/gui2/viewer/widgets.py index 5d9f8747f4..f49859faa3 100644 --- a/src/calibre/gui2/viewer/widgets.py +++ b/src/calibre/gui2/viewer/widgets.py @@ -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: