From e83e783f4187067c4e0a0633ac960c9928c42705 Mon Sep 17 00:00:00 2001 From: Eugenio Parodi Date: Thu, 22 Feb 2024 11:33:10 +0000 Subject: [PATCH 1/4] Add copykitten support --- TermTk/TTkGui/clipboard.py | 42 ++++++++++++++++++++++++-------------- 1 file changed, 27 insertions(+), 15 deletions(-) diff --git a/TermTk/TTkGui/clipboard.py b/TermTk/TTkGui/clipboard.py index 4276d4c1..a59e0984 100644 --- a/TermTk/TTkGui/clipboard.py +++ b/TermTk/TTkGui/clipboard.py @@ -30,21 +30,33 @@ class TTkClipboard(): __slots__ = ('_setText', '_text') def __init__(self) -> None: - if importlib.util.find_spec('pyperclip'): - import pyperclip as _c - self._setText = _c.copy - self._text = _c.paste - elif importlib.util.find_spec('pyperclip3'): - import pyperclip3 as _c - self._setText = _c.copy - self._text = _c.paste - elif importlib.util.find_spec('clipboard'): - import clipboard as _c - self._setText = _c.copy - self._text = _c.paste - else: - self._setText = None - self._text = None + self._setText = None + self._text = None + try: + if importlib.util.find_spec('copykitten'): + TTkLog.info("Using 'copykitten' as clipboard manager") + import copykitten as _c + self._setText = _c.copy + self._text = _c.paste + elif importlib.util.find_spec('pyperclip'): + TTkLog.info("Using 'pyperclip' as clipboard manager") + import pyperclip as _c + self._setText = _c.copy + self._text = _c.paste + elif importlib.util.find_spec('pyperclip3'): + TTkLog.info("Using 'pyperclip3' as clipboard manager") + import pyperclip3 as _c + self._setText = _c.copy + self._text = _c.paste + elif importlib.util.find_spec('clipboard'): + TTkLog.info("Using 'clipboard' as clipboard manager") + import clipboard as _c + self._setText = _c.copy + self._text = _c.paste + except Exception as e: + TTkLog.error("Clipboard error, try to export X11 if you are running this UI via SSH") + for line in str(e).split("\n"): + TTkLog.error(line) def setText(self, text): TTkClipboard._clipboard = text From 4853fd9c5ee642fd6778760e39454bde23459f5d Mon Sep 17 00:00:00 2001 From: Eugenio Parodi Date: Thu, 22 Feb 2024 11:44:41 +0000 Subject: [PATCH 2/4] Semi Factorize the clipboard manager --- TermTk/TTkGui/clipboard.py | 51 ++++++++++++++++++++++++-------------- 1 file changed, 32 insertions(+), 19 deletions(-) diff --git a/TermTk/TTkGui/clipboard.py b/TermTk/TTkGui/clipboard.py index a59e0984..3e621148 100644 --- a/TermTk/TTkGui/clipboard.py +++ b/TermTk/TTkGui/clipboard.py @@ -26,53 +26,66 @@ from TermTk.TTkCore.log import TTkLog class TTkClipboard(): - _clipboard = '' - __slots__ = ('_setText', '_text') + _manager = None + _setText = None + _text = None def __init__(self) -> None: - self._setText = None - self._text = None + if not TTkClipboard._manager: + TTkClipboard._loadClipboardManager() + + @staticmethod + def _loadClipboardManager(): try: if importlib.util.find_spec('copykitten'): TTkLog.info("Using 'copykitten' as clipboard manager") import copykitten as _c - self._setText = _c.copy - self._text = _c.paste + TTkClipboard._manager = _c + TTkClipboard._setText = _c.copy + TTkClipboard._text = _c.paste elif importlib.util.find_spec('pyperclip'): TTkLog.info("Using 'pyperclip' as clipboard manager") import pyperclip as _c - self._setText = _c.copy - self._text = _c.paste + TTkClipboard._manager = _c + TTkClipboard._setText = _c.copy + TTkClipboard._text = _c.paste elif importlib.util.find_spec('pyperclip3'): TTkLog.info("Using 'pyperclip3' as clipboard manager") import pyperclip3 as _c - self._setText = _c.copy - self._text = _c.paste + TTkClipboard._manager = _c + TTkClipboard._setText = _c.copy + TTkClipboard._text = _c.paste elif importlib.util.find_spec('clipboard'): TTkLog.info("Using 'clipboard' as clipboard manager") import clipboard as _c - self._setText = _c.copy - self._text = _c.paste + TTkClipboard._manager = _c + TTkClipboard._setText = _c.copy + TTkClipboard._text = _c.paste + else: + TTkLog.info("No clipboard manager found") + TTkClipboard._manager = "Not Found" except Exception as e: TTkLog.error("Clipboard error, try to export X11 if you are running this UI via SSH") for line in str(e).split("\n"): TTkLog.error(line) - def setText(self, text): + @staticmethod + def setText(text): TTkClipboard._clipboard = text - if self._setText: + if TTkClipboard._setText: try: - self._setText(str(text)) + TTkClipboard._setText(str(text)) except Exception as e: TTkLog.error("Clipboard error, try to export X11 if you are running this UI via SSH") for line in str(e).split("\n"): TTkLog.error(line) - def text(self): - if self._text: - txt = self._text() + @staticmethod + def text(): + if TTkClipboard._text: + txt = TTkClipboard._text() if txt == str(TTkClipboard._clipboard): return TTkClipboard._clipboard else: - return self._text() + return TTkClipboard._text() return TTkClipboard._clipboard From 5a96a9db62f731e4641d6c919e87e1ea4a3047f0 Mon Sep 17 00:00:00 2001 From: Eugenio Parodi Date: Thu, 22 Feb 2024 13:52:27 +0000 Subject: [PATCH 3/4] Added pyclip support in TTkClipboard --- TermTk/TTkGui/clipboard.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/TermTk/TTkGui/clipboard.py b/TermTk/TTkGui/clipboard.py index 3e621148..4ce0f173 100644 --- a/TermTk/TTkGui/clipboard.py +++ b/TermTk/TTkGui/clipboard.py @@ -26,6 +26,28 @@ from TermTk.TTkCore.log import TTkLog class TTkClipboard(): + '''TTkClipboard + + :ref:`Clipboard` + + Example: + + .. code:: python + + from TermTk import TTkClipboard + + # Initialize the clipboard manager + clipboard = TTkClipboard() + + # Push some text to the clipboard + clipboard.setText("Example") + + # Get the text from the clipboard + text = clipboard.text() + + + ''' + _clipboard = "" _manager = None _setText = None _text = None @@ -55,6 +77,12 @@ def _loadClipboardManager(): TTkClipboard._manager = _c TTkClipboard._setText = _c.copy TTkClipboard._text = _c.paste + elif importlib.util.find_spec('pyclip'): + TTkLog.info("Using 'pyclip' as clipboard manager") + import pyclip as _c + TTkClipboard._manager = _c + TTkClipboard._setText = _c.copy + TTkClipboard._text = _c.paste elif importlib.util.find_spec('clipboard'): TTkLog.info("Using 'clipboard' as clipboard manager") import clipboard as _c @@ -71,6 +99,7 @@ def _loadClipboardManager(): @staticmethod def setText(text): + '''setText''' TTkClipboard._clipboard = text if TTkClipboard._setText: try: @@ -82,6 +111,7 @@ def setText(text): @staticmethod def text(): + '''text''' if TTkClipboard._text: txt = TTkClipboard._text() if txt == str(TTkClipboard._clipboard): From 931ed3e8c825cbc20593daaeb8a77bbdad1f20d8 Mon Sep 17 00:00:00 2001 From: Eugenio Parodi Date: Thu, 22 Feb 2024 13:57:37 +0000 Subject: [PATCH 4/4] updated clipboard documentation --- docs/source/info/features/index.rst | 4 +- docs/source/info/resources/clipboard.rst | 63 +++++++++++++++++++++++- docs/source/info/resources/dragdrop.rst | 6 +++ docs/source/info/resources/index.rst | 4 +- 4 files changed, 73 insertions(+), 4 deletions(-) diff --git a/docs/source/info/features/index.rst b/docs/source/info/features/index.rst index 957308f0..bfdc0ecd 100644 --- a/docs/source/info/features/index.rst +++ b/docs/source/info/features/index.rst @@ -31,9 +31,9 @@ Main features * Input/Mouse/Paste Event handling -* Drag and Drop +* :ref:`Drag and Drop ` -* Clipboard support +* :ref:`Clipboard` support * Drawing primitives diff --git a/docs/source/info/resources/clipboard.rst b/docs/source/info/resources/clipboard.rst index 2fd9f957..54b42c43 100644 --- a/docs/source/info/resources/clipboard.rst +++ b/docs/source/info/resources/clipboard.rst @@ -1 +1,62 @@ -TBD \ No newline at end of file +.. _clipboard: + +========= +Clipboard +========= + +.. _pyTermTk: https://github.com/ceccopierangiolieugenio/pyTermTk + + +pyTermTk_ include a clipboard wrapper :class:`~TermTk.TTkGui.clipboard.TTkClipboard`, around any of the following libraries: + +- `copykitten `_ - Robust, dependency-free way to use the system clipboard in Python. +- `pyperclip `_ - Python module for cross-platform clipboard functions. +- `pyperclip3 `_ / `pyclip `_ - Cross-platform Clipboard module for Python with binary support. +- `clipboard `_ - A cross platform clipboard operation library of Python. Works for Windows, Mac and Linux. + +.. raw:: html + + + +The basic pyTermTk_ does not include any of those clipboard managers. +An internal implementation whitin the scope of the app itself is still available. + +If any of the previous listed clipboard managers are installed, pyTermTk_ is able to automatically detect and use them. + +i.e. + +.. code:: bash + + # Assuming no clipboard managers are installed + # you can still copy/paste between editors in this session + # but no text is copied to/from the system clipboard + python3 demo/showcase/textedit.py + + # if pyperclip is installed, + # pyTermTk defaults the clipboard manager to this tool + # any copy/paste is synced with the system clipboard + # it is possible to copy/paste from/to an external editor + pip install pyperclip + python3 demo/showcase/textedit.py + +----- +Usage +----- + +Once initialized the clipboard manager, 2 apis are provided that can be used to access the clipboard (:class:`~TermTk.TTkGui.clipboard.TTkClipboard.setText`, :class:`~TermTk.TTkGui.clipboard.TTkClipboard.text`) + + .. code:: python + + from TermTk import TTkClipboard + + # Initialize the clipboard manager + clipboard = TTkClipboard() + + # Push some text to the clipboard + clipboard.setText("Example") + + # Get the text from the clipboard + text = clipboard.text() \ No newline at end of file diff --git a/docs/source/info/resources/dragdrop.rst b/docs/source/info/resources/dragdrop.rst index 2fd9f957..90ce027f 100644 --- a/docs/source/info/resources/dragdrop.rst +++ b/docs/source/info/resources/dragdrop.rst @@ -1 +1,7 @@ +.. _DnD: + +============= +Drag and Drop +============= + TBD \ No newline at end of file diff --git a/docs/source/info/resources/index.rst b/docs/source/info/resources/index.rst index 1f19488a..7313e546 100644 --- a/docs/source/info/resources/index.rst +++ b/docs/source/info/resources/index.rst @@ -6,4 +6,6 @@ Resources :maxdepth: 1 :hidden: - modal \ No newline at end of file + clipboard + modal + dragdrop \ No newline at end of file