From 426ea1aba9770a69add66860c6bfaf810668c447 Mon Sep 17 00:00:00 2001 From: abdo Date: Fri, 22 Jan 2021 05:56:39 +0300 Subject: [PATCH] Move the logic for selecting a note type to models.py --- qt/aqt/browser.py | 1 + qt/aqt/models.py | 21 ++++++++++++++++++--- qt/aqt/sidebar.py | 13 +++---------- qt/tools/genhooks_gui.py | 5 ----- 4 files changed, 22 insertions(+), 18 deletions(-) diff --git a/qt/aqt/browser.py b/qt/aqt/browser.py index d18348dc5..d6acff4b6 100644 --- a/qt/aqt/browser.py +++ b/qt/aqt/browser.py @@ -1230,6 +1230,7 @@ QTableView {{ gridline-color: {grid} }} ":/icons/notetype.svg", self._note_filter(m.name), item_type=SidebarItemType.NOTETYPE, + id=m.id, ) root.addChild(item) diff --git a/qt/aqt/models.py b/qt/aqt/models.py index ffbcd0310..f4f49d8c0 100644 --- a/qt/aqt/models.py +++ b/qt/aqt/models.py @@ -28,10 +28,17 @@ from aqt.utils import ( class Models(QDialog): - def __init__(self, mw: AnkiQt, parent=None, fromMain=False): + def __init__( + self, + mw: AnkiQt, + parent=None, + fromMain=False, + selected_notetype_id: Optional[int] = None, + ): self.mw = mw parent = parent or mw self.fromMain = fromMain + self.selected_notetype_id = selected_notetype_id QDialog.__init__(self, parent, Qt.Window) self.col = mw.col.weakref() assert self.col @@ -51,6 +58,15 @@ class Models(QDialog): # Models ########################################################################## + def maybe_select_provided_notetype(self): + if not self.selected_notetype_id: + self.form.modelsList.setCurrentRow(0) + return + for i, m in enumerate(self.models): + if m.id == self.selected_notetype_id: + self.form.modelsList.setCurrentRow(i) + break + def setupModels(self) -> None: self.model = None f = self.form @@ -80,8 +96,7 @@ class Models(QDialog): def on_done(fut) -> None: self.updateModelsList(fut.result()) - f.modelsList.setCurrentRow(0) - gui_hooks.models_dialog_will_show(self) + self.maybe_select_provided_notetype() self.mw.taskman.with_progress(self.col.models.all_use_counts, on_done, self) maybeHideClose(box) diff --git a/qt/aqt/sidebar.py b/qt/aqt/sidebar.py index 1ffdd1c5c..e3658ecd7 100644 --- a/qt/aqt/sidebar.py +++ b/qt/aqt/sidebar.py @@ -8,7 +8,6 @@ from enum import Enum import aqt from anki.errors import DeckRenameError -from aqt import gui_hooks from aqt.main import ResetReason from aqt.models import Models from aqt.qt import * @@ -197,12 +196,6 @@ class NewSidebarTreeView(SidebarTreeViewBase): self.browser.renameFilter(item.name) def manage_notetype(self, item: "aqt.browser.SidebarItem") -> None: - def select(dialog: QDialog): - for i, m in enumerate(dialog.models): - if m.name == item.name: - dialog.form.modelsList.setCurrentRow(i) - break - - gui_hooks.models_dialog_will_show.append(select) - Models(self.mw, parent=self.browser, fromMain=True) - gui_hooks.models_dialog_will_show.remove(select) + Models( + self.mw, parent=self.browser, fromMain=True, selected_notetype_id=item.id + ) diff --git a/qt/tools/genhooks_gui.py b/qt/tools/genhooks_gui.py index e239fc429..fdc3af8c8 100644 --- a/qt/tools/genhooks_gui.py +++ b/qt/tools/genhooks_gui.py @@ -742,11 +742,6 @@ gui_hooks.webview_did_inject_style_into_page.append(mytest) ), # Model ################### - Hook( - name="models_dialog_will_show", - args=["dialog: QDialog"], - doc="""Allows changing the models dialog before it is shown.""", - ), Hook( name="models_advanced_will_show", args=["advanced: QDialog"],