From 7f9fc8944c060c580dc0e633c32bc1b5f3676dbe Mon Sep 17 00:00:00 2001 From: Rebecca Breu Date: Mon, 30 Aug 2021 11:08:11 +0200 Subject: [PATCH] Fix copying text items --- CHANGELOG.rst | 2 ++ beeref/items.py | 6 ++++++ beeref/view.py | 2 +- tests/items/test_pixmapitem.py | 9 ++++++++- tests/items/test_textitem.py | 7 +++++++ tests/test_view.py | 18 ++++++++++++++++-- 6 files changed, 40 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 6863a99..89bcccc 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -7,6 +7,8 @@ Added ----- * You can now add plain text notes and paste text from the clipboard +* You can now open bee files from finder on MacOS (by Davin Andrs) + Changed ------- diff --git a/beeref/items.py b/beeref/items.py index 2cd0c28..fd408e3 100644 --- a/beeref/items.py +++ b/beeref/items.py @@ -136,6 +136,9 @@ class BeePixmapItem(BeeItemMixin, QtWidgets.QGraphicsPixmapItem): item.do_flip() return item + def copy_to_clipboard(self, clipboard): + clipboard.setPixmap(self.pixmap()) + @register_item class BeeTextItem(BeeItemMixin, QtWidgets.QGraphicsTextItem): @@ -219,3 +222,6 @@ class BeeTextItem(BeeItemMixin, QtWidgets.QGraphicsTextItem): event.accept() return super().keyPressEvent(event) + + def copy_to_clipboard(self, clipboard): + clipboard.setText(self.toPlainText()) diff --git a/beeref/view.py b/beeref/view.py index 654059f..5b1fcb9 100644 --- a/beeref/view.py +++ b/beeref/view.py @@ -438,7 +438,7 @@ class BeeGraphicsView(QtWidgets.QGraphicsView, ActionsMixin): # At the moment, we can only copy one image to the global # clipboard. (Later, we might create an image of the whole # selection for external copying.) - clipboard.setPixmap(items[0].pixmap()) + items[0].copy_to_clipboard(clipboard) # However, we can copy all items to the internal clipboard: self.scene.copy_selection_to_internal_clipboard() diff --git a/tests/items/test_pixmapitem.py b/tests/items/test_pixmapitem.py index ee95b87..27f809e 100644 --- a/tests/items/test_pixmapitem.py +++ b/tests/items/test_pixmapitem.py @@ -1,6 +1,6 @@ from unittest.mock import patch, MagicMock, PropertyMock -from PyQt6 import QtCore, QtGui +from PyQt6 import QtCore, QtGui, QtWidgets from beeref.items import BeePixmapItem, item_registry @@ -174,3 +174,10 @@ def test_create_copy(qapp, imgfilename3x3): assert item.flip() == -1 assert item.zValue() == 0.5 assert item.scale() == 2.2 + + +def test_item_to_clipboard(qapp, imgfilename3x3): + clipboard = QtWidgets.QApplication.clipboard() + item = BeePixmapItem(QtGui.QImage(imgfilename3x3), 'foo.png') + item.copy_to_clipboard(clipboard) + assert clipboard.pixmap().size() == item.pixmap().size() diff --git a/tests/items/test_textitem.py b/tests/items/test_textitem.py index 32bfad7..b2a5485 100644 --- a/tests/items/test_textitem.py +++ b/tests/items/test_textitem.py @@ -299,3 +299,10 @@ def test_key_press_event_enter(exit_mock, key_press_mock, view): key_press_mock.assert_not_called() exit_mock.assert_called_once_with() assert view.scene.edit_item is None + + +def test_item_to_clipboard(qapp): + clipboard = QtWidgets.QApplication.clipboard() + item = BeeTextItem('foo bar') + item.copy_to_clipboard(clipboard) + assert clipboard.text() == 'foo bar' diff --git a/tests/test_view.py b/tests/test_view.py index 68dc98a..6c3ca36 100644 --- a/tests/test_view.py +++ b/tests/test_view.py @@ -7,7 +7,7 @@ from PyQt6 import QtCore, QtGui, QtWidgets from PyQt6.QtCore import Qt from beeref.config import logfile_name -from beeref.items import BeePixmapItem +from beeref.items import BeePixmapItem, BeeTextItem from beeref.view import BeeGraphicsView @@ -333,7 +333,7 @@ def test_on_action_insert_text(clear_mock, view): @patch('PyQt6.QtWidgets.QApplication.clipboard') -def test_on_action_copy(clipboard_mock, view, imgfilename3x3): +def test_on_action_copy_image(clipboard_mock, view, imgfilename3x3): item = BeePixmapItem(QtGui.QImage(imgfilename3x3)) view.scene.addItem(item) item.setSelected(True) @@ -346,6 +346,20 @@ def test_on_action_copy(clipboard_mock, view, imgfilename3x3): assert mimedata.data('beeref/items') == b'1' +@patch('PyQt6.QtWidgets.QApplication.clipboard') +def test_on_action_copy_text(clipboard_mock, view, imgfilename3x3): + item = BeeTextItem('foo bar') + view.scene.addItem(item) + item.setSelected(True) + mimedata = QtCore.QMimeData() + clipboard_mock.return_value.mimeData.return_value = mimedata + view.on_action_copy() + + clipboard_mock.return_value.setText.assert_called_once_with('foo bar') + view.scene.internal_clipboard == [item] + assert mimedata.data('beeref/items') == b'1' + + @patch('beeref.scene.BeeGraphicsScene.clearSelection') @patch('PyQt6.QtGui.QClipboard.image') def test_on_action_paste_external(