Code cleanup in _MenuOptions.py

svn: r9799
This commit is contained in:
Brian Matherly 2008-01-14 00:24:15 +00:00
parent 21aee67e79
commit f3ea893102
2 changed files with 307 additions and 237 deletions

View File

@ -1,3 +1,6 @@
2008-01-13 Brian Matherly <brian@gramps-project.org>
* src/PluginUtils/_MenuOptions.py: Code cleanup.
2008-01-13 Gary Burton <gary.burton@zen.co.uk> 2008-01-13 Gary Burton <gary.burton@zen.co.uk>
* src/ManagedWindow.py: Revert last change. * src/ManagedWindow.py: Revert last change.

View File

@ -1,7 +1,7 @@
# #
# Gramps - a GTK+/GNOME based genealogy program # Gramps - a GTK+/GNOME based genealogy program
# #
# Copyright (C) 2007 Brian G. Matherly # Copyright (C) 2007-2008 Brian G. Matherly
# #
# 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
@ -22,6 +22,13 @@
""" """
Abstracted option handling. Abstracted option handling.
""" """
#------------------------------------------------------------------------
#
# python modules
#
#------------------------------------------------------------------------
from gettext import gettext as _
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
# #
# gramps modules # gramps modules
@ -30,7 +37,6 @@ Abstracted option handling.
import gtk import gtk
import gobject import gobject
import Utils import Utils
import _Tool as Tool
import GrampsWidgets import GrampsWidgets
import ManagedWindow import ManagedWindow
from Selectors import selector_factory from Selectors import selector_factory
@ -42,23 +48,25 @@ from BasicUtils import name_displayer as _nd
# #
#------------------------------------------------------------------------ #------------------------------------------------------------------------
class LastNameDialog(ManagedWindow.ManagedWindow): class LastNameDialog(ManagedWindow.ManagedWindow):
"""
def __init__(self, database, uistate, track, surnames, skipList=set()): A dialog that allows the selection of a surname from the database.
"""
def __init__(self, database, uistate, track, surnames, skip_list=set()):
self.title = _('Select surname') self.title = _('Select surname')
ManagedWindow.ManagedWindow.__init__(self, uistate, track, self) ManagedWindow.ManagedWindow.__init__(self, uistate, track, self)
self.dlg = gtk.Dialog( flags = gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT | \
None, gtk.DIALOG_NO_SEPARATOR
uistate.window, buttons = (gtk.STOCK_CANCEL, gtk.RESPONSE_REJECT, gtk.STOCK_OK,
gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT | gtk.DIALOG_NO_SEPARATOR, gtk.RESPONSE_ACCEPT)
(gtk.STOCK_CANCEL, gtk.RESPONSE_REJECT, gtk.STOCK_OK, gtk.RESPONSE_ACCEPT)) self.dlg = gtk.Dialog(None, uistate.window, flags, buttons)
self.dlg.set_position(gtk.WIN_POS_CENTER_ON_PARENT) self.dlg.set_position(gtk.WIN_POS_CENTER_ON_PARENT)
self.set_window(self.dlg, None, self.title) self.set_window(self.dlg, None, self.title)
self.window.set_default_size(400, 400) self.window.set_default_size(400, 400)
# build up a container to display all of the people of interest # build up a container to display all of the people of interest
self.model = gtk.ListStore(gobject.TYPE_STRING, gobject.TYPE_INT) self.model = gtk.ListStore(gobject.TYPE_STRING, gobject.TYPE_INT)
self.treeView = gtk.TreeView(self.model) self.tree_view = gtk.TreeView(self.model)
col1 = gtk.TreeViewColumn(_('Surname'), gtk.CellRendererText(), text=0) col1 = gtk.TreeViewColumn(_('Surname'), gtk.CellRendererText(), text=0)
col2 = gtk.TreeViewColumn(_('Count'), gtk.CellRendererText(), text=1) col2 = gtk.TreeViewColumn(_('Count'), gtk.CellRendererText(), text=1)
col1.set_resizable(True) col1.set_resizable(True)
@ -67,14 +75,14 @@ class LastNameDialog(ManagedWindow.ManagedWindow):
col2.set_sizing(gtk.TREE_VIEW_COLUMN_AUTOSIZE) col2.set_sizing(gtk.TREE_VIEW_COLUMN_AUTOSIZE)
col1.set_sort_column_id(0) col1.set_sort_column_id(0)
col2.set_sort_column_id(1) col2.set_sort_column_id(1)
self.treeView.append_column(col1) self.tree_view.append_column(col1)
self.treeView.append_column(col2) self.tree_view.append_column(col2)
self.scrolledWindow = gtk.ScrolledWindow() scrolled_window = gtk.ScrolledWindow()
self.scrolledWindow.add(self.treeView) scrolled_window.add(self.tree_view)
self.scrolledWindow.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC) scrolled_window.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
self.scrolledWindow.set_shadow_type(gtk.SHADOW_OUT) scrolled_window.set_shadow_type(gtk.SHADOW_OUT)
self.dlg.vbox.pack_start(self.scrolledWindow, expand=True, fill=True) self.dlg.vbox.pack_start(scrolled_window, expand=True, fill=True)
self.scrolledWindow.show_all() scrolled_window.show_all()
if len(surnames) == 0: if len(surnames) == 0:
# we could use database.get_surname_list(), but if we do that # we could use database.get_surname_list(), but if we do that
@ -84,14 +92,15 @@ class LastNameDialog(ManagedWindow.ManagedWindow):
# for name in database.get_surname_list(): # for name in database.get_surname_list():
# self.model.append([name, 0]) # self.model.append([name, 0])
# build up the list of surnames, keeping track of the count for each name # build up the list of surnames, keeping track of the count for each
# (this can be a lengthy process, so by passing in the dictionary we can # name (this can be a lengthy process, so by passing in the
# be certain we only do this once) # dictionary we can be certain we only do this once)
progress = Utils.ProgressMeter(_('Finding surnames')) progress = Utils.ProgressMeter(_('Finding surnames'))
progress.set_pass(_('Finding surnames'), database.get_number_of_people()) progress.set_pass(_('Finding surnames'),
for personHandle in database.get_person_handles(False): database.get_number_of_people())
for person_handle in database.get_person_handles(False):
progress.step() progress.step()
person = database.get_person_from_handle(personHandle) person = database.get_person_from_handle(person_handle)
key = person.get_primary_name().get_surname() key = person.get_primary_name().get_surname()
count = 0 count = 0
if key in surnames: if key in surnames:
@ -101,28 +110,32 @@ class LastNameDialog(ManagedWindow.ManagedWindow):
# insert the names and count into the model # insert the names and count into the model
for key in surnames: for key in surnames:
if key.encode('iso-8859-1','xmlcharrefreplace') not in skipList: if key.encode('iso-8859-1','xmlcharrefreplace') not in skip_list:
self.model.append([key, surnames[key]]) self.model.append([key, surnames[key]])
# keep the list sorted starting with the most popular last name # keep the list sorted starting with the most popular last name
self.model.set_sort_column_id(1, gtk.SORT_DESCENDING) self.model.set_sort_column_id(1, gtk.SORT_DESCENDING)
# the "OK" button should be enabled/disabled based on the selection of a row # the "OK" button should be enabled/disabled based on the selection of
self.treeSelection = self.treeView.get_selection() # a row
self.treeSelection.set_mode(gtk.SELECTION_MULTIPLE) self.tree_selection = self.tree_view.get_selection()
self.treeSelection.select_path(0) self.tree_selection.set_mode(gtk.SELECTION_MULTIPLE)
self.tree_selection.select_path(0)
def run(self): def run(self):
"""
Display the dialog and return the selected surnames when done.
"""
response = self.dlg.run() response = self.dlg.run()
surnameSet = set() surname_set = set()
if response == gtk.RESPONSE_ACCEPT: if response == gtk.RESPONSE_ACCEPT:
(mode, paths) = self.treeSelection.get_selected_rows() (mode, paths) = self.tree_selection.get_selected_rows()
for path in paths: for path in paths:
iter = self.model.get_iter(path) i = self.model.get_iter(path)
surname = self.model.get_value(iter, 0) surname = self.model.get_value(i, 0)
surnameSet.add(surname) surname_set.add(surname)
self.dlg.destroy() self.dlg.destroy()
return surnameSet return surname_set
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
# #
@ -195,7 +208,7 @@ class Option:
""" """
return self.__help_str return self.__help_str
def set_help(self,help): def set_help(self, help_text):
""" """
Set the help information for this option. Set the help information for this option.
@ -205,7 +218,7 @@ class Option:
@type value: string @type value: string
@return: nothing @return: nothing
""" """
self.__help_str = help self.__help_str = help_text
def add_dialog_category(self, dialog, category): def add_dialog_category(self, dialog, category):
""" """
@ -241,7 +254,7 @@ class StringOption(Option):
""" """
Option.__init__(self, label, value) Option.__init__(self, label, value)
def make_gui_obj(self, gtk, dialog): def make_gui_obj(self, dialog):
""" """
Add a StringOption (single line text) to the dialog. Add a StringOption (single line text) to the dialog.
""" """
@ -276,7 +289,7 @@ class ColourButtonOption(Option):
""" """
Option.__init__(self, label, value) Option.__init__(self, label, value)
def make_gui_obj(self, gtk, dialog): def make_gui_obj(self, dialog):
""" """
Add a ColorButton to the dialog. Add a ColorButton to the dialog.
""" """
@ -304,7 +317,7 @@ class NumberOption(Option):
This class describes an option that is a simple number with defined maximum This class describes an option that is a simple number with defined maximum
and minimum values. and minimum values.
""" """
def __init__(self,label,value,min,max): def __init__(self, label, value, min_val, max_val):
""" """
@param label: A friendly label to be applied to this option. @param label: A friendly label to be applied to this option.
Example: "Number of generations to include" Example: "Number of generations to include"
@ -321,8 +334,8 @@ class NumberOption(Option):
@return: nothing @return: nothing
""" """
Option.__init__(self, label, value) Option.__init__(self, label, value)
self.__min = min self.__min = min_val
self.__max = max self.__max = max_val
def get_min(self): def get_min(self):
""" """
@ -340,7 +353,7 @@ class NumberOption(Option):
""" """
return self.__max return self.__max
def make_gui_obj(self, gtk, dialog): def make_gui_obj(self, dialog):
""" """
Add a NumberOption to the dialog. Add a NumberOption to the dialog.
""" """
@ -367,21 +380,22 @@ class FloatOption(NumberOption):
This class performs like NumberOption, but allows for float values This class performs like NumberOption, but allows for float values
for the minimum/maximum/increment. for the minimum/maximum/increment.
""" """
def __init__(self, label, value, min, max): def __init__(self, label, value, min_val, max_val):
# Someone who knows python better than I will probably # Someone who knows python better than I will probably
# want to add a parameter for the caller to specify how # want to add a parameter for the caller to specify how
# many decimal points are needed. # many decimal points are needed.
# #
# At the time this function was written, the only code # At the time this function was written, the only code
# that needed this class required 2 decimals. # that needed this class required 2 decimals.
NumberOption.__init__(self, label, value, min, max) NumberOption.__init__(self, label, value, min_val, max_val)
def make_gui_obj(self, gtk, dialog): def make_gui_obj(self, dialog):
""" """
Add a FloatOption to the dialog. Add a FloatOption to the dialog.
""" """
value = self.get_value() value = self.get_value()
adj = gtk.Adjustment(value, lower=self.get_min(), upper=self.get_max(), step_incr=0.01) adj = gtk.Adjustment(value, lower=self.get_min(),
upper=self.get_max(), step_incr=0.01)
self.gobj = gtk.SpinButton(adjustment=adj, digits=2) self.gobj = gtk.SpinButton(adjustment=adj, digits=2)
self.gobj.set_value(value) self.gobj.set_value(value)
@ -392,7 +406,6 @@ class FloatOption(NumberOption):
""" """
return float(self.gobj.get_value()) return float(self.gobj.get_value())
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
# #
# TextOption class # TextOption class
@ -414,7 +427,7 @@ class TextOption(Option):
""" """
Option.__init__(self, label, value) Option.__init__(self, label, value)
def make_gui_obj(self, gtk, dialog): def make_gui_obj(self, dialog):
""" """
Add a TextOption to the dialog. Add a TextOption to the dialog.
""" """
@ -434,9 +447,9 @@ class TextOption(Option):
""" """
Parse the text option (multi-line text). Parse the text option (multi-line text).
""" """
b = self.gtext.get_buffer() buff = self.gtext.get_buffer()
text_val = unicode( b.get_text( b.get_start_iter(), text_val = unicode( buff.get_text( buff.get_start_iter(),
b.get_end_iter(), buff.get_end_iter(),
False) ) False) )
return text_val.split('\n') return text_val.split('\n')
@ -461,7 +474,7 @@ class BooleanOption(Option):
""" """
Option.__init__(self, label, value) Option.__init__(self, label, value)
def make_gui_obj(self, gtk, dialog): def make_gui_obj(self, dialog):
""" """
Add a BooleanOption to the dialog. Add a BooleanOption to the dialog.
""" """
@ -521,11 +534,11 @@ class EnumeratedListOption(Option):
""" """
return self.__items return self.__items
def make_gui_obj(self, gtk, dialog): def make_gui_obj(self, dialog):
""" """
Add an EnumeratedListOption to the dialog. Add an EnumeratedListOption to the dialog.
""" """
v = self.get_value() cur_val = self.get_value()
active_index = 0 active_index = 0
current_index = 0 current_index = 0
self.combo = gtk.combo_box_new_text() self.combo = gtk.combo_box_new_text()
@ -533,7 +546,7 @@ class EnumeratedListOption(Option):
self.gobj.add(self.combo) self.gobj.add(self.combo)
for (value, description) in self.get_items(): for (value, description) in self.get_items():
self.combo.append_text(description) self.combo.append_text(description)
if value == v: if value == cur_val:
active_index = current_index active_index = current_index
current_index += 1 current_index += 1
self.combo.set_active( active_index ) self.combo.set_active( active_index )
@ -583,15 +596,18 @@ class PersonFilterOption(Option):
self.set_value(0) self.set_value(0)
def get_center_person(self): def get_center_person(self):
"""
Get the person for whom the filters have been generated.
"""
return self.__person return self.__person
def get_filter(self): def get_filter(self):
""" """
Return the filter object. Return the currently selected filter object.
""" """
return self.__filters[self.get_value()] return self.__filters[self.get_value()]
def make_gui_obj(self, gtk, dialog): def make_gui_obj(self, dialog):
""" """
Add an PersonFilterOption to the dialog. Add an PersonFilterOption to the dialog.
""" """
@ -607,12 +623,17 @@ class PersonFilterOption(Option):
self.update_gui_obj() self.update_gui_obj()
def __on_value_changed(self, obj): def __on_value_changed(self, obj):
"""
Handle the change of the value.
"""
self.set_value( int(self.combo.get_active()) ) self.set_value( int(self.combo.get_active()) )
def on_change_clicked(self, *obj): def on_change_clicked(self, *obj):
from Selectors import selector_factory """
SelectPerson = selector_factory('Person') Handle the "Change..." button press.
sel_person = SelectPerson(self.dialog.dbstate, """
select_class = selector_factory('Person')
sel_person = select_class(self.dialog.dbstate,
self.dialog.uistate, self.dialog.uistate,
self.dialog.track) self.dialog.track)
new_person = sel_person.run() new_person = sel_person.run()
@ -626,8 +647,8 @@ class PersonFilterOption(Option):
self.combo.get_model().clear() self.combo.get_model().clear()
self.__filters = ReportUtils.get_person_filters(self.__person, self.__filters = ReportUtils.get_person_filters(self.__person,
self.__include_single) self.__include_single)
for filter in self.__filters: for filt in self.__filters:
self.combo.append_text(filter.get_name()) self.combo.append_text(filt.get_name())
if self.get_value() >= len(self.__filters): if self.get_value() >= len(self.__filters):
# Set the value to zero if it is not valid. # Set the value to zero if it is not valid.
@ -662,11 +683,11 @@ class PersonOption(Option):
@type value: DbState @type value: DbState
@return: nothing @return: nothing
""" """
self.dbstate = dbstate self.__dbstate = dbstate
self.db = dbstate.get_database() self.__db = dbstate.get_database()
Option.__init__(self, label, value) Option.__init__(self, label, value)
def make_gui_obj(self, gtk, dialog): def make_gui_obj(self, dialog):
self.dialog = dialog self.dialog = dialog
self.gobj = gtk.HBox() self.gobj = gtk.HBox()
self.person_label = gtk.Label() self.person_label = gtk.Label()
@ -681,32 +702,35 @@ class PersonOption(Option):
self.gobj.pack_start(self.pevt, False) self.gobj.pack_start(self.pevt, False)
self.gobj.pack_end(self.person_button, False) self.gobj.pack_end(self.person_button, False)
person = self.db.get_person_from_gramps_id(self.get_value()) person = self.__db.get_person_from_gramps_id(self.get_value())
if not person: if not person:
person = self.dbstate.get_active_person() person = self.__dbstate.get_active_person()
self.update_person(person) self.update_person(person)
def parse(self): def parse(self):
return self.get_value() return self.get_value()
def get_person_clicked(self, obj): def get_person_clicked(self, obj):
"""
Handle the button to choose a different person.
"""
from Filters import GenericFilter, Rules from Filters import GenericFilter, Rules
rfilter = GenericFilter() rfilter = GenericFilter()
rfilter.set_logical_op('or') rfilter.set_logical_op('or')
rfilter.add_rule(Rules.Person.IsBookmarked([])) rfilter.add_rule(Rules.Person.IsBookmarked([]))
default_person = self.db.get_default_person() default_person = self.__db.get_default_person()
if default_person: if default_person:
id = default_person.get_gramps_id() gid = default_person.get_gramps_id()
rfilter.add_rule(Rules.Person.HasIdOf([id])) rfilter.add_rule(Rules.Person.HasIdOf([gid]))
active_person = self.dbstate.get_active_person() active_person = self.__dbstate.get_active_person()
if active_person: if active_person:
id = active_person.get_gramps_id() gid = active_person.get_gramps_id()
rfilter.add_rule(Rules.Person.HasIdOf([id])) rfilter.add_rule(Rules.Person.HasIdOf([gid]))
SelectPerson = selector_factory('Person') select_class = selector_factory('Person')
sel = SelectPerson(self.dbstate, self.dialog.uistate, sel = select_class(self.__dbstate, self.dialog.uistate,
self.dialog.track, self.dialog.track,
title=_('Select a person for the report'), title=_('Select a person for the report'),
filter=rfilter ) filter=rfilter )
@ -714,6 +738,9 @@ class PersonOption(Option):
self.update_person(person) self.update_person(person)
def update_person(self, person): def update_person(self, person):
"""
Update the currently selected person.
"""
if person: if person:
name = _nd.display(person) name = _nd.display(person)
gid = person.get_gramps_id() gid = person.get_gramps_id()
@ -747,11 +774,11 @@ class PersonListOption(Option):
@type value: string @type value: string
@return: nothing @return: nothing
""" """
self.db = dbstate.get_database() self.__db = dbstate.get_database()
self.dbstate = dbstate self.__dbstate = dbstate
Option.__init__(self, label, value) Option.__init__(self, label, value)
def make_gui_obj(self, gtk, dialog): def make_gui_obj(self, dialog):
""" """
Add a "people picker" widget to the dialog. Add a "people picker" widget to the dialog.
""" """
@ -759,8 +786,8 @@ class PersonListOption(Option):
value = self.get_value() value = self.get_value()
self.model = gtk.ListStore(gobject.TYPE_STRING, gobject.TYPE_STRING) self.model = gtk.ListStore(gobject.TYPE_STRING, gobject.TYPE_STRING)
self.treeView = gtk.TreeView(self.model) self.tree_view = gtk.TreeView(self.model)
self.treeView.set_size_request(150, 150) self.tree_view.set_size_request(150, 150)
col1 = gtk.TreeViewColumn(_('Name' ), gtk.CellRendererText(), text=0) col1 = gtk.TreeViewColumn(_('Name' ), gtk.CellRendererText(), text=0)
col2 = gtk.TreeViewColumn(_('ID' ), gtk.CellRendererText(), text=1) col2 = gtk.TreeViewColumn(_('ID' ), gtk.CellRendererText(), text=1)
col1.set_resizable(True) col1.set_resizable(True)
@ -769,27 +796,31 @@ class PersonListOption(Option):
col2.set_sizing(gtk.TREE_VIEW_COLUMN_AUTOSIZE) col2.set_sizing(gtk.TREE_VIEW_COLUMN_AUTOSIZE)
col1.set_sort_column_id(0) col1.set_sort_column_id(0)
col2.set_sort_column_id(1) col2.set_sort_column_id(1)
self.treeView.append_column(col1) self.tree_view.append_column(col1)
self.treeView.append_column(col2) self.tree_view.append_column(col2)
self.scrolledWindow = gtk.ScrolledWindow() self.scrolled_window = gtk.ScrolledWindow()
self.scrolledWindow.add(self.treeView) self.scrolled_window.add(self.tree_view)
self.scrolledWindow.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC) self.scrolled_window.set_policy(gtk.POLICY_AUTOMATIC,
self.scrolledWindow.set_shadow_type(gtk.SHADOW_OUT) gtk.POLICY_AUTOMATIC)
self.scrolled_window.set_shadow_type(gtk.SHADOW_OUT)
self.hbox = gtk.HBox() self.hbox = gtk.HBox()
self.hbox.pack_start(self.scrolledWindow, expand=True, fill=True) self.hbox.pack_start(self.scrolled_window, expand=True, fill=True)
for gid in value.split(): for gid in value.split():
person = self.db.get_person_from_gramps_id(gid) person = self.__db.get_person_from_gramps_id(gid)
if person: if person:
name = _nd.display(person) name = _nd.display(person)
self.model.append([name, gid]) self.model.append([name, gid])
# now setup the '+' and '-' pushbutton for adding/removing people from the container # now setup the '+' and '-' pushbutton for adding/removing people from
self.addPerson = GrampsWidgets.SimpleButton(gtk.STOCK_ADD, self.addPersonClicked) # the container
self.delPerson = GrampsWidgets.SimpleButton(gtk.STOCK_REMOVE, self.delPersonClicked) self.add_person = GrampsWidgets.SimpleButton(gtk.STOCK_ADD,
self.add_person_clicked)
self.del_person = GrampsWidgets.SimpleButton(gtk.STOCK_REMOVE,
self.del_person_clicked)
self.vbbox = gtk.VButtonBox() self.vbbox = gtk.VButtonBox()
self.vbbox.add(self.addPerson) self.vbbox.add(self.add_person)
self.vbbox.add(self.delPerson) self.vbbox.add(self.del_person)
self.vbbox.set_layout(gtk.BUTTONBOX_SPREAD) self.vbbox.set_layout(gtk.BUTTONBOX_SPREAD)
self.hbox.pack_end(self.vbbox, expand=False) self.hbox.pack_end(self.vbbox, expand=False)
@ -801,26 +832,30 @@ class PersonListOption(Option):
Parse the object and return. Parse the object and return.
""" """
gidlist = '' gidlist = ''
iter = self.model.get_iter_first() i = self.model.get_iter_first()
while (iter): while (i):
gid = self.model.get_value(iter, 1) gid = self.model.get_value(i, 1)
gidlist = gidlist + gid + ' ' gidlist = gidlist + gid + ' '
iter = self.model.iter_next(iter) i = self.model.iter_next(i)
return gidlist return gidlist
def addPersonClicked(self, obj): def add_person_clicked(self, obj):
"""
Handle the add person button.
"""
# people we already have must be excluded # people we already have must be excluded
# so we don't list them multiple times # so we don't list them multiple times
skipList = set() skip_list = set()
iter = self.model.get_iter_first() i = self.model.get_iter_first()
while (iter): while (i):
gid = self.model.get_value(iter, 1) # get the GID stored in column #1 gid = self.model.get_value(i, 1) # get the GID stored in column #1
person = self.db.get_person_from_gramps_id(gid) person = self.__db.get_person_from_gramps_id(gid)
skipList.add(person.get_handle()) skip_list.add(person.get_handle())
iter = self.model.iter_next(iter) i = self.model.iter_next(i)
SelectPerson = selector_factory('Person') select_class = selector_factory('Person')
sel = SelectPerson(self.dbstate, self.dialog.uistate, self.dialog.track, skip=skipList) sel = select_class(self.__dbstate, self.dialog.uistate,
self.dialog.track, skip=skip_list)
person = sel.run() person = sel.run()
if person: if person:
name = _nd.display(person) name = _nd.display(person)
@ -832,37 +867,42 @@ class PersonListOption(Option):
# #
# NOTE: we may want to make this an optional thing, determined # NOTE: we may want to make this an optional thing, determined
# by the use of a parameter at the time this class is instatiated # by the use of a parameter at the time this class is instatiated
familyList = person.get_family_handle_list() family_list = person.get_family_handle_list()
if familyList: for family_handle in family_list:
for familyHandle in familyList: family = self.__db.get_family_from_handle(family_handle)
family = self.db.get_family_from_handle(familyHandle)
if person.get_handle() == family.get_father_handle(): if person.get_handle() == family.get_father_handle():
spouseHandle = family.get_mother_handle() spouse_handle = family.get_mother_handle()
else: else:
spouseHandle = family.get_father_handle() spouse_handle = family.get_father_handle()
if spouseHandle: if spouse_handle and (spouse_handle not in skip_list):
if spouseHandle not in skipList: spouse = self.__db.get_person_from_handle(
import gtk spouse_handle)
spouse = self.db.get_person_from_handle(spouseHandle) spouse_name = _nd.display(spouse)
text = _('Also include %s?') % spouse.get_primary_name().get_regular_name() text = _('Also include %s?') % spouse_name
prompt = gtk.MessageDialog(parent=self.dialog.window, flags=gtk.DIALOG_MODAL, type=gtk.MESSAGE_QUESTION, buttons=gtk.BUTTONS_YES_NO, message_format=text) prompt = gtk.MessageDialog(parent=self.dialog.window,
flags=gtk.DIALOG_MODAL,
type=gtk.MESSAGE_QUESTION,
buttons=gtk.BUTTONS_YES_NO,
message_format=text)
prompt.set_default_response(gtk.RESPONSE_YES) prompt.set_default_response(gtk.RESPONSE_YES)
prompt.set_position(gtk.WIN_POS_CENTER_ON_PARENT) prompt.set_position(gtk.WIN_POS_CENTER_ON_PARENT)
prompt.set_title(_('Select Person')) prompt.set_title(_('Select Person'))
button = prompt.run() button = prompt.run()
prompt.destroy() prompt.destroy()
if button == gtk.RESPONSE_YES: if button == gtk.RESPONSE_YES:
name = _nd.display(spouse)
gid = spouse.get_gramps_id() gid = spouse.get_gramps_id()
self.model.append([name, gid]) self.model.append([spouse_name, gid])
def delPersonClicked(self, obj): def del_person_clicked(self, obj):
(path, column) = self.treeView.get_cursor() """
Handle the delete person button.
"""
(path, column) = self.tree_view.get_cursor()
if (path): if (path):
iter = self.model.get_iter(path) i = self.model.get_iter(path)
self.model.remove(iter) self.model.remove(i)
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
# #
@ -885,11 +925,11 @@ class SurnameColourOption(Option):
@type value: string @type value: string
@return: nothing @return: nothing
""" """
self.db = dbstate.get_database() self.__db = dbstate.get_database()
self.dbstate = dbstate self.__dbstate = dbstate
Option.__init__(self, label, value) Option.__init__(self, label, value)
def make_gui_obj(self, gtk, dialog): def make_gui_obj(self, dialog):
""" """
Add a "surname-colour" widget to the dialog. Add a "surname-colour" widget to the dialog.
""" """
@ -897,9 +937,9 @@ class SurnameColourOption(Option):
self.surnames = {} # list of surnames and count self.surnames = {} # list of surnames and count
self.model = gtk.ListStore(gobject.TYPE_STRING, gobject.TYPE_STRING) self.model = gtk.ListStore(gobject.TYPE_STRING, gobject.TYPE_STRING)
self.treeView = gtk.TreeView(self.model) self.tree_view = gtk.TreeView(self.model)
self.treeView.set_size_request(150, 150) self.tree_view.set_size_request(150, 150)
self.treeView.connect('row-activated', self.clicked) self.tree_view.connect('row-activated', self.row_clicked)
col1 = gtk.TreeViewColumn(_('Surname'), gtk.CellRendererText(), text=0) col1 = gtk.TreeViewColumn(_('Surname'), gtk.CellRendererText(), text=0)
col2 = gtk.TreeViewColumn(_('Colour'), gtk.CellRendererText(), text=1) col2 = gtk.TreeViewColumn(_('Colour'), gtk.CellRendererText(), text=1)
col1.set_resizable(True) col1.set_resizable(True)
@ -907,20 +947,23 @@ class SurnameColourOption(Option):
col1.set_sort_column_id(0) col1.set_sort_column_id(0)
col1.set_sizing(gtk.TREE_VIEW_COLUMN_AUTOSIZE) col1.set_sizing(gtk.TREE_VIEW_COLUMN_AUTOSIZE)
col2.set_sizing(gtk.TREE_VIEW_COLUMN_AUTOSIZE) col2.set_sizing(gtk.TREE_VIEW_COLUMN_AUTOSIZE)
self.treeView.append_column(col1) self.tree_view.append_column(col1)
self.treeView.append_column(col2) self.tree_view.append_column(col2)
self.scrolledWindow = gtk.ScrolledWindow() self.scrolled_window = gtk.ScrolledWindow()
self.scrolledWindow.add(self.treeView) self.scrolled_window.add(self.tree_view)
self.scrolledWindow.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC) self.scrolled_window.set_policy(gtk.POLICY_AUTOMATIC,
self.scrolledWindow.set_shadow_type(gtk.SHADOW_OUT) gtk.POLICY_AUTOMATIC)
self.scrolled_window.set_shadow_type(gtk.SHADOW_OUT)
self.hbox = gtk.HBox() self.hbox = gtk.HBox()
self.hbox.pack_start(self.scrolledWindow, expand=True, fill=True) self.hbox.pack_start(self.scrolled_window, expand=True, fill=True)
self.addSurname = GrampsWidgets.SimpleButton(gtk.STOCK_ADD, self.addSurnameClicked) self.add_surname = GrampsWidgets.SimpleButton(gtk.STOCK_ADD,
self.delSurname = GrampsWidgets.SimpleButton(gtk.STOCK_REMOVE, self.delSurnameClicked) self.add_surname_clicked)
self.del_surname = GrampsWidgets.SimpleButton(gtk.STOCK_REMOVE,
self.del_surname_clicked)
self.vbbox = gtk.VButtonBox() self.vbbox = gtk.VButtonBox()
self.vbbox.add(self.addSurname) self.vbbox.add(self.add_surname)
self.vbbox.add(self.delSurname) self.vbbox.add(self.del_surname)
self.vbbox.set_layout(gtk.BUTTONBOX_SPREAD) self.vbbox.set_layout(gtk.BUTTONBOX_SPREAD)
self.hbox.pack_end(self.vbbox, expand=False) self.hbox.pack_end(self.vbbox, expand=False)
@ -938,56 +981,69 @@ class SurnameColourOption(Option):
""" """
Parse the object and return. Parse the object and return.
""" """
surnameColours = '' surname_colours = ''
iter = self.model.get_iter_first() i = self.model.get_iter_first()
while (iter): while (i):
surname = self.model.get_value(iter, 0) # .encode('iso-8859-1','xmlcharrefreplace') surname = self.model.get_value(i, 0)
colour = self.model.get_value(iter, 1) #surname = surname.encode('iso-8859-1','xmlcharrefreplace')
colour = self.model.get_value(i, 1)
# tried to use a dictionary, and tried to save it as a tuple, # tried to use a dictionary, and tried to save it as a tuple,
# but coulnd't get this to work right -- this is lame, but now # but coulnd't get this to work right -- this is lame, but now
# the surnames and colours are saved as a plain text string # the surnames and colours are saved as a plain text string
surnameColours += surname + ' ' + colour + ' ' surname_colours += surname + ' ' + colour + ' '
iter = self.model.iter_next(iter) i = self.model.iter_next(i)
return surnameColours return surname_colours
def clicked(self, treeview, path, column): def row_clicked(self, treeview, path, column):
"""
Handle the case of a row being clicked on.
"""
# get the surname and colour value for this family # get the surname and colour value for this family
iter = self.model.get_iter(path) i = self.model.get_iter(path)
surname = self.model.get_value(iter, 0) surname = self.model.get_value(i, 0)
colour = gtk.gdk.color_parse(self.model.get_value(iter, 1)) colour = gtk.gdk.color_parse(self.model.get_value(i, 1))
colourDialog = gtk.ColorSelectionDialog('Select colour for %s' % surname) title = 'Select colour for %s' % surname
colourDialog.colorsel.set_current_color(colour) colour_dialog = gtk.ColorSelectionDialog(title)
response = colourDialog.run() colorsel = colour_dialog.colorsel
colorsel.set_current_color(colour)
response = colour_dialog.run()
if response == gtk.RESPONSE_OK: if response == gtk.RESPONSE_OK:
colour = colourDialog.colorsel.get_current_color() colour = colorsel.get_current_color()
colourName = '#%02x%02x%02x' % ( colour_name = '#%02x%02x%02x' % (
int(colour.red *256/65536), int(colour.red *256/65536),
int(colour.green*256/65536), int(colour.green*256/65536),
int(colour.blue *256/65536)) int(colour.blue *256/65536))
self.model.set_value(iter, 1, colourName) self.model.set_value(i, 1, colour_name)
colourDialog.destroy() colour_dialog.destroy()
def addSurnameClicked(self, obj): def add_surname_clicked(self, obj):
skipList = set() """
iter = self.model.get_iter_first() Handle the the add surname button.
while (iter): """
surname = self.model.get_value(iter, 0) skip_list = set()
skipList.add(surname.encode('iso-8859-1','xmlcharrefreplace')) i = self.model.get_iter_first()
iter = self.model.iter_next(iter) while (i):
surname = self.model.get_value(i, 0)
skip_list.add(surname.encode('iso-8859-1','xmlcharrefreplace'))
i = self.model.iter_next(i)
ln = LastNameDialog(self.db, self.dialog.uistate, self.dialog.track, self.surnames, skipList) ln_dialog = LastNameDialog(self.__db, self.dialog.uistate,
surnameSet = ln.run() self.dialog.track, self.surnames, skip_list)
for surname in surnameSet: surname_set = ln_dialog.run()
for surname in surname_set:
self.model.append([surname, '#ffffff']) self.model.append([surname, '#ffffff'])
def delSurnameClicked(self, obj): def del_surname_clicked(self, obj):
(path, column) = self.treeView.get_cursor() """
Handle the the delete surname button.
"""
(path, column) = self.tree_view.get_cursor()
if (path): if (path):
iter = self.model.get_iter(path) i = self.model.get_iter(path)
self.model.remove(iter) self.model.remove(i)
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
# #
@ -1100,12 +1156,19 @@ class Menu:
# #
#------------------------------------------------------------------------ #------------------------------------------------------------------------
class MenuOptions: class MenuOptions:
"""
Introduction
============
A MenuOptions is used to implement the necessary funtions for adding
options to a dialog.
"""
def __init__(self, dbstate): def __init__(self, dbstate):
self.menu = Menu() self.menu = Menu()
# Fill options_dict with report/tool defaults: # Fill options_dict with report/tool defaults:
self.options_dict = {} self.options_dict = {}
self.options_help = {} self.options_help = {}
self.tooltips = gtk.Tooltips()
self.add_menu_options(self.menu, dbstate) self.add_menu_options(self.menu, dbstate)
for name in self.menu.get_all_option_names(): for name in self.menu.get_all_option_names():
option = self.menu.get_option_by_name(name) option = self.menu.get_option_by_name(name)
@ -1113,6 +1176,9 @@ class MenuOptions:
self.options_help[name] = option.get_help() self.options_help[name] = option.get_help()
def make_default_style(self, default_style): def make_default_style(self, default_style):
"""
This function is currently required by some reports.
"""
pass pass
def add_menu_options(self, menu, dbstate): def add_menu_options(self, menu, dbstate):
@ -1126,6 +1192,9 @@ class MenuOptions:
raise NotImplementedError raise NotImplementedError
def add_menu_option(self, category, name, option): def add_menu_option(self, category, name, option):
"""
Add a single option to the menu.
"""
self.menu.add_option(category, name, option) self.menu.add_option(category, name, option)
self.options_dict[name] = option.get_value() self.options_dict[name] = option.get_value()
self.options_help[name] = option.get_help() self.options_help[name] = option.get_help()
@ -1134,15 +1203,13 @@ class MenuOptions:
""" """
Generic method to add user options to the gui. Generic method to add user options to the gui.
""" """
import gtk
self.tooltips = gtk.Tooltips()
for category in self.menu.get_categories(): for category in self.menu.get_categories():
for name in self.menu.get_option_names(category): for name in self.menu.get_option_names(category):
option = self.menu.get_option(category, name) option = self.menu.get_option(category, name)
# override option default with xml-saved value: # override option default with xml-saved value:
if name in self.options_dict: if name in self.options_dict:
option.set_value(self.options_dict[name]) option.set_value(self.options_dict[name])
option.make_gui_obj(gtk, dialog) option.make_gui_obj(dialog)
option.add_dialog_category(dialog, category) option.add_dialog_category(dialog, category)
option.add_tooltip(self.tooltips) option.add_tooltip(self.tooltips)