Skip to content

Commit

Permalink
Ontology - Delete element with delete button
Browse files Browse the repository at this point in the history
  • Loading branch information
PrimozGodec committed Feb 21, 2023
1 parent 1b10efb commit 23b8c59
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
9 changes: 8 additions & 1 deletion orangecontrib/text/widgets/owontology.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from AnyQt.QtCore import Qt, QModelIndex, QItemSelection, Signal, \
QItemSelectionModel
from AnyQt.QtGui import QDropEvent, QStandardItemModel, QStandardItem, \
QPainter, QColor, QPalette, QDragEnterEvent, QDragLeaveEvent
QPainter, QColor, QPalette, QDragEnterEvent, QDragLeaveEvent, QKeyEvent
from AnyQt.QtWidgets import QWidget, QAction, QVBoxLayout, QTreeView, QMenu, \
QToolButton, QGroupBox, QListView, QSizePolicy, QStyledItemDelegate, \
QStyleOptionViewItem, QLineEdit, QFileDialog, QApplication, QDialog, \
Expand Down Expand Up @@ -356,6 +356,13 @@ def _set_from_stack(self):
self._enable_undo_redo()
self.dataChanged.emit()

def keyPressEvent(self, event: QKeyEvent):
"""Delete element with delete or backspace key"""
if event.key() == Qt.Key_Delete or event.key() == Qt.Key_Backspace:
self.__on_remove_recursive()
else:
super().keyPressEvent(event)


class Ontology:
NotModified, Modified = range(2)
Expand Down
24 changes: 23 additions & 1 deletion orangecontrib/text/widgets/tests/test_owontology.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
import numpy as np
from AnyQt.QtCore import Qt, QItemSelectionModel, QItemSelection, \
QItemSelectionRange
from AnyQt.QtWidgets import QFileDialog, QPushButton
from AnyQt.QtWidgets import QFileDialog
from AnyQt.QtTest import QTest

from Orange.data import Table
from Orange.widgets.tests.base import WidgetTest
Expand Down Expand Up @@ -99,6 +100,27 @@ def test_on_remove_recursive(self):
self.view._EditableTreeView__on_remove_recursive()
self.assertEqual(self.view.get_data(), {})

def test_on_remove_with_delete_key(self):
# test with delete button
self.view.set_data(self.data)
model = self.view._EditableTreeView__model
sel_model = self.view._EditableTreeView__tree.selectionModel()
sel_model.select(model.index(0, 0), QItemSelectionModel.ClearAndSelect)

self.assertEqual(self.view.get_data(), {"foo": {"bar": {}, "baz": {}}})
QTest.keyClick(self.view, Qt.Key_Delete)
self.assertDictEqual({}, self.view.get_data())

def test_on_remove_with_backspace_key(self):
self.view.set_data(self.data)
model = self.view._EditableTreeView__model
sel_model = self.view._EditableTreeView__tree.selectionModel()
sel_model.select(model.index(0, 0), QItemSelectionModel.ClearAndSelect)

self.assertEqual(self.view.get_data(), {"foo": {"bar": {}, "baz": {}}})
QTest.keyClick(self.view, Qt.Key_Backspace)
self.assertDictEqual({}, self.view.get_data())

def test_get_words(self):
self.view.set_data(self.data)
self.assertEqual(self.view.get_words(), ["foo", "bar", "baz"])
Expand Down

0 comments on commit 23b8c59

Please sign in to comment.