* src/AutoComp.py (StandardCustomSelector): Add class to handle

(int,str) selections including custom strings.


svn: r4741
This commit is contained in:
Alex Roitman 2005-05-31 18:17:38 +00:00
parent 5652f21069
commit 2c6f640ac5
2 changed files with 111 additions and 1 deletions

View File

@ -1,3 +1,7 @@
2005-05-31 Alex Roitman <shura@gramps-project.org>
* src/AutoComp.py (StandardCustomSelector): Add class to handle
(int,str) selections including custom strings.
2005-05-30 Alex Roitman <shura@gramps-project.org>
* src/RelLib.py: Move constants back to RelLib, as class attributes.
* src/Utils.py: Move constant mappings from const.py.in.

View File

@ -1,7 +1,7 @@
#
# Gramps - a GTK+/GNOME based genealogy program
#
# Copyright (C) 2002-2003 Donald N. Allingham
# Copyright (C) 2002-2005 Donald N. Allingham
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
@ -63,3 +63,109 @@ def fill_option_text(combobox,data):
def get_option(combobox):
store = combobox.get_model()
return store.get_value(combobox.get_active_iter(),0)
#-------------------------------------------------------------------------
#
# StandardCustomSelector class
#
#-------------------------------------------------------------------------
class StandardCustomSelector:
"""
This class provides an interface to selecting from the predefined
options or entering custom string.
The typical usage should be:
sce = StandardCustomSelector(mapping,custom_key,active_key)
whatever_table.attach(sce,...)
and later, when or before the dialog is closed, do:
(i,s) = sce.get_values()
to obtain the tuple of (int,str) corresponding to the user selection.
No selection will return (custom_key,'') if the custom key is given,
or (None,'') if it is not given.
The active_key determines the default selection that will be displayed
upon widget creation. If omitted, the entry will be empty. If present,
then no selection on the user's part will return the
(active_key,mapping[active_key]) tuple.
"""
def __init__(self,mapping,custom_key=None,active_key=None):
"""
Constructor for the StandardCustomSelector class.
@param mapping: The mapping between integer and string constants.
@type mapping: dict
@param custom_key: The key corresponding to the custom string entry
@type custom_key: int
@param active_key: The key for the entry to make active upon creation
@type active_key: int
"""
self.mapping = mapping
self.custom_key = custom_key
self.active_key = active_key
self.active_index = 0
# make model
self.store = gtk.ListStore(gobject.TYPE_INT,gobject.TYPE_STRING)
# fill it up using mapping
self.fill()
# create combo box entry
self.selector = gtk.ComboBoxEntry(self.store,1)
if self.active_key:
self.selector.set_active(self.active_index)
def fill(self):
keys = self.mapping.keys()
keys.sort(self.by_value)
index = 0
for key in keys:
if key != self.custom_key:
self.store.append(row=[key,self.mapping[key]])
if key == self.active_key:
self.active_index = index
index = index + 1
def by_value(self,f,s):
"""
Method for sorting keys based on the values.
"""
fv = self.mapping[f]
sv = self.mapping[s]
return cmp(fv,sv)
def get_values(self):
"""
Get selected values.
@return: Returns a tuple of (int,str) corresponding to the selected or entered text.
@rtype: tuple
"""
ai = self.selector.get_active_iter()
if ai:
i = self.store.get_value(ai,0)
s = self.store.get_value(ai,1)
return (i,s)
entry = self.selector.child
return (self.custom_key,entry.get_text())
#-------------------------------------------------------------------------
#
# Testing code below this point
#
#-------------------------------------------------------------------------
if __name__ == "__main__":
def here(obj,event):
print s.get_values()
gtk.main_quit()
s = StandardCustomSelector({0:'a',1:'b',2:'c'},0)
w = gtk.Dialog()
w.child.add(s.selector)
w.connect('delete-event',here)
w.show_all()
gtk.main()