From 80de18c2b6b222c50c6a08f572953b0b36a44ed3 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sat, 5 Sep 2020 06:33:51 +0530 Subject: [PATCH] Use lowest level ToC section in highlights panel Matches search panel --- src/calibre/gui2/viewer/highlights.py | 10 +++++++++- src/calibre/gui2/viewer/search.py | 7 ++++--- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/src/calibre/gui2/viewer/highlights.py b/src/calibre/gui2/viewer/highlights.py index 8061096bab..41724cf044 100644 --- a/src/calibre/gui2/viewer/highlights.py +++ b/src/calibre/gui2/viewer/highlights.py @@ -77,13 +77,21 @@ class Highlights(QTreeWidget): self.uuid_map = {} highlights = (h for h in highlights if not h.get('removed') and h.get('highlighted_text')) section_map = defaultdict(list) + section_tt_map = {} for h in self.sorted_highlights(highlights): - sec = h.get('top_level_section_title') or _('Unknown') + tsec = h.get('top_level_section_title') + lsec = h.get('lowest_level_section_title') + sec = lsec or tsec or _('Unknown') + if sec != tsec: + section_tt_map[sec] = tsec section_map[sec].append(h) for secnum, (sec, items) in enumerate(section_map.items()): section = QTreeWidgetItem([sec], 1) section.setFlags(Qt.ItemIsEnabled) section.setFont(0, self.section_font) + tt = section_tt_map.get(sec) + if tt: + section.setToolTip(0, _('Top level section in Table of Contents:') + '\n' + tt) self.addTopLevelItem(section) section.setExpanded(True) for itemnum, h in enumerate(items): diff --git a/src/calibre/gui2/viewer/search.py b/src/calibre/gui2/viewer/search.py index ded59d1b3d..37aba9aa06 100644 --- a/src/calibre/gui2/viewer/search.py +++ b/src/calibre/gui2/viewer/search.py @@ -468,9 +468,10 @@ class Results(QTreeWidget): # {{{ lines = [] for i, node in enumerate(toc_nodes): lines.append('\xa0\xa0' * i + '➤ ' + (node.get('title') or _('Unknown'))) - tt = ngettext('Table of Contents section:', 'Table of Contents sections:', len(lines)) - tt += '\n' + '\n'.join(lines) - section.setToolTip(0, tt) + if lines: + tt = ngettext('Table of Contents section:', 'Table of Contents sections:', len(lines)) + tt += '\n' + '\n'.join(lines) + section.setToolTip(0, tt) self.section_map[section_key] = section self.addTopLevelItem(section) section.setExpanded(True)