Add utils.supports_selection()

This commit is contained in:
Florian Bruhin 2016-05-08 22:06:00 +02:00
parent 3e6ac28c66
commit b9b6f357da
4 changed files with 18 additions and 11 deletions

View File

@ -688,7 +688,7 @@ class CommandDispatcher:
s = self._current_url().toString(flags) s = self._current_url().toString(flags)
what = 'URL' what = 'URL'
if sel and QApplication.clipboard().supportsSelection(): if sel and utils.supports_selection():
target = "primary selection" target = "primary selection"
else: else:
sel = False sel = False
@ -833,7 +833,7 @@ class CommandDispatcher:
bg: Open in a background tab. bg: Open in a background tab.
window: Open in new window. window: Open in new window.
""" """
if sel and QApplication.clipboard().supportsSelection(): if sel and utils.supports_selection():
target = "Primary selection" target = "Primary selection"
else: else:
sel = False sel = False
@ -1756,7 +1756,7 @@ class CommandDispatcher:
message.info(self._win_id, "Nothing to yank") message.info(self._win_id, "Nothing to yank")
return return
if sel and QApplication.clipboard().supportsSelection(): if sel and utils.supports_selection():
target = "primary selection" target = "primary selection"
else: else:
sel = False sel = False

View File

@ -28,7 +28,6 @@ import string
from PyQt5.QtCore import (pyqtSignal, pyqtSlot, QObject, QEvent, Qt, QUrl, from PyQt5.QtCore import (pyqtSignal, pyqtSlot, QObject, QEvent, Qt, QUrl,
QTimer) QTimer)
from PyQt5.QtGui import QMouseEvent from PyQt5.QtGui import QMouseEvent
from PyQt5.QtWidgets import QApplication
from PyQt5.QtWebKit import QWebElement from PyQt5.QtWebKit import QWebElement
from PyQt5.QtWebKitWidgets import QWebPage from PyQt5.QtWebKitWidgets import QWebPage
@ -492,7 +491,7 @@ class HintManager(QObject):
context: The HintContext to use. context: The HintContext to use.
""" """
if (context.target == Target.yank_primary and if (context.target == Target.yank_primary and
QApplication.clipboard().supportsSelection()): utils.supports_selection()):
sel = True sel = True
else: else:
sel = False sel = False

View File

@ -757,22 +757,20 @@ def newest_slice(iterable, count):
def set_clipboard(data, selection=False): def set_clipboard(data, selection=False):
"""Set the clipboard to some given data.""" """Set the clipboard to some given data."""
clipboard = QApplication.clipboard() if selection and not supports_selection():
if selection and not clipboard.supportsSelection():
raise SelectionUnsupportedError raise SelectionUnsupportedError
if log_clipboard: if log_clipboard:
what = 'primary selection' if selection else 'clipboard' what = 'primary selection' if selection else 'clipboard'
log.misc.debug("Setting fake {}: {}".format(what, json.dumps(data))) log.misc.debug("Setting fake {}: {}".format(what, json.dumps(data)))
else: else:
mode = QClipboard.Selection if selection else QClipboard.Clipboard mode = QClipboard.Selection if selection else QClipboard.Clipboard
clipboard.setText(data, mode=mode) QApplication.clipboard().setText(data, mode=mode)
def get_clipboard(selection=False): def get_clipboard(selection=False):
"""Get data from the clipboard.""" """Get data from the clipboard."""
global fake_clipboard global fake_clipboard
clipboard = QApplication.clipboard() if selection and not supports_selection():
if selection and not clipboard.supportsSelection():
raise SelectionUnsupportedError raise SelectionUnsupportedError
if fake_clipboard is not None: if fake_clipboard is not None:
@ -780,6 +778,11 @@ def get_clipboard(selection=False):
fake_clipboard = None fake_clipboard = None
else: else:
mode = QClipboard.Selection if selection else QClipboard.Clipboard mode = QClipboard.Selection if selection else QClipboard.Clipboard
data = clipboard.text(mode=mode) data = QApplication.clipboard().text(mode=mode)
return data return data
def supports_selection():
"""Check if the OS supports primary selection."""
return QApplication.clipboard().supportsSelection()

View File

@ -979,3 +979,8 @@ class TestGetSetClipboard:
utils.fake_clipboard = 'fake clipboard text' utils.fake_clipboard = 'fake clipboard text'
utils.get_clipboard(selection=selection) utils.get_clipboard(selection=selection)
assert utils.fake_clipboard is None assert utils.fake_clipboard is None
@pytest.mark.parametrize('selection', [True, False])
def test_supports_selection(self, clipboard_mock, selection):
clipboard_mock.supportsSelection.return_value = selection
assert utils.supports_selection == selection