def _create_gui(self): self.setWindowTitle(_("Preview")) qconnect(self.finished, self._on_finished) self.silentlyClose = True self.vbox = QVBoxLayout() self.vbox.setContentsMargins(0, 0, 0, 0) self._web = AnkiWebView(title="previewer") self.vbox.addWidget(self._web) self.bbox = QDialogButtonBox() self._replay = self.bbox.addButton(_("Replay Audio"), QDialogButtonBox.ActionRole) self._replay.setAutoDefault(False) self._replay.setShortcut(QKeySequence("R")) self._replay.setToolTip(_("Shortcut key: %s" % "R")) qconnect(self._replay.clicked, self._on_replay_audio) both_sides_button = QCheckBox(_("Show Both Sides")) both_sides_button.setShortcut(QKeySequence("B")) both_sides_button.setToolTip(_("Shortcut key: %s" % "B")) self.bbox.addButton(both_sides_button, QDialogButtonBox.ActionRole) self._show_both_sides = self.mw.col.conf.get("previewBothSides", False) both_sides_button.setChecked(self._show_both_sides) qconnect(both_sides_button.toggled, self._on_show_both_sides) self.vbox.addWidget(self.bbox) self.setLayout(self.vbox) restoreGeom(self, "preview")
def _create_gui(self) -> None: self.setWindowTitle(tr.actions_preview()) self.close_shortcut = QShortcut(QKeySequence("Ctrl+Shift+P"), self) qconnect(self.close_shortcut.activated, self.close) qconnect(self.finished, self._on_finished) self.silentlyClose = True self.vbox = QVBoxLayout() self.vbox.setContentsMargins(0, 0, 0, 0) self._web = AnkiWebView(title="previewer") self.vbox.addWidget(self._web) self.bbox = QDialogButtonBox() self._replay = self.bbox.addButton( tr.actions_replay_audio(), QDialogButtonBox.ButtonRole.ActionRole) self._replay.setAutoDefault(False) self._replay.setShortcut(QKeySequence("R")) self._replay.setToolTip(tr.actions_shortcut_key(val="R")) qconnect(self._replay.clicked, self._on_replay_audio) both_sides_button = QCheckBox(tr.qt_misc_back_side_only()) both_sides_button.setShortcut(QKeySequence("B")) both_sides_button.setToolTip(tr.actions_shortcut_key(val="B")) self.bbox.addButton(both_sides_button, QDialogButtonBox.ButtonRole.ActionRole) self._show_both_sides = self.mw.col.get_config_bool( Config.Bool.PREVIEW_BOTH_SIDES) both_sides_button.setChecked(self._show_both_sides) qconnect(both_sides_button.toggled, self._on_show_both_sides) self.vbox.addWidget(self.bbox) self.setLayout(self.vbox) restoreGeom(self, "preview")
def _create_gui(self): self.setWindowTitle(tr(TR.ACTIONS_PREVIEW)) qconnect(self.finished, self._on_finished) self.silentlyClose = True self.vbox = QVBoxLayout() self.vbox.setContentsMargins(0, 0, 0, 0) self._web = AnkiWebView(title="previewer") self.vbox.addWidget(self._web) self.bbox = QDialogButtonBox() self._replay = self.bbox.addButton(tr(TR.ACTIONS_REPLAY_AUDIO), QDialogButtonBox.ActionRole) self._replay.setAutoDefault(False) self._replay.setShortcut(QKeySequence("R")) self._replay.setToolTip(tr(TR.ACTIONS_SHORTCUT_KEY, val="R")) qconnect(self._replay.clicked, self._on_replay_audio) both_sides_button = QCheckBox(tr(TR.QT_MISC_BACK_SIDE_ONLY)) both_sides_button.setShortcut(QKeySequence("B")) both_sides_button.setToolTip(tr(TR.ACTIONS_SHORTCUT_KEY, val="B")) self.bbox.addButton(both_sides_button, QDialogButtonBox.ActionRole) self._show_both_sides = self.mw.col.conf.get("previewBothSides", False) both_sides_button.setChecked(self._show_both_sides) qconnect(both_sides_button.toggled, self._on_show_both_sides) self.vbox.addWidget(self.bbox) self.setLayout(self.vbox) restoreGeom(self, "preview")
def __init__(self, parent, question, help=None, edit=None, default="", \ title="Anki", minWidth=400): QDialog.__init__(self, parent) self.setWindowTitle(title) self.question = question self.help = help self.qlabel = QLabel(question) self.setMinimumWidth(minWidth) v = QVBoxLayout() v.addWidget(self.qlabel) if not edit: edit = QLineEdit() self.l = edit if default: self.l.setText(default) self.l.selectAll() v.addWidget(self.l) buts = QDialogButtonBox.Ok | QDialogButtonBox.Cancel if help: buts |= QDialogButtonBox.Help b = QDialogButtonBox(buts) v.addWidget(b) self.setLayout(v) b.button(QDialogButtonBox.Ok).clicked.connect(self.accept) b.button(QDialogButtonBox.Cancel).clicked.connect(self.reject) if help: b.button(QDialogButtonBox.Help).clicked.connect(self.helpRequested)
def _setupUi(self): flabel = QLabel("In this field:") self.fsel = QComboBox() fields = self._getFields() self.fsel.addItems(fields) self.cb = QCheckBox() self.cb.setText("transform to plain text") f_hbox = QHBoxLayout() f_hbox.addWidget(flabel) f_hbox.addWidget(self.fsel) f_hbox.addWidget(self.cb) f_hbox.setAlignment(Qt.AlignLeft) button_box = QDialogButtonBox( QDialogButtonBox.Ok | QDialogButtonBox.Cancel, orientation=Qt.Horizontal, parent=self, ) bottom_hbox = QHBoxLayout() bottom_hbox.addWidget(button_box) vbox_main = QVBoxLayout() vbox_main.addLayout(f_hbox) vbox_main.addLayout(bottom_hbox) self.setLayout(vbox_main) self.setWindowTitle("Batch Clean Selected Notes") button_box.rejected.connect(self.reject) button_box.accepted.connect(self.accept) self.rejected.connect(self.reject) self.accepted.connect(self.accept) self.fsel.setFocus()
def initUI(self): vlay = QVBoxLayout() self.input_line = PanelInputLine() self.list_box = QListWidget() for i in range(self.max_items): self.list_box.insertItem(i, '') vlay.addWidget(self.input_line) vlay.addWidget(self.list_box) self.buttonbox = QDialogButtonBox( QDialogButtonBox.StandardButton.Ok | QDialogButtonBox.StandardButton.Cancel) vlay.addWidget(self.buttonbox) # self.buttonbox.accepted.disconnect(self.accept) # leads to: TypeError: disconnect() failed between 'accepted' and 'accept' self.buttonbox.accepted.connect(self.accept) self.buttonbox.rejected.connect(self.reject) self.update_listbox() self.setLayout(vlay) self.resize(800, 350) restoreGeom(self, "TT/TIFP") self.list_box.setAlternatingRowColors(True) # connections self.input_line.im_changed.connect(self.text_changed) self.input_line.textChanged.connect(self.text_changed) self.input_line.returnPressed.connect(self.return_pressed) self.input_line.down_pressed.connect(self.down_pressed) self.input_line.up_pressed.connect(self.up_pressed) self.list_box.itemDoubleClicked.connect(self.item_doubleclicked) self.list_box.installEventFilter(self) self.input_line.setFocus()
def _ui_bottom_row(self): hbox = QHBoxLayout() buttons = QDialogButtonBox(Qt.Horizontal, self) # Button to export changelog to a CSV file export_btn = buttons.addButton("&Export full history", QDialogButtonBox.ActionRole) export_btn.setToolTip("Export full history to CSV") export_btn.clicked.connect(lambda _: self.onExport()) # Button to close this dialog close_btn = buttons.addButton("&Close", QDialogButtonBox.RejectRole) close_btn.clicked.connect(self.close) hbox.addWidget(buttons) return hbox
def onTargetDeck(self): from aqt.tagedit import TagEdit t = self.card.template() d = QDialog(self) d.setWindowTitle("Anki") d.setMinimumWidth(400) l = QVBoxLayout() lab = QLabel(_("""\ Enter deck to place new %s cards in, or leave blank:""") % self.card.template()['name']) lab.setWordWrap(True) l.addWidget(lab) te = TagEdit(d, type=1) te.setCol(self.col) l.addWidget(te) if t['did']: te.setText(self.col.decks.get(t['did'])['name']) te.selectAll() bb = QDialogButtonBox(QDialogButtonBox.Close) bb.rejected.connect(d.close) l.addWidget(bb) d.setLayout(l) d.exec_() if not te.text().strip(): t['did'] = None else: t['did'] = self.col.decks.id(te.text())
def onCheckMediaDB(self): self.progress.start(immediate=True) (nohave, unused, warnings) = self.col.media.check() self.progress.finish() # generate report report = "" if warnings: report += "\n".join(warnings) + "\n" if unused: if report: report += "\n\n\n" report += _( "In media folder but not used by any cards:") report += "\n" + "\n".join(unused) if nohave: if report: report += "\n\n\n" report += _( "Used on cards but missing from media folder:") report += "\n" + "\n".join(nohave) if not report: tooltip(_("No unused or missing files found.")) return # show report and offer to delete diag = QDialog(self) diag.setWindowTitle("Anki") layout = QVBoxLayout(diag) diag.setLayout(layout) text = QTextEdit() text.setReadOnly(True) text.setPlainText(report) layout.addWidget(text) box = QDialogButtonBox(QDialogButtonBox.Close) layout.addWidget(box) if unused: b = QPushButton(_("Delete Unused Files")) b.setAutoDefault(False) box.addButton(b, QDialogButtonBox.ActionRole) b.clicked.connect( lambda c, u=unused, d=diag: self.deleteUnused(u, d)) box.rejected.connect(diag.reject) diag.setMinimumHeight(400) diag.setMinimumWidth(500) restoreGeom(diag, "checkmediadb") diag.exec_() saveGeom(diag, "checkmediadb")
def get_id_and_pass_from_user(mw: aqt.main.AnkiQt, username: str = "", password: str = "") -> tuple[str, str]: diag = QDialog(mw) diag.setWindowTitle("Anki") disable_help_button(diag) diag.setWindowModality(Qt.WindowModality.WindowModal) vbox = QVBoxLayout() info_label = QLabel( without_unicode_isolation( tr.sync_account_required( link="https://ankiweb.net/account/register"))) info_label.setOpenExternalLinks(True) info_label.setWordWrap(True) vbox.addWidget(info_label) vbox.addSpacing(20) g = QGridLayout() l1 = QLabel(tr.sync_ankiweb_id_label()) g.addWidget(l1, 0, 0) user = QLineEdit() user.setText(username) g.addWidget(user, 0, 1) l2 = QLabel(tr.sync_password_label()) g.addWidget(l2, 1, 0) passwd = QLineEdit() passwd.setText(password) passwd.setEchoMode(QLineEdit.EchoMode.Password) g.addWidget(passwd, 1, 1) vbox.addLayout(g) bb = QDialogButtonBox( QDialogButtonBox.StandardButton.Ok | QDialogButtonBox.StandardButton.Cancel) # type: ignore bb.button(QDialogButtonBox.StandardButton.Ok).setAutoDefault(True) qconnect(bb.accepted, diag.accept) qconnect(bb.rejected, diag.reject) vbox.addWidget(bb) diag.setLayout(vbox) diag.show() accepted = diag.exec() if not accepted: return ("", "") return (user.text().strip(), passwd.text())
def get_id_and_pass_from_user(mw: aqt.main.AnkiQt, username="", password="") -> Tuple[str, str]: diag = QDialog(mw) diag.setWindowTitle("Anki") diag.setWindowFlags(self.windowFlags() & ~Qt.WindowContextHelpButtonHint) # type: ignore diag.setWindowModality(Qt.WindowModal) vbox = QVBoxLayout() info_label = QLabel( without_unicode_isolation( tr(TR.SYNC_ACCOUNT_REQUIRED, link="https://ankiweb.net/account/register"))) info_label.setOpenExternalLinks(True) info_label.setWordWrap(True) vbox.addWidget(info_label) vbox.addSpacing(20) g = QGridLayout() l1 = QLabel(tr(TR.SYNC_ANKIWEB_ID_LABEL)) g.addWidget(l1, 0, 0) user = QLineEdit() user.setText(username) g.addWidget(user, 0, 1) l2 = QLabel(tr(TR.SYNC_PASSWORD_LABEL)) g.addWidget(l2, 1, 0) passwd = QLineEdit() passwd.setText(password) passwd.setEchoMode(QLineEdit.Password) g.addWidget(passwd, 1, 1) vbox.addLayout(g) bb = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel) # type: ignore bb.button(QDialogButtonBox.Ok).setAutoDefault(True) qconnect(bb.accepted, diag.accept) qconnect(bb.rejected, diag.reject) vbox.addWidget(bb) diag.setLayout(vbox) diag.show() accepted = diag.exec_() if not accepted: return ("", "") return (user.text().strip(), passwd.text())
def __init__(self, parent, bodyhtml, jsSavecommand, wintitle, dialogname): super(ExtraWysiwygEditorForField, self).__init__(parent) self.jsSavecommand = jsSavecommand self.parent = parent self.setWindowTitle(wintitle) self.resize(810, 700) restoreGeom(self, "805891399_winsize") mainLayout = QVBoxLayout() mainLayout.setContentsMargins(0, 0, 0, 0) mainLayout.setSpacing(0) self.setLayout(mainLayout) self.web = MyWebView(self) # maybe also self.parent? self.web.allowDrops = True # default in webview/AnkiWebView is False self.web.title = dialogname self.web.contextMenuEvent = self.contextMenuEvent mainLayout.addWidget(self.web) self.buttonBox = QDialogButtonBox(self) self.buttonBox.setOrientation(Qt.Horizontal) self.buttonBox.setStandardButtons(QDialogButtonBox.Cancel | QDialogButtonBox.Save) mainLayout.addWidget(self.buttonBox) self.buttonBox.accepted.connect(self.onAccept) self.buttonBox.rejected.connect(self.onReject) QMetaObject.connectSlotsByName(self) acceptShortcut = QShortcut(QKeySequence("Ctrl+Return"), self) acceptShortcut.activated.connect(self.onAccept) zoomIn_Shortcut = QShortcut(QKeySequence("Ctrl++"), self) zoomIn_Shortcut.activated.connect(self.web.zoom_in) zoomOut_Shortcut = QShortcut(QKeySequence("Ctrl+-"), self) zoomOut_Shortcut.activated.connect(self.web.zoom_out) self.web.stdHtml(body=bodyhtml, css=cssfiles, js=addon_jsfiles + other_jsfiles, head="", context=self)
def _ui_bottom_row(self): hbox = QHBoxLayout() buttons = QDialogButtonBox(Qt.Horizontal, self) # Button to check if content needs to be changed check_btn = buttons.addButton("&Check", QDialogButtonBox.ActionRole) check_btn.setToolTip("Check") check_btn.clicked.connect(lambda _: self.onCheck()) # Button to generate diff of proposed content changes diff_btn = buttons.addButton("&Diff", QDialogButtonBox.ActionRole) diff_btn.setToolTip("Show diff") diff_btn.clicked.connect(lambda _: self.onDiff()) # Button to make the proposed changes fix_btn = buttons.addButton("&Fix", QDialogButtonBox.ActionRole) fix_btn.setToolTip("Fix") fix_btn.clicked.connect(lambda _: self.onFix()) # Button to close this dialog close_btn = buttons.addButton("&Close", QDialogButtonBox.RejectRole) close_btn.clicked.connect(self.close) hbox.addWidget(buttons) return hbox
def _ui_bottom_row(self): hbox = QHBoxLayout() buttons = QDialogButtonBox(Qt.Horizontal, self) # Button to check if content needs to be changed check_btn = buttons.addButton("&Dry-run", QDialogButtonBox.ActionRole) check_btn.setToolTip("Dry-run") check_btn.clicked.connect(lambda _: self.onCheck(mode="dryrun")) # Button to diff the proposed changes diff_btn = buttons.addButton("&Diff", QDialogButtonBox.ActionRole) diff_btn.setToolTip("Diff") diff_btn.clicked.connect(lambda _: self.onCheck(mode="diff")) # Button to make the proposed changes fix_btn = buttons.addButton("&Update", QDialogButtonBox.ActionRole) fix_btn.setToolTip("Update") fix_btn.clicked.connect(lambda _: self.onCheck(mode="update")) # Button to close this dialog close_btn = buttons.addButton("&Close", QDialogButtonBox.RejectRole) close_btn.clicked.connect(self.close) hbox.addWidget(buttons) return hbox
def showText(txt, parent=None, type="text", run=True, geomKey=None, \ minWidth=500, minHeight=400, title="Anki", copyBtn=False): if not parent: parent = aqt.mw.app.activeWindow() or aqt.mw diag = QDialog(parent) diag.setWindowTitle(title) layout = QVBoxLayout(diag) diag.setLayout(layout) text = QTextBrowser() text.setOpenExternalLinks(True) if type == "text": text.setPlainText(txt) else: text.setHtml(txt) layout.addWidget(text) box = QDialogButtonBox(QDialogButtonBox.Close) layout.addWidget(box) if copyBtn: def onCopy(): QApplication.clipboard().setText(text.toPlainText()) btn = QPushButton(_("Copy to Clipboard")) btn.clicked.connect(onCopy) box.addButton(btn, QDialogButtonBox.ActionRole) def onReject(): if geomKey: saveGeom(diag, geomKey) QDialog.reject(diag) box.rejected.connect(onReject) def onFinish(): if geomKey: saveGeom(diag, geomKey) box.accepted.connect(onFinish) diag.setMinimumHeight(minHeight) diag.setMinimumWidth(minWidth) if geomKey: restoreGeom(diag, geomKey) if run: diag.exec_() else: return diag, box
def _getUserPass(self): d = QDialog(self.mw) d.setWindowTitle("Anki") d.setWindowModality(Qt.WindowModal) vbox = QVBoxLayout() l = QLabel( _("""\ <h1>Account Required</h1> A free account is required to keep your collection synchronized. Please \ <a href="%s">sign up</a> for an account, then \ enter your details below.""") % "https://ankiweb.net/account/login") l.setOpenExternalLinks(True) l.setWordWrap(True) vbox.addWidget(l) vbox.addSpacing(20) g = QGridLayout() l1 = QLabel(_("AnkiWeb ID:")) g.addWidget(l1, 0, 0) user = QLineEdit() g.addWidget(user, 0, 1) l2 = QLabel(_("Password:")) g.addWidget(l2, 1, 0) passwd = QLineEdit() passwd.setEchoMode(QLineEdit.Password) g.addWidget(passwd, 1, 1) vbox.addLayout(g) bb = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel) bb.button(QDialogButtonBox.Ok).setAutoDefault(True) bb.accepted.connect(d.accept) bb.rejected.connect(d.reject) vbox.addWidget(bb) d.setLayout(vbox) d.show() accepted = d.exec_() u = user.text() p = passwd.text() if not accepted or not u or not p: return return (u, p)
def chooseList(prompt, choices, startrow=0, parent=None): if not parent: parent = aqt.mw.app.activeWindow() d = QDialog(parent) d.setWindowModality(Qt.WindowModal) l = QVBoxLayout() d.setLayout(l) t = QLabel(prompt) l.addWidget(t) c = QListWidget() c.addItems(choices) c.setCurrentRow(startrow) l.addWidget(c) bb = QDialogButtonBox(QDialogButtonBox.Ok) bb.accepted.connect(d.accept) l.addWidget(bb) d.exec_() return c.currentRow()
class Previewer(QDialog): _last_state = None _card_changed = False _last_render: Union[int, float] = 0 _timer = None _show_both_sides = False def __init__(self, parent: QWidget, mw: AnkiQt, on_close: Callable[[], None]): super().__init__(None, Qt.Window) self._open = True self._parent = parent self._close_callback = on_close self.mw = mw icon = QIcon() icon.addPixmap(QPixmap(":/icons/anki.png"), QIcon.Normal, QIcon.Off) self.setWindowIcon(icon) 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() self.show() def _create_gui(self): self.setWindowTitle(tr(TR.ACTIONS_PREVIEW)) qconnect(self.finished, self._on_finished) self.silentlyClose = True self.vbox = QVBoxLayout() self.vbox.setContentsMargins(0, 0, 0, 0) self._web = AnkiWebView(title="previewer") self.vbox.addWidget(self._web) self.bbox = QDialogButtonBox() self._replay = self.bbox.addButton(tr(TR.ACTIONS_REPLAY_AUDIO), QDialogButtonBox.ActionRole) self._replay.setAutoDefault(False) self._replay.setShortcut(QKeySequence("R")) self._replay.setToolTip(tr(TR.ACTIONS_SHORTCUT_KEY, val="R")) qconnect(self._replay.clicked, self._on_replay_audio) both_sides_button = QCheckBox(tr(TR.QT_MISC_BACK_SIDE_ONLY)) both_sides_button.setShortcut(QKeySequence("B")) both_sides_button.setToolTip(tr(TR.ACTIONS_SHORTCUT_KEY, val="B")) self.bbox.addButton(both_sides_button, QDialogButtonBox.ActionRole) self._show_both_sides = self.mw.col.conf.get("previewBothSides", False) both_sides_button.setChecked(self._show_both_sides) qconnect(both_sides_button.toggled, self._on_show_both_sides) self.vbox.addWidget(self.bbox) self.setLayout(self.vbox) restoreGeom(self, "preview") def _on_finished(self, ok): saveGeom(self, "preview") self.mw.progress.timer(100, self._on_close, False) def _on_replay_audio(self): if self._state == "question": replay_audio(self.card(), True) elif self._state == "answer": replay_audio(self.card(), False) def close(self): self._on_close() super().close() def _on_close(self): self._open = False self._close_callback() def _setup_web_view(self): jsinc = [ "js/vendor/jquery.min.js", "js/vendor/browsersel.js", "js/mathjax.js", "js/vendor/mathjax/tex-chtml.js", "js/reviewer.js", ] self._web.stdHtml( self.mw.reviewer.revHtml(), css=["css/reviewer.css"], js=jsinc, context=self, ) self._web.set_bridge_command(self._on_bridge_cmd, self) def _on_bridge_cmd(self, cmd: str) -> Any: if cmd.startswith("play:"): play_clicked_audio(cmd, self.card()) 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 |= self.card_changed() # avoid rendering in quick succession elap_ms = int((time.time() - self._last_render) * 1000) delay = 300 if elap_ms < delay: self._timer = self.mw.progress.timer(delay - elap_ms, self._render_scheduled, False) else: self._render_scheduled() def cancel_timer(self): if self._timer: self._timer.stop() self._timer = None def _render_scheduled(self) -> None: self.cancel_timer() self._last_render = time.time() if not self._open: return c = self.card() func = "_showQuestion" if not c: txt = tr(TR.QT_MISC_PLEASE_SELECT_1_CARD) bodyclass = "" self._last_state = None else: if self._show_both_sides: self._state = "answer" elif self._card_changed: self._state = "question" currentState = self._state_and_mod() if currentState == self._last_state: # nothing has changed, avoid refreshing return # need to force reload even if answer txt = c.q(reload=True) if self._state == "answer": func = "_showAnswer" txt = c.a() txt = re.sub(r"\[\[type:[^]]+\]\]", "", txt) bodyclass = theme_manager.body_classes_for_card_ord(c.ord) if c.autoplay(): AnkiWebView.setPlaybackRequiresGesture(False) if self._show_both_sides: # if we're showing both sides at once, remove any audio # from the answer that's appeared on the question already question_audio = c.question_av_tags() only_on_answer_audio = [ x for x in c.answer_av_tags() if x not in question_audio ] audio = question_audio + only_on_answer_audio elif self._state == "question": audio = c.question_av_tags() else: audio = c.answer_av_tags() av_player.play_tags(audio) else: AnkiWebView.setPlaybackRequiresGesture(True) av_player.clear_queue_and_maybe_interrupt() txt = self.mw.prepare_card_text_for_display(txt) txt = gui_hooks.card_will_show( txt, c, "preview" + self._state.capitalize()) self._last_state = self._state_and_mod() self._web.eval("{}({},'{}');".format(func, json.dumps(txt), bodyclass)) self._card_changed = False def _on_show_both_sides(self, toggle): self._show_both_sides = toggle self.mw.col.conf["previewBothSides"] = toggle self.mw.col.setMod() if self._state == "answer" and not toggle: self._state = "question" self.render_card() def _state_and_mod(self): c = self.card() n = c.note() n.load() return (self._state, c.id, n.mod) def state(self) -> str: return self._state
class Previewer(QDialog): _last_state: LastStateAndMod | None = None _card_changed = False _last_render: int | float = 0 _timer: QTimer | None = None _show_both_sides = False def __init__(self, parent: QWidget, mw: AnkiQt, on_close: Callable[[], None]) -> None: super().__init__(None, Qt.WindowType.Window) mw.garbage_collect_on_dialog_finish(self) self._open = True self._parent = parent self._close_callback = on_close self.mw = mw disable_help_button(self) setWindowIcon(self) def card(self) -> Card | None: raise NotImplementedError def card_changed(self) -> bool: raise NotImplementedError def open(self) -> None: self._state = "question" self._last_state = None self._create_gui() self._setup_web_view() self.render_card() self.show() def _create_gui(self) -> None: self.setWindowTitle(tr.actions_preview()) self.close_shortcut = QShortcut(QKeySequence("Ctrl+Shift+P"), self) qconnect(self.close_shortcut.activated, self.close) qconnect(self.finished, self._on_finished) self.silentlyClose = True self.vbox = QVBoxLayout() self.vbox.setContentsMargins(0, 0, 0, 0) self._web = AnkiWebView(title="previewer") self.vbox.addWidget(self._web) self.bbox = QDialogButtonBox() self._replay = self.bbox.addButton( tr.actions_replay_audio(), QDialogButtonBox.ButtonRole.ActionRole) self._replay.setAutoDefault(False) self._replay.setShortcut(QKeySequence("R")) self._replay.setToolTip(tr.actions_shortcut_key(val="R")) qconnect(self._replay.clicked, self._on_replay_audio) both_sides_button = QCheckBox(tr.qt_misc_back_side_only()) both_sides_button.setShortcut(QKeySequence("B")) both_sides_button.setToolTip(tr.actions_shortcut_key(val="B")) self.bbox.addButton(both_sides_button, QDialogButtonBox.ButtonRole.ActionRole) self._show_both_sides = self.mw.col.get_config_bool( Config.Bool.PREVIEW_BOTH_SIDES) both_sides_button.setChecked(self._show_both_sides) qconnect(both_sides_button.toggled, self._on_show_both_sides) self.vbox.addWidget(self.bbox) self.setLayout(self.vbox) restoreGeom(self, "preview") def _on_finished(self, ok: int) -> None: saveGeom(self, "preview") self.mw.progress.timer(100, self._on_close, False) def _on_replay_audio(self) -> None: if self._state == "question": replay_audio(self.card(), True) elif self._state == "answer": replay_audio(self.card(), False) def close(self) -> None: self._on_close() super().close() def _on_close(self) -> None: self._open = False self._close_callback() self._web = None def _setup_web_view(self) -> None: self._web.stdHtml( self.mw.reviewer.revHtml(), css=["css/reviewer.css"], js=[ "js/mathjax.js", "js/vendor/mathjax/tex-chtml.js", "js/reviewer.js", ], context=self, ) self._web.set_bridge_command(self._on_bridge_cmd, self) def _on_bridge_cmd(self, cmd: str) -> Any: if cmd.startswith("play:"): play_clicked_audio(cmd, self.card()) def _update_flag_and_mark_icons(self, card: Card | None) -> None: if card: flag = card.user_flag() marked = card.note(reload=True).has_tag(MARKED_TAG) else: flag = 0 marked = False self._web.eval(f"_drawFlag({flag}); _drawMark({json.dumps(marked)});") def render_card(self) -> None: self.cancel_timer() # Keep track of whether render() has ever been called # with cardChanged=True since the last successful render self._card_changed |= self.card_changed() # avoid rendering in quick succession elap_ms = int((time.time() - self._last_render) * 1000) delay = 300 if elap_ms < delay: self._timer = self.mw.progress.timer(delay - elap_ms, self._render_scheduled, False) else: self._render_scheduled() def cancel_timer(self) -> None: if self._timer: self._timer.stop() self._timer = None def _render_scheduled(self) -> None: self.cancel_timer() self._last_render = time.time() if not self._open: return c = self.card() self._update_flag_and_mark_icons(c) func = "_showQuestion" ans_txt = "" if not c: txt = tr.qt_misc_please_select_1_card() bodyclass = "" self._last_state = None else: if self._show_both_sides: self._state = "answer" elif self._card_changed: self._state = "question" currentState = self._state_and_mod() if currentState == self._last_state: # nothing has changed, avoid refreshing return # need to force reload even if answer txt = c.question(reload=True) ans_txt = c.answer() if self._state == "answer": func = "_showAnswer" txt = ans_txt txt = re.sub(r"\[\[type:[^]]+\]\]", "", txt) bodyclass = theme_manager.body_classes_for_card_ord(c.ord) if c.autoplay(): if self._show_both_sides: # if we're showing both sides at once, remove any audio # from the answer that's appeared on the question already question_audio = c.question_av_tags() only_on_answer_audio = [ x for x in c.answer_av_tags() if x not in question_audio ] audio = question_audio + only_on_answer_audio elif self._state == "question": audio = c.question_av_tags() else: audio = c.answer_av_tags() av_player.play_tags(audio) else: av_player.clear_queue_and_maybe_interrupt() txt = self.mw.prepare_card_text_for_display(txt) txt = gui_hooks.card_will_show( txt, c, f"preview{self._state.capitalize()}") self._last_state = self._state_and_mod() js: str if self._state == "question": ans_txt = self.mw.col.media.escape_media_filenames(ans_txt) js = f"{func}({json.dumps(txt)}, {json.dumps(ans_txt)}, '{bodyclass}');" else: js = f"{func}({json.dumps(txt)}, '{bodyclass}');" self._web.eval(js) self._card_changed = False def _on_show_both_sides(self, toggle: bool) -> None: self._show_both_sides = toggle self.mw.col.set_config_bool(Config.Bool.PREVIEW_BOTH_SIDES, toggle) if self._state == "answer" and not toggle: self._state = "question" self.render_card() def _state_and_mod(self) -> tuple[str, int, int]: c = self.card() n = c.note() n.load() return (self._state, c.id, n.mod) def state(self) -> str: return self._state
class TagDialogExtended__BasicOrTagEdit(QDialog): def __init__(self, parent, tags, alltags): QDialog.__init__(self, parent, Qt.WindowType.Window) # super().__init__(parent) self.basic_mode = gc("dialog type: basic_but_quick") self.parent = parent self.alltags = alltags self.gridLayout = QGridLayout(self) self.gridLayout.setObjectName("gridLayout") self.label = QLabel("Edit tags:") self.gridLayout.addWidget(self.label, 0, 0, 1, 1) self.verticalLayout = QVBoxLayout() self.verticalLayout.setObjectName("verticalLayout") self.gridLayout.addLayout(self.verticalLayout, 1, 0, 1, 1) self.buttonBox = QDialogButtonBox(self) self.buttonBox.setOrientation(Qt.Orientation.Horizontal) self.buttonBox.setStandardButtons( QDialogButtonBox.StandardButton.Cancel | QDialogButtonBox.StandardButton.Ok) self.shortcut = QShortcut(QKeySequence("Ctrl+Return"), self) self.shortcut.activated.connect(self.accept) self.helpButton = QPushButton( "add empty line", clicked=lambda: self.maybe_add_line(force=True)) self.buttonBox.addButton(self.helpButton, QDialogButtonBox.ButtonRole.HelpRole) self.filterbutton = QPushButton("edit tag for current line", clicked=self.tagselector) self.buttonBox.addButton(self.filterbutton, QDialogButtonBox.ButtonRole.ResetRole) self.searchButton = QPushButton( "search", clicked=lambda: self.do_browser_search(extra_search="")) self.buttonBox.addButton(self.searchButton, QDialogButtonBox.ButtonRole.ResetRole) self.gridLayout.addWidget(self.buttonBox, 2, 0, 1, 1) self.buttonBox.accepted.connect(self.accept) self.buttonBox.rejected.connect(self.reject) self.setWindowTitle("Anki - Edit Tags") originalheight = self.height() restoreGeom(self, "TagDialogExtended") self.resize(self.width(), originalheight) if not tags: tags = [ "", ] else: tags.append("") self.line_list = [] for t in tags: self.maybe_add_line(t) self.cut = gc("in tag lines dialog: open filterdialog for single tag") if self.cut: self.filterbutton.setToolTip('shortcut: {}'.format(self.cut)) self.selkey = QShortcut(QKeySequence(self.cut), self) self.selkey.activated.connect(self.tagselector) self.browser_scut = gc("in tag lines dialog: search browser for tag") if self.browser_scut: self.searchButton.setToolTip('shortcut: {}'.format( self.browser_scut)) self.browser_scut_key = QShortcut(QKeySequence(self.browser_scut), self) self.browser_scut_key.activated.connect( lambda: self.do_browser_search(extra_search="")) # don't also set Ctrl+t,a/gc("editor: show filterdialog to add single tag") for # self.tagselector: What if the user has already set them to the same etc. I'd have # to do a lot of checking self.addnl = gc("in tag lines dialog: insert additional line") if self.addnl: self.helpButton.setToolTip('shortcut: {}'.format(self.addnl)) self.addnlscut = QShortcut(QKeySequence(self.addnl), self) self.addnlscut.activated.connect( lambda: self.maybe_add_line(force=True)) def current_tags_list(self): return [t.text() for t in self.line_list if t] def do_browser_search(self, extra_search=""): # Use the current line's text or the last line if the current one is an empty line note_tags = self.current_tags_list() searched_tag = shared_variables.focused_line.text() or ( note_tags[-1] if len(note_tags) > 0 else "") if searched_tag: browser = dialogs.open('Browser', mw) browser.setFilter('tag:"{}*" {}'.format(searched_tag, extra_search)) self.accept() else: tooltip("empty tag was selected for search") def tagselector(self): text = shared_variables.focused_line.text() d = FilterDialog(parent=self, values=self.alltags, allownew=True, prefill=text) if d.exec(): shared_variables.focused_line.setText(d.selkey) else: shared_variables.focused_line.setFocus() def change_focus_by_one(self, Down=True): for index, edit in enumerate(self.line_list): if edit == shared_variables.focused_line: if Down: if index == len( self.line_list) - 1: # if in last line go up self.line_list[0].setFocus() break else: newidx = index + 1 self.line_list[newidx].setFocus() break else: # go up if index == 0: # if in last line go up newidx = len(self.line_list) - 1 self.line_list[newidx].setFocus() break else: self.line_list[index - 1].setFocus() break def maybe_add_line(self, tag="", force=False): if self.line_list and not self.line_list[-1].text( ) and not force: # last lineedit is empty: self.line_list[-1].setFocus() self.line_list[-1].setText(tag) else: if self.basic_mode: te = MyBasicEdit(self) te.setText(tag) self.verticalLayout.addWidget(te) te.setFocus() self.line_list.append(te) else: te = MyTagEdit(self) te.setCol(mw.col) te.setText(tag) self.verticalLayout.addWidget(te) te.hideCompleter() te.setFocus() self.line_list.append(te) def accept(self): self.tagstring = "" for t in self.line_list: if not self.basic_mode: t.hideCompleter() text = t.text() if text: self.tagstring += text + " " saveGeom(self, "TagDialogExtended") QDialog.accept(self) def reject(self): saveGeom(self, "TagDialogExtended") QDialog.reject(self)
class ExtraWysiwygEditorForField(QDialog): def __init__(self, parent, bodyhtml, jsSavecommand, wintitle, dialogname): super(ExtraWysiwygEditorForField, self).__init__(parent) self.jsSavecommand = jsSavecommand self.parent = parent self.setWindowTitle(wintitle) self.resize(810, 700) restoreGeom(self, "805891399_winsize") mainLayout = QVBoxLayout() mainLayout.setContentsMargins(0, 0, 0, 0) mainLayout.setSpacing(0) self.setLayout(mainLayout) self.web = MyWebView(self) # maybe also self.parent? self.web.allowDrops = True # default in webview/AnkiWebView is False self.web.title = dialogname self.web.contextMenuEvent = self.contextMenuEvent mainLayout.addWidget(self.web) self.buttonBox = QDialogButtonBox(self) self.buttonBox.setOrientation(Qt.Horizontal) self.buttonBox.setStandardButtons(QDialogButtonBox.Cancel | QDialogButtonBox.Save) mainLayout.addWidget(self.buttonBox) self.buttonBox.accepted.connect(self.onAccept) self.buttonBox.rejected.connect(self.onReject) QMetaObject.connectSlotsByName(self) acceptShortcut = QShortcut(QKeySequence("Ctrl+Return"), self) acceptShortcut.activated.connect(self.onAccept) zoomIn_Shortcut = QShortcut(QKeySequence("Ctrl++"), self) zoomIn_Shortcut.activated.connect(self.web.zoom_in) zoomOut_Shortcut = QShortcut(QKeySequence("Ctrl+-"), self) zoomOut_Shortcut.activated.connect(self.web.zoom_out) self.web.stdHtml(body=bodyhtml, css=cssfiles, js=addon_jsfiles + other_jsfiles, head="", context=self) def onAccept(self): global editedfieldcontent editedfieldcontent = self.web.sync_execJavaScript(self.jsSavecommand) self.web = None # self.web._page.windowCloseRequested() # native qt signal not callable # self.web._page.windowCloseRequested.connect(self.web._page.window_close_requested) saveGeom(self, "805891399_winsize") self.accept() # self.done(0) def onReject(self): ok = askUser("Close and lose current input?") if ok: saveGeom(self, "805891399_winsize") self.web = None self.reject() def closeEvent(self, event): ok = askUser("Close and lose current input?") if ok: self.web = None event.accept() else: event.ignore()
def __init__(self, parent, tags, alltags): QDialog.__init__(self, parent, Qt.WindowType.Window) # super().__init__(parent) self.basic_mode = gc("dialog type: basic_but_quick") self.parent = parent self.alltags = alltags self.gridLayout = QGridLayout(self) self.gridLayout.setObjectName("gridLayout") self.label = QLabel("Edit tags:") self.gridLayout.addWidget(self.label, 0, 0, 1, 1) self.verticalLayout = QVBoxLayout() self.verticalLayout.setObjectName("verticalLayout") self.gridLayout.addLayout(self.verticalLayout, 1, 0, 1, 1) self.buttonBox = QDialogButtonBox(self) self.buttonBox.setOrientation(Qt.Orientation.Horizontal) self.buttonBox.setStandardButtons( QDialogButtonBox.StandardButton.Cancel | QDialogButtonBox.StandardButton.Ok) self.shortcut = QShortcut(QKeySequence("Ctrl+Return"), self) self.shortcut.activated.connect(self.accept) self.helpButton = QPushButton( "add empty line", clicked=lambda: self.maybe_add_line(force=True)) self.buttonBox.addButton(self.helpButton, QDialogButtonBox.ButtonRole.HelpRole) self.filterbutton = QPushButton("edit tag for current line", clicked=self.tagselector) self.buttonBox.addButton(self.filterbutton, QDialogButtonBox.ButtonRole.ResetRole) self.searchButton = QPushButton( "search", clicked=lambda: self.do_browser_search(extra_search="")) self.buttonBox.addButton(self.searchButton, QDialogButtonBox.ButtonRole.ResetRole) self.gridLayout.addWidget(self.buttonBox, 2, 0, 1, 1) self.buttonBox.accepted.connect(self.accept) self.buttonBox.rejected.connect(self.reject) self.setWindowTitle("Anki - Edit Tags") originalheight = self.height() restoreGeom(self, "TagDialogExtended") self.resize(self.width(), originalheight) if not tags: tags = [ "", ] else: tags.append("") self.line_list = [] for t in tags: self.maybe_add_line(t) self.cut = gc("in tag lines dialog: open filterdialog for single tag") if self.cut: self.filterbutton.setToolTip('shortcut: {}'.format(self.cut)) self.selkey = QShortcut(QKeySequence(self.cut), self) self.selkey.activated.connect(self.tagselector) self.browser_scut = gc("in tag lines dialog: search browser for tag") if self.browser_scut: self.searchButton.setToolTip('shortcut: {}'.format( self.browser_scut)) self.browser_scut_key = QShortcut(QKeySequence(self.browser_scut), self) self.browser_scut_key.activated.connect( lambda: self.do_browser_search(extra_search="")) # don't also set Ctrl+t,a/gc("editor: show filterdialog to add single tag") for # self.tagselector: What if the user has already set them to the same etc. I'd have # to do a lot of checking self.addnl = gc("in tag lines dialog: insert additional line") if self.addnl: self.helpButton.setToolTip('shortcut: {}'.format(self.addnl)) self.addnlscut = QShortcut(QKeySequence(self.addnl), self) self.addnlscut.activated.connect( lambda: self.maybe_add_line(force=True))