Fix new deck not being selected (#1681)

* Improve type annotations in studydeck.py

* Use `super()`

* Explicitly delete widget after closing dialog if parent is None

* Consolidate common cleanup tasks into a function

..., and connect it to `finished` signal

* Fix new deck not being selected

* Remove redundant assignments
This commit is contained in:
Hikaru Y 2022-02-22 20:51:23 +09:00 committed by GitHub
parent 131a94dd85
commit eafe426622
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,7 +1,9 @@
# Copyright: Ankitects Pty Ltd and contributors # Copyright: Ankitects Pty Ltd and contributors
# License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html # License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
from typing import Optional from __future__ import annotations
from typing import Callable
import aqt import aqt
import aqt.forms import aqt.forms
@ -28,24 +30,25 @@ class StudyDeck(QDialog):
def __init__( def __init__(
self, self,
mw: aqt.AnkiQt, mw: aqt.AnkiQt,
names: Callable = None, names: Callable[[], list[str]] | None = None,
accept: str = None, accept: str | None = None,
title: str = None, title: str | None = None,
help: HelpPageArgument = HelpPage.KEYBOARD_SHORTCUTS, help: HelpPageArgument = HelpPage.KEYBOARD_SHORTCUTS,
current: Optional[str] = None, current: str | None = None,
cancel: bool = True, cancel: bool = True,
parent: Optional[QWidget] = None, parent: QWidget | None = None,
dyn: bool = False, dyn: bool = False,
buttons: Optional[list[Union[str, QPushButton]]] = None, buttons: list[str | QPushButton] | None = None,
geomKey: str = "default", geomKey: str = "default",
callback: Union[Callable, None] = None, callback: Callable[[StudyDeck], None] | None = None,
) -> None: ) -> None:
QDialog.__init__(self, parent or mw) super().__init__(parent)
if not parent:
mw.garbage_collect_on_dialog_finish(self)
self.mw = mw self.mw = mw
self.form = aqt.forms.studydeck.Ui_Dialog() self.form = aqt.forms.studydeck.Ui_Dialog()
self.form.setupUi(self) self.form.setupUi(self)
self.form.filter.installEventFilter(self) self.form.filter.installEventFilter(self)
self.cancel = cancel
gui_hooks.state_did_reset.append(self.onReset) gui_hooks.state_did_reset.append(self.onReset)
self.geomKey = f"studyDeck-{geomKey}" self.geomKey = f"studyDeck-{geomKey}"
restoreGeom(self, self.geomKey) restoreGeom(self, self.geomKey)
@ -79,14 +82,15 @@ class StudyDeck(QDialog):
else: else:
self.nameFunc = names self.nameFunc = names
self.origNames = names() self.origNames = names()
self.name: Optional[str] = None self.name: str | None = None
self.ok = self.form.buttonBox.addButton( self.form.buttonBox.addButton(
accept or tr.decks_study(), QDialogButtonBox.ButtonRole.AcceptRole accept or tr.decks_study(), QDialogButtonBox.ButtonRole.AcceptRole
) )
self.setModal(True) self.setModal(True)
qconnect(self.form.buttonBox.helpRequested, lambda: openHelp(help)) qconnect(self.form.buttonBox.helpRequested, lambda: openHelp(help))
qconnect(self.form.filter.textEdited, self.redraw) qconnect(self.form.filter.textEdited, self.redraw)
qconnect(self.form.list.itemDoubleClicked, self.accept) qconnect(self.form.list.itemDoubleClicked, self.accept)
qconnect(self.finished, self.on_finished)
self.show() self.show()
# redraw after show so position at center correct # redraw after show so position at center correct
self.redraw("", current) self.redraw("", current)
@ -121,7 +125,7 @@ class StudyDeck(QDialog):
return True return True
return False return False
def redraw(self, filt: str, focus: Optional[str] = None) -> None: def redraw(self, filt: str, focus: str | None = None) -> None:
self.filt = filt self.filt = filt
self.focus = focus self.focus = focus
self.names = [n for n in self.origNames if self._matches(n, filt)] self.names = [n for n in self.origNames if self._matches(n, filt)]
@ -152,21 +156,17 @@ class StudyDeck(QDialog):
self.redraw(self.filt, self.focus) self.redraw(self.filt, self.focus)
def accept(self) -> None: def accept(self) -> None:
saveGeom(self, self.geomKey)
gui_hooks.state_did_reset.remove(self.onReset)
row = self.form.list.currentRow() row = self.form.list.currentRow()
if row < 0: if row < 0:
showInfo(tr.decks_please_select_something()) showInfo(tr.decks_please_select_something())
return return
self.name = self.names[self.form.list.currentRow()] self.name = self.names[self.form.list.currentRow()]
self.accept_with_callback()
def accept_with_callback(self) -> None:
if self.callback: if self.callback:
self.callback(self) self.callback(self)
QDialog.accept(self) super().accept()
def reject(self) -> None:
saveGeom(self, self.geomKey)
gui_hooks.state_did_reset.remove(self.onReset)
QDialog.reject(self)
def onAddDeck(self) -> None: def onAddDeck(self) -> None:
row = self.form.list.currentRow() row = self.form.list.currentRow()
@ -178,11 +178,11 @@ class StudyDeck(QDialog):
def success(out: OpChangesWithId) -> None: def success(out: OpChangesWithId) -> None:
deck = self.mw.col.decks.get(DeckId(out.id)) deck = self.mw.col.decks.get(DeckId(out.id))
self.name = deck["name"] self.name = deck["name"]
self.accept_with_callback()
# make sure we clean up reset hook when manually exiting
gui_hooks.state_did_reset.remove(self.onReset)
QDialog.accept(self)
if diag := add_deck_dialog(parent=self, default_text=default): if diag := add_deck_dialog(parent=self, default_text=default):
diag.success(success).run_in_background() diag.success(success).run_in_background()
def on_finished(self) -> None:
saveGeom(self, self.geomKey)
gui_hooks.state_did_reset.remove(self.onReset)