2002-10-25 10:22:51 +05:30
|
|
|
#
|
|
|
|
# Gramps - a GTK+/GNOME based genealogy program
|
|
|
|
#
|
2003-06-03 08:24:56 +05:30
|
|
|
# Copyright (C) 2000 Donald N. Allingham
|
2002-10-25 10:22:51 +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
|
|
|
|
#
|
|
|
|
|
2002-11-14 11:01:42 +05:30
|
|
|
from gobject import TYPE_STRING, TYPE_PYOBJECT
|
2002-10-25 10:22:51 +05:30
|
|
|
import gtk
|
|
|
|
|
2003-02-24 10:21:57 +05:30
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# ListModel
|
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
2002-10-25 10:22:51 +05:30
|
|
|
class ListModel:
|
2004-08-11 09:12:38 +05:30
|
|
|
def __init__(self,tree,dlist,select_func=None,
|
|
|
|
event_func=None,mode=gtk.SELECTION_SINGLE):
|
2002-10-25 10:22:51 +05:30
|
|
|
self.tree = tree
|
|
|
|
l = len(dlist)
|
2002-11-17 10:36:43 +05:30
|
|
|
self.mylist = [TYPE_STRING]*l + [TYPE_PYOBJECT]
|
|
|
|
|
2005-02-24 05:55:34 +05:30
|
|
|
self.tree.set_rules_hint(True)
|
2003-05-30 05:57:59 +05:30
|
|
|
self.model = None
|
2003-06-11 08:24:57 +05:30
|
|
|
self.selection = None
|
2002-12-29 11:14:35 +05:30
|
|
|
self.mode = mode
|
2003-06-03 08:24:56 +05:30
|
|
|
self.new_model()
|
2002-11-14 11:01:42 +05:30
|
|
|
self.data_index = l
|
2003-01-06 10:44:49 +05:30
|
|
|
self.count = 0
|
2003-05-30 05:57:59 +05:30
|
|
|
self.cid = None
|
2002-12-06 09:07:31 +05:30
|
|
|
self.cids = []
|
2003-10-14 09:52:24 +05:30
|
|
|
self.idmap = {}
|
2002-10-25 10:22:51 +05:30
|
|
|
|
|
|
|
cnum = 0
|
|
|
|
for name in dlist:
|
|
|
|
renderer = gtk.CellRendererText()
|
2003-07-27 09:20:57 +05:30
|
|
|
renderer.set_fixed_height_from_font(1)
|
2002-10-25 10:22:51 +05:30
|
|
|
column = gtk.TreeViewColumn(name[0],renderer,text=cnum)
|
2005-02-24 05:55:34 +05:30
|
|
|
column.set_reorderable(True)
|
2002-11-10 06:08:58 +05:30
|
|
|
column.set_min_width(name[2])
|
2003-07-31 17:28:08 +05:30
|
|
|
|
2002-11-03 02:49:58 +05:30
|
|
|
if name[0] == '':
|
2005-02-24 05:55:34 +05:30
|
|
|
column.set_visible(False)
|
2002-11-03 02:49:58 +05:30
|
|
|
else:
|
2005-02-24 05:55:34 +05:30
|
|
|
column.set_resizable(True)
|
2002-11-25 10:00:36 +05:30
|
|
|
if name[1] == -1:
|
2005-02-24 05:55:34 +05:30
|
|
|
column.set_clickable(False)
|
2002-11-25 10:00:36 +05:30
|
|
|
else:
|
2005-02-24 05:55:34 +05:30
|
|
|
column.set_clickable(True)
|
2002-12-06 09:07:31 +05:30
|
|
|
column.set_sort_column_id(name[1])
|
|
|
|
|
2002-10-25 10:22:51 +05:30
|
|
|
cnum = cnum + 1
|
2002-12-06 09:07:31 +05:30
|
|
|
self.cids.append(name[1])
|
2003-01-06 10:44:49 +05:30
|
|
|
if name[0] != '':
|
2002-12-06 09:07:31 +05:30
|
|
|
self.tree.append_column(column)
|
2002-12-11 10:48:47 +05:30
|
|
|
|
2002-11-19 09:45:02 +05:30
|
|
|
self.connect_model()
|
|
|
|
|
2002-11-10 06:08:58 +05:30
|
|
|
if select_func:
|
|
|
|
self.selection.connect('changed',select_func)
|
|
|
|
if event_func:
|
2002-11-14 11:01:42 +05:30
|
|
|
self.double_click = event_func
|
|
|
|
self.tree.connect('event',self.button_press)
|
|
|
|
|
2003-01-08 10:28:00 +05:30
|
|
|
def unselect(self):
|
|
|
|
self.selection.unselect_all()
|
|
|
|
|
2003-01-02 10:01:52 +05:30
|
|
|
def set_reorderable(self,order):
|
|
|
|
self.tree.set_reorderable(order)
|
|
|
|
|
2002-11-17 10:36:43 +05:30
|
|
|
def new_model(self):
|
2003-05-30 05:57:59 +05:30
|
|
|
if self.model:
|
|
|
|
self.cid = self.model.get_sort_column_id()
|
2003-06-11 08:24:57 +05:30
|
|
|
del self.model
|
|
|
|
del self.selection
|
2003-01-06 10:44:49 +05:30
|
|
|
self.count = 0
|
2003-06-11 08:24:57 +05:30
|
|
|
|
2002-11-17 10:36:43 +05:30
|
|
|
self.model = gtk.ListStore(*self.mylist)
|
2003-06-03 08:24:56 +05:30
|
|
|
self.selection = self.tree.get_selection()
|
|
|
|
self.selection.set_mode(self.mode)
|
|
|
|
self.sel_iter = None
|
|
|
|
|
2002-11-17 10:36:43 +05:30
|
|
|
def connect_model(self):
|
|
|
|
self.tree.set_model(self.model)
|
2003-06-03 08:24:56 +05:30
|
|
|
if self.sel_iter:
|
|
|
|
self.selection.select_iter(self.sel_iter)
|
2003-10-21 08:37:19 +05:30
|
|
|
|
|
|
|
# if the sort column has not been defined (val[0] == -2), set
|
|
|
|
# the sort column to the first column
|
|
|
|
|
|
|
|
val = self.model.get_sort_column_id()
|
|
|
|
if val[0] == -2 and self.cid:
|
2003-05-30 05:57:59 +05:30
|
|
|
self.model.set_sort_column_id(self.cid[0],self.cid[1])
|
2002-12-06 09:07:31 +05:30
|
|
|
self.sort()
|
2002-12-04 10:28:07 +05:30
|
|
|
|
|
|
|
def sort(self):
|
|
|
|
val = self.model.get_sort_column_id()
|
2002-12-06 09:07:31 +05:30
|
|
|
col = val[0]
|
2003-01-06 10:44:49 +05:30
|
|
|
if col < 0:
|
|
|
|
return
|
2002-12-06 09:07:31 +05:30
|
|
|
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 10:28:07 +05:30
|
|
|
self.model.sort_column_changed()
|
2002-11-17 10:36:43 +05:30
|
|
|
|
2002-11-14 11:01:42 +05:30
|
|
|
def get_selected(self):
|
|
|
|
return self.selection.get_selected()
|
2002-11-10 06:08:58 +05:30
|
|
|
|
2003-01-06 10:44:49 +05:30
|
|
|
def get_row_at(self,x,y):
|
|
|
|
path = self.tree.get_path_at_pos(x,y)
|
|
|
|
if path == None:
|
|
|
|
return self.count -1
|
|
|
|
else:
|
|
|
|
return path[0][0]-1
|
|
|
|
|
|
|
|
def get_selected_row(self):
|
2004-08-20 17:41:56 +05:30
|
|
|
store, node = self.selection.get_selected()
|
|
|
|
if node:
|
|
|
|
rows = store.get_path(node)
|
2003-01-06 10:44:49 +05:30
|
|
|
return rows[0]
|
|
|
|
else:
|
|
|
|
return -1
|
|
|
|
|
2002-12-29 11:14:35 +05:30
|
|
|
def get_selected_objects(self):
|
2003-01-06 10:44:49 +05:30
|
|
|
if self.count == 0:
|
|
|
|
return []
|
|
|
|
elif self.mode == gtk.SELECTION_SINGLE:
|
2004-08-20 17:41:56 +05:30
|
|
|
store,node = self.selection.get_selected()
|
|
|
|
if node:
|
|
|
|
return [self.model.get_value(node,self.data_index)]
|
2002-12-29 11:14:35 +05:30
|
|
|
else:
|
|
|
|
return []
|
|
|
|
else:
|
|
|
|
mlist = []
|
|
|
|
self.selection.selected_foreach(self.blist,mlist)
|
|
|
|
return mlist
|
|
|
|
|
2003-01-06 10:44:49 +05:30
|
|
|
def get_icon(self):
|
|
|
|
if self.mode == gtk.SELECTION_SINGLE:
|
2004-08-20 17:41:56 +05:30
|
|
|
store,node = self.selection.get_selected()
|
|
|
|
path = self.model.get_path(node)
|
2003-01-06 10:44:49 +05:30
|
|
|
else:
|
|
|
|
mlist = []
|
|
|
|
self.selection.selected_foreach(self.blist,mlist)
|
|
|
|
path = self.model.get_path(mlist[0])
|
|
|
|
return self.tree.create_row_drag_icon(path)
|
|
|
|
|
2004-08-20 17:41:56 +05:30
|
|
|
def blist(self,store,path,node,list):
|
|
|
|
list.append(self.model.get_value(node,self.data_index))
|
2002-12-29 11:14:35 +05:30
|
|
|
|
2002-11-03 02:49:58 +05:30
|
|
|
def clear(self):
|
2003-01-06 10:44:49 +05:30
|
|
|
self.count = 0
|
2002-11-03 02:49:58 +05:30
|
|
|
self.model.clear()
|
2002-11-14 11:01:42 +05:30
|
|
|
|
2004-08-20 17:41:56 +05:30
|
|
|
def remove(self,node):
|
|
|
|
self.model.remove(node)
|
2003-01-06 10:44:49 +05:30
|
|
|
self.count = self.count - 1
|
2002-11-15 09:19:39 +05:30
|
|
|
|
2004-08-20 17:41:56 +05:30
|
|
|
def get_row(self,node):
|
|
|
|
row = self.model.get_path(node)
|
2002-11-21 10:11:40 +05:30
|
|
|
return row[0]
|
|
|
|
|
|
|
|
def select_row(self,row):
|
|
|
|
self.selection.select_path((row))
|
2003-06-11 08:24:57 +05:30
|
|
|
|
2004-08-20 17:41:56 +05:30
|
|
|
def select_iter(self,node):
|
|
|
|
self.selection.select_iter(node)
|
2002-11-21 10:11:40 +05:30
|
|
|
|
2004-08-20 17:41:56 +05:30
|
|
|
def get_object(self,node):
|
|
|
|
return self.model.get_value(node,self.data_index)
|
2002-11-03 02:49:58 +05:30
|
|
|
|
2003-06-03 08:24:56 +05:30
|
|
|
def insert(self,position,data,info=None,select=0):
|
2003-01-06 10:44:49 +05:30
|
|
|
self.count = self.count + 1
|
2004-08-20 17:41:56 +05:30
|
|
|
node = self.model.insert(position)
|
2002-10-25 10:22:51 +05:30
|
|
|
col = 0
|
2004-08-20 17:41:56 +05:30
|
|
|
for obj in data:
|
|
|
|
self.model.set_value(node,col,obj)
|
2002-10-25 10:22:51 +05:30
|
|
|
col = col + 1
|
2004-08-20 17:41:56 +05:30
|
|
|
self.model.set_value(node,col,info)
|
2003-10-14 09:52:24 +05:30
|
|
|
if info:
|
2004-08-20 17:41:56 +05:30
|
|
|
self.idmap[str(info)] = node
|
2002-11-25 10:00:36 +05:30
|
|
|
if select:
|
2004-08-20 17:41:56 +05:30
|
|
|
self.selection.select_iter(node)
|
|
|
|
return node
|
2003-06-03 08:24:56 +05:30
|
|
|
|
2004-08-20 17:41:56 +05:30
|
|
|
def get_data(self,node,cols):
|
|
|
|
return [ self.model.get_value(node,c) for c in cols ]
|
2003-06-03 08:24:56 +05:30
|
|
|
|
|
|
|
def add(self,data,info=None,select=0):
|
2005-05-18 00:54:00 +05:30
|
|
|
self.count += 1
|
2004-08-20 17:41:56 +05:30
|
|
|
node = self.model.append()
|
2003-06-02 19:04:18 +05:30
|
|
|
col = 0
|
2004-08-20 17:41:56 +05:30
|
|
|
for obj in data:
|
|
|
|
self.model.set_value(node,col,obj)
|
2005-05-18 00:54:00 +05:30
|
|
|
col += 1
|
2004-08-20 17:41:56 +05:30
|
|
|
self.model.set_value(node,col,info)
|
2003-10-14 09:52:24 +05:30
|
|
|
if info:
|
2004-08-20 17:41:56 +05:30
|
|
|
self.idmap[str(info)] = node
|
2003-06-02 19:04:18 +05:30
|
|
|
if select:
|
2004-08-20 17:41:56 +05:30
|
|
|
self.sel_iter = node
|
2003-07-31 17:28:08 +05:30
|
|
|
self.selection.select_iter(self.sel_iter)
|
2004-08-20 17:41:56 +05:30
|
|
|
return node
|
2003-06-03 08:24:56 +05:30
|
|
|
|
2004-08-20 17:41:56 +05:30
|
|
|
def set(self,node,data,info=None,select=0):
|
2003-06-16 10:27:25 +05:30
|
|
|
col = 0
|
2004-08-20 17:41:56 +05:30
|
|
|
for obj in data:
|
|
|
|
self.model.set_value(node,col,obj)
|
2005-05-18 00:54:00 +05:30
|
|
|
col += 1
|
2004-08-20 17:41:56 +05:30
|
|
|
self.model.set_value(node,col,info)
|
2003-10-14 09:52:24 +05:30
|
|
|
if info:
|
2004-08-20 17:41:56 +05:30
|
|
|
self.idmap[str(info)] = node
|
2003-06-16 10:27:25 +05:30
|
|
|
if select:
|
2004-08-20 17:41:56 +05:30
|
|
|
self.sel_iter = node
|
|
|
|
return node
|
2003-06-16 10:27:25 +05:30
|
|
|
|
2002-11-15 09:19:39 +05:30
|
|
|
def add_and_select(self,data,info=None):
|
2003-01-06 10:44:49 +05:30
|
|
|
self.count = self.count + 1
|
2004-08-20 17:41:56 +05:30
|
|
|
node = self.model.append()
|
2002-11-15 09:19:39 +05:30
|
|
|
col = 0
|
2004-08-20 17:41:56 +05:30
|
|
|
for obj in data:
|
|
|
|
self.model.set_value(node,col,obj)
|
2002-11-15 09:19:39 +05:30
|
|
|
col = col + 1
|
2003-10-14 09:52:24 +05:30
|
|
|
if info:
|
2004-08-20 17:41:56 +05:30
|
|
|
self.idmap[str(info)] = node
|
|
|
|
self.model.set_value(node,col,info)
|
|
|
|
self.selection.select_iter(node)
|
2003-06-02 04:16:00 +05:30
|
|
|
|
|
|
|
def center_selected(self):
|
2004-08-20 17:41:56 +05:30
|
|
|
model,node = self.selection.get_selected()
|
|
|
|
if node:
|
|
|
|
path = model.get_path(node)
|
2005-02-24 05:55:34 +05:30
|
|
|
self.tree.scroll_to_cell(path,None,True,0.5,0.5)
|
2002-10-25 10:22:51 +05:30
|
|
|
|
2002-11-14 11:01:42 +05:30
|
|
|
def button_press(self,obj,event):
|
|
|
|
if event.type == gtk.gdk._2BUTTON_PRESS and event.button == 1:
|
|
|
|
self.double_click(obj)
|
|
|
|
return 1
|
2003-01-10 10:51:32 +05:30
|
|
|
return 0
|
2003-07-31 17:28:08 +05:30
|
|
|
|
2003-10-14 09:52:24 +05:30
|
|
|
def find(self,info):
|
2003-10-15 09:49:51 +05:30
|
|
|
if info in self.idmap.keys():
|
2004-08-20 17:41:56 +05:30
|
|
|
node = self.idmap[str(info)]
|
|
|
|
self.selection.select_iter(node)
|
2003-10-14 09:52:24 +05:30
|
|
|
|
2003-07-31 17:28:08 +05:30
|
|
|
def cleanup(self):
|
2003-10-14 09:52:24 +05:30
|
|
|
pass
|