1

fixing up preferences, anki host module

This commit is contained in:
Alex Yatskov 2012-12-23 14:45:48 -08:00
parent c4c27334d5
commit 35019782a5
4 changed files with 37 additions and 71 deletions

View File

@ -23,50 +23,50 @@ import re
class Anki: class Anki:
def addFact(self, fields, tags=unicode()): def addNote(self, fields, tags=unicode()):
fact = self.createFact(fields, tags) note = self.createNote(fields, tags)
if not fact: if not note:
return None return None
action = lang._('Add') action = lang._('Add')
deck = self.deck() collection = self.collection()
deck.setUndoStart(action) collection.setUndoStart(action)
deck.addFact(fact, False) collection.addNote(note, False)
deck.setUndoEnd(action) collection.setUndoEnd(action)
deck.rebuildCounts() collection.rebuildCounts()
ankiqt.mw.updateTitleBar() ankiqt.mw.updateTitleBar()
ankiqt.mw.statusView.redraw() ankiqt.mw.statusView.redraw()
return fact.id return note.id
def canAddFact(self, fields): def canAddNote(self, fields):
return bool(self.createFact(fields)) return bool(self.createNote(fields))
def createFact(self, fields, tags=unicode()): def createNote(self, fields, tags=unicode()):
deck = self.deck() collection = self.collection()
fact = deck.newFact() note = collection.newnote()
fact.tags = self.cleanupTags(tags) note.tags = self.cleanupTags(tags)
try: try:
for field in fact.fields: for field in note.fields:
field.value = fields.get(field.getName()) or unicode() field.value = fields.get(field.getName()) or unicode()
if not fact.fieldValid(field) or not fact.fieldUnique(field, deck.s): if not note.fieldValid(field) or not note.fieldUnique(field, collection.s):
return None return None
except KeyError: except KeyError:
return None return None
return fact return note
def browseFact(self, factId): def browseNote(self, noteId):
browser = ui.dialogs.get('CardList', self.window()) browser = ui.dialogs.get('CardList', self.window())
browser.dialog.filterEdit.setText('fid:' + str(factId)) browser.dialog.filterEdit.setText('fid:' + str(noteId))
browser.updateSearch() browser.updateSearch()
browser.onFact() browser.onnote()
def cleanupTags(self, tags): def cleanupTags(self, tags):
@ -74,30 +74,28 @@ class Anki:
def fields(self): def fields(self):
return [ return [field['name'] for field in self.currentModel()['flds']]
(field.name, field.required, field.unique) for field in self.model().fieldModels
]
def deck(self):
return self.window().deck
def model(self):
return self.deck().currentModel
def window(self): def window(self):
return aqt.mw return aqt.mw
def form(self):
return self.window().form
def toolsMenu(self): def toolsMenu(self):
return self.window().form.menuTools return self.form().menuTools
def addHook(self, name, callback): def collection(self):
hooks.addHook(name, callback) return self.window().col
def removeHook(self, name, callback): def models(self):
hooks.removeHook(name, callback) return self.collection().models
def currentModel(self):
return self.models().current()

View File

@ -97,8 +97,6 @@ class Preferences:
self.searchGroupByExp = self.readAttrBool(search, 'groupByExp', self.searchGroupByExp) self.searchGroupByExp = self.readAttrBool(search, 'groupByExp', self.searchGroupByExp)
for anki in root.getElementsByTagName('anki'): for anki in root.getElementsByTagName('anki'):
self.ankiShowIcon = self.readAttrBool(anki, 'showIcon', self.ankiShowIcon)
for tag in anki.getElementsByTagName('tag'): for tag in anki.getElementsByTagName('tag'):
value = self.readAttrStr(tag, 'value', unicode()) value = self.readAttrStr(tag, 'value', unicode())
self.ankiTags.append(value) self.ankiTags.append(value)
@ -164,7 +162,6 @@ class Preferences:
anki = doc.createElement('anki') anki = doc.createElement('anki')
root.appendChild(anki) root.appendChild(anki)
self.writeAttrBool(anki, 'showIcon', self.ankiShowIcon)
for value in self.ankiTags: for value in self.ankiTags:
tag = doc.createElement('tag') tag = doc.createElement('tag')

View File

@ -51,8 +51,7 @@ class DialogPreferences(QtGui.QDialog):
self.spinSearchResultMax.setValue(self.preferences.searchResultMax) self.spinSearchResultMax.setValue(self.preferences.searchResultMax)
self.checkSearchGroupByExp.setChecked(self.preferences.searchGroupByExp) self.checkSearchGroupByExp.setChecked(self.preferences.searchGroupByExp)
self.checkAnkiShowIcon.setChecked(self.preferences.ankiShowIcon) self.tabAnki.setEnabled(self.anki is not None)
self.tabAnki.setEnabled(bool(self.anki))
if self.anki: if self.anki:
self.setAnkiFields(self.anki.fields(), self.preferences.ankiFields) self.setAnkiFields(self.anki.fields(), self.preferences.ankiFields)
@ -66,7 +65,6 @@ class DialogPreferences(QtGui.QDialog):
self.preferences.searchResultMax = self.spinSearchResultMax.value() self.preferences.searchResultMax = self.spinSearchResultMax.value()
self.preferences.searchGroupByExp = self.checkSearchGroupByExp.isChecked() self.preferences.searchGroupByExp = self.checkSearchGroupByExp.isChecked()
self.preferences.ankiShowIcon = self.checkAnkiShowIcon.isChecked()
if self.anki: if self.anki:
self.preferences.ankiFields = self.ankiFields() self.preferences.ankiFields = self.ankiFields()
@ -86,7 +84,7 @@ class DialogPreferences(QtGui.QDialog):
def setAnkiFields(self, fieldsAnki, fieldsPrefs): def setAnkiFields(self, fieldsAnki, fieldsPrefs):
self.tableAnkiFields.setRowCount(len(fieldsAnki)) self.tableAnkiFields.setRowCount(len(fieldsAnki))
for i, (name, required, unique) in enumerate(fieldsAnki): for i, name in enumerate(fieldsAnki):
columns = list() columns = list()
itemName = QtGui.QTableWidgetItem(name) itemName = QtGui.QTableWidgetItem(name)
@ -96,16 +94,6 @@ class DialogPreferences(QtGui.QDialog):
itemValue = QtGui.QTableWidgetItem(fieldsPrefs.get(name, unicode())) itemValue = QtGui.QTableWidgetItem(fieldsPrefs.get(name, unicode()))
columns.append(itemValue) columns.append(itemValue)
itemRequired = QtGui.QTableWidgetItem()
itemRequired.setFlags(QtCore.Qt.ItemIsUserCheckable)
itemRequired.setCheckState(QtCore.Qt.Checked if required else QtCore.Qt.Unchecked)
columns.append(itemRequired)
itemUnique = QtGui.QTableWidgetItem()
itemUnique.setFlags(QtCore.Qt.ItemIsUserCheckable)
itemUnique.setCheckState(QtCore.Qt.Checked if unique else QtCore.Qt.Unchecked)
columns.append(itemUnique)
for j, column in enumerate(columns): for j, column in enumerate(columns):
self.tableAnkiFields.setItem(i, j, column) self.tableAnkiFields.setItem(i, j, column)

View File

@ -193,16 +193,6 @@
<string>Value</string> <string>Value</string>
</property> </property>
</column> </column>
<column>
<property name="text">
<string>Required</string>
</property>
</column>
<column>
<property name="text">
<string>Unique</string>
</property>
</column>
</widget> </widget>
</item> </item>
<item> <item>
@ -320,13 +310,6 @@
</item> </item>
</layout> </layout>
</item> </item>
<item>
<widget class="QCheckBox" name="checkAnkiShowIcon">
<property name="text">
<string>Show icon in toolbar</string>
</property>
</widget>
</item>
</layout> </layout>
</widget> </widget>
</widget> </widget>