remove the word preview from previewer variable

As suggested by glutanimate https://github.com/ankitects/anki/pull/532#discussion_r401255290
This commit is contained in:
Arthur Milchior 2020-04-02 09:48:25 +02:00
parent 92cec5c327
commit 0e21978577
2 changed files with 138 additions and 143 deletions

View File

@ -1564,20 +1564,19 @@ where id in %s"""
def onTogglePreview(self): def onTogglePreview(self):
if self._previewer: if self._previewer:
self._previewer._closePreview() self._previewer._close()
self._previewer = None self._previewer = None
else: else:
self._previewer = BrowserPreviewer(self, self.mw) self._previewer = BrowserPreviewer(self, self.mw)
self._previewer._openPreview() self._previewer._open()
def _renderPreview(self, cardChanged=False): def _renderPreview(self, cardChanged=False):
if self._previewer: if self._previewer:
self._previewer._renderPreview(cardChanged) self._previewer._render(cardChanged)
def _cancelPreviewTimer(self): def _cancelPreviewTimer(self):
if self._previewer: if self._previewer:
self._previewer._cancelPreviewTimer() self._previewer._cancelTimer()
# Card deletion # Card deletion
###################################################################### ######################################################################

View File

@ -23,10 +23,10 @@ from aqt.webview import AnkiWebView
class Previewer: class Previewer:
_lastPreviewState = None _lastState = None
_previewCardChanged = False _cardChanged = False
_lastPreviewRender: Union[int, float] = 0 _lastRender: Union[int, float] = 0
_previewTimer = None _timer = None
def __init__(self, parent: QWidget, mw: AnkiQt): def __init__(self, parent: QWidget, mw: AnkiQt):
self.parent = parent self.parent = parent
@ -35,62 +35,62 @@ class Previewer:
def card(self) -> Optional[Card]: def card(self) -> Optional[Card]:
raise NotImplementedError raise NotImplementedError
def _openPreview(self): def _open(self):
self._previewState = "question" self._state = "question"
self._lastPreviewState = None self._lastState = None
self._create_gui() self._create_gui()
self._setupPreviewWebview() self._setupWebview()
self._renderPreview(True) self._render(True)
self._previewWindow.show() self._window.show()
def _create_gui(self): def _create_gui(self):
self._previewWindow = QDialog(None, Qt.Window) self._window = QDialog(None, Qt.Window)
self._previewWindow.setWindowTitle(_("Preview")) self._window.setWindowTitle(_("Preview"))
self._previewWindow.finished.connect(self._onPreviewFinished) self._window.finished.connect(self._onFinished)
self._previewWindow.silentlyClose = True self._window.silentlyClose = True
self.vbox = QVBoxLayout() self.vbox = QVBoxLayout()
self.vbox.setContentsMargins(0, 0, 0, 0) self.vbox.setContentsMargins(0, 0, 0, 0)
self._previewWeb = AnkiWebView(title="previewer") self._web = AnkiWebView(title="previewer")
self.vbox.addWidget(self._previewWeb) self.vbox.addWidget(self._web)
self.bbox = QDialogButtonBox() self.bbox = QDialogButtonBox()
self._previewReplay = self.bbox.addButton( self._replay = self.bbox.addButton(
_("Replay Audio"), QDialogButtonBox.ActionRole _("Replay Audio"), QDialogButtonBox.ActionRole
) )
self._previewReplay.setAutoDefault(False) self._replay.setAutoDefault(False)
self._previewReplay.setShortcut(QKeySequence("R")) self._replay.setShortcut(QKeySequence("R"))
self._previewReplay.setToolTip(_("Shortcut key: %s" % "R")) self._replay.setToolTip(_("Shortcut key: %s" % "R"))
self._previewReplay.clicked.connect(self._onReplayAudio) self._replay.clicked.connect(self._onReplayAudio)
self.previewShowBothSides = QCheckBox(_("Show Both Sides")) self.showBothSides = QCheckBox(_("Show Both Sides"))
self.previewShowBothSides.setShortcut(QKeySequence("B")) self.showBothSides.setShortcut(QKeySequence("B"))
self.previewShowBothSides.setToolTip(_("Shortcut key: %s" % "B")) self.showBothSides.setToolTip(_("Shortcut key: %s" % "B"))
self.bbox.addButton(self.previewShowBothSides, QDialogButtonBox.ActionRole) self.bbox.addButton(self.showBothSides, QDialogButtonBox.ActionRole)
self._previewBothSides = self.mw.col.conf.get("previewBothSides", False) self._bothSides = self.mw.col.conf.get("previewBothSides", False)
self.previewShowBothSides.setChecked(self._previewBothSides) self.showBothSides.setChecked(self._bothSides)
self.previewShowBothSides.toggled.connect(self._onPreviewShowBothSides) self.showBothSides.toggled.connect(self._onShowBothSides)
self.vbox.addWidget(self.bbox) self.vbox.addWidget(self.bbox)
self._previewWindow.setLayout(self.vbox) self._window.setLayout(self.vbox)
restoreGeom(self._previewWindow, "preview") restoreGeom(self._window, "preview")
def _onPreviewFinished(self, ok): def _onFinished(self, ok):
saveGeom(self._previewWindow, "preview") saveGeom(self._window, "preview")
self.mw.progress.timer(100, self._onClosePreview, False) self.mw.progress.timer(100, self._onClose, False)
def _onReplayAudio(self): def _onReplayAudio(self):
self.mw.reviewer.replayAudio(self) self.mw.reviewer.replayAudio(self)
def _closePreview(self): def _close(self):
if self._previewWindow: if self._window:
self._previewWindow.close() self._window.close()
self._onClosePreview() self._onClose()
def _onClosePreview(self): def _onClose(self):
self._previewWindow = None self._window = None
def _setupPreviewWebview(self): def _setupWebview(self):
jsinc = [ jsinc = [
"jquery.js", "jquery.js",
"browsersel.js", "browsersel.js",
@ -101,61 +101,59 @@ class Previewer:
self._previewWeb.stdHtml( self._previewWeb.stdHtml(
self.mw.reviewer.revHtml(), css=["reviewer.css"], js=jsinc, context=self, self.mw.reviewer.revHtml(), css=["reviewer.css"], js=jsinc, context=self,
) )
self._previewWeb.set_bridge_command( self._web.set_bridge_command(self._on_bridge_cmd, self)
self._on_preview_bridge_cmd, self,
)
def _on_preview_bridge_cmd(self, cmd: str) -> Any: def _on_bridge_cmd(self, cmd: str) -> Any:
if cmd.startswith("play:"): if cmd.startswith("play:"):
play_clicked_audio(cmd, self.card()) play_clicked_audio(cmd, self.card())
def _renderPreview(self, cardChanged=False): def _render(self, cardChanged=False):
self._cancelPreviewTimer() self._cancelTimer()
# Keep track of whether _renderPreview() has ever been called # Keep track of whether _render() has ever been called
# with cardChanged=True since the last successful render # with cardChanged=True since the last successful render
self._previewCardChanged |= cardChanged self._cardChanged |= cardChanged
# avoid rendering in quick succession # avoid rendering in quick succession
elapMS = int((time.time() - self._lastPreviewRender) * 1000) elapMS = int((time.time() - self._lastRender) * 1000)
delay = 300 delay = 300
if elapMS < delay: if elapMS < delay:
self._previewTimer = self.mw.progress.timer( self._timer = self.mw.progress.timer(
delay - elapMS, self._renderScheduledPreview, False delay - elapMS, self._renderScheduled, False
) )
else: else:
self._renderScheduledPreview() self._renderScheduled()
def _cancelPreviewTimer(self): def _cancelTimer(self):
if self._previewTimer: if self._timer:
self._previewTimer.stop() self._timer.stop()
self._previewTimer = None self._timer = None
def _renderScheduledPreview(self) -> None: def _renderScheduled(self) -> None:
self._cancelPreviewTimer() self._cancelTimer()
self._lastPreviewRender = time.time() self._lastRender = time.time()
if not self._previewWindow: if not self._window:
return return
c = self.card() c = self.card()
func = "_showQuestion" func = "_showQuestion"
if not c: if not c:
txt = _("(please select 1 card)") txt = _("(please select 1 card)")
bodyclass = "" bodyclass = ""
self._lastPreviewState = None self._lastState = None
else: else:
if self._previewBothSides: if self._bothSides:
self._previewState = "answer" self._state = "answer"
elif self._previewCardChanged: elif self._cardChanged:
self._previewState = "question" self._state = "question"
currentState = self._previewStateAndMod() currentState = self._stateAndMod()
if currentState == self._lastPreviewState: if currentState == self._lastState:
# nothing has changed, avoid refreshing # nothing has changed, avoid refreshing
return return
# need to force reload even if answer # need to force reload even if answer
txt = c.q(reload=True) txt = c.q(reload=True)
if self._previewState == "answer": if self._state == "answer":
func = "_showAnswer" func = "_showAnswer"
txt = c.a() txt = c.a()
txt = re.sub(r"\[\[type:[^]]+\]\]", "", txt) txt = re.sub(r"\[\[type:[^]]+\]\]", "", txt)
@ -163,7 +161,7 @@ class Previewer:
bodyclass = theme_manager.body_classes_for_card_ord(c.ord) bodyclass = theme_manager.body_classes_for_card_ord(c.ord)
if self.mw.reviewer.autoplay(c): if self.mw.reviewer.autoplay(c):
if self._previewBothSides: if self._bothSides:
# if we're showing both sides at once, remove any audio # if we're showing both sides at once, remove any audio
# from the answer that's appeared on the question already # from the answer that's appeared on the question already
question_audio = c.question_av_tags() question_audio = c.question_av_tags()
@ -171,7 +169,7 @@ class Previewer:
x for x in c.answer_av_tags() if x not in question_audio x for x in c.answer_av_tags() if x not in question_audio
] ]
audio = question_audio + only_on_answer_audio audio = question_audio + only_on_answer_audio
elif self._previewState == "question": elif self._state == "question":
audio = c.question_av_tags() audio = c.question_av_tags()
else: else:
audio = c.answer_av_tags() audio = c.answer_av_tags()
@ -180,26 +178,24 @@ class Previewer:
av_player.clear_queue_and_maybe_interrupt() av_player.clear_queue_and_maybe_interrupt()
txt = self.mw.prepare_card_text_for_display(txt) txt = self.mw.prepare_card_text_for_display(txt)
txt = gui_hooks.card_will_show( txt = gui_hooks.card_will_show(txt, c, "preview" + self._state.capitalize())
txt, c, "preview" + self._previewState.capitalize() self._lastState = self._stateAndMod()
) self._web.eval("{}({},'{}');".format(func, json.dumps(txt), bodyclass))
self._lastPreviewState = self._previewStateAndMod() self._cardChanged = False
self._previewWeb.eval("{}({},'{}');".format(func, json.dumps(txt), bodyclass))
self._previewCardChanged = False
def _onPreviewShowBothSides(self, toggle): def _onShowBothSides(self, toggle):
self._previewBothSides = toggle self._bothSides = toggle
self.mw.col.conf["previewBothSides"] = toggle self.mw.col.conf["previewBothSides"] = toggle
self.mw.col.setMod() self.mw.col.setMod()
if self._previewState == "answer" and not toggle: if self._state == "answer" and not toggle:
self._previewState = "question" self._state = "question"
self._renderPreview() self._render()
def _previewStateAndMod(self): def _stateAndMod(self):
c = self.card() c = self.card()
n = c.note() n = c.note()
n.load() n.load()
return (self._previewState, c.id, n.mod) return (self._state, c.id, n.mod)
class MultipleCardsPreviewer(Previewer): class MultipleCardsPreviewer(Previewer):
@ -209,55 +205,55 @@ class MultipleCardsPreviewer(Previewer):
def _create_gui(self): def _create_gui(self):
super()._create_gui() super()._create_gui()
self._previewPrev = self.bbox.addButton("<", QDialogButtonBox.ActionRole) self._prev = self.bbox.addButton("<", QDialogButtonBox.ActionRole)
self._previewPrev.setAutoDefault(False) self._prev.setAutoDefault(False)
self._previewPrev.setShortcut(QKeySequence("Left")) self._prev.setShortcut(QKeySequence("Left"))
self._previewPrev.setToolTip(_("Shortcut key: Left arrow")) self._prev.setToolTip(_("Shortcut key: Left arrow"))
self._previewNext = self.bbox.addButton(">", QDialogButtonBox.ActionRole) self._next = self.bbox.addButton(">", QDialogButtonBox.ActionRole)
self._previewNext.setAutoDefault(True) self._next.setAutoDefault(True)
self._previewNext.setShortcut(QKeySequence("Right")) self._next.setShortcut(QKeySequence("Right"))
self._previewNext.setToolTip(_("Shortcut key: Right arrow or Enter")) self._next.setToolTip(_("Shortcut key: Right arrow or Enter"))
self._previewPrev.clicked.connect(self._onPreviewPrev) self._prev.clicked.connect(self._onPrev)
self._previewNext.clicked.connect(self._onPreviewNext) self._next.clicked.connect(self._onNext)
def _onPreviewPrev(self): def _onPrev(self):
if self._previewState == "answer" and not self._previewBothSides: if self._state == "answer" and not self._bothSides:
self._previewState = "question" self._state = "question"
self._renderPreview() self._render()
else: else:
self._onPreviewPrevCard() self._onPrevCard()
def _onPreviewPrevCard(self): def _onPrevCard(self):
... ...
def _onPreviewNext(self): def _onNext(self):
if self._previewState == "question": if self._state == "question":
self._previewState = "answer" self._state = "answer"
self._renderPreview() self._render()
else: else:
self._onPreviewNextCard() self._onNextCard()
def _onPreviewNextCard(self): def _onNextCard(self):
... ...
def _updatePreviewButtons(self): def _updateButtons(self):
if not self._previewWindow: if not self._window:
return return
self._previewPrev.setEnabled(self._should_enable_prev()) self._prev.setEnabled(self._should_enable_prev())
self._previewNext.setEnabled(self._should_enable_next()) self._next.setEnabled(self._should_enable_next())
def _should_enable_prev(self): def _should_enable_prev(self):
return self._previewState == "answer" and not self._previewBothSides return self._state == "answer" and not self._bothSides
def _should_enable_next(self): def _should_enable_next(self):
return self._previewState == "question" return self._state == "question"
def _onClosePreview(self): def _onClose(self):
super()._onClosePreview() super()._onClose()
self._previewPrev = None self._prev = None
self._previewNext = None self._next = None
class BrowserPreviewer(MultipleCardsPreviewer): class BrowserPreviewer(MultipleCardsPreviewer):
@ -267,16 +263,16 @@ class BrowserPreviewer(MultipleCardsPreviewer):
else: else:
return None return None
def _onPreviewFinished(self, ok): def _onFinished(self, ok):
super()._onPreviewFinished(ok) super()._onFinished(ok)
self.parent.form.previewButton.setChecked(False) self.parent.form.previewButton.setChecked(False)
def _onPreviewPrevCard(self): def _onPrevCard(self):
self.parent.editor.saveNow( self.parent.editor.saveNow(
lambda: self.parent._moveCur(QAbstractItemView.MoveUp) lambda: self.parent._moveCur(QAbstractItemView.MoveUp)
) )
def _onPreviewNextCard(self): def _onNextCard(self):
self.parent.editor.saveNow( self.parent.editor.saveNow(
lambda: self.parent._moveCur(QAbstractItemView.MoveDown) lambda: self.parent._moveCur(QAbstractItemView.MoveDown)
) )
@ -290,13 +286,13 @@ class BrowserPreviewer(MultipleCardsPreviewer):
or self.parent.currentRow() < self.parent.model.rowCount(None) - 1 or self.parent.currentRow() < self.parent.model.rowCount(None) - 1
) )
def _onClosePreview(self): def _onClose(self):
super()._onClosePreview() super()._onClose()
self.parent.previewer = None self.parent.previewer = None
def _renderScheduledPreview(self) -> None: def _renderScheduled(self) -> None:
super()._renderScheduledPreview() super()._renderScheduled()
self._updatePreviewButtons() self._updateButtons()
class ListCardsPreviewer(MultipleCardsPreviewer): class ListCardsPreviewer(MultipleCardsPreviewer):
@ -320,18 +316,18 @@ class ListCardsPreviewer(MultipleCardsPreviewer):
self.cards[self.index] = self.mw.col.getCard(self.cards[self.index]) self.cards[self.index] = self.mw.col.getCard(self.cards[self.index])
return self.cards[self.index] return self.cards[self.index]
def _openPreview(self): def _open(self):
if not self.cards: if not self.cards:
return return
super()._openPreview() super()._open()
def _onPreviewPrevCard(self): def _onPrevCard(self):
self.index -= 1 self.index -= 1
self._renderPreview() self._render()
def _onPreviewNextCard(self): def _onNextCard(self):
self.index += 1 self.index += 1
self._renderPreview() self._render()
def _should_enable_prev(self): def _should_enable_prev(self):
return super()._should_enable_prev() or self.index > 0 return super()._should_enable_prev() or self.index > 0
@ -340,11 +336,11 @@ class ListCardsPreviewer(MultipleCardsPreviewer):
return super()._should_enable_next() or self.index < len(self.cards) - 1 return super()._should_enable_next() or self.index < len(self.cards) - 1
def _on_other_side(self): def _on_other_side(self):
if self._previewState == "question": if self._state == "question":
self._previewState = "answer" self._state = "answer"
else: else:
self._previewState = "question" self._state = "question"
self._renderPreview() self._render()
class SingleCardPreviewer(Previewer): class SingleCardPreviewer(Previewer):
@ -367,8 +363,8 @@ class SingleCardPreviewer(Previewer):
self._other_side.clicked.connect(self._on_other_side) self._other_side.clicked.connect(self._on_other_side)
def _on_other_side(self): def _on_other_side(self):
if self._previewState == "question": if self._state == "question":
self._previewState = "answer" self._state = "answer"
else: else:
self._previewState = "question" self._state = "question"
self._renderPreview() self._render()