fix IndexError when switching notetypes

Closes #780
https://github.com/ankitects/anki/pull/780
This commit is contained in:
Damien Elmes 2020-10-05 13:33:54 +10:00
parent 8fa865e8f4
commit bf72773531
2 changed files with 22 additions and 4 deletions

View file

@ -55,7 +55,9 @@ class AddCards(QDialog):
self.editor = aqt.editor.Editor(self.mw, self.form.fieldsArea, self, True)
def setupChoosers(self) -> None:
self.modelChooser = aqt.modelchooser.ModelChooser(self.mw, self.form.modelArea)
self.modelChooser = aqt.modelchooser.ModelChooser(
self.mw, self.form.modelArea, on_activated=self.show_notetype_selector
)
self.deckChooser = aqt.deckchooser.DeckChooser(self.mw, self.form.deckArea)
def helpRequested(self):
@ -92,6 +94,9 @@ class AddCards(QDialog):
def setAndFocusNote(self, note: Note) -> None:
self.editor.setNote(note, focusTo=0)
def show_notetype_selector(self) -> None:
self.editor.saveNow(self.modelChooser.onModelChange)
def onModelChange(self, unused=None) -> None:
oldNote = self.editor.note
note = self.mw.col.newNote()

View file

@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-
# Copyright: Ankitects Pty Ltd and contributors
# License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
from typing import Optional
from anki.lang import _
from aqt import AnkiQt, gui_hooks
@ -9,12 +10,25 @@ from aqt.utils import shortcut
class ModelChooser(QHBoxLayout):
def __init__(self, mw: AnkiQt, widget: QWidget, label: bool = True) -> None:
def __init__(
self,
mw: AnkiQt,
widget: QWidget,
label: bool = True,
on_activated: Optional[Callable[[], None]] = None,
) -> None:
"""If provided, on_activated() will be called when the button is clicked,
and the caller can call .onModelChange() to pull up the dialog when they
are ready."""
QHBoxLayout.__init__(self)
self.widget = widget # type: ignore
self.mw = mw
self.deck = mw.col
self.label = label
if on_activated:
self.on_activated = on_activated
else:
self.on_activated = self.onModelChange
self.setContentsMargins(0, 0, 0, 0)
self.setSpacing(8)
self.setupModels()
@ -27,9 +41,8 @@ class ModelChooser(QHBoxLayout):
self.addWidget(self.modelLabel)
# models box
self.models = QPushButton()
# self.models.setStyleSheet("* { text-align: left; }")
self.models.setToolTip(shortcut(_("Change Note Type (Ctrl+N)")))
QShortcut(QKeySequence("Ctrl+N"), self.widget, activated=self.onModelChange) # type: ignore
QShortcut(QKeySequence("Ctrl+N"), self.widget, activated=self.on_activated) # type: ignore
self.models.setAutoDefault(False)
self.addWidget(self.models)
qconnect(self.models.clicked, self.onModelChange)