From 23586ffe90f7aa17e81cd335e6e59dc72a8b9aeb Mon Sep 17 00:00:00 2001 From: Damien Elmes Date: Sun, 26 Apr 2020 12:34:25 +1000 Subject: [PATCH] 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. --- qt/aqt/clayout.py | 36 ++++++++++++++++++++++++++++-------- qt/aqt/fields.py | 21 +++++++++++++++------ qt/designer/fields.ui | 2 +- 3 files changed, 44 insertions(+), 15 deletions(-) diff --git a/qt/aqt/clayout.py b/qt/aqt/clayout.py index d09f0595f..aec6c838b 100644 --- a/qt/aqt/clayout.py +++ b/qt/aqt/clayout.py @@ -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") diff --git a/qt/aqt/fields.py b/qt/aqt/fields.py index 40cc963a1..107ca0346 100644 --- a/qt/aqt/fields.py +++ b/qt/aqt/fields.py @@ -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") diff --git a/qt/designer/fields.ui b/qt/designer/fields.ui index 6ba5507de..c0dc3bfc3 100644 --- a/qt/designer/fields.ui +++ b/qt/designer/fields.ui @@ -147,7 +147,7 @@ Qt::Horizontal - QDialogButtonBox::Close|QDialogButtonBox::Help + QDialogButtonBox::Cancel|QDialogButtonBox::Help|QDialogButtonBox::Save