From 26426cb11e1553e224aba8bc33c16ee7cf91ddf6 Mon Sep 17 00:00:00 2001 From: Damien Elmes Date: Fri, 24 Jul 2020 11:57:22 +1000 Subject: [PATCH] fix preview not updating on edit https://forums.ankiweb.net/t/modification-and-preview/1331 --- qt/aqt/browser.py | 6 +++--- qt/aqt/previewer.py | 20 +++++++++++++++++--- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/qt/aqt/browser.py b/qt/aqt/browser.py index f1a0cf63a..a40c2064f 100644 --- a/qt/aqt/browser.py +++ b/qt/aqt/browser.py @@ -881,10 +881,10 @@ QTableView {{ gridline-color: {grid} }} self.singleCard = True self._updateFlagsMenu() gui_hooks.browser_did_change_row(self) - self._renderPreview(True) def refreshCurrentCard(self, note: Note) -> None: self.model.refreshNote(note) + self._renderPreview() def onLoadNote(self, editor): self.refreshCurrentCard(editor.note) @@ -1513,9 +1513,9 @@ where id in %s""" self._previewer = PreviewDialog(self, self.mw, self._on_preview_closed) self._previewer.open() - def _renderPreview(self, cardChanged=False): + def _renderPreview(self): if self._previewer: - self._previewer.render_card(cardChanged) + self._previewer.render_card() def _cleanup_preview(self): if self._previewer: diff --git a/qt/aqt/previewer.py b/qt/aqt/previewer.py index c50cfd38e..7cfefd7af 100644 --- a/qt/aqt/previewer.py +++ b/qt/aqt/previewer.py @@ -50,12 +50,15 @@ class Previewer(QDialog): def card(self) -> Optional[Card]: raise NotImplementedError + def card_changed(self) -> bool: + raise NotImplementedError + def open(self): self._state = "question" self._last_state = None self._create_gui() self._setup_web_view() - self.render_card(True) + self.render_card() self.show() def _create_gui(self): @@ -124,11 +127,11 @@ class Previewer(QDialog): if cmd.startswith("play:"): play_clicked_audio(cmd, self.card()) - def render_card(self, cardChanged=False): + def render_card(self): self.cancel_timer() # Keep track of whether render() has ever been called # with cardChanged=True since the last successful render - self._card_changed |= cardChanged + self._card_changed |= self.card_changed() # avoid rendering in quick succession elap_ms = int((time.time() - self._last_render) * 1000) delay = 300 @@ -277,12 +280,23 @@ class MultiCardPreviewer(Previewer): class BrowserPreviewer(MultiCardPreviewer): + _last_card_id = 0 + def card(self) -> Optional[Card]: if self._parent.singleCard: return self._parent.card else: return None + def card_changed(self) -> bool: + c = self.card() + if not c: + return True + else: + changed = c.id != self._last_card_id + self._last_card_id = c.id + return changed + def _on_finished(self, ok): super()._on_finished(ok) self._parent.form.previewButton.setChecked(False)