anki/qt/aqt/emptycards.py

103 lines
3.4 KiB
Python
Raw Normal View History

# 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
2021-02-02 14:30:53 +01:00
from concurrent.futures import Future
from typing import Any
import aqt
from anki.cards import CardId
2021-02-08 10:11:16 +01:00
from anki.collection import EmptyCardsReport
2020-05-20 05:39:21 +02:00
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()
2021-02-02 14:30:53 +01:00
def on_done(fut: Future) -> None:
mw.progress.finish()
report: EmptyCardsReport = fut.result()
if not report.notes:
2021-03-26 04:48:26 +01:00
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.weakref()
self.report = report
self.form = aqt.forms.emptycards.Ui_Dialog()
self.form.setupUi(self)
restoreGeom(self, "emptycards")
2021-03-26 04:48:26 +01:00
self.setWindowTitle(tr.empty_cards_window_title())
disable_help_button(self)
2021-03-26 04:48:26 +01:00
self.form.keep_notes.setText(tr.empty_cards_preserve_notes_checkbox())
self.form.webview.set_title("empty cards")
self.form.webview.set_bridge_command(self._on_note_link_clicked, self)
2020-05-20 05:39:21 +02:00
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:
saveGeom(self, "emptycards")
qconnect(self.finished, on_finished)
self._delete_button = self.form.buttonBox.addButton(
tr.empty_cards_delete_button(), QDialogButtonBox.ButtonRole.ActionRole
)
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()
2021-02-01 14:28:21 +01:00
def delete() -> int:
return self._delete_cards(self.form.keep_notes.isChecked())
2021-02-02 14:30:53 +01:00
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] = []
2021-02-08 10:11:16 +01:00
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])
2020-06-04 10:21:04 +02:00
self.mw.col.remove_cards_and_orphaned_notes(to_delete)
return len(to_delete)