Skip to content

Commit

Permalink
Use sub-menus for fields, grouped by note type
Browse files Browse the repository at this point in the history
  • Loading branch information
hssm committed Feb 9, 2016
1 parent 122b8d9 commit 96200b3
Showing 1 changed file with 22 additions and 16 deletions.
38 changes: 22 additions & 16 deletions advancedbrowser/note_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@ def onAdvBrowserLoad(self, advBrowser):
# We build this dictionary once to avoid needlessly finding the
# field order for every single row when sorting. It's
# significantly faster that way.
# { mid -> {fldName -> pos}}
# {mid -> {fldName -> pos}}
self.modelFieldPos = {}

# List of our columns
self.customColumns = []
# Dictionary of columns. A column can exist in multiple places, because
# different note types may have the same field name.
# {fld['name'] -> CustomColumn}}
self.customColumns = {}

self.buildMappings()

Expand All @@ -33,18 +35,18 @@ def fldOnData(c, n, t):
field = self.fieldTypes[t]
if field in c.note().keys():
return anki.utils.stripHTML(c.note()[field])

for type, name in self.fieldTypes.iteritems():
srt = ("(select valueForField(mid, flds, '%s') "
"from notes where id = c.nid)" % name)

cc = advBrowser.newCustomColumn(
type = type,
name = name,
onData = fldOnData,
onSort = getOnSort(srt)
type=type,
name=name,
onData=fldOnData,
onSort=getOnSort(srt)
)
self.customColumns.append(cc)
self.customColumns[name] = cc

def onBuildContextMenu(self, contextMenu):
# Models might have changed so rebuild our mappings.
Expand All @@ -53,10 +55,14 @@ def onBuildContextMenu(self, contextMenu):

# Create a new sub-menu for our columns
fldGroup = contextMenu.newSubMenu("Fields")
for column in self.customColumns:
fldGroup.addItem(column)
# And a sub-menu for each note type
for model in mw.col.models.models.itervalues():
modelGroup = fldGroup.newSubMenu(model['name'])
for fld in model['flds']:
modelGroup.addItem(self.customColumns[fld['name']])


def buildMappings(self):
def buildMappings(self):
for model in mw.col.models.all():
# For some reason, some mids return as unicode, so convert to int
mid = int(model['id'])
Expand All @@ -70,10 +76,10 @@ def buildMappings(self):
for field in model['flds']:
name = field['name']
ord = field['ord']
type = "_field_"+name #prefix to avoid potential clashes
type = "_field_"+name # prefix to avoid potential clashes
self.modelFieldPos[mid][name] = ord
self.modelFieldPos[mid32][name] = ord
if type not in self.fieldTypes: #avoid dupes
if type not in self.fieldTypes: # avoid dupes
self.fieldTypes[type] = name

def valueForField(self, mid, flds, fldName):
Expand Down Expand Up @@ -102,7 +108,7 @@ def valueForField(self, mid, flds, fldName):
print "_modelFieldPos" + self.modelFieldPos
print "Error was: " + e.message

def myLoadCollection(self, _self):
def myLoadCollection(self, _self):
# Create a new SQL function that we can use in our queries.
mw.col.db._db.create_function("valueForField", 3, self.valueForField)

Expand Down

0 comments on commit 96200b3

Please # to comment.