1

updating preferences ui

This commit is contained in:
Alex Yatskov 2012-12-23 16:02:00 -08:00
parent 35019782a5
commit ce4b150848
4 changed files with 61 additions and 10 deletions

View File

@ -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']]

View File

@ -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')

View File

@ -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)

View File

@ -172,10 +172,30 @@
<string>Anki</string>
</attribute>
<layout class="QVBoxLayout" name="verticalLayout_5">
<item>
<layout class="QHBoxLayout" name="horizontalLayout_4">
<item>
<widget class="QLabel" name="label_12">
<property name="sizePolicy">
<sizepolicy hsizetype="Maximum" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Model to use</string>
</property>
</widget>
</item>
<item>
<widget class="QComboBox" name="comboBoxAnkiModel"/>
</item>
</layout>
</item>
<item>
<widget class="QLabel" name="label_11">
<property name="text">
<string>Specify how your deck's model fields are populated when adding facts</string>
<string>Specify how your model fields are populated when adding facts</string>
</property>
</widget>
</item>