2002-10-25 04:52:51 +00:00
|
|
|
#
|
|
|
|
# Gramps - a GTK+/GNOME based genealogy program
|
|
|
|
#
|
|
|
|
# Copyright (C) 2000 Donald N. Allingham
|
|
|
|
#
|
|
|
|
# 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
|
|
|
|
#
|
|
|
|
|
2002-11-14 05:31:42 +00:00
|
|
|
from gobject import TYPE_STRING, TYPE_PYOBJECT
|
2002-10-25 04:52:51 +00:00
|
|
|
import gtk
|
|
|
|
|
|
|
|
class ListModel:
|
2002-12-14 05:07:09 +00:00
|
|
|
def __init__(self,tree,dlist,select_func=None,event_func=None,mode=gtk.SELECTION_SINGLE):
|
2002-10-25 04:52:51 +00:00
|
|
|
self.tree = tree
|
|
|
|
l = len(dlist)
|
2002-11-17 05:06:43 +00:00
|
|
|
self.mylist = [TYPE_STRING]*l + [TYPE_PYOBJECT]
|
|
|
|
|
2002-11-25 04:30:36 +00:00
|
|
|
self.tree.set_rules_hint(gtk.TRUE)
|
2002-11-17 05:06:43 +00:00
|
|
|
self.new_model()
|
2002-10-25 04:52:51 +00:00
|
|
|
self.selection = self.tree.get_selection()
|
2002-12-14 05:07:09 +00:00
|
|
|
self.selection.set_mode(mode)
|
2002-11-14 05:31:42 +00:00
|
|
|
|
|
|
|
self.data_index = l
|
2002-12-06 03:37:31 +00:00
|
|
|
|
|
|
|
self.cids = []
|
2002-10-25 04:52:51 +00:00
|
|
|
|
|
|
|
cnum = 0
|
|
|
|
for name in dlist:
|
|
|
|
renderer = gtk.CellRendererText()
|
|
|
|
column = gtk.TreeViewColumn(name[0],renderer,text=cnum)
|
2002-11-10 00:38:58 +00:00
|
|
|
column.set_min_width(name[2])
|
2002-11-02 21:19:58 +00:00
|
|
|
if name[0] == '':
|
|
|
|
column.set_visible(gtk.FALSE)
|
|
|
|
else:
|
|
|
|
column.set_resizable(gtk.TRUE)
|
2002-11-25 04:30:36 +00:00
|
|
|
if name[1] == -1:
|
|
|
|
column.set_clickable(gtk.FALSE)
|
|
|
|
else:
|
|
|
|
column.set_clickable(gtk.TRUE)
|
2002-12-06 03:37:31 +00:00
|
|
|
column.set_sort_column_id(name[1])
|
|
|
|
|
2002-10-25 04:52:51 +00:00
|
|
|
cnum = cnum + 1
|
2002-12-06 03:37:31 +00:00
|
|
|
self.cids.append(name[1])
|
2002-11-14 05:31:42 +00:00
|
|
|
if name[1] != -1:
|
2002-12-06 03:37:31 +00:00
|
|
|
self.tree.append_column(column)
|
2002-12-11 05:18:47 +00:00
|
|
|
|
|
|
|
if self.cids[0] > 0:
|
|
|
|
self.model.set_sort_column_id(self.cids[0],gtk.SORT_ASCENDING)
|
2002-11-19 04:15:02 +00:00
|
|
|
self.connect_model()
|
|
|
|
|
2002-11-10 00:38:58 +00:00
|
|
|
if select_func:
|
|
|
|
self.selection.connect('changed',select_func)
|
|
|
|
if event_func:
|
2002-11-14 05:31:42 +00:00
|
|
|
self.double_click = event_func
|
|
|
|
self.tree.connect('event',self.button_press)
|
|
|
|
|
2002-11-17 05:06:43 +00:00
|
|
|
def new_model(self):
|
|
|
|
self.model = gtk.ListStore(*self.mylist)
|
|
|
|
|
|
|
|
def connect_model(self):
|
|
|
|
self.tree.set_model(self.model)
|
2002-12-06 03:37:31 +00:00
|
|
|
self.sort()
|
2002-12-04 04:58:07 +00:00
|
|
|
|
|
|
|
def sort(self):
|
|
|
|
val = self.model.get_sort_column_id()
|
2002-12-06 03:37:31 +00:00
|
|
|
col = val[0]
|
|
|
|
if col > 0:
|
|
|
|
self.model.set_sort_column_id(col,val[1])
|
|
|
|
else:
|
|
|
|
self.model.set_sort_column_id(self.cids[0],val[1])
|
2002-12-04 04:58:07 +00:00
|
|
|
self.model.sort_column_changed()
|
2002-11-17 05:06:43 +00:00
|
|
|
|
2002-11-14 05:31:42 +00:00
|
|
|
def get_selected(self):
|
|
|
|
return self.selection.get_selected()
|
2002-11-10 00:38:58 +00:00
|
|
|
|
2002-11-02 21:19:58 +00:00
|
|
|
def clear(self):
|
|
|
|
self.model.clear()
|
2002-11-14 05:31:42 +00:00
|
|
|
|
2002-11-15 03:49:39 +00:00
|
|
|
def remove(self,iter):
|
|
|
|
self.model.remove(iter)
|
|
|
|
|
2002-11-21 04:41:40 +00:00
|
|
|
def get_row(self,iter):
|
|
|
|
row = self.model.get_path(iter)
|
|
|
|
return row[0]
|
|
|
|
|
|
|
|
def select_row(self,row):
|
|
|
|
self.selection.select_path((row))
|
|
|
|
|
2002-11-14 05:31:42 +00:00
|
|
|
def get_object(self,iter):
|
|
|
|
return self.model.get_value(iter,self.data_index)
|
2002-11-02 21:19:58 +00:00
|
|
|
|
2002-11-25 04:30:36 +00:00
|
|
|
def add(self,data,info=None,select=0):
|
2002-10-25 04:52:51 +00:00
|
|
|
iter = self.model.append()
|
|
|
|
col = 0
|
|
|
|
for object in data:
|
|
|
|
self.model.set_value(iter,col,object)
|
|
|
|
col = col + 1
|
2002-11-14 05:31:42 +00:00
|
|
|
self.model.set_value(iter,col,info)
|
2002-11-25 04:30:36 +00:00
|
|
|
if select:
|
|
|
|
self.selection.select_iter(iter)
|
2002-11-15 03:49:39 +00:00
|
|
|
|
|
|
|
def add_and_select(self,data,info=None):
|
|
|
|
iter = self.model.append()
|
|
|
|
col = 0
|
|
|
|
for object in data:
|
|
|
|
self.model.set_value(iter,col,object)
|
|
|
|
col = col + 1
|
|
|
|
self.model.set_value(iter,col,info)
|
|
|
|
self.selection.select_iter(iter)
|
2002-10-25 04:52:51 +00:00
|
|
|
|
2002-11-14 05:31:42 +00:00
|
|
|
def button_press(self,obj,event):
|
|
|
|
if event.type == gtk.gdk._2BUTTON_PRESS and event.button == 1:
|
|
|
|
self.double_click(obj)
|
|
|
|
return 1
|