Added right-click "Copy all" to all QuickTables.

This currently copies all data from the TreeView to the clipboard,
including non-viewed columns.

Refactored rows2text to model_to_text, and moved to gramps.gui.utils

Added text_to_clipboard to gramps.gui.utils as well.
This commit is contained in:
Doug Blank 2015-05-09 09:49:19 -04:00
parent f4f83e456e
commit 2ad372bcba
3 changed files with 54 additions and 16 deletions

View File

@ -49,6 +49,7 @@ from gramps.gen.const import GRAMPS_LOCALE as glocale
_ = glocale.translation.sgettext
from gramps.gen.simple import SimpleTable
from gramps.gen.errors import WindowActiveError
from gramps.gui.utils import model_to_text, text_to_clipboard
from ...widgets.multitreeview import MultiTreeView
from ...ddtargets import DdTargets
from ..quick import run_quick_report_by_name
@ -118,6 +119,11 @@ class QuickTable(SimpleTable):
if index is not None:
self.popup = Gtk.Menu()
popup = self.popup
menu_item = Gtk.MenuItem(label=_("Copy all"))
menu_item.connect("activate", lambda widget: text_to_clipboard(model_to_text(treeview.get_model())))
popup.append(menu_item)
menu_item.show()
# Now add more items to popup menu, if available
if (index is not None and self._link[index]):
# See details (edit, etc):
objclass, handle = self._link[index]

View File

@ -629,3 +629,47 @@ class AvailableUpdates(threading.Thread):
self.addon_update_list = available_updates()
if len(self.addon_update_list) > 0:
GLib.idle_add(self.emit_update_available)
def model_to_text(model, cols=None, treeiter=None, indent="",
level=None, sep=", "):
"""
Given a model, return the text from the rows as a string.
model - the actual model
cols - a list representing the columns to get, or None for all
treeiter - (optional), initially, the first iterator
ident - the current indent level text
level - use None for no level indicator, or number (eg, 1)
sep - separating text between columns
"""
text = ""
if treeiter is None:
treeiter = model.get_iter_first()
while treeiter != None:
if cols is None:
items = sep.join([str(item) for item in model[treeiter][:]])
else:
items = sep.join([str(model[treeiter][col]) for col in cols])
if level is not None:
text += (indent + str(level) + ". " + items + "\n")
else:
text += (indent + items + "\n")
if model.iter_has_child(treeiter):
childiter = model.iter_children(treeiter)
if level is not None:
text += model_to_text(model, cols, childiter, indent + (" " * 4),
level + 1, sep)
else:
text += model_to_text(model, cols, childiter, indent + (" " * 4),
sep=sep)
treeiter = model.iter_next(treeiter)
return text
def text_to_clipboard(text):
"""
Put any text into the clipboard
"""
from gi.repository import Gdk
from gi.repository import Gtk
clipboard = Gtk.Clipboard.get_for_display(Gdk.Display.get_default(),
Gdk.SELECTION_CLIPBOARD)
clipboard.set_text(text, -1)

View File

@ -48,6 +48,7 @@ from gramps.gen.errors import WindowActiveError
from gramps.gen.display.name import displayer as name_displayer
from gramps.gen.datehandler import get_date
from gramps.gen.utils.db import get_birth_or_fallback, get_death_or_fallback
from gramps.gui.utils import model_to_text, text_to_clipboard
class Descendant(Gramplet):
@ -126,22 +127,9 @@ class Descendant(Gramplet):
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)
text = model_to_text(model, [0, 1], level=1)
text_to_clipboard(text)
def db_changed(self):
self.update()