Skip to content

Commit

Permalink
Merge pull request #948 from PrimozGodec/ontology-remove-on-delete-key
Browse files Browse the repository at this point in the history
[ENH] Remove elements with delete/backspace key
  • Loading branch information
VesnaT authored Feb 24, 2023
2 parents 13992c7 + e62e017 commit 6bb6202
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 3 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
9 changes: 8 additions & 1 deletion orangecontrib/text/widgets/owwordlist.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from AnyQt.QtCore import Qt, QModelIndex, QItemSelectionModel, \
QItemSelection, QItemSelectionRange, Signal
from AnyQt.QtGui import QKeySequence, QPalette, QColor, QPainter
from AnyQt.QtGui import QKeySequence, QPalette, QColor, QPainter, QKeyEvent
from AnyQt.QtWidgets import QListView, QSizePolicy, QGridLayout, QLineEdit, \
QRadioButton, QGroupBox, QToolButton, QMenu, QAction, QFileDialog, \
QStyledItemDelegate, QStyleOptionViewItem, QWidget
Expand Down Expand Up @@ -573,6 +573,13 @@ def _save_state(self):
self.word_list_library = [s.as_dict() for s in self.library_model]
self.words = self.words_model[:]

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

def send_report(self):
library = self.library_model[self.word_list_index].name \
if self.library_model else "/"
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
21 changes: 21 additions & 0 deletions orangecontrib/text/widgets/tests/test_owwordlist.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from AnyQt.QtCore import Qt
from AnyQt.QtWidgets import QGroupBox, QFileDialog
from AnyQt.QtTest import QTest

from Orange.data import Table, StringVariable, Domain
from Orange.widgets.tests.base import WidgetTest
Expand Down Expand Up @@ -239,6 +240,26 @@ def test_remove_word(self):
self.widget._OWWordList__on_remove_word()
self.assertIsNone(self.get_output(self.widget.Outputs.words))

def test_remove_word_del_key(self):
self.widget._set_selected_words([0])
QTest.keyClick(self.widget, Qt.Key_Delete)
output = self.get_output(self.widget.Outputs.words)
self.assertListEqual(list(output.metas[:, 0]), ["bar", "baz"])

QTest.keyClick(self.widget, Qt.Key_Delete)
QTest.keyClick(self.widget, Qt.Key_Delete)
self.assertIsNone(self.get_output(self.widget.Outputs.words))

def test_remove_word_backspace_key(self):
self.widget._set_selected_words([0])
QTest.keyClick(self.widget, Qt.Key_Backspace)
output = self.get_output(self.widget.Outputs.words)
self.assertListEqual(list(output.metas[:, 0]), ["bar", "baz"])

QTest.keyClick(self.widget, Qt.Key_Backspace)
QTest.keyClick(self.widget, Qt.Key_Backspace)
self.assertIsNone(self.get_output(self.widget.Outputs.words))

def test_remove_word_commit_invoked_once(self):
self.widget._set_selected_words([0, 1])
self.widget.commit = Mock()
Expand Down

0 comments on commit 6bb6202

Please sign in to comment.