Use public interface of Gtk.TreeModel in listview code

svn: r21165
This commit is contained in:
Nick Hall 2013-01-18 18:46:09 +00:00
parent 0cf2bc0032
commit 774ceff34b
7 changed files with 89 additions and 141 deletions

View File

@ -63,7 +63,7 @@ from gramps.gen.errors import WindowActiveError
from ..filters import SearchBar from ..filters import SearchBar
from ..widgets.menuitem import add_menuitem from ..widgets.menuitem import add_menuitem
from gramps.gen.constfunc import UNITYPE from gramps.gen.constfunc import UNITYPE
from gramps.gen.const import CUSTOM_FILTERS, USE_TIPS from gramps.gen.const import CUSTOM_FILTERS
from gramps.gen.utils.debug import profile from gramps.gen.utils.debug import profile
from gramps.gen.utils.string import data_recover_msg from gramps.gen.utils.string import data_recover_msg
from gramps.gen.utils.file import get_unicode_path_from_file_chooser from gramps.gen.utils.file import get_unicode_path_from_file_chooser
@ -178,7 +178,8 @@ class ListView(NavigationView):
self.list.drag_dest_set_target_list(tglist) self.list.drag_dest_set_target_list(tglist)
scrollwindow = Gtk.ScrolledWindow() scrollwindow = Gtk.ScrolledWindow()
scrollwindow.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC) scrollwindow.set_policy(Gtk.PolicyType.AUTOMATIC,
Gtk.PolicyType.AUTOMATIC)
scrollwindow.set_shadow_type(Gtk.ShadowType.ETCHED_IN) scrollwindow.set_shadow_type(Gtk.ShadowType.ETCHED_IN)
scrollwindow.add(self.list) scrollwindow.add(self.list)
@ -326,9 +327,6 @@ class ListView(NavigationView):
self.__display_column_sort() self.__display_column_sort()
self.goto_active(None) self.goto_active(None)
if USE_TIPS and self.model.tooltip_column() is not None:
self.list.set_tooltip_column(self.model.tooltip_column())
self.dirty = False self.dirty = False
cput4 = time.clock() cput4 = time.clock()
self.uistate.show_filter_results(self.dbstate, self.uistate.show_filter_results(self.dbstate,
@ -404,27 +402,21 @@ class ListView(NavigationView):
if not handle or handle in self.selected_handles(): if not handle or handle in self.selected_handles():
return return
if self.model.get_flags() & Gtk.TreeModelFlags.LIST_ONLY: iter_ = self.model.get_iter_from_handle(handle)
# Flat if iter_:
path = self.model.node_map.get_path_from_handle(handle) if not (self.model.get_flags() & Gtk.TreeModelFlags.LIST_ONLY):
else: # Expand tree
# Tree parent_iter = self.model.iter_parent(iter_)
path = None if parent_iter:
node = self.model.get_node(handle) parent_path = self.model.get_path(parent_iter)
if node:
iter = self.model.get_iter(node)
has_parent, parent_iter = self.model.do_iter_parent(iter)
if has_parent:
parent_path = self.model.do_get_path(parent_iter)
if parent_path:
parent_path_list = parent_path.get_indices() parent_path_list = parent_path.get_indices()
for i in range(len(parent_path_list)): for i in range(len(parent_path_list)):
expand_path = Gtk.TreePath( expand_path = Gtk.TreePath(
tuple([x for x in parent_path_list[:i+1]])) tuple([x for x in parent_path_list[:i+1]]))
self.list.expand_row(expand_path, False) self.list.expand_row(expand_path, False)
path = self.model.do_get_path(iter)
if path is not None: # Select active object
path = self.model.get_path(iter_)
self.selection.unselect_all() self.selection.unselect_all()
self.selection.select_path(path) self.selection.select_path(path)
self.list.scroll_to_cell(path, None, 1, 0.5, 0) self.list.scroll_to_cell(path, None, 1, 0.5, 0)
@ -568,15 +560,11 @@ class ListView(NavigationView):
if not prompt: if not prompt:
self.uistate.set_busy_cursor(False) self.uistate.set_busy_cursor(False)
def blist(self, store, path, iter, sel_list): def blist(self, store, path, iter_, sel_list):
'''GtkTreeSelectionForeachFunc '''GtkTreeSelectionForeachFunc
construct a list sel_list with all selected handles construct a list sel_list with all selected handles
''' '''
if store.do_get_flags() & Gtk.TreeModelFlags.LIST_ONLY: handle = store.get_handle_from_iter(iter_)
handle = store.node_map.get_handle(path.get_indices()[0])
else:
handle = store.get_handle(store.get_node_from_iter(iter))
if handle is not None: if handle is not None:
sel_list.append(handle) sel_list.append(handle)
@ -810,10 +798,9 @@ class ListView(NavigationView):
# Tree # Tree
store, paths = self.selection.get_selected_rows() store, paths = self.selection.get_selected_rows()
if paths: if paths:
firstsel = paths[0] firstsel = self.model.get_iter(paths[0])
firstnode = self.model.get_node_from_iter( handle = self.model.get_handle_from_iter(firstsel)
self.model.do_get_iter(firstsel)[1]) if len(paths)==1 and handle is None:
if len(paths)==1 and firstnode.handle is None:
return self.expand_collapse_tree_branch() return self.expand_collapse_tree_branch()
else: else:
self.edit(obj) self.edit(obj)
@ -894,19 +881,17 @@ class ListView(NavigationView):
if event.keyval in (Gdk.KEY_Return, Gdk.KEY_KP_Enter): if event.keyval in (Gdk.KEY_Return, Gdk.KEY_KP_Enter):
store, paths = self.selection.get_selected_rows() store, paths = self.selection.get_selected_rows()
if paths: if paths:
firstsel = paths[0] iter_ = self.model.get_iter(paths[0])
firstnode = self.model.get_node_from_iter( handle = self.model.get_handle_from_iter(iter_)
self.model.do_get_iter(firstsel)[1]) if len(paths) == 1 and handle is None:
if len(paths) == 1 and firstnode.handle is None:
return self.expand_collapse_tree_branch() return self.expand_collapse_tree_branch()
else: else:
if event.keyval in (Gdk.KEY_Return, Gdk.KEY_KP_Enter): if event.keyval in (Gdk.KEY_Return, Gdk.KEY_KP_Enter):
store, paths = self.selection.get_selected_rows() store, paths = self.selection.get_selected_rows()
if paths: if paths:
firstsel = paths[0] iter_ = self.model.get_iter(paths[0])
firstnode = self.model.get_node_from_iter( handle = self.model.get_handle_from_iter(iter_)
self.model.do_get_iter(firstsel)[1]) if len(paths) == 1 and handle is None:
if len(paths) == 1 and firstnode.handle is None:
return self.expand_collapse_tree() return self.expand_collapse_tree()
else: else:
self.edit(obj) self.edit(obj)
@ -921,9 +906,9 @@ class ListView(NavigationView):
store, paths = self.selection.get_selected_rows() store, paths = self.selection.get_selected_rows()
if paths: if paths:
firstsel = paths[0] firstsel = paths[0]
firstnode = self.model.get_node_from_iter( iter_ = self.model.get_iter(firstsel)
self.model.do_get_iter(firstsel)[1]) handle = self.model.get_handle_from_iter(iter_)
if firstnode.handle: if handle:
return False return False
if self.list.row_expanded(firstsel): if self.list.row_expanded(firstsel):
self.list.collapse_row(firstsel) self.list.collapse_row(firstsel)
@ -940,9 +925,9 @@ class ListView(NavigationView):
store, paths = self.selection.get_selected_rows() store, paths = self.selection.get_selected_rows()
if paths: if paths:
firstsel = paths[0] firstsel = paths[0]
firstnode = self.model.get_node_from_iter( iter_ = self.model.get_iter(firstsel)
self.model.do_get_iter(firstsel)[1]) handle = self.model.get_handle_from_iter(iter_)
if firstnode.handle: if handle:
return False return False
if self.list.row_expanded(firstsel): if self.list.row_expanded(firstsel):
self.list.collapse_row(firstsel) self.list.collapse_row(firstsel)
@ -1062,36 +1047,29 @@ class ListView(NavigationView):
ofile.end_row() ofile.end_row()
else: else:
# Tree model # Tree model
hasmodel, iter = self.model.do_get_iter((0,)) iter_ = self.model.get_iter((0,))
if hasmodel: if iter_:
node = self.model.get_node_from_iter(iter) self.write_node(iter_, len(levels), [], ofile, data_cols)
self.write_node(node, len(levels), [], ofile, data_cols)
ofile.end_page() ofile.end_page()
ofile.close() ofile.close()
def write_node(self, node, depth, level, ofile, data_cols): def write_node(self, iter_, depth, level, ofile, data_cols):
if node is None:
return while iter_:
while node is not None: new_level = level + [self.model.get_value(iter_, 0)]
iternode = self.model.get_iter(node) if self.model.get_handle_from_iter(iter_):
new_level = level + [self.model.do_get_value(iternode, 0)]
if self.model.get_handle(node):
ofile.start_row() ofile.start_row()
padded_level = new_level + [''] * (depth - len(new_level)) padded_level = new_level + [''] * (depth - len(new_level))
list(map(ofile.write_cell, padded_level)) list(map(ofile.write_cell, padded_level))
for index in data_cols: for index in data_cols:
ofile.write_cell(self.model.do_get_value(iternode, index)) ofile.write_cell(self.model.get_value(iter_, index))
ofile.end_row() ofile.end_row()
has_child, first_child = self.model.do_iter_children(iternode) first_child = self.model.iter_children(iter_)
if has_child: self.write_node(first_child, depth, new_level, ofile, data_cols)
self.write_node(self.model.get_node_from_iter(first_child),
depth, new_level, ofile, data_cols) iter_ = self.model.iter_next(iter_)
has_next = self.model.do_iter_next(iternode)
node = self.model.get_node_from_iter(iternode)
if not has_next:
node = None
#################################################################### ####################################################################
# Template functions # Template functions

View File

@ -174,7 +174,7 @@ class CitationTreeModel(CitationBaseModel, TreeBaseModel):
data The object data. data The object data.
""" """
sort_key = self.sort_func2(data) sort_key = self.sort_func2(data)
if self.get_node(data[5]): if self._get_node(data[5]):
self.add_node(data[5], handle, sort_key, handle, secondary=True) self.add_node(data[5], handle, sort_key, handle, secondary=True)
def do_get_n_columns(self): def do_get_n_columns(self):

View File

@ -526,7 +526,7 @@ class FlatBaseModel(GObject.Object, Gtk.TreeModel):
col = search[1][0] col = search[1][0]
text = search[1][1] text = search[1][1]
inv = search[1][2] inv = search[1][2]
func = lambda x: self.get_value_from_handle(x, col) or UEMPTY func = lambda x: self._get_value(x, col) or UEMPTY
if search[2]: if search[2]:
self.search = ExactSearchFilter(func, text, inv) self.search = ExactSearchFilter(func, text, inv)
else: else:
@ -705,17 +705,12 @@ class FlatBaseModel(GObject.Object, Gtk.TreeModel):
node = self.do_get_iter(path)[1] node = self.do_get_iter(path)[1]
self.row_changed(path, node) self.row_changed(path, node)
def handle2path(self, handle):
"""
Obtain from a handle, a path.
Part of common api with flat/treebasemodel
"""
return self.on_get_path_from_handle(handle)
def get_iter_from_handle(self, handle): def get_iter_from_handle(self, handle):
""" """
Get the iter for a gramps handle. Get the iter for a gramps handle.
""" """
if self.node_map.get_path_from_handle(handle) is None:
return None
return self.node_map.new_iter(handle) return self.node_map.new_iter(handle)
def get_handle_from_iter(self, iter): def get_handle_from_iter(self, iter):
@ -775,7 +770,7 @@ class FlatBaseModel(GObject.Object, Gtk.TreeModel):
except IndexError: except IndexError:
return False, Gtk.TreeIter() return False, Gtk.TreeIter()
def get_value_from_handle(self, handle, col): def _get_value(self, handle, col):
""" """
Given handle and column, return unicode value in the column Given handle and column, return unicode value in the column
We need this to search in the column in the GUI We need this to search in the column in the GUI
@ -796,7 +791,7 @@ class FlatBaseModel(GObject.Object, Gtk.TreeModel):
""" """
#print 'do_get_val', iter, iter.user_data, col #print 'do_get_val', iter, iter.user_data, col
handle = self.node_map._index2hndl[iter.user_data][1] handle = self.node_map._index2hndl[iter.user_data][1]
val = self.get_value_from_handle(handle, col) val = self._get_value(handle, col)
#print 'val is', val, type(val) #print 'val is', val, type(val)
#GTK 3 should convert unicode objects automatically, but this #GTK 3 should convert unicode objects automatically, but this

View File

@ -359,7 +359,7 @@ class PlaceTreeModel(PlaceBaseModel, TreeBaseModel):
if node.name: if node.name:
return '<i>%s</i>' % cgi.escape(node.name) return '<i>%s</i>' % cgi.escape(node.name)
else: else:
level = len(self.do_get_path(self.get_iter(node)).get_indices()) level = len(self.do_get_path(self._get_iter(node)).get_indices())
heading = '<i>%s</i>' % cgi.escape(COUNTRYLEVELS['default'][level]) heading = '<i>%s</i>' % cgi.escape(COUNTRYLEVELS['default'][level])
# This causes a problem with Gtk3 unless we cast to str. # This causes a problem with Gtk3 unless we cast to str.
return str(heading) return str(heading)

View File

@ -644,7 +644,7 @@ class TreeBaseModel(GObject.Object, Gtk.TreeModel):
if not self._in_build: if not self._in_build:
# emit row_inserted signal # emit row_inserted signal
iternode = self.get_iter(child_node) iternode = self._get_iter(child_node)
path = self.do_get_path(iternode) path = self.do_get_path(iternode)
self.row_inserted(path, iternode) self.row_inserted(path, iternode)
if handle: if handle:
@ -681,7 +681,7 @@ class TreeBaseModel(GObject.Object, Gtk.TreeModel):
self.__displayed -= 1 self.__displayed -= 1
self.__total -= 1 self.__total -= 1
elif node.parent: # don't remove the hidden root node elif node.parent: # don't remove the hidden root node
iternode = self.get_iter(node) iternode = self._get_iter(node)
path = self.do_get_path(iternode) path = self.do_get_path(iternode)
self.nodemap.node(node.parent).remove_child(node, self.nodemap) self.nodemap.node(node.parent).remove_child(node, self.nodemap)
del self.tree[node.ref] del self.tree[node.ref]
@ -719,7 +719,7 @@ class TreeBaseModel(GObject.Object, Gtk.TreeModel):
if node.parent is None: if node.parent is None:
path = iter = None path = iter = None
else: else:
iternode = self.get_iter(node) iternode = self._get_iter(node)
path = self.do_get_path(iternode) path = self.do_get_path(iternode)
# activate when https://bugzilla.gnome.org/show_bug.cgi?id=684558 # activate when https://bugzilla.gnome.org/show_bug.cgi?id=684558
# is resolved # is resolved
@ -749,7 +749,7 @@ class TreeBaseModel(GObject.Object, Gtk.TreeModel):
""" """
if sys.version_info[0] >= 3: if sys.version_info[0] >= 3:
assert isinstance(handle, str) assert isinstance(handle, str)
if self.get_node(handle) is not None: if self._get_node(handle) is not None:
return # row already exists return # row already exists
cput = time.clock() cput = time.clock()
if not self.search or \ if not self.search or \
@ -773,7 +773,7 @@ class TreeBaseModel(GObject.Object, Gtk.TreeModel):
if sys.version_info[0] >= 3: if sys.version_info[0] >= 3:
assert isinstance(handle, str) assert isinstance(handle, str)
cput = time.clock() cput = time.clock()
node = self.get_node(handle) node = self._get_node(handle)
if node is None: if node is None:
return # row not currently displayed return # row not currently displayed
@ -786,7 +786,7 @@ class TreeBaseModel(GObject.Object, Gtk.TreeModel):
if not parent.children: if not parent.children:
if parent.handle: if parent.handle:
# emit row_has_child_toggled signal # emit row_has_child_toggled signal
iternode = self.get_iter(parent) iternode = self._get_iter(parent)
path = self.do_get_path(iternode) path = self.do_get_path(iternode)
self.row_has_child_toggled(path, iternode) self.row_has_child_toggled(path, iternode)
else: else:
@ -804,7 +804,7 @@ class TreeBaseModel(GObject.Object, Gtk.TreeModel):
""" """
if sys.version_info[0] >= 3: if sys.version_info[0] >= 3:
assert isinstance(handle, str) assert isinstance(handle, str)
if self.get_node(handle) is None: if self._get_node(handle) is None:
return # row not currently displayed return # row not currently displayed
self.delete_row_by_handle(handle) self.delete_row_by_handle(handle)
@ -813,7 +813,7 @@ class TreeBaseModel(GObject.Object, Gtk.TreeModel):
# If the node hasn't moved, all we need is to call row_changed. # If the node hasn't moved, all we need is to call row_changed.
#self.row_changed(path, node) #self.row_changed(path, node)
def new_iter(self, nodeid): def _new_iter(self, nodeid):
""" """
Return a new iter containing the nodeid in the nodemap Return a new iter containing the nodeid in the nodemap
""" """
@ -824,7 +824,7 @@ class TreeBaseModel(GObject.Object, Gtk.TreeModel):
iter.user_data = nodeid iter.user_data = nodeid
return iter return iter
def get_iter(self, node): def _get_iter(self, node):
""" """
Return an iter from the node. Return an iter from the node.
iters are always created afresh iters are always created afresh
@ -835,37 +835,20 @@ class TreeBaseModel(GObject.Object, Gtk.TreeModel):
:param path: node as it appears in the treeview :param path: node as it appears in the treeview
:type path: Node :type path: Node
""" """
iter = self.new_iter(id(node)) iter = self._new_iter(id(node))
return iter return iter
def get_handle(self, node): def _get_node(self, handle):
"""
Get the gramps handle for a node. Return None if the node does
not correspond to a gramps object.
"""
handle = node.handle
if handle and not isinstance(handle, UNITYPE):
handle = handle.decode('utf-8')
return handle
def get_node(self, handle):
""" """
Get the node for a handle. Get the node for a handle.
""" """
return self.handle2node.get(handle) return self.handle2node.get(handle)
def handle2path(self, handle):
"""
Obtain from a handle, a path.
Part of common api with flat/treebasemodel
"""
return self.do_get_path(self.get_iter(self.get_node(handle)))
def get_iter_from_handle(self, handle): def get_iter_from_handle(self, handle):
""" """
Get the iter for a gramps handle. Get the iter for a gramps handle.
""" """
return self.get_iter(self._get_node(handle)) return self._get_iter(self._get_node(handle))
def get_handle_from_iter(self, iter): def get_handle_from_iter(self, iter):
""" """
@ -964,7 +947,7 @@ class TreeBaseModel(GObject.Object, Gtk.TreeModel):
for index in pathlist: for index in pathlist:
_index = (-index - 1) if self.__reverse else index _index = (-index - 1) if self.__reverse else index
node = self.nodemap.node(node.children[_index][1]) node = self.nodemap.node(node.children[_index][1])
return True, self.get_iter(node) return True, self._get_iter(node)
def get_node_from_iter(self, iter): def get_node_from_iter(self, iter):
if iter and iter.user_data: if iter and iter.user_data:
@ -1024,7 +1007,7 @@ class TreeBaseModel(GObject.Object, Gtk.TreeModel):
nodeid = nodeparent.children[-1 if self.__reverse else 0][1] nodeid = nodeparent.children[-1 if self.__reverse else 0][1]
else: else:
return False, None return False, None
return True, self.new_iter(nodeid) return True, self._new_iter(nodeid)
def do_iter_has_child(self, iter): def do_iter_has_child(self, iter):
""" """
@ -1054,7 +1037,7 @@ class TreeBaseModel(GObject.Object, Gtk.TreeModel):
if node.children: if node.children:
if len(node.children) > index: if len(node.children) > index:
_index = (-index - 1) if self.__reverse else index _index = (-index - 1) if self.__reverse else index
return True, self.new_iter(node.children[_index][1]) return True, self._new_iter(node.children[_index][1])
else: else:
return False, None return False, None
else: else:
@ -1066,6 +1049,6 @@ class TreeBaseModel(GObject.Object, Gtk.TreeModel):
""" """
node = self.get_node_from_iter(iterchild) node = self.get_node_from_iter(iterchild)
if node.parent: if node.parent:
return True, self.new_iter(node.parent) return True, self._new_iter(node.parent)
else: else:
return False, None return False, None

View File

@ -170,9 +170,8 @@ class PersonTreeView(BasePersonView):
pathids = path.get_indices() pathids = path.get_indices()
if len(pathids) == 1: if len(pathids) == 1:
path = Gtk.TreePath((pathids[0], 0)) path = Gtk.TreePath((pathids[0], 0))
nodeiter = model.do_get_iter(path)[1] iter_ = model.get_iter(path)
node = model.get_node_from_iter(nodeiter) handle = model.get_handle_from_iter(iter_)
handle = model.get_handle(node)
basepers = self.dbstate.db.get_person_from_handle(handle) basepers = self.dbstate.db.get_person_from_handle(handle)
if basepers: if basepers:
preset_name(basepers, name) preset_name(basepers, name)

View File

@ -201,34 +201,27 @@ class PlaceTreeView(PlaceBaseView):
level1 = level2 = level3 = "" level1 = level2 = level3 = ""
if len(pathlist) == 1: if len(pathlist) == 1:
path = pathlist[0] path = pathlist[0]
suc, node = model.do_get_iter(path) iter_ = model.get_iter(path)
if suc: if iter_:
noden = model.get_node_from_iter(node)
if len(path) == 1: if len(path) == 1:
level[0] = noden.name level[0] = model.get_node_from_iter(iter_).name
elif len(path) == 2: elif len(path) == 2:
level[1] = noden.name level[1] = model.get_node_from_iter(iter_).name
suc, parent = model.do_iter_parent(node) parent = model.iter_parent(iter_)
parentn = model.get_node_from_iter(parent) level[0] = model.get_node_from_iter(parent).name
level[0] = parentn.name
elif len(path) == 3: elif len(path) == 3:
level[2] = noden.name level[2] = model.get_node_from_iter(iter_).name
suc, parent = model.do_iter_parent(node) parent = model.iter_parent(iter_)
parentn = model.get_node_from_iter(parent) level[1] = model.get_node_from_iter(parent).name
level[1] = parentn.name parent = model.iter_parent(parent)
suc, parent = model.do_iter_parent(parent) level[0] = model.get_node_from_iter(parent).name
parentn = model.get_node_from_iter(parent)
level[0] = parentn.name
else: else:
suc, parent = model.do_iter_parent(node) parent = model.iter_parent(iter_)
parentn = model.get_node_from_iter(parent) level[2] = model.get_node_from_iter(parent).name
level[2] = parentn.name parent = model.iter_parent(parent)
suc, parent = model.do_iter_parent(parent) level[1] = model.get_node_from_iter(parent).name
parentn = model.get_node_from_iter(parent) parent = model.iter_parent(parent)
level[1] = parentn.name level[0] = model.get_node_from_iter(parent).name
suc, parent = model.do_iter_parent(parent)
parentn = model.get_node_from_iter(parent)
level[0] = parentn.name
for ind in [0, 1, 2]: for ind in [0, 1, 2]:
if level[ind] and level[ind] == COUNTRYLEVELS['default'][ind+1]: if level[ind] and level[ind] == COUNTRYLEVELS['default'][ind+1]: