From a0460762ede957a70447f6ed2d553305a84b1712 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Fri, 16 Apr 2021 07:47:51 +0530 Subject: [PATCH] E-book viewer: Highlights panel: Dont expand all sections when adding/deleting/modifying highlights. Fixes #1924598 [Adding new highlight in ebook viewer expands all sections.](https://bugs.launchpad.net/calibre/+bug/1924598) --- src/calibre/gui2/viewer/highlights.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/calibre/gui2/viewer/highlights.py b/src/calibre/gui2/viewer/highlights.py index 1c5b824451..513fbb029a 100644 --- a/src/calibre/gui2/viewer/highlights.py +++ b/src/calibre/gui2/viewer/highlights.py @@ -199,8 +199,15 @@ class Highlights(QTreeWidget): def current_item_changed(self, current, previous): self.current_highlight_changed.emit(current.data(0, Qt.ItemDataRole.UserRole) if current is not None else None) - def load(self, highlights): + def load(self, highlights, preserve_state=False): s = self.style() + expanded_chapters = set() + if preserve_state: + root = self.invisibleRootItem() + for i in range(root.childCount()): + chapter = root.child(i) + if chapter.isExpanded(): + expanded_chapters.add(chapter.data(0, Qt.ItemDataRole.DisplayRole)) icon_size = s.pixelMetric(QStyle.PixelMetric.PM_SmallIconSize, None, self) dpr = self.devicePixelRatioF() is_dark = is_dark_theme() @@ -234,7 +241,7 @@ class Highlights(QTreeWidget): if tt: section.setToolTip(0, tt) self.addTopLevelItem(section) - section.setExpanded(True) + section.setExpanded(not preserve_state or sec in expanded_chapters) for itemnum, h in enumerate(items): txt = h.get('highlighted_text') txt = txt.replace('\n', ' ') @@ -268,7 +275,7 @@ class Highlights(QTreeWidget): def refresh(self, highlights): h = self.current_highlight - self.load(highlights) + self.load(highlights, preserve_state=True) if h is not None: idx = self.uuid_map.get(h['uuid']) if idx is not None: