Skip to content

Commit

Permalink
owpaintdata: Fix undo of add new class label
Browse files Browse the repository at this point in the history
'undoable' param was passed from triggered signal (checked state).
This was wrong.
  • Loading branch information
ales-erjavec committed Jan 21, 2025
1 parent 6c4af05 commit b7673dc
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 7 deletions.
9 changes: 2 additions & 7 deletions Orange/widgets/data/owpaintdata.py
Original file line number Diff line number Diff line change
Expand Up @@ -1074,19 +1074,14 @@ def reset_to_input(self):

self.commit.deferred()

def add_new_class_label(self, undoable=True):

def add_new_class_label(self):
newlabel = next(label for label in namegen('C', 1)
if label not in self.class_model)

command = SimpleUndoCommand(
lambda: self.class_model.append(newlabel),
lambda: self.class_model.__delitem__(-1)
)
if undoable:
self.undo_stack.push(command)
else:
command.redo()
self.undo_stack.push(command)

def remove_selected_class_label(self):
index = self.selected_class_label()
Expand Down
27 changes: 27 additions & 0 deletions Orange/widgets/data/tests/test_owpaintdata.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from orangecanvas.gui.test import mouseMove

from Orange.data import Table, DiscreteVariable, ContinuousVariable, Domain
from Orange.widgets.utils import itemmodels
from Orange.widgets.data import owpaintdata
from Orange.widgets.data.owpaintdata import OWPaintData
from Orange.widgets.tests.base import WidgetTest, datasets
Expand Down Expand Up @@ -189,6 +190,32 @@ def assert_close(p1, p2):
w.undo_stack.undo()
assert_close(w.data[0], p0)

def test_add_remove_class(self):
def put_instance():
w.set_current_tool(owpaintdata.PutInstanceTool)
QTest.mouseClick(viewport, Qt.LeftButton)

def assert_class_column_equal(data):
assert_array_equal(np.array(w.data)[:, 2].ravel(), data)

w = self.widget
viewport = w.plotview.viewport()
put_instance()
itemmodels.select_row(w.classValuesView, 1)
put_instance()
w.add_new_class_label()
itemmodels.select_row(w.classValuesView, 2)
put_instance()
self.assertSequenceEqual(w.class_model, ["C1", "C2", "C3"])
assert_class_column_equal([0, 1, 2])
itemmodels.select_row(w.classValuesView, 0)
w.remove_selected_class_label()
self.assertSequenceEqual(w.class_model, ["C2", "C3"])
assert_class_column_equal([0, 1])
w.undo_stack.undo()
self.assertSequenceEqual(w.class_model, ["C1", "C2", "C3"])
assert_class_column_equal([0, 1, 2])


class TestCommands(unittest.TestCase):
def test_merge_cmd(self): # pylint: disable=import-outside-toplevel
Expand Down

0 comments on commit b7673dc

Please sign in to comment.