Skip to content

Commit

Permalink
Exit edit mode with Escape
Browse files Browse the repository at this point in the history
  • Loading branch information
rbreu committed Nov 28, 2023
1 parent bf6d138 commit 3f50c56
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 15 additions & 7 deletions beeref/items.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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):
Expand Down
17 changes: 17 additions & 0 deletions tests/items/test_textitem.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit 3f50c56

Please sign in to comment.