Move the logic for selecting a note type to models.py

This commit is contained in:
abdo 2021-01-22 05:56:39 +03:00
parent 918bba6365
commit 426ea1aba9
4 changed files with 22 additions and 18 deletions

View File

@ -1230,6 +1230,7 @@ QTableView {{ gridline-color: {grid} }}
":/icons/notetype.svg", ":/icons/notetype.svg",
self._note_filter(m.name), self._note_filter(m.name),
item_type=SidebarItemType.NOTETYPE, item_type=SidebarItemType.NOTETYPE,
id=m.id,
) )
root.addChild(item) root.addChild(item)

View File

@ -28,10 +28,17 @@ from aqt.utils import (
class Models(QDialog): 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 self.mw = mw
parent = parent or mw parent = parent or mw
self.fromMain = fromMain self.fromMain = fromMain
self.selected_notetype_id = selected_notetype_id
QDialog.__init__(self, parent, Qt.Window) QDialog.__init__(self, parent, Qt.Window)
self.col = mw.col.weakref() self.col = mw.col.weakref()
assert self.col assert self.col
@ -51,6 +58,15 @@ class Models(QDialog):
# Models # 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: def setupModels(self) -> None:
self.model = None self.model = None
f = self.form f = self.form
@ -80,8 +96,7 @@ class Models(QDialog):
def on_done(fut) -> None: def on_done(fut) -> None:
self.updateModelsList(fut.result()) self.updateModelsList(fut.result())
f.modelsList.setCurrentRow(0) self.maybe_select_provided_notetype()
gui_hooks.models_dialog_will_show(self)
self.mw.taskman.with_progress(self.col.models.all_use_counts, on_done, self) self.mw.taskman.with_progress(self.col.models.all_use_counts, on_done, self)
maybeHideClose(box) maybeHideClose(box)

View File

@ -8,7 +8,6 @@ from enum import Enum
import aqt import aqt
from anki.errors import DeckRenameError from anki.errors import DeckRenameError
from aqt import gui_hooks
from aqt.main import ResetReason from aqt.main import ResetReason
from aqt.models import Models from aqt.models import Models
from aqt.qt import * from aqt.qt import *
@ -197,12 +196,6 @@ class NewSidebarTreeView(SidebarTreeViewBase):
self.browser.renameFilter(item.name) self.browser.renameFilter(item.name)
def manage_notetype(self, item: "aqt.browser.SidebarItem") -> None: def manage_notetype(self, item: "aqt.browser.SidebarItem") -> None:
def select(dialog: QDialog): Models(
for i, m in enumerate(dialog.models): self.mw, parent=self.browser, fromMain=True, selected_notetype_id=item.id
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)

View File

@ -742,11 +742,6 @@ gui_hooks.webview_did_inject_style_into_page.append(mytest)
), ),
# Model # Model
################### ###################
Hook(
name="models_dialog_will_show",
args=["dialog: QDialog"],
doc="""Allows changing the models dialog before it is shown.""",
),
Hook( Hook(
name="models_advanced_will_show", name="models_advanced_will_show",
args=["advanced: QDialog"], args=["advanced: QDialog"],