mirror of
https://github.com/rbreu/beeref.git
synced 2026-03-11 08:54:28 +00:00
Exit edit mode with Escape
This commit is contained in:
parent
bf6d138241
commit
3f50c56c18
3 changed files with 33 additions and 7 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -547,19 +547,22 @@ class BeeTextItem(BeeItemMixin, QtWidgets.QGraphicsTextItem):
|
|||
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 @@ class BeeTextItem(BeeItemMixin, QtWidgets.QGraphicsTextItem):
|
|||
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):
|
||||
|
|
|
|||
|
|
@ -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):
|
||||
|
|
|
|||
Loading…
Reference in a new issue