2002-10-20 19:55:16 +05:30
|
|
|
#
|
|
|
|
# Gramps - a GTK+/GNOME based genealogy program
|
|
|
|
#
|
2006-05-02 23:02:54 +05:30
|
|
|
# Copyright (C) 2002-2006 Donald N. Allingham
|
2002-10-20 19:55:16 +05:30
|
|
|
#
|
|
|
|
# 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
|
|
|
|
# the Free Software Foundation; either version 2 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program; if not, write to the Free Software
|
|
|
|
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
#
|
|
|
|
|
2011-10-23 08:43:50 +05:30
|
|
|
# $Id$
|
2003-12-17 21:36:36 +05:30
|
|
|
|
2007-05-20 09:13:38 +05:30
|
|
|
"""
|
2008-02-24 19:25:55 +05:30
|
|
|
Provide autocompletion functionality.
|
2007-05-20 09:13:38 +05:30
|
|
|
"""
|
|
|
|
|
2005-06-07 05:20:33 +05:30
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# Standard python modules
|
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
import locale
|
|
|
|
|
2002-10-20 19:55:16 +05:30
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# GNOME modules
|
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
import gtk
|
2008-01-22 03:33:43 +05:30
|
|
|
import gobject
|
2002-10-20 19:55:16 +05:30
|
|
|
|
2007-05-15 09:47:12 +05:30
|
|
|
def fill_combo(combo, data_list):
|
2007-05-20 09:13:38 +05:30
|
|
|
"""
|
|
|
|
Fill a combo box with completion data
|
|
|
|
"""
|
2008-01-22 03:33:43 +05:30
|
|
|
store = gtk.ListStore(gobject.TYPE_STRING)
|
2004-08-04 10:23:29 +05:30
|
|
|
|
2009-09-03 02:24:22 +05:30
|
|
|
for data in data_list:
|
|
|
|
if data:
|
|
|
|
store.append(row=[data])
|
2008-01-06 01:40:26 +05:30
|
|
|
|
2004-10-29 06:19:40 +05:30
|
|
|
combo.set_model(store)
|
|
|
|
combo.set_text_column(0)
|
|
|
|
completion = gtk.EntryCompletion()
|
|
|
|
completion.set_model(store)
|
|
|
|
completion.set_minimum_key_length(1)
|
|
|
|
completion.set_text_column(0)
|
|
|
|
combo.child.set_completion(completion)
|
2004-08-04 10:23:29 +05:30
|
|
|
|
2007-05-15 09:47:12 +05:30
|
|
|
def fill_entry(entry, data_list):
|
2007-05-20 09:13:38 +05:30
|
|
|
"""
|
|
|
|
Fill a entry with completion data
|
|
|
|
"""
|
2009-09-03 02:24:22 +05:30
|
|
|
|
2008-01-22 03:33:43 +05:30
|
|
|
store = gtk.ListStore(gobject.TYPE_STRING)
|
2006-04-05 03:25:35 +05:30
|
|
|
|
2009-09-03 02:24:22 +05:30
|
|
|
for data in data_list:
|
|
|
|
if data:
|
|
|
|
store.append(row=[data])
|
|
|
|
|
2004-10-29 06:19:40 +05:30
|
|
|
completion = gtk.EntryCompletion()
|
|
|
|
completion.set_model(store)
|
|
|
|
completion.set_minimum_key_length(1)
|
|
|
|
completion.set_text_column(0)
|
|
|
|
entry.set_completion(completion)
|
2004-05-02 09:40:33 +05:30
|
|
|
|
2005-05-31 23:47:38 +05:30
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# StandardCustomSelector class
|
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
2009-05-21 22:49:50 +05:30
|
|
|
class StandardCustomSelector(object):
|
2005-05-31 23:47:38 +05:30
|
|
|
"""
|
|
|
|
This class provides an interface to selecting from the predefined
|
|
|
|
options or entering custom string.
|
|
|
|
|
|
|
|
The typical usage should be:
|
2005-06-01 18:44:29 +05:30
|
|
|
type_sel = StandardCustomSelector(mapping,None,custom_key,active_key)
|
|
|
|
whatever_table.attach(type_sel,...)
|
|
|
|
or
|
|
|
|
type_sel = StandardCustomSelector(mapping,cbe,custom_key,active_key)
|
|
|
|
with the existing ComboBoxEntry cbe.
|
|
|
|
|
|
|
|
To set up the combo box, specify the active key at creation time,
|
|
|
|
or later (or with custom text) use:
|
2008-01-06 01:40:26 +05:30
|
|
|
type_sel.set_values(i,s)
|
2005-05-31 23:47:38 +05:30
|
|
|
|
|
|
|
and later, when or before the dialog is closed, do:
|
2005-06-01 18:44:29 +05:30
|
|
|
(i,s) = type_sel.get_values()
|
2005-05-31 23:47:38 +05:30
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
"""
|
2007-05-15 09:47:12 +05:30
|
|
|
def __init__(self, mapping, cbe=None, custom_key=None, active_key=None,
|
2006-04-01 05:16:34 +05:30
|
|
|
additional=None):
|
2005-05-31 23:47:38 +05:30
|
|
|
"""
|
|
|
|
Constructor for the StandardCustomSelector class.
|
|
|
|
|
2009-06-25 03:26:07 +05:30
|
|
|
:param cbe: Existing ComboBoxEntry widget to use.
|
|
|
|
:type cbe: gtk.ComboBoxEntry
|
|
|
|
: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
|
2005-05-31 23:47:38 +05:30
|
|
|
"""
|
2005-06-01 00:10:17 +05:30
|
|
|
|
|
|
|
# set variables
|
2005-05-31 23:47:38 +05:30
|
|
|
self.mapping = mapping
|
|
|
|
self.custom_key = custom_key
|
|
|
|
self.active_key = active_key
|
|
|
|
self.active_index = 0
|
2006-04-01 05:16:34 +05:30
|
|
|
self.additional = additional
|
2006-06-16 09:02:51 +05:30
|
|
|
|
2005-05-31 23:47:38 +05:30
|
|
|
# make model
|
2008-01-22 03:33:43 +05:30
|
|
|
self.store = gtk.ListStore(gobject.TYPE_INT, gobject.TYPE_STRING)
|
2005-06-01 00:10:17 +05:30
|
|
|
|
2005-05-31 23:47:38 +05:30
|
|
|
# fill it up using mapping
|
|
|
|
self.fill()
|
2005-06-01 00:10:17 +05:30
|
|
|
|
2005-05-31 23:47:38 +05:30
|
|
|
# create combo box entry
|
2005-06-01 18:44:29 +05:30
|
|
|
if cbe:
|
|
|
|
self.selector = cbe
|
|
|
|
self.selector.set_model(self.store)
|
|
|
|
self.selector.set_text_column(1)
|
|
|
|
else:
|
2007-05-15 09:47:12 +05:30
|
|
|
self.selector = gtk.ComboBoxEntry(self.store, 1)
|
2008-06-16 20:31:46 +05:30
|
|
|
if self.active_key is not None:
|
2005-05-31 23:47:38 +05:30
|
|
|
self.selector.set_active(self.active_index)
|
|
|
|
|
2005-06-01 00:10:17 +05:30
|
|
|
# make autocompletion work
|
|
|
|
completion = gtk.EntryCompletion()
|
|
|
|
completion.set_model(self.store)
|
|
|
|
completion.set_minimum_key_length(1)
|
|
|
|
completion.set_text_column(1)
|
|
|
|
self.selector.child.set_completion(completion)
|
|
|
|
|
2005-05-31 23:47:38 +05:30
|
|
|
def fill(self):
|
2007-05-20 09:13:38 +05:30
|
|
|
"""
|
|
|
|
Fill with data
|
|
|
|
"""
|
2009-05-27 02:18:09 +05:30
|
|
|
keys = sorted(self.mapping, self.by_value)
|
2005-05-31 23:47:38 +05:30
|
|
|
index = 0
|
|
|
|
for key in keys:
|
|
|
|
if key != self.custom_key:
|
2007-05-15 09:47:12 +05:30
|
|
|
self.store.append(row=[key, self.mapping[key]])
|
2005-05-31 23:47:38 +05:30
|
|
|
if key == self.active_key:
|
|
|
|
self.active_index = index
|
2009-09-03 02:24:22 +05:30
|
|
|
index += 1
|
2005-05-31 23:47:38 +05:30
|
|
|
|
2006-04-01 05:16:34 +05:30
|
|
|
if self.additional:
|
2006-04-28 09:02:04 +05:30
|
|
|
for event_type in self.additional:
|
2008-05-26 01:25:47 +05:30
|
|
|
if isinstance(event_type, basestring):
|
2006-06-16 09:02:51 +05:30
|
|
|
if event_type:
|
|
|
|
self.store.append(row=[self.custom_key, event_type])
|
2008-05-26 01:25:47 +05:30
|
|
|
elif isinstance(event_type, tuple):
|
2006-06-16 09:02:51 +05:30
|
|
|
if event_type[1]:
|
|
|
|
self.store.append(row=[event_type[0], event_type[1]])
|
2006-04-28 09:02:04 +05:30
|
|
|
else:
|
2006-05-02 23:02:54 +05:30
|
|
|
self.store.append(row=[int(event_type), str(event_type)])
|
2006-04-01 05:16:34 +05:30
|
|
|
if key == self.active_key:
|
|
|
|
self.active_index = index
|
2009-09-03 02:24:22 +05:30
|
|
|
index += 1
|
2006-04-01 05:16:34 +05:30
|
|
|
|
2007-05-15 09:47:12 +05:30
|
|
|
def by_value(self, first, second):
|
2005-05-31 23:47:38 +05:30
|
|
|
"""
|
|
|
|
Method for sorting keys based on the values.
|
|
|
|
"""
|
2007-05-15 09:47:12 +05:30
|
|
|
fvalue = self.mapping[first]
|
|
|
|
svalue = self.mapping[second]
|
|
|
|
return locale.strcoll(fvalue, svalue)
|
2005-05-31 23:47:38 +05:30
|
|
|
|
|
|
|
def get_values(self):
|
|
|
|
"""
|
|
|
|
Get selected values.
|
|
|
|
|
2009-06-25 03:26:07 +05:30
|
|
|
:returns: Returns (int,str) tuple corresponding to the selection.
|
|
|
|
:rtype: tuple
|
2005-05-31 23:47:38 +05:30
|
|
|
"""
|
2007-05-15 09:47:12 +05:30
|
|
|
active_iter = self.selector.get_active_iter()
|
|
|
|
if active_iter:
|
|
|
|
int_val = self.store.get_value(active_iter, 0)
|
|
|
|
str_val = self.store.get_value(active_iter, 1)
|
|
|
|
if str_val != self.mapping[int_val]:
|
|
|
|
str_val = self.selector.child.get_text().strip()
|
2005-06-01 18:44:29 +05:30
|
|
|
else:
|
2007-05-15 09:47:12 +05:30
|
|
|
int_val = self.custom_key
|
|
|
|
str_val = self.selector.child.get_text().strip()
|
2009-05-27 23:03:45 +05:30
|
|
|
if str_val in self.mapping.itervalues():
|
2009-05-23 00:13:40 +05:30
|
|
|
for key in self.mapping:
|
2007-05-15 09:47:12 +05:30
|
|
|
if str_val == self.mapping[key]:
|
|
|
|
int_val = key
|
2005-06-01 18:44:29 +05:30
|
|
|
break
|
|
|
|
else:
|
2007-05-15 09:47:12 +05:30
|
|
|
int_val = self.custom_key
|
|
|
|
return (int_val, str_val)
|
2005-06-01 18:44:29 +05:30
|
|
|
|
2007-05-15 09:47:12 +05:30
|
|
|
def set_values(self, val):
|
2005-06-01 18:44:29 +05:30
|
|
|
"""
|
|
|
|
Set values according to given tuple.
|
|
|
|
|
2009-06-25 03:26:07 +05:30
|
|
|
:param val: (int,str) tuple with the values to set.
|
|
|
|
:type val: tuple
|
2005-06-01 18:44:29 +05:30
|
|
|
"""
|
2007-06-27 10:20:33 +05:30
|
|
|
key, text = val
|
2009-05-23 00:13:40 +05:30
|
|
|
if key in self.mapping and key != self.custom_key:
|
2007-06-27 10:20:33 +05:30
|
|
|
self.store.foreach(self.set_int_value, key)
|
2008-06-16 20:31:46 +05:30
|
|
|
elif self.custom_key is not None:
|
2007-06-27 10:20:33 +05:30
|
|
|
self.selector.child.set_text(text)
|
2005-06-01 18:44:29 +05:30
|
|
|
else:
|
|
|
|
print "StandardCustomSelector.set(): Option not available:", val
|
|
|
|
|
2007-06-27 10:20:33 +05:30
|
|
|
def set_int_value(self, model, path, node, val):
|
|
|
|
if model.get_value(node, 0) == val:
|
|
|
|
self.selector.set_active_iter(node)
|
2005-06-01 18:44:29 +05:30
|
|
|
return True
|
|
|
|
return False
|
2005-05-31 23:47:38 +05:30
|
|
|
|
2008-01-06 01:40:26 +05:30
|
|
|
|