From fe39bc3588690382543c6abe3d4dfc949a23245f Mon Sep 17 00:00:00 2001 From: Benoit Pierre Date: Tue, 28 Jun 2022 18:51:58 +0200 Subject: [PATCH 1/4] gui_qt/utils: add `ActionCopyViewSelctionToClipboard` helper Create an action that copy a view current selection to clipboard when triggered with the standard "copy" shortcut. --- plover/gui_qt/utils.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/plover/gui_qt/utils.py b/plover/gui_qt/utils.py index e51a17a2f..3549d596a 100644 --- a/plover/gui_qt/utils.py +++ b/plover/gui_qt/utils.py @@ -1,12 +1,26 @@ - from PyQt5.QtCore import QSettings +from PyQt5.QtGui import QGuiApplication, QKeySequence from PyQt5.QtWidgets import ( + QAction, QMainWindow, QToolBar, QToolButton, QWidget, ) +from plover import _ + + +def ActionCopyViewSelectionToClipboard(view): + def copy_selection_to_clipboard(): + indexes = view.selectedIndexes() + data = view.model().mimeData(indexes) + QGuiApplication.clipboard().setMimeData(data) + action = QAction(_('Copy selection to clipboard')) + action.setShortcut(QKeySequence(QKeySequence.Copy)) + action.triggered.connect(copy_selection_to_clipboard) + return action + def ToolButton(action): button = QToolButton() From 7b0a95b3b108693bf3bfc4a2880ee5e9109fe5e3 Mon Sep 17 00:00:00 2001 From: Benoit Pierre Date: Tue, 28 Jun 2022 18:53:54 +0200 Subject: [PATCH 2/4] gui_qt/paper_tape: support copying the selection to clipboard --- plover/gui_qt/paper_tape.py | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/plover/gui_qt/paper_tape.py b/plover/gui_qt/paper_tape.py index 76e94d241..62b180676 100644 --- a/plover/gui_qt/paper_tape.py +++ b/plover/gui_qt/paper_tape.py @@ -2,6 +2,7 @@ from PyQt5.QtCore import ( QAbstractListModel, + QMimeData, QModelIndex, Qt, ) @@ -16,9 +17,9 @@ from plover import _, system -from plover.gui_qt.paper_tape_ui import Ui_PaperTape -from plover.gui_qt.utils import ToolBar -from plover.gui_qt.tool import Tool +from .paper_tape_ui import Ui_PaperTape +from .utils import ActionCopyViewSelectionToClipboard, ToolBar +from .tool import Tool STYLE_PAPER, STYLE_RAW = ( @@ -98,6 +99,17 @@ def append(self, stroke): self._stroke_list.append(stroke) self.endInsertRows() + def mimeTypes(self): + return ['text/plain'] + + def mimeData(self, indexes): + data = QMimeData() + data.setText('\n'.join(filter(None, ( + self.data(index, Qt.DisplayRole) + for index in indexes + )))) + return data + class PaperTape(Tool, Ui_PaperTape): @@ -116,6 +128,9 @@ def __init__(self, engine): self.header.setContentsMargins(4, 0, 0, 0) self.styles.addItems(TAPE_STYLES) self.tape.setModel(self._model) + self.tape.setSelectionMode(self.tape.ExtendedSelection) + self._copy_action = ActionCopyViewSelectionToClipboard(self.tape) + self.tape.addAction(self._copy_action) # Toolbar. self.layout().addWidget(ToolBar( self.action_ToggleOnTop, From f4b604512892714c97a64b2738f2c9aab18c6b34 Mon Sep 17 00:00:00 2001 From: Benoit Pierre Date: Tue, 28 Jun 2022 18:55:00 +0200 Subject: [PATCH 3/4] gui_qt/suggestions_widget: support copying the selection to clipboard --- plover/gui_qt/suggestions_widget.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/plover/gui_qt/suggestions_widget.py b/plover/gui_qt/suggestions_widget.py index 9256c34e0..c6755a25c 100644 --- a/plover/gui_qt/suggestions_widget.py +++ b/plover/gui_qt/suggestions_widget.py @@ -1,5 +1,6 @@ from PyQt5.QtCore import ( QAbstractListModel, + QMimeData, QModelIndex, Qt, ) @@ -19,6 +20,8 @@ from plover import _ from plover.translation import escape_translation +from .utils import ActionCopyViewSelectionToClipboard + # i18n: Widget: “SuggestionsWidget”. NO_SUGGESTIONS_STRING = _('no suggestions') @@ -138,12 +141,26 @@ def extend(self, suggestion_list): self._suggestion_list.extend(suggestion_list) self.endInsertRows() + def mimeTypes(self): + return ['text/plain'] + + def mimeData(self, indexes): + data = QMimeData() + data.setText('\n'.join(filter(None, ( + self.data(index, Qt.AccessibleTextRole) + for index in indexes + )))) + return data + class SuggestionsWidget(QListView): def __init__(self, parent=None): super().__init__(parent=parent) self.setResizeMode(self.Adjust) + self.setSelectionMode(self.ExtendedSelection) + self._copy_action = ActionCopyViewSelectionToClipboard(self) + self.addAction(self._copy_action) self._model = SuggestionsModel() self._delegate = SuggestionsDelegate(self) self.setModel(self._model) From e6ff9b73a7762069fdc9067cdb7b5868f90f309d Mon Sep 17 00:00:00 2001 From: Benoit Pierre Date: Tue, 28 Jun 2022 19:04:22 +0200 Subject: [PATCH 4/4] add news entry --- news.d/feature/1539.ui.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 news.d/feature/1539.ui.md diff --git a/news.d/feature/1539.ui.md b/news.d/feature/1539.ui.md new file mode 100644 index 000000000..2ca9e6e2e --- /dev/null +++ b/news.d/feature/1539.ui.md @@ -0,0 +1 @@ +Change the paper tape / suggestions widget selection mode to "extended" (allow selecting multiple items, support shift/control), and allow copying the current selection to clipboard using the standard copy shortcut.