1

Reader now works with multiple profiles

Former-commit-id: 8b63fc882c208986956eaab5e046c7e2174137b1
This commit is contained in:
Alex Yatskov 2013-11-10 13:59:37 -08:00
parent d61442ada5
commit b82a42f875
2 changed files with 25 additions and 17 deletions

View File

@ -258,14 +258,14 @@ class MainWindowReader(QtGui.QMainWindow, reader_ui.Ui_MainWindowReader):
definition['glossary'], definition['glossary'],
definition['sentence'] definition['sentence']
) )
self.ankiAddFact(markup) self.ankiAddFact('vocab', markup)
if command == 'addReading': if command == 'addReading':
markup = reader_util.buildFactMarkupReading( markup = reader_util.buildFactMarkupReading(
definition['reading'], definition['reading'],
definition['glossary'], definition['glossary'],
definition['sentence'] definition['sentence']
) )
self.ankiAddFact(markup) self.ankiAddFact('vocab', markup)
elif command == 'copyDefinition': elif command == 'copyDefinition':
reader_util.copyDefinitions([definition]) reader_util.copyDefinitions([definition])
@ -429,11 +429,15 @@ class MainWindowReader(QtGui.QMainWindow, reader_ui.Ui_MainWindowReader):
self.state.searchText = text self.state.searchText = text
def ankiAddFact(self, markup): def ankiAddFact(self, profile, markup):
if self.anki is None: if self.anki is None:
return False return False
fields = reader_util.replaceMarkupInFields(self.preferences['tags'], markup) profile = self.preferences["profiles"].get(profile)
if profile is None:
return False
fields = reader_util.replaceMarkupInFields(profile['fields'], markup)
tagsSplit = reader_util.splitTags(unicode(self.comboTags.currentText())) tagsSplit = reader_util.splitTags(unicode(self.comboTags.currentText()))
tagsJoined = ' '.join(tagsSplit) tagsJoined = ' '.join(tagsSplit)
@ -444,8 +448,7 @@ class MainWindowReader(QtGui.QMainWindow, reader_ui.Ui_MainWindowReader):
self.comboTags.insertItem(0, tagsJoined) self.comboTags.insertItem(0, tagsJoined)
self.preferences.updateFactTags(tagsJoined) self.preferences.updateFactTags(tagsJoined)
#FIXME factId = self.anki.addNote(profile['deck'], profile['model'], fields, tagsSplit)
factId = self.anki.addNote(self.preferences.ankiDeck, self.preferences.ankiModel, fields, tagsSplit)
if factId is None: if factId is None:
return False return False
@ -463,11 +466,16 @@ class MainWindowReader(QtGui.QMainWindow, reader_ui.Ui_MainWindowReader):
return True return True
def ankiIsFactValid(self, markup): def ankiIsFactValid(self, profile, markup):
#FIXME if self.anki is None:
if self.anki is not None: return False
fields = reader_util.replaceMarkupInFields(self.preferences.ankiFields, markup)
return self.anki.canAddNote(self.preferences.ankiDeck, self.preferences.ankiModel, fields) profile = self.preferences["profiles"].get(profile)
if profile is None:
return False
fields = reader_util.replaceMarkupInFields(profile['fields'], markup)
return self.anki.canAddNote(profile['deck'], profile['model'], fields)
def updateSampleMouseEvent(self, event): def updateSampleMouseEvent(self, event):
@ -563,7 +571,7 @@ class MainWindowReader(QtGui.QMainWindow, reader_ui.Ui_MainWindowReader):
def updateDefinitions(self): def updateDefinitions(self):
html = reader_util.buildDefinitionsHtml(self.state.definitions, self.ankiIsFactValid) html = reader_util.buildDefinitionsHtml(self.state.definitions, self.ankiIsFactValid, 'vocab')
self.textDefinitions.setHtml(html) self.textDefinitions.setHtml(html)

View File

@ -122,7 +122,7 @@ def copyDefinitions(definitions):
QtGui.QApplication.clipboard().setText(text) QtGui.QApplication.clipboard().setText(text)
def buildDefinitionHtml(definition, factIndex, factQuery): def buildDefinitionHtml(definition, factIndex, factQuery, profile):
reading = unicode() reading = unicode()
if definition['reading']: if definition['reading']:
reading = u'[{0}]'.format(definition['reading']) reading = u'[{0}]'.format(definition['reading'])
@ -134,9 +134,9 @@ def buildDefinitionHtml(definition, factIndex, factQuery):
links = '<a href = "copyDefinition:{0}"><img src = "://img/img/icon_copy_definition.png" align = "right"/></a>'.format(factIndex) links = '<a href = "copyDefinition:{0}"><img src = "://img/img/icon_copy_definition.png" align = "right"/></a>'.format(factIndex)
if factQuery: if factQuery:
if factQuery(buildFactMarkupExpression(definition['expression'], definition['reading'], definition['glossary'])): if factQuery(profile, buildFactMarkupExpression(definition['expression'], definition['reading'], definition['glossary'])):
links += '<a href = "addExpression:{0}"><img src = "://img/img/icon_add_expression.png" align = "right"/></a>'.format(factIndex) links += '<a href = "addExpression:{0}"><img src = "://img/img/icon_add_expression.png" align = "right"/></a>'.format(factIndex)
if factQuery(buildFactMarkupReading(definition['reading'], definition['glossary'])): if factQuery(profile, buildFactMarkupReading(definition['reading'], definition['glossary'])):
links += '<a href = "addReading:{0}"><img src = "://img/img/icon_add_reading.png" align = "right"/></a>'.format(factIndex) links += '<a href = "addReading:{0}"><img src = "://img/img/icon_add_reading.png" align = "right"/></a>'.format(factIndex)
html = u""" html = u"""
@ -149,7 +149,7 @@ def buildDefinitionHtml(definition, factIndex, factQuery):
return html return html
def buildDefinitionsHtml(definitions, factQuery): def buildDefinitionsHtml(definitions, factQuery, profile):
palette = QtGui.QApplication.palette() palette = QtGui.QApplication.palette()
toolTipBg = palette.color(QtGui.QPalette.Window).name() toolTipBg = palette.color(QtGui.QPalette.Window).name()
toolTipFg = palette.color(QtGui.QPalette.WindowText).name() toolTipFg = palette.color(QtGui.QPalette.WindowText).name()
@ -162,7 +162,7 @@ def buildDefinitionsHtml(definitions, factQuery):
if len(definitions) > 0: if len(definitions) > 0:
for i, definition in enumerate(definitions): for i, definition in enumerate(definitions):
html += buildDefinitionHtml(definition, i, factQuery) html += buildDefinitionHtml(definition, i, factQuery, profile)
else: else:
html += """ html += """
<p>No definitions to display.</p> <p>No definitions to display.</p>