From 2ad372bcbae4856218797b096481f261f4dd3bec Mon Sep 17 00:00:00 2001 From: Doug Blank Date: Sat, 9 May 2015 09:49:19 -0400 Subject: [PATCH] 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. --- gramps/gui/plug/quick/_quicktable.py | 6 ++++ gramps/gui/utils.py | 44 +++++++++++++++++++++++++++ gramps/plugins/gramplet/descendant.py | 20 +++--------- 3 files changed, 54 insertions(+), 16 deletions(-) diff --git a/gramps/gui/plug/quick/_quicktable.py b/gramps/gui/plug/quick/_quicktable.py index a15114af2..ea1f64c8b 100644 --- a/gramps/gui/plug/quick/_quicktable.py +++ b/gramps/gui/plug/quick/_quicktable.py @@ -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] diff --git a/gramps/gui/utils.py b/gramps/gui/utils.py index cc6972413..89f78780c 100644 --- a/gramps/gui/utils.py +++ b/gramps/gui/utils.py @@ -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) diff --git a/gramps/plugins/gramplet/descendant.py b/gramps/plugins/gramplet/descendant.py index 5b00b7cca..af3d72253 100644 --- a/gramps/plugins/gramplet/descendant.py +++ b/gramps/plugins/gramplet/descendant.py @@ -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,23 +127,10 @@ 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()