Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for copying the paper tape / suggestions widget selection to clipboard #1539

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions news.d/feature/1539.ui.md
Original file line number Diff line number Diff line change
@@ -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.
21 changes: 18 additions & 3 deletions plover/gui_qt/paper_tape.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from PyQt5.QtCore import (
QAbstractListModel,
QMimeData,
QModelIndex,
Qt,
)
Expand All @@ -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 = (
Expand Down Expand Up @@ -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):

Expand All @@ -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,
Expand Down
17 changes: 17 additions & 0 deletions plover/gui_qt/suggestions_widget.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from PyQt5.QtCore import (
QAbstractListModel,
QMimeData,
QModelIndex,
Qt,
)
Expand All @@ -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')
Expand Down Expand Up @@ -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)
Expand Down
16 changes: 15 additions & 1 deletion plover/gui_qt/utils.py
Original file line number Diff line number Diff line change
@@ -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()
Expand Down