Skip to content

Commit

Permalink
oweditdomain: Clear domain_change_hints for key that was used to restore
Browse files Browse the repository at this point in the history
  • Loading branch information
ales-erjavec committed Aug 5, 2024
1 parent dee7b83 commit 27280fd
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 10 deletions.
25 changes: 16 additions & 9 deletions Orange/widgets/data/oweditdomain.py
Original file line number Diff line number Diff line change
Expand Up @@ -1580,6 +1580,9 @@ def variable_icon(var):
#: Any warnings applying to the transform (`list[tuple[Msg, str]]`)
RestoreWarningRole = TransformRole + 1

#: Hint key that was used to load stored settings.
RestoreHintKey = RestoreWarningRole + 1


class VariableEditDelegate(QStyledItemDelegate):
ReinterpretNames = {
Expand Down Expand Up @@ -2133,12 +2136,12 @@ def reset_selected(self):
modified = []
for ind in self.selected_var_indices():
midx = model.index(ind)
model.setData(midx, [], TransformRole)
model.setData(midx, None, RestoreWarningRole)
if midx.data(TransformRole):
model.setData(midx, [], TransformRole)
var = midx.data(Qt.EditRole)
self._store_transform(var, [])
modified.append(var)
key = model.data(midx, RestoreHintKey)
var = midx.data(Qt.EditRole)
self._domain_change_hints.pop(key, None)
modified.append(var)
if modified:
with disconnected(editor.variable_changed,
self._on_variable_changed):
Expand All @@ -2154,6 +2157,8 @@ def reset_all(self):
midx = model.index(i)
model.setData(midx, [], TransformRole)
model.setData(midx, None, RestoreWarningRole)
key = model.data(midx, RestoreHintKey)
self._domain_change_hints.pop(key, None)
self.open_editor()
self._invalidate()
self._update_restore_warnings()
Expand Down Expand Up @@ -2224,6 +2229,7 @@ def _restore(self):
tr, msgs = self._sanitize_transform(coldesc.vtype, tr)
model.setData(midx, tr, TransformRole)
model.setData(midx, msgs, RestoreWarningRole)
model.setData(midx, key, RestoreHintKey)
if first_key is None:
first_key = key
# Reduce the number of hints to MAX_HINTS, but keep all current hints
Expand Down Expand Up @@ -2251,7 +2257,7 @@ def messages(midx):
)
self.Warning.cat_mapping_does_not_apply.clear()
# Show warnings for non-applicable transforms
for msg, names in groupby(msgs, key=itemgetter(0)):
for msg, names in groupby(sorted(msgs), key=itemgetter(0)):
msg(", ".join(map(itemgetter(1), names)))

def _on_selection_changed(self, _, deselected):
Expand Down Expand Up @@ -2308,15 +2314,16 @@ def _on_variable_changed(self):
self._update_restore_warnings()

def _store_transform(
self, var: Variable, transform: Iterable[Transform], deconvar=None
self, var: Variable, transform: Sequence[Transform] | None, deconvar=None
) -> None:
deconvar = deconvar or deconstruct(var)
# Remove the existing key (if any) to put the new one at the end,
# to make sure it comes after the sentinel
self._domain_change_hints.pop(deconvar, None)
# pylint: disable=unsupported-assignment-operation
self._domain_change_hints[deconvar] = \
[deconstruct(t) for t in transform]
if transform:
self._domain_change_hints[deconvar] = \
[deconstruct(t) for t in transform]

def _find_stored_transform(
self, var: Variable
Expand Down
16 changes: 15 additions & 1 deletion Orange/widgets/data/tests/test_oweditdomain.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@
VariableEditDelegate, TransformRole,
RealVector, TimeVector, StringVector, make_dict_mapper,
LookupMappingTransform, as_float_or_nan, column_str_repr,
GroupItemsDialog, VariableListModel, StrpTime, RestoreOriginal, BaseEditor
GroupItemsDialog, VariableListModel, StrpTime, RestoreOriginal, BaseEditor,
RestoreWarningRole
)
from Orange.widgets.data.owcolor import OWColor, ColorRole
from Orange.widgets.tests.base import WidgetTest, GuiTest
Expand Down Expand Up @@ -383,6 +384,12 @@ def restore(state):
self.assertEqual(output.domain.class_var.values,
("Iris-setosa", "Iris-versicolor", "Iris-virginica"))

restore({viris_1: [("Rename", ("K",),),
("CategoriesMapping", ([("A", "AA")],))]})
self.assertTrue(w.Warning.cat_mapping_does_not_apply.is_shown())
w.reset_all()
self.assertFalse(w.Warning.cat_mapping_does_not_apply.is_shown())

select_row(w.variables_view, 4)
w.reset_selected()
self.assertFalse(w.Warning.cat_mapping_does_not_apply.is_shown())
Expand Down Expand Up @@ -1227,6 +1234,13 @@ def get_style_option(row: int) -> QStyleOptionViewItem:
)
p.assert_called_once()

set_item(1, {
TransformRole: Rename("bb"),
RestoreWarningRole: ("bb", "aa"),
})
opt = get_style_option(1)
self.assertEqual(opt.palette.color(QPalette.Text), QColor(Qt.yellow))


class TestTransforms(TestCase):
def _test_common(self, var):
Expand Down

0 comments on commit 27280fd

Please sign in to comment.