diff --git a/glue/viewers/table/qt/tests/test_data_viewer.py b/glue/viewers/table/qt/tests/test_data_viewer.py index 2f9b78671..07314fac7 100644 --- a/glue/viewers/table/qt/tests/test_data_viewer.py +++ b/glue/viewers/table/qt/tests/test_data_viewer.py @@ -549,3 +549,65 @@ def test_table_with_dask_column(): colors = ['#aa0000', '#aa0000', '#aa0000', None, None] check_values_and_color(model, data, colors) + + +def test_table_preserve_model_after_selection(): + + # Regression test for a bug that caused table viewers to return + # to default sorting after a new subset was created with the row + # selection tool. This occurred because the model was reset. + + app = get_qapp() # noqa + + d = Data(a=[1, 2, 3, 4, 5], + b=[3.2, 1.2, 4.5, 3.3, 2.2], + c=['e', 'b', 'c', 'a', 'f'], label='test') + + dc = DataCollection([d]) + + gapp = GlueApplication(dc) + + viewer = gapp.new_data_viewer(TableViewer) + viewer.add_data(d) + + model = viewer.ui.table.model() + + model.sort(1, Qt.AscendingOrder) + + data = {'a': [2, 5, 1, 4, 3], + 'b': [1.2, 2.2, 3.2, 3.3, 4.5], + 'c': ['b', 'f', 'e', 'a', 'c']} + colors = [None for _ in range(5)] + + check_values_and_color(model, data, colors) + + # Create a new subset using the row selection tool + + subset_mode = gapp._session.edit_subset_mode + subset_mode.edit_subset = None + viewer.toolbar.actions['table:rowselect'].toggle() + + def press_key(key): + event = QtGui.QKeyEvent(QtCore.QEvent.KeyPress, key, Qt.NoModifier) + app.postEvent(viewer.ui.table, event) + app.processEvents() + + # Select the second row + press_key(Qt.Key_Tab) + press_key(Qt.Key_Down) + press_key(Qt.Key_Enter) + + process_events() + + # Check that the table model is still the same, which it + # should be since we aren't changing the viewer Data + + post_model = viewer.ui.table.model() + assert post_model == model + + # Check that the order is still the same + + color = d.subsets[0].style.color + colors[1] = color + + check_values_and_color(post_model, data, colors)