From 3f50c56c1856339b623265f2cc2fdb2a676483a3 Mon Sep 17 00:00:00 2001 From: Rebecca Breu Date: Tue, 28 Nov 2023 21:46:37 +0100 Subject: [PATCH] Exit edit mode with Escape --- CHANGELOG.rst | 1 + beeref/items.py | 22 +++++++++++++++------- tests/items/test_textitem.py | 17 +++++++++++++++++ 3 files changed, 33 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 774ada6..97c30af 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -16,6 +16,7 @@ Added * A scene can now be exported to a single image (File -> Export Scene...) * Editing of text items will now be undoable after leaving edit mode * Empty text items will be deleted after leaving edit mode +* Text edit mode can now be aborted with Escape Changed diff --git a/beeref/items.py b/beeref/items.py index 754583a..be4b5ce 100644 --- a/beeref/items.py +++ b/beeref/items.py @@ -547,19 +547,22 @@ def enter_edit_mode(self): Qt.TextInteractionFlag.TextEditorInteraction) self.scene().edit_item = self - def exit_edit_mode(self): + def exit_edit_mode(self, commit=True): logger.debug(f'Exiting edit mode on {self}') self.edit_mode = False # reset selection: self.setTextCursor(QtGui.QTextCursor(self.document())) self.setTextInteractionFlags(Qt.TextInteractionFlag.NoTextInteraction) - self.scene().undo_stack.push( - commands.ChangeText(self, self.toPlainText(), self.old_text)) - self.scene().edit_item = None - if not self.toPlainText().strip(): - logger.debug(f'Removing empty text item') + if commit: self.scene().undo_stack.push( - commands.DeleteItems(self.scene(), [self])) + commands.ChangeText(self, self.toPlainText(), self.old_text)) + self.scene().edit_item = None + if not self.toPlainText().strip(): + logger.debug('Removing empty text item') + self.scene().undo_stack.push( + commands.DeleteItems(self.scene(), [self])) + else: + self.setPlainText(self.old_text) def has_selection_handles(self): return super().has_selection_handles() and not self.edit_mode @@ -570,6 +573,11 @@ def keyPressEvent(self, event): self.exit_edit_mode() event.accept() return + if (event.key() == Qt.Key.Key_Escape + and event.modifiers() == Qt.KeyboardModifier.NoModifier): + self.exit_edit_mode(commit=False) + event.accept() + return super().keyPressEvent(event) def copy_to_clipboard(self, clipboard): diff --git a/tests/items/test_textitem.py b/tests/items/test_textitem.py index ca527fc..e0e9f0c 100644 --- a/tests/items/test_textitem.py +++ b/tests/items/test_textitem.py @@ -244,6 +244,23 @@ def test_exit_edit_mode_when_text_empty(view): assert view.scene.edit_item is None +def test_exit_edit_mode_when_commit_false(setcursor_mock, cursor_mock, view): + item = BeeTextItem('foo bar') + item.edit_mode = True + item.old_text = 'old' + view.scene.addItem(item) + view.scene.edit_item = item + item.exit_edit_mode(commit=False) + assert item.edit_mode is False + assert view.scene.edit_item is None + flags = item.textInteractionFlags() + assert flags == Qt.TextInteractionFlag.NoTextInteraction + cursor_mock.assert_called_once_with(item.document()) + setcursor_mock.assert_called_once_with(cursor_mock.return_value) + assert view.scene.edit_item is None + assert item.toPlaintText() == 'old' + + @patch('PyQt6.QtWidgets.QGraphicsTextItem.keyPressEvent') @patch('beeref.items.BeeTextItem.exit_edit_mode') def test_key_press_event_any_key(exit_mock, key_press_mock, view):