From 50812974e0b9a0d319e7fbbfed3969b021ef8654 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sat, 10 May 2014 20:53:29 +0530 Subject: [PATCH] Edit Book: Update the Table of Contents view automatically when the ncx file is edited. Also avoid unnecessary rebuilds of the Table of Contents view. --- src/calibre/ebooks/oeb/polish/toc.py | 3 ++- src/calibre/gui2/tweak_book/boss.py | 4 ++++ src/calibre/gui2/tweak_book/toc.py | 25 +++++++++++++++++++------ src/calibre/gui2/tweak_book/ui.py | 1 - 4 files changed, 25 insertions(+), 8 deletions(-) diff --git a/src/calibre/ebooks/oeb/polish/toc.py b/src/calibre/ebooks/oeb/polish/toc.py index 967c6cc34f..c614bd489b 100644 --- a/src/calibre/ebooks/oeb/polish/toc.py +++ b/src/calibre/ebooks/oeb/polish/toc.py @@ -186,9 +186,10 @@ def get_toc(container, verify_destinations=True): toc = find_existing_toc(container) if toc is None or not container.has_name(toc): ans = TOC() - ans.lang = ans.uid = None + ans.lang = ans.uid = ans.toc_file_name = None return ans ans = parse_ncx(container, toc) + ans.toc_file_name = toc if verify_destinations: verify_toc_destinations(container, ans) return ans diff --git a/src/calibre/gui2/tweak_book/boss.py b/src/calibre/gui2/tweak_book/boss.py index a73bea8ad0..578ec79e0b 100644 --- a/src/calibre/gui2/tweak_book/boss.py +++ b/src/calibre/gui2/tweak_book/boss.py @@ -1122,6 +1122,10 @@ class Boss(QObject): def editor_data_changed(self, editor): self.gui.preview.start_refresh_timer() + for name, ed in editors.iteritems(): + if ed is editor: + self.gui.toc_view.start_refresh_timer(name) + break def editor_undo_redo_state_changed(self, *args): self.apply_current_editor_state() diff --git a/src/calibre/gui2/tweak_book/toc.py b/src/calibre/gui2/tweak_book/toc.py index 3c86e358a6..dcebcc6ec5 100644 --- a/src/calibre/gui2/tweak_book/toc.py +++ b/src/calibre/gui2/tweak_book/toc.py @@ -118,7 +118,6 @@ class TOCViewer(QWidget): self.setLayout(l) l.setContentsMargins(0, 0, 0, 0) - self.is_visible = False self.view = QTreeWidget(self) self.delegate = Delegate(self.view) self.view.setItemDelegate(self.delegate) @@ -136,9 +135,22 @@ class TOCViewer(QWidget): self.refresh_action = QAction(QIcon(I('view-refresh.png')), _('&Refresh'), self) self.refresh_action.triggered.connect(self.refresh) + self.refresh_timer = t = QTimer(self) + t.setInterval(1000), t.setSingleShot(True) + t.timeout.connect(self.auto_refresh) + self.toc_name = None + self.currently_editing = None + + def start_refresh_timer(self, name): + if self.isVisible() and self.toc_name == name: + self.refresh_timer.start() + + def auto_refresh(self): + if self.isVisible(): + self.refresh() def refresh(self): - self.refresh_requested.emit() # Give boos a chance to commit dirty editors to the container + self.refresh_requested.emit() # Give boss a chance to commit dirty editors to the container self.build() def item_pressed(self, item): @@ -176,6 +188,7 @@ class TOCViewer(QWidget): if c is None: return toc = get_toc(c, verify_destinations=False) + self.toc_name = getattr(toc, 'toc_file_name', None) def process_node(toc, parent): for child in toc: @@ -191,12 +204,12 @@ class TOCViewer(QWidget): self.view.clear() process_node(toc, self.view.invisibleRootItem()) - def visibility_changed(self, visible): - self.is_visible = visible - if visible: + def showEvent(self, ev): + if self.toc_name is None or not ev.spontaneous(): self.build() + return super(TOCViewer, self).showEvent(ev) def update_if_visible(self): - if self.is_visible: + if self.isVisible(): self.build() diff --git a/src/calibre/gui2/tweak_book/ui.py b/src/calibre/gui2/tweak_book/ui.py index b4ac60f893..9570bbf95c 100644 --- a/src/calibre/gui2/tweak_book/ui.py +++ b/src/calibre/gui2/tweak_book/ui.py @@ -612,7 +612,6 @@ class Main(MainWindow): d.setWidget(self.toc_view) self.addDockWidget(Qt.LeftDockWidgetArea, d) d.close() # Hidden by default - d.visibilityChanged.connect(self.toc_view.visibility_changed) d = create(_('Checkpoints'), 'checkpoints') d.setAllowedAreas(Qt.LeftDockWidgetArea | Qt.RightDockWidgetArea | Qt.BottomDockWidgetArea | Qt.TopDockWidgetArea)