Added right-click API to ListModel; allow copy all from Descendants

Gramplet.
This commit is contained in:
Doug Blank 2015-05-07 12:04:14 -04:00 committed by Ross Gammon
parent 774855496f
commit ecac48e5b1
2 changed files with 62 additions and 6 deletions

View File

@ -75,10 +75,12 @@ class ListModel(object):
event_func: Function called when the user double-clicks on a row.
mode: Selection mode for TreeView. See Gtk documentation.
list_mode: "list" or "tree"
right_click: Function called when the user right-clicks on a row.
"""
def __init__(self, tree, dlist, select_func=None, event_func=None,
mode=Gtk.SelectionMode.SINGLE, list_mode="list"):
mode=Gtk.SelectionMode.SINGLE, list_mode="list",
right_click=None):
self.tree = tree
self.tree.set_fixed_height_mode(True)
@ -86,6 +88,8 @@ class ListModel(object):
self.data_index = 0
self.sel_iter = None
self.list_mode = list_mode # "list", or "tree"
self.double_click = None
self.right_click = None
for info in dlist:
col_type = TEXT
@ -124,8 +128,11 @@ class ListModel(object):
if select_func:
self.selection.connect('changed', select_func)
if event_func:
self.double_click = event_func
if event_func or right_click:
if event_func:
self.double_click = event_func
if right_click:
self.right_click = right_click
self.tree.connect('button-press-event', self.__button_press)
def __build_image_column(self, cnum, name, renderer, column):
@ -474,9 +481,15 @@ class ListModel(object):
"""
Called when a button press is executed
"""
from gramps.gui.utils import is_right_click
if event.type == Gdk.EventType._2BUTTON_PRESS and event.button == 1:
self.double_click(obj)
return True
if self.double_click:
self.double_click(obj)
return True
elif is_right_click(event):
if self.right_click:
self.right_click(obj, event)
return True
return False
def find(self, info):

View File

@ -34,6 +34,7 @@ _ = glocale.translation.gettext
#
#-------------------------------------------------------------------------
from gi.repository import Gtk
from gi.repository import Gdk
#------------------------------------------------------------------------
#
@ -68,7 +69,8 @@ class Descendant(Gramplet):
('', NOSORT, 1), # tooltip
('', NOSORT, 100)] # handle
self.model = ListModel(self.view, titles, list_mode="tree",
event_func=self.cb_double_click)
event_func=self.cb_double_click,
right_click=self.cb_right_click)
return self.view
def get_has_data(self, active_handle):
@ -100,6 +102,47 @@ class Descendant(Gramplet):
except WindowActiveError:
pass
def cb_right_click(self, treeview, event):
"""
Handle right click on treeview.
"""
(model, iter_) = treeview.get_selection().get_selected()
sensitivity = 1 if iter_ else 0
menu = Gtk.Menu()
menu.set_title(_('Descendent Menu'))
entries = [
(_("Edit"), lambda obj: self.cb_double_click(treeview), sensitivity),
(None, None, 0),
(_("Copy all"), lambda obj: self.on_copy_all(treeview), 1),
]
for stock_id, callback, sensitivity in entries:
item = Gtk.ImageMenuItem(stock_id)
if callback:
item.connect("activate", callback)
item.set_sensitive(sensitivity)
item.show()
menu.append(item)
self.menu = menu
self.menu.popup(None, None, None, None, event.button, event.time)
def on_copy_all(self, treeview):
def rows2text(store, treeiter, indent, level):
text = ""
while treeiter != None:
text += (indent + str(level) + ". " + store[treeiter][0] + " " + store[treeiter][1] + "\n")
if store.iter_has_child(treeiter):
childiter = store.iter_children(treeiter)
text += rows2text(store, childiter, indent + (" " * 4), level + 1)
treeiter = store.iter_next(treeiter)
return text
model = treeview.get_model()
rootiter = model.get_iter_first()
text = rows2text(model, rootiter, "", 1)
clipboard = Gtk.Clipboard.get_for_display(Gdk.Display.get_default(),
Gdk.SELECTION_CLIPBOARD)
clipboard.set_text(text, -1)
def db_changed(self):
self.update()