From 9e74f75bee0d24a68bf94acdace14384e65ceabe Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Thu, 10 Dec 2020 21:51:02 +0530 Subject: [PATCH] Make drawing of annotations existence dot more robust in annots browser --- src/calibre/gui2/library/annotations.py | 6 ++---- src/calibre/gui2/viewer/widgets.py | 10 +++++++--- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/src/calibre/gui2/library/annotations.py b/src/calibre/gui2/library/annotations.py index b69a223eaa..00fb7a8463 100644 --- a/src/calibre/gui2/library/annotations.py +++ b/src/calibre/gui2/library/annotations.py @@ -79,7 +79,7 @@ class AnnotsResultsDelegate(ResultsDelegate): def result_data(self, result): if not isinstance(result, dict): - return None, None, None, None + return None, None, None, None, None full_text = result['text'].replace('\x1f', ' ') parts = full_text.split('\x1d', 2) before = after = '' @@ -90,9 +90,7 @@ class AnnotsResultsDelegate(ResultsDelegate): before, text = parts else: text = parts[0] - if result.get('annotation', {}).get('notes'): - before = '•' + (before or '') - return False, before, text, after + return False, before, text, after, bool(result.get('annotation', {}).get('notes')) # }}} diff --git a/src/calibre/gui2/viewer/widgets.py b/src/calibre/gui2/viewer/widgets.py index cf3693fe2b..5a9838905b 100644 --- a/src/calibre/gui2/viewer/widgets.py +++ b/src/calibre/gui2/viewer/widgets.py @@ -19,13 +19,13 @@ class ResultsDelegate(QStyledItemDelegate): # {{{ def result_data(self, result): if not hasattr(result, 'is_hidden'): - return None, None, None, None - return result.is_hidden, result.before, result.text, result.after + return None, None, None, None, None + return result.is_hidden, result.before, result.text, result.after, False def paint(self, painter, option, index): QStyledItemDelegate.paint(self, painter, option, index) result = index.data(Qt.ItemDataRole.UserRole) - is_hidden, result_before, result_text, result_after = self.result_data(result) + is_hidden, result_before, result_text, result_after, show_leading_dot = self.result_data(result) if result_text is None: return painter.save() @@ -45,6 +45,8 @@ class ResultsDelegate(QStyledItemDelegate): # {{{ rect = option.rect.adjusted(option.decorationSize.width() + 4 if is_hidden else 0, 0, 0, 0) painter.setClipRect(rect) before = re.sub(r'\s+', ' ', result_before) + if show_leading_dot: + before = '•' + before before_width = 0 if before: before_width = painter.boundingRect(rect, flags, before).width() @@ -58,6 +60,8 @@ class ResultsDelegate(QStyledItemDelegate): # {{{ match_width = painter.boundingRect(rect, flags, text).width() if match_width >= rect.width() - 3 * ellipsis_width: efm = QFontMetrics(emphasis_font) + if show_leading_dot: + text = '•' + text text = efm.elidedText(text, Qt.TextElideMode.ElideRight, rect.width()) painter.drawText(rect, flags, text) else: