Anki/qt/aqt/emptycards.py
Aristotelis 5b0f371791
Fix AnkiWebPage not being initialized for default web view kinds (e.g. in add-ons) (#3933)
* add AnkiWebView subclasses for stats, empty cards and find dupes ui

* update ui files to use subclassed webviews instead

* remove superfluous calls to AnkiWebView.set_kind

* Avoid set_kind() race condition in legacy stats webview

Replacing the web view is a hacky workaround, but likely a reasonable compromise for a legacy view that we do not want to maintain a separate Qt form for.

* Slightly refactor AnkiWebView subclass creation and tweak inline comment

+ Extend create_ankiwebview_subclass() with the ability to set any
  init time AnkiWebView argument
+ Introduce some nice-to-haves in terms of static type checking support
  and IDE autocompletion
+ Mark helper function as private to discourage add-on use

* Drop `AnkiWebView.set_kind` completely

There no longer is an Anki-internal use case for changing the web view kind after initializing a web view, and add-ons almost certainly do not have any use for it either.

Given that setting the kind after web view construction can lead  to known race conditions with `domDone` signals, we should remove this method to discourage uses like this in both Anki code and add-on consumers.

There currenty only seems to be one add-on calling `set_kind()`, so this seem like a justifiable API change.

---------

Co-authored-by: llama <100429699+iamllama@users.noreply.github.com>
2025-04-22 21:22:40 +10:00

107 lines
3.6 KiB
Python

# Copyright: Ankitects Pty Ltd and contributors
# License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
from __future__ import annotations
import re
from concurrent.futures import Future
from typing import Any
import aqt
import aqt.forms
import aqt.main
from anki.cards import CardId
from anki.collection import EmptyCardsReport
from aqt import gui_hooks
from aqt.qt import QDialog, QDialogButtonBox, qconnect
from aqt.utils import disable_help_button, restoreGeom, saveGeom, tooltip, tr
def show_empty_cards(mw: aqt.main.AnkiQt) -> None:
mw.progress.start()
def on_done(fut: Future) -> None:
mw.progress.finish()
report: EmptyCardsReport = fut.result()
if not report.notes:
tooltip(tr.empty_cards_not_found())
return
diag = EmptyCardsDialog(mw, report)
diag.show()
mw.taskman.run_in_background(mw.col.get_empty_cards, on_done)
class EmptyCardsDialog(QDialog):
silentlyClose = True
def __init__(self, mw: aqt.main.AnkiQt, report: EmptyCardsReport) -> None:
super().__init__(mw)
self.mw = mw
self.mw.garbage_collect_on_dialog_finish(self)
self.report = report
self.form = aqt.forms.emptycards.Ui_Dialog()
self.form.setupUi(self)
restoreGeom(self, "emptycards")
self.setWindowTitle(tr.empty_cards_window_title())
disable_help_button(self)
self.form.keep_notes.setText(tr.empty_cards_preserve_notes_checkbox())
self.form.webview.set_bridge_command(self._on_note_link_clicked, self)
gui_hooks.empty_cards_will_show(self)
# make the note ids clickable
html = re.sub(
r"\[anki:nid:(\d+)\]",
"<a href=# onclick=\"pycmd('nid:\\1'); return false\">\\1</a>: ",
report.report,
)
style = "<style>.allempty { color: red; }</style>"
self.form.webview.stdHtml(style + html, context=self)
def on_finished(code: Any) -> None:
self.form.webview.cleanup()
self.form.webview = None # type: ignore
saveGeom(self, "emptycards")
qconnect(self.finished, on_finished)
self._delete_button = self.form.buttonBox.addButton(
tr.empty_cards_delete_button(), QDialogButtonBox.ButtonRole.ActionRole
)
assert self._delete_button is not None
self._delete_button.setAutoDefault(False)
qconnect(self._delete_button.clicked, self._on_delete)
def _on_note_link_clicked(self, link: str) -> None:
aqt.dialogs.open("Browser", self.mw, search=(link,))
def _on_delete(self) -> None:
self.mw.progress.start()
def delete() -> int:
return self._delete_cards(self.form.keep_notes.isChecked())
def on_done(fut: Future) -> None:
self.mw.progress.finish()
try:
count = fut.result()
finally:
self.close()
tooltip(tr.empty_cards_deleted_count(cards=count))
self.mw.reset()
self.mw.taskman.run_in_background(delete, on_done)
def _delete_cards(self, keep_notes: bool) -> int:
to_delete: list[CardId] = []
note: EmptyCardsReport.NoteWithEmptyCards
for note in self.report.notes:
if keep_notes and note.will_delete_note:
# leave first card
to_delete.extend([CardId(id) for id in note.card_ids[1:]])
else:
to_delete.extend([CardId(id) for id in note.card_ids])
self.mw.col.remove_cards_and_orphaned_notes(to_delete)
return len(to_delete)