1
This commit is contained in:
Alex Yatskov 2012-12-24 09:24:02 -08:00
parent 96ebd208fd
commit 7fa6904812
3 changed files with 42 additions and 38 deletions

View File

@ -18,43 +18,44 @@
import aqt import aqt
import anki.hooks import anki.hooks
import re
class Anki: class Anki:
def addNote(self, deckName, modelName, fields, tags=unicode()): def addNote(self, deckName, modelName, fields, tags=list()):
note = self.createNote(fields, deckName, modelName, tags) note = self.createNote(fields, deckName, modelName, tags)
if not note: if note is not None:
return None
note.model()['did'] = self.currentDeck()['id']
self.collection().addNote(note) self.collection().addNote(note)
self.decks().save(self.currentDeck())
self.window().requireReset() self.window().requireReset()
return note.id
def canAddNote(self, deckName, modelName, fields):
return bool(self.createNote(deckName, modelName, fields))
def canAddNote(self, modelName, fields): def createNote(self, deckName, modelName, fields, tags=list()):
return bool(self.createNote(modelName, fields))
def createNote(self, modelName, fields, tags=unicode()):
model = self.findModel(modelName) model = self.findModel(modelName)
if model is None: if model is None:
return None return None
conf = self.collection().conf deck = self.findDeck(deckName)
deck = self.currentDeck() if deck is None:
return None
if conf['curModel'] != model['id'] or deck['mid'] != model['id']: #~ conf = self.collection().conf
conf['curModel'] = deck['mid'] = model['id'] #~ deck = self.currentDeck()
self.collection().decks.save(deck) #~ if conf['curModel'] != model['id'] or deck['mid'] != model['id']:
anki.hooks.runHook('currentModelChanged') #~ conf['curModel'] = deck['mid'] = model['id']
self.window().reset() #~ self.collection().decks.save(deck)
#~ anki.hooks.runHook('currentModelChanged')
#~ self.window().reset()
note = self.collection().newNote() #~ self.collection().conf['curModel'] = model['id']
note.tags = re.split('[;,\s]', tags) #~ deck['mid'] = model['id']
note = anki.notes.Note(self.collection(), model)
note.model()['did'] = deck['id']
note.tags = tags
for name, value in fields.items(): for name, value in fields.items():
note[name] = value note[name] = value
@ -62,12 +63,11 @@ class Anki:
return None if note.dupeOrEmpty() else note return None if note.dupeOrEmpty() else note
def browseNote(self, noteId): #~ def browseNote(self, noteId):
pass #~ browser = ui.dialogs.get('CardList', self.window())
#browser = ui.dialogs.get('CardList', self.window()) #~ browser.dialog.filterEdit.setText('fid:' + str(noteId))
#browser.dialog.filterEdit.setText('fid:' + str(noteId)) #~ browser.updateSearch()
#browser.updateSearch() #~ browser.onnote()
#browser.onnote()
def window(self): def window(self):

View File

@ -100,9 +100,7 @@ class MainWindowReader(QtGui.QMainWindow):
if self.preferences.uiReaderSize != None: if self.preferences.uiReaderSize != None:
self.resize(QtCore.QSize(*self.preferences.uiReaderSize)) self.resize(QtCore.QSize(*self.preferences.uiReaderSize))
for tags in self.preferences.ankiTags: self.comboTags.addItems(self.preferences.ankiTags)
self.comboTags.addItem(tags)
self.applyPreferencesContent() self.applyPreferencesContent()
@ -440,15 +438,17 @@ class MainWindowReader(QtGui.QMainWindow):
fields = reader_util.replaceMarkupInFields(self.preferences.ankiFields, markup) fields = reader_util.replaceMarkupInFields(self.preferences.ankiFields, markup)
tags = self.anki.cleanupTags(unicode(self.comboTags.currentText())) tagsSplit = reader_util.splitTags(unicode(self.comboTags.currentText()))
tagIndex = self.comboTags.findText(tags) tagsJoined = ' '.join(tagsSplit)
tagIndex = self.comboTags.findText(tagsJoined)
if tagIndex > 0: if tagIndex > 0:
self.comboTags.removeItem(tagIndex) self.comboTags.removeItem(tagIndex)
if tagIndex != 0: if tagIndex != 0:
self.comboTags.insertItem(0, tags) self.comboTags.insertItem(0, tagsJoined)
self.preferences.updateFactTags(tags) self.preferences.updateFactTags(tagsJoined)
factId = self.anki.addNote(self.preferences.ankiDeck, self.preferences.ankiModel, fields, tags) factId = self.anki.addNote(self.preferences.ankiDeck, self.preferences.ankiModel, fields, tagsSplit)
if not factId: if not factId:
return False return False
@ -471,7 +471,7 @@ class MainWindowReader(QtGui.QMainWindow):
return False return False
fields = reader_util.replaceMarkupInFields(self.preferences.ankiFields, markup) fields = reader_util.replaceMarkupInFields(self.preferences.ankiFields, markup)
return self.anki.canAddNote(self.preferences.ankiModel, fields) return self.anki.canAddNote(self.preferences.ankiDeck, self.preferences.ankiModel, fields)
def updateSampleMouseEvent(self, event): def updateSampleMouseEvent(self, event):

View File

@ -118,6 +118,10 @@ def buildFactMarkupReading(reading, glossary, sentence=None):
} }
def splitTags(tags):
return re.split('[;,\s]', tags)
def convertDefinitions(definitions, sentence=None): def convertDefinitions(definitions, sentence=None):
return [ return [
Definition(*(definition + (sentence,))) for definition in definitions Definition(*(definition + (sentence,))) for definition in definitions