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:
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:
self.collection().addNote(note)
self.decks().save(self.currentDeck())
self.window().requireReset()
collection = self.collection()
collection.addNote(note)
collection.autosave()
self.startEditing()
def canAddNote(self, deckName, modelName, fields):
@ -34,25 +35,14 @@ class Anki:
def createNote(self, deckName, modelName, fields, tags=list()):
model = self.findModel(modelName)
model = self.models().byName(modelName)
if model is None:
return None
deck = self.findDeck(deckName)
deck = self.decks().byName(deckName)
if deck is 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.model()['did'] = deck['id']
note.tags = tags
@ -70,16 +60,20 @@ class Anki:
#~ browser.onnote()
def startEditing(self):
self.window().requireReset()
def stopEditing(self):
self.window().maybeReset()
def window(self):
return aqt.mw
def form(self):
return self.window().form
def toolsMenu(self):
return self.form().menuTools
return self.window().form.menuTools
def collection(self):
@ -94,16 +88,9 @@ class Anki:
return self.models().allNames()
def modelFieldNames(self, model):
return [field['name'] for field in model['flds']]
def findModel(self, name):
return self.models().byName(name)
def currentModel(self):
return self.models().current()
def modelFieldNames(self, modelName):
model = self.models().byName(modelName)
return None if model is None else [field['name'] for field in model['flds']]
def decks(self):
@ -112,12 +99,3 @@ class Anki:
def deckNames(self):
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):
model = self.anki.findModel(self.comboBoxAnkiModel.currentText())
fieldNames = list() if model is None else self.anki.modelFieldNames(model)
modelName = self.comboBoxAnkiModel.currentText()
fieldNames = self.anki.modelFieldNames(modelName) or list()
self.setAnkiFields(fieldNames, self.preferences.ankiFields)

View File

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