Remove empty text items when leaving editing mode

This commit is contained in:
Rebecca Breu 2023-11-28 21:37:57 +01:00
parent a0d09692d7
commit bf6d138241
3 changed files with 24 additions and 4 deletions

View file

@ -14,6 +14,8 @@ Added
large zoom factor are exempt to make sure that icons, pixel sprites
etc can be viewed correctly.
* 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
Changed
@ -21,7 +23,6 @@ Changed
* "Save as" will now open pre-select the folder of the currently opened file
* "Save" and "Save as" are now inactive when the scene is empty
* Editing of text items will now be undoable after leaving edit mode
Fixed

View file

@ -556,6 +556,10 @@ class BeeTextItem(BeeItemMixin, QtWidgets.QGraphicsTextItem):
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')
self.scene().undo_stack.push(
commands.DeleteItems(self.scene(), [self]))
def has_selection_handles(self):
return super().has_selection_handles() and not self.edit_mode
@ -564,7 +568,6 @@ class BeeTextItem(BeeItemMixin, QtWidgets.QGraphicsTextItem):
if (event.key() in (Qt.Key.Key_Enter, Qt.Key.Key_Return)
and event.modifiers() == Qt.KeyboardModifier.NoModifier):
self.exit_edit_mode()
self.scene().edit_item = None
event.accept()
return
super().keyPressEvent(event)

View file

@ -215,6 +215,7 @@ def test_enter_edit_mode(view):
def test_exit_edit_mode(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()
@ -224,6 +225,23 @@ def test_exit_edit_mode(setcursor_mock, cursor_mock, view):
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
def test_exit_edit_mode_when_text_empty(view):
item = BeeTextItem(' \r\n\t')
item.edit_mode = True
item.old_text = 'old'
view.scene.addItem(item)
view.scene.edit_item = item
item.exit_edit_mode()
assert item.edit_mode is False
assert view.scene.edit_item is None
flags = item.textInteractionFlags()
assert flags == Qt.TextInteractionFlag.NoTextInteraction
assert item.scene() is None
assert view.scene.items() == []
assert view.scene.edit_item is None
@patch('PyQt6.QtWidgets.QGraphicsTextItem.keyPressEvent')
@ -283,7 +301,6 @@ def test_key_press_event_return(exit_mock, key_press_mock, view):
item.keyPressEvent(event)
key_press_mock.assert_not_called()
exit_mock.assert_called_once_with()
assert view.scene.edit_item is None
@patch('PyQt6.QtWidgets.QGraphicsTextItem.keyPressEvent')
@ -298,7 +315,6 @@ def test_key_press_event_enter(exit_mock, key_press_mock, view):
item.keyPressEvent(event)
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):