don't leave orphaned facts around when editing

This commit is contained in:
Damien Elmes 2011-12-13 02:11:02 +09:00
parent 177a2d027b
commit dcb56ef265
3 changed files with 15 additions and 8 deletions

View file

@ -75,7 +75,6 @@ class AddCards(QDialog):
b.setEnabled(False)
self.historyButton = b
# FIXME: need to make sure to clean up note on exit
def setupNewNote(self, set=True):
f = self.mw.col.newNote()
f.tags = f.model()['tags']

View file

@ -11,11 +11,9 @@ from aqt.utils import saveGeom, restoreGeom, getBase, mungeQA, \
showWarning, openHelp
from anki.utils import isMac, isWin
# raise Exception("Remember to disallow media&latex refs in edit.")
class CardLayout(QDialog):
def __init__(self, mw, note, ord=0, parent=None):
def __init__(self, mw, note, ord=0, parent=None, addMode=False):
QDialog.__init__(self, parent or mw, Qt.Window)
self.mw = aqt.mw
self.parent = parent or mw
@ -24,6 +22,11 @@ class CardLayout(QDialog):
self.col = self.mw.col
self.mm = self.mw.col.models
self.model = note.model()
self.mw.checkpoint(_("Card Layout"))
self.addMode = addMode
if addMode:
# save it to DB temporarily
note.flush()
self.setupTabs()
self.setupButtons()
self.setWindowTitle(_("%s Layout") % self.model['name'])
@ -31,7 +34,6 @@ class CardLayout(QDialog):
v1.addWidget(self.tabs)
v1.addLayout(self.buttons)
self.setLayout(v1)
self.mw.checkpoint(_("Card Layout"))
self.redraw()
restoreGeom(self, "CardLayout")
self.exec_()
@ -257,6 +259,9 @@ Enter deck to place new %s cards in, or leave blank:""") %
self.reject()
def reject(self):
if self.addMode:
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")

View file

@ -330,7 +330,8 @@ class Editor(object):
ord = self.card.ord
else:
ord = 0
CardLayout(self.mw, self.note, ord=ord, parent=self.parentWindow)
CardLayout(self.mw, self.note, ord=ord, parent=self.parentWindow,
addMode=self.addMode)
self.loadNote()
# JS->Python bridge
@ -345,7 +346,8 @@ class Editor(object):
(type, txt) = str.split(":", 1)
self.note.fields[self.currentField] = self.mungeHTML(txt)
self.mw.requireReset()
self.note.flush()
if not self.addMode:
self.note.flush()
if type == "blur":
if not self._keepButtons:
self.disableButtons()
@ -526,7 +528,8 @@ class Editor(object):
m['did'] = self.note.did
m['tags'] = self.note.tags
self.mw.col.models.save(m)
self.note.flush()
if not self.addMode:
self.note.flush()
runHook("tagsUpdated", self.note)
# Format buttons