Skip to content
This repository has been archived by the owner on Mar 22, 2018. It is now read-only.

Commit

Permalink
pass comparison function (just added for #225) to sort the entries in…
Browse files Browse the repository at this point in the history
… the rank_combo. closes #232.
  • Loading branch information
mfrasca committed Dec 22, 2015
1 parent a713702 commit 5551216
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 5 deletions.
4 changes: 3 additions & 1 deletion bauble/editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -652,7 +652,6 @@ def attach_completion(self, entry,

return completion

# TODO: add the ability to pass a sort function
# TODO: add a default value to set in the combo
def init_translatable_combo(self, combo, translations, default=None,
cmp=None):
Expand All @@ -676,6 +675,9 @@ def init_translatable_combo(self, combo, translations, default=None,
model = gtk.ListStore(object, str)
if isinstance(translations, dict):
translations = sorted(translations.iteritems(), key=lambda x: x[1])
if cmp is not None:
translations = sorted(translations,
cmp=lambda a, b: cmp(a[0], b[0]))
for key, value in translations:
model.append([key, value])
combo.set_model(model)
Expand Down
6 changes: 3 additions & 3 deletions bauble/plugins/plants/species_editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
from bauble.plugins.plants.genus import Genus, GenusSynonym
from bauble.plugins.plants.species_model import (
Species, SpeciesDistribution, VernacularName, SpeciesSynonym, Habit,
infrasp_rank_values)
infrasp_rank_values, compare_rank)


class SpeciesEditorPresenter(editor.GenericEditorPresenter):
Expand Down Expand Up @@ -419,8 +419,8 @@ def __init__(self, presenter, level):

# rank combo
self.rank_combo = gtk.ComboBox()
self.presenter.view.init_translatable_combo(self.rank_combo,
infrasp_rank_values)
self.presenter.view.init_translatable_combo(
self.rank_combo, infrasp_rank_values, cmp=compare_rank)
utils.set_widget_value(self.rank_combo, rank)
presenter.view.connect(self.rank_combo,
'changed', self.on_rank_combo_changed)
Expand Down
2 changes: 1 addition & 1 deletion bauble/plugins/plants/species_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def compare_rank(rank1, rank2):

ordering = [u'familia', u'subfamilia', u'tribus', u'subtribus',
u'genus', u'subgenus', u'species', u'subsp.',
u'var.', u'subvar.', u'f.', u'subf.']
u'var.', u'subvar.', u'f.', u'subf.', u'cv.', None]
return ordering.index(rank1).__cmp__(ordering.index(rank2))


Expand Down

0 comments on commit 5551216

Please sign in to comment.