Enhance event type selector to use sub-menus

This commit is contained in:
Nick Hall 2014-01-06 21:23:59 +00:00
parent 6b98ad9319
commit 6618cdcfb2
5 changed files with 96 additions and 27 deletions

View File

@ -30,6 +30,7 @@ Provide autocompletion functionality.
#
#-------------------------------------------------------------------------
import locale
from gen.ggettext import sgettext as _
#-------------------------------------------------------------------------
#
@ -110,7 +111,7 @@ class StandardCustomSelector(object):
"""
def __init__(self, mapping, cbe=None, custom_key=None, active_key=None,
additional=None):
additional=None, menu=None):
"""
Constructor for the StandardCustomSelector class.
@ -130,57 +131,85 @@ class StandardCustomSelector(object):
self.active_key = active_key
self.active_index = 0
self.additional = additional
# make model
self.store = gtk.ListStore(gobject.TYPE_INT, gobject.TYPE_STRING)
# fill it up using mapping
self.fill()
self.menu = menu
# create combo box entry
if cbe:
self.selector = cbe
self.selector.set_model(self.store)
self.selector.set_text_column(1)
else:
self.selector = gtk.ComboBoxEntry(self.store, 1)
if self.active_key is not None:
self.selector.set_active(self.active_index)
self.selector = gtk.ComboBoxEntry()
# create models
if menu:
self.store = self.create_menu()
completion_store = self.create_list()
else:
self.store = self.create_list()
completion_store = self.store
self.selector.set_model(self.store)
self.selector.set_text_column(1)
if menu:
for cell in self.selector.get_cells():
self.selector.add_attribute(cell, 'sensitive', 2)
#if self.active_key is not None:
#self.selector.set_active(self.active_index)
# make autocompletion work
completion = gtk.EntryCompletion()
completion.set_model(self.store)
completion.set_model(completion_store)
completion.set_minimum_key_length(1)
completion.set_text_column(1)
self.selector.child.set_completion(completion)
def fill(self):
def create_menu(self):
"""
Fill with data
Create a model and fill it with a two-level tree corresponding to the
menu.
"""
store = gtk.TreeStore(int, str, bool)
for heading, items in self.menu:
if self.active_key in items:
parent = None
else:
parent = store.append(None, row=[-999, heading, False])
for item in items:
store.append(parent, row=[item, self.mapping[item], True])
if self.additional:
parent = store.append(None, row=[-999, _('Custom'), False])
for event_type in self.additional:
key, value = self.get_key_and_value(event_type)
store.append(parent, row=[key, value, True])
return store
def create_list(self):
"""
Create a model and fill it with a sorted flat list.
"""
store = gtk.ListStore(int, str)
keys = sorted(self.mapping, self.by_value)
index = 0
for key in keys:
if key != self.custom_key:
self.store.append(row=[key, self.mapping[key]])
store.append(row=[key, self.mapping[key]])
if key == self.active_key:
self.active_index = index
index += 1
if self.additional:
for event_type in self.additional:
if isinstance(event_type, basestring):
if event_type:
self.store.append(row=[self.custom_key, event_type])
elif isinstance(event_type, tuple):
if event_type[1]:
self.store.append(row=[event_type[0], event_type[1]])
else:
self.store.append(row=[int(event_type), str(event_type)])
key, value = self.get_key_and_value(event_type)
store.append(row=[key, value])
if key == self.active_key:
self.active_index = index
index += 1
return store
def by_value(self, first, second):
"""
Method for sorting keys based on the values.
@ -235,4 +264,17 @@ class StandardCustomSelector(object):
return True
return False
def get_key_and_value(self, event_type):
"""
Return the key and value for the given event type. The event type may be
a string representing a custom type, an (int, str) tuple or an EventType
instance.
"""
if isinstance(event_type, basestring):
if event_type:
return (self.custom_key, event_type)
elif isinstance(event_type, tuple):
if event_type[1]:
return (event_type[0], event_type[1])
else:
return(int(event_type), str(event_type))

View File

@ -136,6 +136,27 @@ class EventType(GrampsType):
RETIREMENT = 43
WILL = 44
_MENU = [[_('Life Events'),
[BIRTH, BAPTISM, DEATH, BURIAL, CREMATION, ADOPT]],
[_('Family'),
[ENGAGEMENT, MARRIAGE, DIVORCE, ANNULMENT, MARR_SETTL, MARR_LIC,
MARR_CONTR, MARR_BANNS, DIV_FILING, MARR_ALT]],
[_('Religious'),
[CHRISTEN, ADULT_CHRISTEN, CONFIRMATION, FIRST_COMMUN, BLESS,
BAR_MITZVAH, BAS_MITZVAH, RELIGION]],
[_('Vocational'),
[OCCUPATION, RETIREMENT, ELECTED, MILITARY_SERV, ORDINATION]],
[_('Academic'),
[EDUCATION, DEGREE, GRADUATION]],
[_('Travel'),
[EMIGRATION, IMMIGRATION, NATURALIZATION]],
[_('Legal'),
[PROBATE, WILL]],
[_('Residence'),
[RESIDENCE, CENSUS, PROPERTY]],
[_('Other'),
[CAUSE_DEATH, MED_INFO, NOB_TITLE, NUM_MARRIAGES]]]
_CUSTOM = CUSTOM
_DEFAULT = BIRTH

View File

@ -100,6 +100,7 @@ class GrampsType(object):
_DATAMAP = []
_BLACKLIST = None
_MENU = None
__metaclass__ = GrampsTypeMeta
__slots__ = ('__value', '__string')
@ -236,6 +237,9 @@ class GrampsType(object):
def get_custom(self):
return self._CUSTOM
def get_menu(self):
return self._MENU
def __cmp__(self, value):
if isinstance(value, int):
return cmp(self.__value, value)

View File

@ -401,7 +401,8 @@ class MySelect(gtk.ComboBoxEntry):
self.sel = AutoComp.StandardCustomSelector(type_class._I2SMAP, self,
type_class._CUSTOM,
type_class._DEFAULT,
additional)
additional,
type_class._MENU)
self.show()
def get_text(self):

View File

@ -456,7 +456,8 @@ class MonitoredDataType(object):
obj,
get_val().get_custom(),
default,
additional=custom_values)
additional=custom_values,
menu=get_val().get_menu())
self.sel.set_values((int(get_val()), str(get_val())))
self.obj.set_sensitive(not readonly)