Refactor SelectPerson so that it inherits from BaseSelector like all the other selectors

svn: r11966
This commit is contained in:
Gary Burton 2009-02-11 20:13:46 +00:00
parent ae3a2b7542
commit e5db28ee6d
2 changed files with 113 additions and 145 deletions

View File

@ -2,6 +2,7 @@
# Gramps - a GTK+/GNOME based genealogy program # Gramps - a GTK+/GNOME based genealogy program
# #
# Copyright (C) 2003-2006 Donald N. Allingham # Copyright (C) 2003-2006 Donald N. Allingham
# 2009 Gary Burton
# #
# This program is free software; you can redistribute it and/or modify # 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 # it under the terms of the GNU General Public License as published by
@ -37,6 +38,7 @@ from gtk import glade
import const import const
import ManagedWindow import ManagedWindow
from Filters import SearchBar from Filters import SearchBar
from DisplayModels import PeopleModel
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
# #
@ -60,7 +62,13 @@ class BaseSelector(ManagedWindow.ManagedWindow):
set of handles to skip in the view, and search_bar to show the set of handles to skip in the view, and search_bar to show the
SearchBar at the top or not. SearchBar at the top or not.
""" """
self.title = self.get_window_title() self.filter = filter
# Set window title, some selectors may set self.title in their __init__
try:
assert self.title
except AttributeError:
self.title = self.get_window_title()
ManagedWindow.ManagedWindow.__init__(self, uistate, track, self) ManagedWindow.ManagedWindow.__init__(self, uistate, track, self)
@ -70,6 +78,7 @@ class BaseSelector(ManagedWindow.ManagedWindow):
self.db = dbstate.db self.db = dbstate.db
self.glade = glade.XML(const.GLADE_FILE,"select_person","gramps") self.glade = glade.XML(const.GLADE_FILE,"select_person","gramps")
window = self.glade.get_widget('select_person') window = self.glade.get_widget('select_person')
self.showall = self.glade.get_widget('showall')
title_label = self.glade.get_widget('title') title_label = self.glade.get_widget('title')
vbox = self.glade.get_widget('select_person_vbox') vbox = self.glade.get_widget('select_person_vbox')
self.tree = self.glade.get_widget('plist') self.tree = self.glade.get_widget('plist')
@ -98,7 +107,6 @@ class BaseSelector(ManagedWindow.ManagedWindow):
self.build_tree() self.build_tree()
self.selection = self.tree.get_selection() self.selection = self.tree.get_selection()
self._local_init() self._local_init()
self._set_size() self._set_size()
@ -106,8 +114,10 @@ class BaseSelector(ManagedWindow.ManagedWindow):
#show or hide search bar? #show or hide search bar?
self.set_show_search_bar(show_search_bar) self.set_show_search_bar(show_search_bar)
#Hide showall always (used in person selector only) #Hide showall always (used in person selector only)
showbox = self.glade.get_widget('showall') if self.filter is not None:
showbox.hide() self.showall.show()
else:
self.showall.hide()
def add_columns(self,tree): def add_columns(self,tree):
tree.set_fixed_height_mode(True) tree.set_fixed_height_mode(True)
@ -160,7 +170,7 @@ class BaseSelector(ManagedWindow.ManagedWindow):
def _on_row_activated(self, treeview, path, view_col): def _on_row_activated(self, treeview, path, view_col):
self.window.response(gtk.RESPONSE_OK) self.window.response(gtk.RESPONSE_OK)
def _local_init(self): def _local_init(self):
# define selector-specific init routine # define selector-specific init routine
pass pass
@ -243,11 +253,16 @@ class BaseSelector(ManagedWindow.ManagedWindow):
self.add_columns(self.tree) self.add_columns(self.tree)
#reset the model with correct sorting #reset the model with correct sorting
self.model = self.get_model_class()(self.db, self.sort_col, if self.get_model_class() is PeopleModel:
self.sortorder, self.model = PeopleModel(self.db,
sort_map=self.column_order(), (PeopleModel.FAST, self.filter),
skip=self.skip_list, skip=self.skip_list)
search=filter_info) else:
self.model = self.get_model_class()(self.db, self.sort_col,
self.sortorder,
sort_map=self.column_order(),
skip=self.skip_list,
search=filter_info)
self.tree.set_model(self.model) self.tree.set_model(self.model)
@ -259,7 +274,10 @@ class BaseSelector(ManagedWindow.ManagedWindow):
self.columns[self.sort_col].set_sort_order(self.sortorder) self.columns[self.sort_col].set_sort_order(self.sortorder)
# set the search column to be the sorted column # set the search column to be the sorted column
search_col = self.column_order()[self.sort_col][1] if self.get_model_class() is PeopleModel:
search_col = 0
else:
search_col = self.column_order()[self.sort_col][1]
self.tree.set_search_column(search_col) self.tree.set_search_column(search_col)
self.setupcols = False self.setupcols = False
@ -285,3 +303,14 @@ class BaseSelector(ManagedWindow.ManagedWindow):
self.tree.scroll_to_cell(path, None, 1, 0.5, 0) self.tree.scroll_to_cell(path, None, 1, 0.5, 0)
return True return True
def show_toggle(self, obj):
if obj.get_active():
filter = None
else:
filter = self.filter
self.model = PeopleModel(self.db, (PeopleModel.FAST, filter),
skip=self.skip_list)
self.tree.set_model(self.model)
self.tree.grab_focus()

View File

@ -1,7 +1,8 @@
# #
# Gramps - a GTK+/GNOME based genealogy program # Gramps - a GTK+/GNOME based genealogy program
# #
# Copyright (C) 2003-2004 Donald N. Allingham # Copyright (C) 2003-2006 Donald N. Allingham
# 2009 Gary Burton
# #
# This program is free software; you can redistribute it and/or modify # 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 # it under the terms of the GNU General Public License as published by
@ -26,167 +27,105 @@
# #
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
from gettext import gettext as _ from gettext import gettext as _
#-------------------------------------------------------------------------
#
# GTK/Gnome modules
#
#-------------------------------------------------------------------------
import gtk import gtk
from gtk import glade
import pango
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
# #
# gramps modules # gramps modules
# #
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
import const
from DisplayModels import PeopleModel from DisplayModels import PeopleModel
import ManagedWindow from _BaseSelector import BaseSelector
import Config
column_names = [
_('Name'),
_('ID') ,
_('Gender'),
_('Birth Date'),
_('Birth Place'),
_('Death Date'),
_('Death Place'),
_('Spouse'),
_('Last Change'),
]
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
# #
# SelectPerson # SelectEvent
# #
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
class SelectPerson(ManagedWindow.ManagedWindow): class SelectPerson(BaseSelector):
def __init__(self, dbstate, uistate, track=[], title='', def __init__(self, dbstate, uistate, track=[], title = None, filter = None,
filter=None, skip=[]): skip=set(), show_search_bar = False):
if title:
# SelectPerson may have a title passed to it which should be used
# instead of the default defined for get_window_title()
if title is not None:
self.title = title self.title = title
else:
self.title = _("Select Person")
ManagedWindow.ManagedWindow.__init__(self, uistate, track, self) BaseSelector.__init__(self, dbstate, uistate, track, filter,
skip, show_search_bar)
self.renderer = gtk.CellRendererText() def _local_init(self):
self.renderer.set_property('ellipsize',pango.ELLIPSIZE_END) """
self.dbstate = dbstate Perform local initialisation for this class
self.glade = glade.XML(const.GLADE_FILE,"select_person","gramps") """
self.plist = self.glade.get_widget('plist') self.width_key = Config.PERSON_SEL_WIDTH
self.showall = self.glade.get_widget('showall') self.height_key = Config.PERSON_SEL_HEIGHT
self.notebook = self.glade.get_widget('notebook') self.tree.connect('key-press-event', self._key_press)
self.plist.connect('row-activated', self._on_row_activated)
self.plist.connect('key-press-event', self._key_press)
self.selection = self.plist.get_selection()
self.selection.set_mode(gtk.SELECTION_SINGLE)
window = self.glade.get_widget('select_person')
title_label = self.glade.get_widget('title')
self.set_window(window,title_label,self.title)
self.filter = filter
if self.filter:
self.showall.show()
self.skip = skip
self.model = PeopleModel(self.dbstate.db,
(PeopleModel.FAST, filter),
skip=skip)
self.add_columns(self.plist)
self.plist.set_model(self.model)
self.showall.connect('toggled',self.show_toggle) self.showall.connect('toggled',self.show_toggle)
self.show()
def show_toggle(self, obj): def get_window_title(self):
if obj.get_active(): return _("Select Person")
filt = None
else:
filt = self.filter
self.model = PeopleModel(self.dbstate.db,
(PeopleModel.FAST, filt),
skip=self.skip)
self.plist.set_model(self.model)
def build_menu_names(self, obj):
return (self.title, None)
def add_columns(self, tree):
try:
column = gtk.TreeViewColumn(
_('Name'),
self.renderer,
text=0,
foreground=self.model.marker_color_column)
except AttributeError:
column = gtk.TreeViewColumn(_('Name'), self.renderer, text=0)
column.set_resizable(True)
column.set_sizing(gtk.TREE_VIEW_COLUMN_FIXED)
column.set_fixed_width(225)
tree.append_column(column)
for pair in self.dbstate.db.get_person_column_order():
if not pair[0]:
continue
name = column_names[pair[1]]
try:
column = gtk.TreeViewColumn(
name, self.renderer, markup=pair[1],
foreground=self.model.marker_color_column)
except AttributeError:
column = gtk.TreeViewColumn(
name, self.renderer, markup=pair[1])
column.set_resizable(True)
column.set_fixed_width(pair[2])
column.set_sizing(gtk.TREE_VIEW_COLUMN_FIXED)
tree.append_column(column)
def select_function(self,store,path,iter,id_list): def get_model_class(self):
id_list.append(self.model.get_value(iter,PeopleModel.COLUMN_INT_ID)) return PeopleModel
def get_selected_ids(self): def get_column_titles(self):
mlist = [] return [
self.plist.get_selection().selected_foreach(self.select_function,mlist) (_('Name'), 250, BaseSelector.TEXT),
return mlist (_('ID'), 75, BaseSelector.TEXT),
(_('Gender'), 75, BaseSelector.TEXT),
(_('Birth Date'), 150, BaseSelector.TEXT),
(_('Birth Place'), 150, BaseSelector.TEXT),
(_('Death Date'), 150, BaseSelector.TEXT),
(_('Death Place'), 150, BaseSelector.TEXT),
(_('Spouse'), 150, BaseSelector.TEXT),
(_('Last Change'), 150, BaseSelector.TEXT)
]
def run(self): def get_from_handle_func(self):
val = self.window.run() return self.db.get_person_from_handle
if val == gtk.RESPONSE_OK:
idlist = self.get_selected_ids() def get_handle_column(self):
self.close() return PeopleModel.COLUMN_INT_ID
if idlist and idlist[0]:
return_value = self.dbstate.db.get_person_from_handle(idlist[0]) def column_order(self):
else: """
return_value = None returns a tuple indicating the column order of the model
return return_value """
elif val != gtk.RESPONSE_DELETE_EVENT: return self.db.get_person_column_order()
self.close()
return None def column_view_names(self):
"""
Get correct column view names on which model is based
"""
column_names = [
_('Name'),
_('ID') ,
_('Gender'),
_('Birth Date'),
_('Birth Place'),
_('Death Date'),
_('Death Place'),
_('Spouse'),
_('Last Change'),
]
return column_names
def _on_row_activated(self, treeview, path, view_col):
store, paths = self.selection.get_selected_rows()
if paths and len(paths[0]) == 2 :
self.window.response(gtk.RESPONSE_OK)
def _key_press(self, obj, event): def _key_press(self, obj, event):
if not event.state or event.state in (gtk.gdk.MOD2_MASK, ): if not event.state or event.state in (gtk.gdk.MOD2_MASK, ):
if event.keyval in (gtk.keysyms.Return, gtk.keysyms.KP_Enter): if event.keyval in (gtk.keysyms.Return, gtk.keysyms.KP_Enter):
store, paths = self.selection.get_selected_rows() store, paths = self.selection.get_selected_rows()
if paths and len(paths[0]) == 1 : if paths and len(paths[0]) == 1 :
if self.plist.row_expanded(paths[0]): if self.tree.row_expanded(paths[0]):
self.plist.collapse_row(paths[0]) self.tree.collapse_row(paths[0])
else: else:
self.plist.expand_row(paths[0], 0) self.tree.expand_row(paths[0], 0)
return True return True
return False return False
def _on_row_activated(self, treeview, path, view_col):
store, paths = self.selection.get_selected_rows()
if paths and len(paths[0]) == 2 :
self.window.response(gtk.RESPONSE_OK)