2008-05-09 18:20:04 +05:30
|
|
|
#
|
|
|
|
# Gramps - a GTK+/GNOME based genealogy program
|
|
|
|
#
|
|
|
|
# Copyright (C) 2008 Zsolt Foldvari
|
|
|
|
#
|
|
|
|
# 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
|
|
|
|
#
|
|
|
|
|
|
|
|
# $Id$
|
|
|
|
|
2008-05-28 01:23:25 +05:30
|
|
|
"The ValidatedComboEntry widget class."
|
|
|
|
|
|
|
|
__all__ = ["ValidatedComboEntry"]
|
2008-05-09 18:20:04 +05:30
|
|
|
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# Python modules
|
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
import logging
|
2008-05-28 01:23:25 +05:30
|
|
|
_LOG = logging.getLogger(".widgets.validatedcomboentry")
|
2008-05-09 18:20:04 +05:30
|
|
|
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# GTK modules
|
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
import gtk
|
|
|
|
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
2008-05-28 01:23:25 +05:30
|
|
|
# ValidatedComboEntry class
|
2008-05-09 18:20:04 +05:30
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
2008-05-28 01:23:25 +05:30
|
|
|
class ValidatedComboEntry(gtk.ComboBox, gtk.CellLayout):
|
2008-05-09 18:20:04 +05:30
|
|
|
"""A ComboBoxEntry widget with validation.
|
|
|
|
|
2008-05-28 01:23:25 +05:30
|
|
|
ValidatedComboEntry may have data type other then string, and is set
|
|
|
|
with the C{datatype} contructor parameter.
|
2008-05-09 18:20:04 +05:30
|
|
|
|
|
|
|
Its behaviour is different from gtk.ComboBoxEntry in the way how
|
|
|
|
the entry part of the widget is handled. While gtk.ComboBoxEntry
|
|
|
|
emits the 'changed' signal immediatelly the text in the entry is
|
2008-05-28 01:23:25 +05:30
|
|
|
changed, ValidatedComboEntry emits the signal only after the text is
|
2008-05-09 18:20:04 +05:30
|
|
|
activated (enter is pressed, the focus is moved out) and validated.
|
|
|
|
|
|
|
|
Validation function is an optional feature and activated only if a
|
|
|
|
validator function is given at instantiation.
|
|
|
|
|
|
|
|
The entry can be set as editable or not editable using the
|
|
|
|
L{set_entry_editable} method.
|
|
|
|
|
|
|
|
"""
|
2008-05-28 01:23:25 +05:30
|
|
|
__gtype_name__ = "ValidatedComboEntry"
|
2008-05-09 18:20:04 +05:30
|
|
|
|
2010-08-31 15:30:46 +05:30
|
|
|
def __init__(self, datatype, model=None, column=-1, validator=None, width=-1):
|
2008-05-09 18:20:04 +05:30
|
|
|
gtk.ComboBox.__init__(self, model)
|
|
|
|
|
|
|
|
self._entry = gtk.Entry()
|
2010-08-31 15:30:46 +05:30
|
|
|
self._entry.set_width_chars(width)
|
2008-05-28 01:23:25 +05:30
|
|
|
# <hack description="set the GTK_ENTRY(self._entry)->is_cell_renderer
|
2008-05-09 18:20:04 +05:30
|
|
|
# flag to TRUE in order to tell the entry to fill its allocation.">
|
|
|
|
dummy_event = gtk.gdk.Event(gtk.gdk.NOTHING)
|
|
|
|
self._entry.start_editing(dummy_event)
|
|
|
|
# </hack>
|
|
|
|
self.add(self._entry)
|
|
|
|
self._entry.show()
|
|
|
|
|
|
|
|
self._text_renderer = gtk.CellRendererText()
|
|
|
|
self.pack_start(self._text_renderer, False)
|
|
|
|
|
2008-05-28 01:23:25 +05:30
|
|
|
self._data_type = datatype
|
|
|
|
self._data_column = -1
|
|
|
|
self.set_data_column(column)
|
|
|
|
|
2008-05-09 18:20:04 +05:30
|
|
|
self._active_text = ''
|
2008-05-28 01:23:25 +05:30
|
|
|
self._active_data = None
|
2008-05-09 18:20:04 +05:30
|
|
|
self.set_active(-1)
|
|
|
|
|
|
|
|
self._validator = validator
|
|
|
|
|
|
|
|
self._entry.connect('activate', self._on_entry_activate)
|
2008-05-14 17:18:37 +05:30
|
|
|
self._entry.connect('focus-in-event', self._on_entry_focus_in_event)
|
2008-05-09 18:20:04 +05:30
|
|
|
self._entry.connect('focus-out-event', self._on_entry_focus_out_event)
|
|
|
|
self._entry.connect('key-press-event', self._on_entry_key_press_event)
|
2008-05-28 01:23:25 +05:30
|
|
|
self.connect('changed', self._on_changed)
|
|
|
|
self._internal_change = False
|
2008-05-09 18:20:04 +05:30
|
|
|
|
|
|
|
self._has_frame_changed()
|
|
|
|
self.connect('notify', self._on_notify)
|
|
|
|
|
|
|
|
# Virtual overriden methods
|
|
|
|
|
|
|
|
def do_mnemonic_activate(self, group_cycling):
|
|
|
|
self._entry.grab_focus()
|
|
|
|
return True
|
|
|
|
|
|
|
|
def do_grab_focus(self):
|
|
|
|
self._entry.grab_focus()
|
|
|
|
|
|
|
|
# Signal handlers
|
|
|
|
|
|
|
|
def _on_entry_activate(self, entry):
|
|
|
|
"""Signal handler.
|
|
|
|
|
|
|
|
Called when the entry is activated.
|
|
|
|
|
|
|
|
"""
|
|
|
|
self._entry_changed(entry)
|
|
|
|
|
2008-05-14 17:18:37 +05:30
|
|
|
def _on_entry_focus_in_event(self, widget, event):
|
|
|
|
"""Signal handler.
|
|
|
|
|
|
|
|
Called when the focus enters the entry, and is used for saving
|
|
|
|
the entry's text for later comparison.
|
|
|
|
|
|
|
|
"""
|
|
|
|
self._text_on_focus_in = self._entry.get_text()
|
|
|
|
|
2008-05-09 18:20:04 +05:30
|
|
|
def _on_entry_focus_out_event(self, widget, event):
|
|
|
|
"""Signal handler.
|
|
|
|
|
|
|
|
Called when the focus leaves the entry.
|
|
|
|
|
|
|
|
"""
|
2008-05-14 17:18:37 +05:30
|
|
|
if (self._entry.get_text() != self._text_on_focus_in):
|
|
|
|
self._entry_changed(widget)
|
2008-05-09 18:20:04 +05:30
|
|
|
|
|
|
|
def _on_entry_key_press_event(self, entry, event):
|
|
|
|
"""Signal handler.
|
|
|
|
|
|
|
|
Its purpose is to handle escape button.
|
|
|
|
|
|
|
|
"""
|
2008-05-15 16:24:47 +05:30
|
|
|
# FIXME Escape never reaches here, the dialog eats it, I assume.
|
2008-05-09 18:20:04 +05:30
|
|
|
if event.keyval == gtk.keysyms.Escape:
|
|
|
|
entry.set_text(self._active_text)
|
2008-05-28 01:23:25 +05:30
|
|
|
entry.set_position(-1)
|
|
|
|
return True
|
2008-05-09 18:20:04 +05:30
|
|
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
def _on_changed(self, combobox):
|
|
|
|
"""Signal handler.
|
|
|
|
|
|
|
|
Called when the active row is changed in the combo box.
|
|
|
|
|
|
|
|
"""
|
2008-05-28 01:23:25 +05:30
|
|
|
if self._internal_change:
|
|
|
|
return
|
|
|
|
|
2008-05-09 18:20:04 +05:30
|
|
|
iter = self.get_active_iter()
|
|
|
|
if iter:
|
|
|
|
model = self.get_model()
|
2008-05-28 01:23:25 +05:30
|
|
|
self._active_data = model.get_value(iter, self._data_column)
|
|
|
|
self._active_text = str(self._active_data)
|
2008-05-15 16:24:47 +05:30
|
|
|
self._entry.set_text(self._active_text)
|
2008-05-09 18:20:04 +05:30
|
|
|
|
|
|
|
def _on_notify(self, object, gparamspec):
|
|
|
|
"""Signal handler.
|
|
|
|
|
|
|
|
Called whenever a property of the object is changed.
|
|
|
|
|
|
|
|
"""
|
|
|
|
if gparamspec.name == 'has-frame':
|
|
|
|
self._has_frame_changed()
|
|
|
|
|
|
|
|
# Private methods
|
|
|
|
|
|
|
|
def _entry_changed(self, entry):
|
|
|
|
new_text = entry.get_text()
|
|
|
|
|
2008-05-28 01:23:25 +05:30
|
|
|
try:
|
|
|
|
new_data = self._data_type(new_text)
|
|
|
|
|
|
|
|
if (self._validator is not None) and not self._validator(new_data):
|
|
|
|
raise ValueError
|
|
|
|
except ValueError:
|
2008-05-09 18:20:04 +05:30
|
|
|
entry.set_text(self._active_text)
|
2008-05-28 01:23:25 +05:30
|
|
|
entry.set_position(-1)
|
2008-05-09 18:20:04 +05:30
|
|
|
return
|
|
|
|
|
|
|
|
self._active_text = new_text
|
2008-05-28 01:23:25 +05:30
|
|
|
self._active_data = new_data
|
|
|
|
|
|
|
|
self._internal_change = True
|
|
|
|
new_iter = self._is_in_model(new_data)
|
|
|
|
if new_iter is None:
|
|
|
|
self.set_active(-1)
|
|
|
|
else:
|
|
|
|
self.set_active_iter(new_iter)
|
|
|
|
self._internal_change = False
|
2008-05-09 18:20:04 +05:30
|
|
|
|
|
|
|
def _has_frame_changed(self):
|
|
|
|
has_frame = self.get_property('has-frame')
|
|
|
|
self._entry.set_has_frame(has_frame)
|
2008-05-28 01:23:25 +05:30
|
|
|
|
|
|
|
def _is_in_model(self, data):
|
|
|
|
"""Check if given data is in the model or not.
|
|
|
|
|
|
|
|
@param data: data value to check
|
|
|
|
@type data: depends on the actual data type of the object
|
|
|
|
@returns: position of 'data' in the model
|
|
|
|
@returntype: gtk.TreeIter or None
|
|
|
|
|
|
|
|
"""
|
|
|
|
model = self.get_model()
|
|
|
|
|
|
|
|
iter = model.get_iter_first()
|
|
|
|
while iter:
|
|
|
|
if model.get_value(iter, self._data_column) == data:
|
|
|
|
break
|
|
|
|
iter = model.iter_next(iter)
|
|
|
|
|
|
|
|
return iter
|
2008-05-09 18:20:04 +05:30
|
|
|
|
|
|
|
# Public methods
|
|
|
|
|
2008-05-28 01:23:25 +05:30
|
|
|
def set_data_column(self, data_column):
|
|
|
|
if data_column < 0:
|
2008-05-09 18:20:04 +05:30
|
|
|
return
|
|
|
|
|
2008-05-28 01:23:25 +05:30
|
|
|
model = self.get_model()
|
|
|
|
if model is None:
|
2008-05-09 18:20:04 +05:30
|
|
|
return
|
|
|
|
|
2008-05-28 01:23:25 +05:30
|
|
|
if data_column > model.get_n_columns():
|
|
|
|
return
|
|
|
|
|
|
|
|
if self._data_column == -1:
|
|
|
|
self._data_column = data_column
|
|
|
|
self.set_attributes(self._text_renderer, text=data_column)
|
2008-05-09 18:20:04 +05:30
|
|
|
|
2008-05-28 01:23:25 +05:30
|
|
|
def get_data_column(self):
|
|
|
|
return self._data_column
|
2008-05-09 18:20:04 +05:30
|
|
|
|
2008-05-28 01:23:25 +05:30
|
|
|
def set_active_data(self, data):
|
|
|
|
# set it via entry so that it will be also validated
|
2008-05-09 18:20:04 +05:30
|
|
|
if self._entry:
|
2008-05-28 01:23:25 +05:30
|
|
|
self._entry.set_text(str(data))
|
2008-05-14 17:18:37 +05:30
|
|
|
self._entry_changed(self._entry)
|
2008-05-09 18:20:04 +05:30
|
|
|
|
2008-05-28 01:23:25 +05:30
|
|
|
def get_active_data(self):
|
|
|
|
return self._active_data
|
2008-05-09 18:20:04 +05:30
|
|
|
|
|
|
|
def set_entry_editable(self, is_editable):
|
|
|
|
self._entry.set_editable(is_editable)
|