2007-12-21 05:48:39 +05:30
|
|
|
#
|
|
|
|
# Gramps - a GTK+/GNOME based genealogy program
|
|
|
|
#
|
2008-01-02 18:50:26 +05:30
|
|
|
# Copyright (C) 2008 Donald N. Allingham
|
2007-12-21 05:48:39 +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
|
|
|
|
#
|
|
|
|
|
|
|
|
"""
|
|
|
|
Provides a simplified table creation interface
|
|
|
|
"""
|
|
|
|
|
|
|
|
import copy
|
2007-12-30 01:25:27 +05:30
|
|
|
|
2007-12-21 11:52:46 +05:30
|
|
|
import gen.lib
|
|
|
|
import Errors
|
2007-12-30 01:25:27 +05:30
|
|
|
import DateHandler
|
2007-12-21 05:48:39 +05:30
|
|
|
|
|
|
|
class SimpleTable:
|
|
|
|
"""
|
|
|
|
Provides a simplified table creation interface.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, access, doc, title=None):
|
|
|
|
"""
|
2007-12-21 11:52:46 +05:30
|
|
|
Initializes the class with a simpledb, and simpledoc
|
2007-12-21 05:48:39 +05:30
|
|
|
"""
|
|
|
|
self.access = access
|
2007-12-21 11:52:46 +05:30
|
|
|
self.simpledoc = doc # simpledoc; simpledoc.doc = docgen object
|
2007-12-21 05:48:39 +05:30
|
|
|
self.title = title
|
|
|
|
self.__columns = []
|
|
|
|
self.__rows = []
|
2007-12-21 11:52:46 +05:30
|
|
|
self.__link = []
|
2007-12-21 05:48:39 +05:30
|
|
|
self.__sort_col = None
|
|
|
|
self.__sort_reverse = False
|
|
|
|
|
2007-12-21 23:50:04 +05:30
|
|
|
def get_row_count(self):
|
|
|
|
return len(self.__rows)
|
|
|
|
|
2007-12-21 05:48:39 +05:30
|
|
|
def columns(self, *columns):
|
2007-12-21 11:52:46 +05:30
|
|
|
"""
|
|
|
|
Set the columns
|
|
|
|
"""
|
2007-12-21 05:48:39 +05:30
|
|
|
self.__columns = list(copy.copy(columns))
|
2008-01-03 19:48:18 +05:30
|
|
|
self.__sort_vals = [[] for i in range(len(self.__columns))]
|
2007-12-21 05:48:39 +05:30
|
|
|
|
2007-12-21 11:52:46 +05:30
|
|
|
def on_table_doubleclick(self, obj, path, view_column):
|
|
|
|
"""
|
|
|
|
Handle events on tables. obj is a treeview
|
|
|
|
"""
|
2008-01-03 19:48:18 +05:30
|
|
|
from QuickReports import run_quick_report_by_name
|
2008-01-02 18:50:26 +05:30
|
|
|
from Editors import (EditPerson, EditEvent, EditFamily, EditSource,
|
|
|
|
EditPlace, EditRepository)
|
2007-12-21 11:52:46 +05:30
|
|
|
selection = obj.get_selection()
|
|
|
|
store, node = selection.get_selected()
|
|
|
|
if not node:
|
|
|
|
return
|
|
|
|
index = store.get_value(node, 0) # index
|
|
|
|
if self.__link[index]:
|
2008-01-02 18:50:26 +05:30
|
|
|
objclass, handle = self.__link[index]
|
|
|
|
if objclass == 'Person':
|
2007-12-21 11:52:46 +05:30
|
|
|
person = self.access.dbase.get_person_from_handle(handle)
|
|
|
|
try:
|
|
|
|
EditPerson(self.simpledoc.doc.dbstate,
|
|
|
|
self.simpledoc.doc.uistate, [], person)
|
|
|
|
return True # handled event
|
|
|
|
except Errors.WindowActiveError:
|
|
|
|
pass
|
2008-01-02 18:50:26 +05:30
|
|
|
elif objclass == 'Event':
|
2007-12-21 11:52:46 +05:30
|
|
|
event = self.access.dbase.get_event_from_handle(handle)
|
|
|
|
try:
|
|
|
|
EditEvent(self.simpledoc.doc.dbstate,
|
|
|
|
self.simpledoc.doc.uistate, [], event)
|
|
|
|
return True # handled event
|
|
|
|
except Errors.WindowActiveError:
|
|
|
|
pass
|
2008-01-02 18:50:26 +05:30
|
|
|
elif objclass == 'Family':
|
|
|
|
ref = self.access.dbase.get_family_from_handle(handle)
|
|
|
|
try:
|
|
|
|
EditFamily(self.simpledoc.doc.dbstate,
|
|
|
|
self.simpledoc.doc.uistate, [], ref)
|
|
|
|
return True # handled event
|
|
|
|
except Errors.WindowActiveError:
|
|
|
|
pass
|
|
|
|
elif objclass == 'Source':
|
|
|
|
ref = self.access.dbase.get_source_from_handle(handle)
|
|
|
|
try:
|
|
|
|
EditSource(self.simpledoc.doc.dbstate,
|
|
|
|
self.simpledoc.doc.uistate, [], ref)
|
|
|
|
return True # handled event
|
|
|
|
except Errors.WindowActiveError:
|
|
|
|
pass
|
|
|
|
elif objclass == 'Place':
|
|
|
|
ref = self.access.dbase.get_place_from_handle(handle)
|
|
|
|
try:
|
|
|
|
EditPlace(self.simpledoc.doc.dbstate,
|
|
|
|
self.simpledoc.doc.uistate, [], ref)
|
|
|
|
return True # handled event
|
|
|
|
except Errors.WindowActiveError:
|
|
|
|
pass
|
|
|
|
elif objclass == 'Repository':
|
|
|
|
ref = self.access.dbase.get_repository_from_handle(handle)
|
|
|
|
try:
|
|
|
|
EditRepository(self.simpledoc.doc.dbstate,
|
|
|
|
self.simpledoc.doc.uistate, [], ref)
|
|
|
|
return True # handled event
|
|
|
|
except Errors.WindowActiveError:
|
|
|
|
pass
|
|
|
|
elif objclass == 'Date':
|
|
|
|
run_quick_report_by_name(self.gui.dbstate,
|
|
|
|
self.gui.uistate,
|
|
|
|
'onthisday',
|
|
|
|
date)
|
2007-12-21 11:52:46 +05:30
|
|
|
return False # didn't handle event
|
|
|
|
|
|
|
|
def on_table_click(self, obj):
|
|
|
|
"""
|
|
|
|
Handle events on tables. obj is a treeview
|
|
|
|
"""
|
|
|
|
selection = obj.get_selection()
|
|
|
|
store, node = selection.get_selected()
|
|
|
|
if not node:
|
|
|
|
return
|
|
|
|
index = store.get_value(node, 0) # index
|
|
|
|
if self.__link[index]:
|
2008-01-02 18:50:26 +05:30
|
|
|
objclass, handle = self.__link[index]
|
|
|
|
if objclass == 'Person':
|
2007-12-21 11:52:46 +05:30
|
|
|
person = self.access.dbase.get_person_from_handle(handle)
|
|
|
|
self.simpledoc.doc.dbstate.change_active_person(person)
|
|
|
|
return True
|
|
|
|
return False # didn't handle event
|
|
|
|
|
2008-01-03 19:48:18 +05:30
|
|
|
def row_sort_val(self, col, val):
|
|
|
|
"""
|
|
|
|
Adds a row of data to sort by.
|
|
|
|
"""
|
|
|
|
self.__sort_vals[col].append(val)
|
|
|
|
|
2007-12-21 05:48:39 +05:30
|
|
|
def row(self, *data):
|
2007-12-21 11:52:46 +05:30
|
|
|
"""
|
|
|
|
Add a row of data.
|
|
|
|
"""
|
|
|
|
retval = []
|
|
|
|
link = None
|
|
|
|
for item in data:
|
2008-01-02 18:50:26 +05:30
|
|
|
# FIXME: add better text representations of these objects
|
2007-12-21 11:52:46 +05:30
|
|
|
if type(item) in [str, unicode]:
|
|
|
|
retval.append(item)
|
|
|
|
elif isinstance(item, gen.lib.Person):
|
|
|
|
name = self.access.name(item)
|
|
|
|
retval.append(name)
|
|
|
|
link = ('Person', item.handle)
|
2007-12-30 21:09:00 +05:30
|
|
|
elif isinstance(item, gen.lib.Family):
|
2008-01-02 18:50:26 +05:30
|
|
|
father = self.access.father(item)
|
|
|
|
mother = self.access.mother(item)
|
|
|
|
text = ""
|
|
|
|
if father:
|
|
|
|
text += " " + self.access.name(father)
|
|
|
|
else:
|
|
|
|
text += " " + _("Unknown father")
|
|
|
|
text += " and"
|
|
|
|
if mother:
|
|
|
|
text += " " + self.access.name(mother)
|
|
|
|
else:
|
|
|
|
text += " " + _("Unknown mother")
|
|
|
|
retval.append(text)
|
|
|
|
link = ('Family', item.handle)
|
2007-12-30 21:09:00 +05:30
|
|
|
elif isinstance(item, gen.lib.Source):
|
|
|
|
retval.append(_('Source'))
|
2008-01-02 18:50:26 +05:30
|
|
|
link = ('Souce', item.handle)
|
2007-12-21 11:52:46 +05:30
|
|
|
elif isinstance(item, gen.lib.Event):
|
|
|
|
name = self.access.event_type(item)
|
|
|
|
retval.append(name)
|
|
|
|
link = ('Event', item.handle)
|
2007-12-30 21:09:00 +05:30
|
|
|
elif isinstance(item, gen.lib.MediaObject):
|
|
|
|
retval.append(_('Media'))
|
2008-01-02 18:50:26 +05:30
|
|
|
link = ('Media', item.handle)
|
2007-12-30 21:09:00 +05:30
|
|
|
elif isinstance(item, gen.lib.Place):
|
|
|
|
retval.append(_('Place'))
|
2008-01-02 18:50:26 +05:30
|
|
|
link = ('Place', item.handle)
|
2007-12-30 21:09:00 +05:30
|
|
|
elif isinstance(item, gen.lib.Repository):
|
|
|
|
retval.append(_('Repository'))
|
2008-01-02 18:50:26 +05:30
|
|
|
link = ('Repository', item.handle)
|
2007-12-30 21:09:00 +05:30
|
|
|
elif isinstance(item, gen.lib.Note):
|
|
|
|
retval.append(_('Note'))
|
2008-01-02 18:50:26 +05:30
|
|
|
link = ('Note', item.handle)
|
2007-12-30 01:25:27 +05:30
|
|
|
elif isinstance(item, gen.lib.Date):
|
|
|
|
text = DateHandler.displayer.display(item)
|
|
|
|
retval.append(text)
|
2008-01-02 18:50:26 +05:30
|
|
|
link = ('Date', item)
|
2007-12-21 11:52:46 +05:30
|
|
|
else:
|
|
|
|
raise AttributeError, ("unknown object type: '%s': %s" %
|
|
|
|
(item, type(item)))
|
|
|
|
self.__link.append(link)
|
|
|
|
self.__rows.append(retval)
|
2007-12-21 05:48:39 +05:30
|
|
|
|
|
|
|
def sort(self, column_name, reverse=False):
|
|
|
|
self.__sort_col = column_name
|
|
|
|
self.__sort_reverse = reverse
|
|
|
|
|
|
|
|
def __sort(self):
|
|
|
|
idx = self.__columns.index(self.__sort_col)
|
|
|
|
if self.__sort_reverse:
|
|
|
|
self.__rows.sort(lambda a, b: -cmp(a[idx],b[idx]))
|
|
|
|
else:
|
|
|
|
self.__rows.sort(lambda a, b: cmp(a[idx],b[idx]))
|
|
|
|
|
|
|
|
def write(self):
|
2007-12-21 11:52:46 +05:30
|
|
|
if self.simpledoc.doc.type == "standard":
|
|
|
|
doc = self.simpledoc.doc
|
|
|
|
doc.start_table('simple','Table')
|
2007-12-21 05:48:39 +05:30
|
|
|
columns = len(self.__columns)
|
|
|
|
if self.title:
|
2007-12-21 11:52:46 +05:30
|
|
|
doc.start_row()
|
|
|
|
doc.start_cell('TableHead',columns)
|
|
|
|
doc.start_paragraph('TableTitle')
|
|
|
|
doc.write_text(_(self.title))
|
|
|
|
doc.end_paragraph()
|
|
|
|
doc.end_cell()
|
|
|
|
doc.end_row()
|
2007-12-21 05:48:39 +05:30
|
|
|
if self.__sort_col:
|
|
|
|
self.__sort()
|
2007-12-21 11:52:46 +05:30
|
|
|
doc.start_row()
|
2007-12-21 05:48:39 +05:30
|
|
|
for col in self.__columns:
|
2007-12-21 11:52:46 +05:30
|
|
|
doc.start_cell('TableNormalCell',1)
|
|
|
|
doc.write_text(col,'TableTitle')
|
|
|
|
doc.end_cell()
|
|
|
|
doc.end_row()
|
2007-12-21 05:48:39 +05:30
|
|
|
for row in self.__rows:
|
2007-12-21 11:52:46 +05:30
|
|
|
doc.start_row()
|
2007-12-21 05:48:39 +05:30
|
|
|
for col in row:
|
2007-12-21 11:52:46 +05:30
|
|
|
doc.start_cell('TableNormalCell',1)
|
|
|
|
doc.write_text(col,'Normal')
|
|
|
|
doc.end_cell()
|
|
|
|
doc.end_row()
|
|
|
|
doc.end_table()
|
|
|
|
doc.start_paragraph("Normal")
|
|
|
|
doc.end_paragraph()
|
|
|
|
elif self.simpledoc.doc.type == "gtk":
|
2007-12-21 05:48:39 +05:30
|
|
|
import gtk
|
2007-12-21 11:52:46 +05:30
|
|
|
buffer = self.simpledoc.doc.buffer
|
|
|
|
text_view = self.simpledoc.doc.text_view
|
|
|
|
model_index = 1 # start after index
|
2007-12-21 05:48:39 +05:30
|
|
|
if self.__sort_col:
|
|
|
|
sort_index = self.__columns.index(self.__sort_col)
|
|
|
|
else:
|
|
|
|
sort_index = 0
|
|
|
|
treeview = gtk.TreeView()
|
|
|
|
treeview.set_grid_lines(gtk.TREE_VIEW_GRID_LINES_BOTH)
|
2007-12-21 11:52:46 +05:30
|
|
|
treeview.connect('row-activated', self.on_table_doubleclick)
|
|
|
|
treeview.connect('cursor-changed', self.on_table_click)
|
2007-12-21 05:48:39 +05:30
|
|
|
renderer = gtk.CellRendererText()
|
2007-12-21 11:52:46 +05:30
|
|
|
types = [int] # index
|
2008-01-03 19:48:18 +05:30
|
|
|
cnt = 0
|
|
|
|
sort_data = []
|
|
|
|
sort_data_types = []
|
2007-12-21 05:48:39 +05:30
|
|
|
for col in self.__columns:
|
|
|
|
types.append(type(col))
|
|
|
|
column = gtk.TreeViewColumn(col,renderer,text=model_index)
|
2008-01-03 19:48:18 +05:30
|
|
|
if self.__sort_vals[cnt] != []:
|
|
|
|
sort_data.append(self.__sort_vals[cnt])
|
|
|
|
column.set_sort_column_id(len(self.__columns) +
|
|
|
|
len(sort_data))
|
|
|
|
sort_data_types.append(int)
|
|
|
|
else:
|
|
|
|
column.set_sort_column_id(model_index)
|
2007-12-21 05:48:39 +05:30
|
|
|
treeview.append_column(column)
|
|
|
|
#if model_index == sort_index:
|
|
|
|
# FIXME: what to set here?
|
|
|
|
model_index += 1
|
2008-01-03 19:48:18 +05:30
|
|
|
cnt += 1
|
2007-12-21 23:50:04 +05:30
|
|
|
if self.title:
|
|
|
|
self.simpledoc.paragraph(self.title)
|
2007-12-21 11:52:46 +05:30
|
|
|
# Make a GUI to put the tree view in
|
2008-01-03 19:48:18 +05:30
|
|
|
types += sort_data_types
|
2007-12-21 05:48:39 +05:30
|
|
|
model = gtk.ListStore(*types)
|
|
|
|
treeview.set_model(model)
|
|
|
|
iter = buffer.get_end_iter()
|
|
|
|
anchor = buffer.create_child_anchor(iter)
|
2008-01-05 08:32:56 +05:30
|
|
|
text_view.add_child_at_anchor(treeview, anchor)
|
2007-12-21 11:52:46 +05:30
|
|
|
count = 0
|
2007-12-21 05:48:39 +05:30
|
|
|
for data in self.__rows:
|
2008-01-03 19:48:18 +05:30
|
|
|
model.append(row=([count] + list(data) + [col[count] for col in sort_data]))
|
2007-12-21 11:52:46 +05:30
|
|
|
count += 1
|
2008-01-05 08:32:56 +05:30
|
|
|
text_view.show_all()
|
2007-12-21 11:52:46 +05:30
|
|
|
self.simpledoc.paragraph("")
|
2007-12-21 23:50:04 +05:30
|
|
|
self.simpledoc.paragraph("")
|