diff --git a/stats.py b/stats.py index fcd514cc..d15d9622 100644 --- a/stats.py +++ b/stats.py @@ -3,7 +3,8 @@ import csv import tkinter as tk from sys import platform from tkinter import ttk -from typing import TYPE_CHECKING, Any, AnyStr, Dict, List, NamedTuple, Optional, Sequence, cast +import tkinter +from typing import Callable, TYPE_CHECKING, Any, AnyStr, Dict, List, NamedTuple, Optional, Sequence, cast import companion import EDMCLogging @@ -11,6 +12,7 @@ import myNotebook as nb # noqa: N813 from edmc_data import ship_name_map from l10n import Locale from monitor import monitor +from ttkHyperlinkLabel import HyperlinkLabel logger = EDMCLogging.get_main_logger() @@ -342,10 +344,11 @@ class StatsResults(tk.Toplevel): page = self.addpage(notebook) for thing in stats[1:3]: - self.addpagerow(page, [thing[0], self.credits(int(thing[1]))]) # assumes things two and three are money + # assumes things two and three are money + self.addpagerow(page, [thing[0], self.credits(int(thing[1]))], with_copy=True) for thing in stats[3:]: - self.addpagerow(page, thing) + self.addpagerow(page, thing, with_copy=True) ttk.Frame(page).grid(pady=5) # bottom spacer notebook.add(page, text=_('Status')) # Status dialog title @@ -361,7 +364,7 @@ class StatsResults(tk.Toplevel): shiplist = ships(data) for ship_data in shiplist: # skip id, last item is money - self.addpagerow(page, list(ship_data[1:-1]) + [self.credits(int(ship_data[-1]))]) + self.addpagerow(page, list(ship_data[1:-1]) + [self.credits(int(ship_data[-1]))], with_copy=True) ttk.Frame(page).grid(pady=5) # bottom spacer notebook.add(page, text=_('Ships')) # Status dialog title @@ -417,14 +420,14 @@ class StatsResults(tk.Toplevel): :param header: The headers to add to the page :param align: The alignment of the page, defaults to None """ - self.addpagerow(parent, header, align=align) + self.addpagerow(parent, header, align=align, with_copy=False) ttk.Separator(parent, orient=tk.HORIZONTAL).grid(columnspan=len(header), padx=10, pady=2, sticky=tk.EW) def addpagespacer(self, parent) -> None: """Add a spacer to the page.""" self.addpagerow(parent, ['']) - def addpagerow(self, parent: tk.Frame, content: Sequence[str], align: Optional[str] = None): + def addpagerow(self, parent: tk.Frame, content: Sequence[str], align: Optional[str] = None, with_copy: bool = False): """ Add a single row to parent. @@ -434,7 +437,11 @@ class StatsResults(tk.Toplevel): """ row = -1 # To silence unbound warnings for i in range(len(content)): + # label = HyperlinkLabel(parent, text=content[i], popup_copy=True) label = nb.Label(parent, text=content[i]) + if with_copy: + label.bind('', self.copy_callback(label, content[i])) + if i == 0: label.grid(padx=10, sticky=tk.W) row = parent.grid_size()[1]-1 @@ -449,3 +456,16 @@ class StatsResults(tk.Toplevel): """Localised string of given int, including a trailing ` Cr`.""" # TODO: Locale is a class, this calls an instance method on it with an int as its `self` return Locale.string_from_number(value, 0) + ' Cr' # type: ignore + + @staticmethod + def copy_callback(label: tk.Label, text_to_copy: str) -> Callable[..., None]: + """Copy data in Label to clipboard.""" + def do_copy(event: tkinter.Event) -> None: + label.clipboard_clear() + label.clipboard_append(text_to_copy) + old_bg = label['bg'] + label['bg'] = 'gray49' + + label.after(100, (lambda: label.configure(bg=old_bg))) + + return do_copy