1

now mostly working; doing some cleanup

This commit is contained in:
Alex Yatskov 2012-12-24 18:53:41 -08:00
parent 7fa6904812
commit 7a07ab461c
3 changed files with 50 additions and 67 deletions

View File

@ -22,11 +22,12 @@ import anki.hooks
class Anki: class Anki:
def addNote(self, deckName, modelName, fields, tags=list()): def addNote(self, deckName, modelName, fields, tags=list()):
note = self.createNote(fields, deckName, modelName, tags) note = self.createNote(deckName, modelName, fields, tags)
if note is not None: if note is not None:
self.collection().addNote(note) collection = self.collection()
self.decks().save(self.currentDeck()) collection.addNote(note)
self.window().requireReset() collection.autosave()
self.startEditing()
def canAddNote(self, deckName, modelName, fields): def canAddNote(self, deckName, modelName, fields):
@ -34,25 +35,14 @@ class Anki:
def createNote(self, deckName, modelName, fields, tags=list()): def createNote(self, deckName, modelName, fields, tags=list()):
model = self.findModel(modelName) model = self.models().byName(modelName)
if model is None: if model is None:
return None return None
deck = self.findDeck(deckName) deck = self.decks().byName(deckName)
if deck is None: if deck is None:
return None return None
#~ conf = self.collection().conf
#~ deck = self.currentDeck()
#~ if conf['curModel'] != model['id'] or deck['mid'] != model['id']:
#~ conf['curModel'] = deck['mid'] = model['id']
#~ self.collection().decks.save(deck)
#~ anki.hooks.runHook('currentModelChanged')
#~ self.window().reset()
#~ self.collection().conf['curModel'] = model['id']
#~ deck['mid'] = model['id']
note = anki.notes.Note(self.collection(), model) note = anki.notes.Note(self.collection(), model)
note.model()['did'] = deck['id'] note.model()['did'] = deck['id']
note.tags = tags note.tags = tags
@ -70,16 +60,20 @@ class Anki:
#~ browser.onnote() #~ browser.onnote()
def startEditing(self):
self.window().requireReset()
def stopEditing(self):
self.window().maybeReset()
def window(self): def window(self):
return aqt.mw return aqt.mw
def form(self):
return self.window().form
def toolsMenu(self): def toolsMenu(self):
return self.form().menuTools return self.window().form.menuTools
def collection(self): def collection(self):
@ -94,16 +88,9 @@ class Anki:
return self.models().allNames() return self.models().allNames()
def modelFieldNames(self, model): def modelFieldNames(self, modelName):
return [field['name'] for field in model['flds']] model = self.models().byName(modelName)
return None if model is None else [field['name'] for field in model['flds']]
def findModel(self, name):
return self.models().byName(name)
def currentModel(self):
return self.models().current()
def decks(self): def decks(self):
@ -112,12 +99,3 @@ class Anki:
def deckNames(self): def deckNames(self):
return self.decks().allNames() return self.decks().allNames()
def findDeck(self, name):
return self.decks().byName(name)
def currentDeck(self):
return self.decks().current()

View File

@ -149,6 +149,6 @@ class DialogPreferences(QtGui.QDialog):
def onAnkiModelChanged(self, index): def onAnkiModelChanged(self, index):
model = self.anki.findModel(self.comboBoxAnkiModel.currentText()) modelName = self.comboBoxAnkiModel.currentText()
fieldNames = list() if model is None else self.anki.modelFieldNames(model) fieldNames = self.anki.modelFieldNames(modelName) or list()
self.setAnkiFields(fieldNames, self.preferences.ankiFields) self.setAnkiFields(fieldNames, self.preferences.ankiFields)

View File

@ -93,11 +93,11 @@ class MainWindowReader(QtGui.QMainWindow):
def applyPreferences(self): def applyPreferences(self):
if self.preferences.uiReaderState != None: if self.preferences.uiReaderState is not None:
self.restoreState(QtCore.QByteArray.fromBase64(self.preferences.uiReaderState)) self.restoreState(QtCore.QByteArray.fromBase64(self.preferences.uiReaderState))
if self.preferences.uiReaderPosition != None: if self.preferences.uiReaderPosition is not None:
self.move(QtCore.QPoint(*self.preferences.uiReaderPosition)) self.move(QtCore.QPoint(*self.preferences.uiReaderPosition))
if self.preferences.uiReaderSize != None: if self.preferences.uiReaderSize is not None:
self.resize(QtCore.QSize(*self.preferences.uiReaderSize)) self.resize(QtCore.QSize(*self.preferences.uiReaderSize))
self.comboTags.addItems(self.preferences.ankiTags) self.comboTags.addItems(self.preferences.ankiTags)
@ -123,7 +123,10 @@ class MainWindowReader(QtGui.QMainWindow):
self.preferences.uiReaderState = self.saveState().toBase64() self.preferences.uiReaderState = self.saveState().toBase64()
self.preferences.save() self.preferences.save()
if self.closed: if self.anki is not None:
self.anki.stopEditing()
if self.closed is not None:
self.closed() self.closed()
@ -164,7 +167,7 @@ class MainWindowReader(QtGui.QMainWindow):
dialog = DialogPreferences(self, self.preferences, self.anki) dialog = DialogPreferences(self, self.preferences, self.anki)
if dialog.exec_() == QtGui.QDialog.Accepted: if dialog.exec_() == QtGui.QDialog.Accepted:
self.applyPreferencesContent() self.applyPreferencesContent()
if self.updated: if self.updated is not None:
self.updated() self.updated()
@ -256,6 +259,7 @@ class MainWindowReader(QtGui.QMainWindow):
definition.sentence definition.sentence
) )
self.ankiAddFact(markup) self.ankiAddFact(markup)
self.updateDefinitions()
if command == 'addFactReading': if command == 'addFactReading':
markup = reader_util.buildFactMarkupReading( markup = reader_util.buildFactMarkupReading(
definition.reading, definition.reading,
@ -263,6 +267,7 @@ class MainWindowReader(QtGui.QMainWindow):
definition.sentence definition.sentence
) )
self.ankiAddFact(markup) self.ankiAddFact(markup)
self.updateDefinitions()
elif command == 'copyDefinition': elif command == 'copyDefinition':
reader_util.copyDefinitions([definition]) reader_util.copyDefinitions([definition])
@ -342,11 +347,11 @@ class MainWindowReader(QtGui.QMainWindow):
self.setStatus(u'Loaded file {0}'.format(filename)) self.setStatus(u'Loaded file {0}'.format(filename))
self.setWindowTitle(u'Yomichan - {0} ({1})'.format(os.path.split(filename)[1], encoding)) self.setWindowTitle(u'Yomichan - {0} ({1})'.format(os.path.split(filename)[1], encoding))
def openFileByExtension(self, filename): def openFileByExtension(self, filename):
self.clearArchiveFiles() self.clearArchiveFiles()
if tarfile.is_tarfile(filename): if tarfile.is_tarfile(filename):
# opening an empty tar file raises ReadError
with tarfile.open(filename, 'r:*') as tp: with tarfile.open(filename, 'r:*') as tp:
files = [f for f in tp.getnames() if tp.getmember(f).isfile()] files = [f for f in tp.getnames() if tp.getmember(f).isfile()]
names = [f.decode('utf-8') for f in files] names = [f.decode('utf-8') for f in files]
@ -360,8 +365,7 @@ class MainWindowReader(QtGui.QMainWindow):
content = fp.read() content = fp.read()
fp.close() fp.close()
else: else:
# Using index because of encoding difficulties index, ok = self.selectFileName(names)
(index, ok) = self.selectFileName(names)
if ok: if ok:
fp = tp.extractfile(files[index]) fp = tp.extractfile(files[index])
content = fp.read() content = fp.read()
@ -381,15 +385,17 @@ class MainWindowReader(QtGui.QMainWindow):
if self.state.archiveIndex is not None: if self.state.archiveIndex is not None:
return (self.state.archiveIndex, True) return (self.state.archiveIndex, True)
(item, ok) = QtGui.QInputDialog.getItem( item, ok = QtGui.QInputDialog.getItem(
self, self,
'Yomichan', 'Yomichan',
'Select file to open:', 'Select file to open:',
self.formatQStringList(names), self.formatQStringList(names),
current = 0, current = 0,
editable=False) editable=False
(index, success) = self.getItemIndex(item) )
return (index - 1, ok and success)
index, success = self.getItemIndex(item)
return index - 1, ok and success
def getItemIndex(self, item): def getItemIndex(self, item):
@ -433,11 +439,10 @@ class MainWindowReader(QtGui.QMainWindow):
def ankiAddFact(self, markup): def ankiAddFact(self, markup):
if not self.anki: if self.anki is None:
return False return False
fields = reader_util.replaceMarkupInFields(self.preferences.ankiFields, markup) fields = reader_util.replaceMarkupInFields(self.preferences.ankiFields, markup)
tagsSplit = reader_util.splitTags(unicode(self.comboTags.currentText())) tagsSplit = reader_util.splitTags(unicode(self.comboTags.currentText()))
tagsJoined = ' '.join(tagsSplit) tagsJoined = ' '.join(tagsSplit)
@ -449,7 +454,7 @@ class MainWindowReader(QtGui.QMainWindow):
self.preferences.updateFactTags(tagsJoined) self.preferences.updateFactTags(tagsJoined)
factId = self.anki.addNote(self.preferences.ankiDeck, self.preferences.ankiModel, fields, tagsSplit) factId = self.anki.addNote(self.preferences.ankiDeck, self.preferences.ankiModel, fields, tagsSplit)
if not factId: if factId is None:
return False return False
expression, reading = markup['%e'], markup['%r'] expression, reading = markup['%e'], markup['%r']
@ -467,7 +472,7 @@ class MainWindowReader(QtGui.QMainWindow):
def ankiIsFactValid(self, markup): def ankiIsFactValid(self, markup):
if not self.anki: if self.anki is None:
return False return False
fields = reader_util.replaceMarkupInFields(self.preferences.ankiFields, markup) fields = reader_util.replaceMarkupInFields(self.preferences.ankiFields, markup)
@ -528,12 +533,12 @@ class MainWindowReader(QtGui.QMainWindow):
def updateArchiveFiles(self, filename, names): def updateArchiveFiles(self, filename, names):
self.menuOpenArchive.setEnabled(True) self.menuOpenArchive.setEnabled(True)
for name in self.formatQStringList(names): for name in self.formatQStringList(names):
(index, ok) = self.getItemIndex(name) index, ok = self.getItemIndex(name)
if ok: if ok:
index = index - 1 index = index - 1
self.menuOpenArchive.addAction(name, (lambda fn=filename, idx=index: self.openFileInArchive(fn, idx))) self.menuOpenArchive.addAction(name, lambda fn=filename, idx=index: self.openFileInArchive(fn, idx))
else: else:
self.menuOpenArchive.addAction(name, (lambda fn=filename: self.openFile(fn))) self.menuOpenArchive.addAction(name, lambda fn=filename: self.openFile(fn))
def openFileInArchive(self, filename, index): def openFileInArchive(self, filename, index):