mirror of
https://github.com/ankitects/anki.git
synced 2025-09-20 15:02:21 -04:00
catch template errors, and start on discarding changes to fields/cards
This is only a first step - all the current methods that implicitly save need to be updated.
This commit is contained in:
parent
8bde0d4ac1
commit
23586ffe90
3 changed files with 44 additions and 15 deletions
|
@ -11,6 +11,7 @@ import aqt
|
|||
from anki.cards import Card
|
||||
from anki.consts import *
|
||||
from anki.lang import _, ngettext
|
||||
from anki.rsbackend import TemplateError
|
||||
from anki.utils import isMac, isWin, joinFields
|
||||
from aqt import gui_hooks
|
||||
from aqt.qt import *
|
||||
|
@ -273,10 +274,15 @@ class CardLayout(QDialog):
|
|||
l.addWidget(flip)
|
||||
qconnect(flip.clicked, self.onFlip)
|
||||
l.addStretch()
|
||||
close = QPushButton(_("Close"))
|
||||
save = QPushButton(_("Save"))
|
||||
save.setAutoDefault(False)
|
||||
l.addWidget(save)
|
||||
qconnect(save.clicked, self.accept)
|
||||
|
||||
close = QPushButton(_("Cancel"))
|
||||
close.setAutoDefault(False)
|
||||
l.addWidget(close)
|
||||
qconnect(close.clicked, self.accept)
|
||||
qconnect(close.clicked, self.reject)
|
||||
|
||||
# Cards
|
||||
##########################################################################
|
||||
|
@ -596,23 +602,37 @@ Enter deck to place new %s cards in, or leave blank:"""
|
|||
# Closing & Help
|
||||
######################################################################
|
||||
|
||||
def accept(self):
|
||||
self.reject()
|
||||
def accept(self) -> None:
|
||||
try:
|
||||
self.mm.save(self.model)
|
||||
except TemplateError as e:
|
||||
# fixme: i18n
|
||||
showWarning("Unable to save changes: " + str(e))
|
||||
return
|
||||
|
||||
def reject(self):
|
||||
self.mw.reset()
|
||||
self.cleanup()
|
||||
return QDialog.accept(self)
|
||||
|
||||
def reject(self) -> None:
|
||||
# discard mutations - in the future we should load a fresh
|
||||
# copy at the start instead
|
||||
self.mm._remove_from_cache(self.model["id"])
|
||||
self.cleanup()
|
||||
return QDialog.reject(self)
|
||||
|
||||
def cleanup(self) -> None:
|
||||
self.cancelPreviewTimer()
|
||||
av_player.stop_and_clear_queue()
|
||||
# fixme
|
||||
if self.addMode:
|
||||
# remove the filler fields we added
|
||||
for name in self.emptyFields:
|
||||
self.note[name] = ""
|
||||
self.mw.col.db.execute("delete from notes where id = ?", self.note.id)
|
||||
self.mm.save(self.model, templates=True)
|
||||
self.mw.reset()
|
||||
saveGeom(self, "CardLayout")
|
||||
self.pform.frontWeb = None
|
||||
self.pform.backWeb = None
|
||||
return QDialog.reject(self)
|
||||
|
||||
def onHelp(self):
|
||||
openHelp("templates")
|
||||
|
|
|
@ -4,6 +4,7 @@
|
|||
import aqt
|
||||
from anki.consts import *
|
||||
from anki.lang import _, ngettext
|
||||
from anki.rsbackend import TemplateError
|
||||
from aqt.qt import *
|
||||
from aqt.utils import askUser, getOnlyText, openHelp, showWarning
|
||||
|
||||
|
@ -22,7 +23,8 @@ class FieldDialog(QDialog):
|
|||
self.form.setupUi(self)
|
||||
self.setWindowTitle(_("Fields for %s") % self.model["name"])
|
||||
self.form.buttonBox.button(QDialogButtonBox.Help).setAutoDefault(False)
|
||||
self.form.buttonBox.button(QDialogButtonBox.Close).setAutoDefault(False)
|
||||
self.form.buttonBox.button(QDialogButtonBox.Cancel).setAutoDefault(False)
|
||||
self.form.buttonBox.button(QDialogButtonBox.Save).setAutoDefault(False)
|
||||
self.currentIdx = None
|
||||
self.oldSortField = self.model["sortf"]
|
||||
self.fillFields()
|
||||
|
@ -167,17 +169,24 @@ class FieldDialog(QDialog):
|
|||
fld["rtl"] = f.rtl.isChecked()
|
||||
|
||||
def reject(self):
|
||||
self.mm._remove_from_cache(self.model["id"])
|
||||
QDialog.reject(self)
|
||||
|
||||
def accept(self):
|
||||
self.saveField()
|
||||
if self.oldSortField != self.model["sortf"]:
|
||||
self.mw.progress.start()
|
||||
self.mw.col.updateFieldCache(self.mm.nids(self.model))
|
||||
self.mw.progress.finish()
|
||||
self.mm.save(self.model)
|
||||
self.mw.reset()
|
||||
QDialog.reject(self)
|
||||
try:
|
||||
self.mm.save(self.model)
|
||||
except TemplateError as e:
|
||||
# fixme: i18n
|
||||
showWarning("Unable to save changes: " + str(e))
|
||||
return
|
||||
|
||||
def accept(self):
|
||||
self.reject()
|
||||
self.mw.reset()
|
||||
QDialog.accept(self)
|
||||
|
||||
def onHelp(self):
|
||||
openHelp("fields")
|
||||
|
|
|
@ -147,7 +147,7 @@
|
|||
<enum>Qt::Horizontal</enum>
|
||||
</property>
|
||||
<property name="standardButtons">
|
||||
<set>QDialogButtonBox::Close|QDialogButtonBox::Help</set>
|
||||
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Help|QDialogButtonBox::Save</set>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
|
|
Loading…
Reference in a new issue