From ce4b15084862123a6877b9c5ae2d106150f58690 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Sun, 23 Dec 2012 16:02:00 -0800 Subject: [PATCH] updating preferences ui --- yomi_base/anki_host.py | 28 +++++++++++++++++++++------- yomi_base/preference_data.py | 5 ++++- yomi_base/preferences.py | 16 +++++++++++++++- yomi_base/ui/preferences.ui | 22 +++++++++++++++++++++- 4 files changed, 61 insertions(+), 10 deletions(-) diff --git a/yomi_base/anki_host.py b/yomi_base/anki_host.py index 3de445b..a19ef5b 100644 --- a/yomi_base/anki_host.py +++ b/yomi_base/anki_host.py @@ -17,12 +17,18 @@ import aqt -#from anki import hooks, lang -#from ankiqt import ui import re class Anki: + def __init__(self, modelName=None): + self.setModelName(modelName) + + + def setModelName(self, modelName): + self.modelName = modelName + + def addNote(self, fields, tags=unicode()): note = self.createNote(fields, tags) if not note: @@ -73,10 +79,6 @@ class Anki: return re.sub('[;,]', unicode(), tags).strip() - def fields(self): - return [field['name'] for field in self.currentModel()['flds']] - - def window(self): return aqt.mw @@ -97,5 +99,17 @@ class Anki: return self.collection().models + def modelNames(self): + return self.models().allNames() + + def currentModel(self): - return self.models().current() + for model in self.models().models.values(): + if model['name'] == self.modelName: + return model + + + def currentModelFieldNames(self): + model = self.currentModel() + if model is not None: + return [field['name'] for field in model['flds']] diff --git a/yomi_base/preference_data.py b/yomi_base/preference_data.py index 473b233..9af2380 100644 --- a/yomi_base/preference_data.py +++ b/yomi_base/preference_data.py @@ -49,7 +49,7 @@ class Preferences: self.ankiFields = dict() self.ankiTags = list() - self.ankiShowIcon = True + self.ankiModel = unicode() def load(self, filename=None): @@ -97,6 +97,8 @@ class Preferences: self.searchGroupByExp = self.readAttrBool(search, 'groupByExp', self.searchGroupByExp) for anki in root.getElementsByTagName('anki'): + self.ankiModel = self.readAttrStr(anki, 'model', unicode()) + for tag in anki.getElementsByTagName('tag'): value = self.readAttrStr(tag, 'value', unicode()) self.ankiTags.append(value) @@ -162,6 +164,7 @@ class Preferences: anki = doc.createElement('anki') root.appendChild(anki) + self.writeAttrStr(anki, 'model', self.ankiModel) for value in self.ankiTags: tag = doc.createElement('tag') diff --git a/yomi_base/preferences.py b/yomi_base/preferences.py index 87bbfc5..477b60d 100644 --- a/yomi_base/preferences.py +++ b/yomi_base/preferences.py @@ -30,6 +30,7 @@ class DialogPreferences(QtGui.QDialog): self.buttonContentColorBg.clicked.connect(self.onButtonColorBgClicked) self.comboContentFontFamily.currentFontChanged.connect(self.onFontFamilyChanged) self.spinContentFontSize.valueChanged.connect(self.onFontSizeChanged) + self.comboBoxAnkiModel.currentIndexChanged.connect(self.onAnkiModelChanged) self.preferences = preferences self.anki = anki @@ -53,7 +54,10 @@ class DialogPreferences(QtGui.QDialog): self.tabAnki.setEnabled(self.anki is not None) if self.anki: - self.setAnkiFields(self.anki.fields(), self.preferences.ankiFields) + self.comboBoxAnkiModel.blockSignals(True) + self.comboBoxAnkiModel.addItems(self.anki.modelNames()) + self.comboBoxAnkiModel.blockSignals(False) + self.comboBoxAnkiModel.setCurrentIndex(self.comboBoxAnkiModel.findText(self.preferences.ankiModel)) def dialogToData(self): @@ -66,6 +70,7 @@ class DialogPreferences(QtGui.QDialog): self.preferences.searchGroupByExp = self.checkSearchGroupByExp.isChecked() if self.anki: + self.preferences.ankiModel = unicode(self.comboBoxAnkiModel.currentText()) self.preferences.ankiFields = self.ankiFields() @@ -82,6 +87,9 @@ class DialogPreferences(QtGui.QDialog): def setAnkiFields(self, fieldsAnki, fieldsPrefs): + if fieldsAnki is None: + fieldsAnki = list() + self.tableAnkiFields.setRowCount(len(fieldsAnki)) for i, name in enumerate(fieldsAnki): @@ -135,3 +143,9 @@ class DialogPreferences(QtGui.QDialog): def onFontSizeChanged(self, size): self.preferences.uiContentFontSize = size self.updateSampleText() + + + def onAnkiModelChanged(self, index): + self.preferences.ankiModel = self.comboBoxAnkiModel.currentText() + self.anki.setModelName(self.preferences.ankiModel) + self.setAnkiFields(self.anki.currentModelFieldNames(), self.preferences.ankiFields) diff --git a/yomi_base/ui/preferences.ui b/yomi_base/ui/preferences.ui index acb30ff..2bd1838 100644 --- a/yomi_base/ui/preferences.ui +++ b/yomi_base/ui/preferences.ui @@ -172,10 +172,30 @@ Anki + + + + + + + 0 + 0 + + + + Model to use + + + + + + + + - Specify how your deck's model fields are populated when adding facts + Specify how your model fields are populated when adding facts