Skip to content

Commit

Permalink
Merge pull request #2372 from Carifio24/table-select-sort
Browse files Browse the repository at this point in the history
Only recreate table model when Data layer changes
  • Loading branch information
dhomeier authored Mar 23, 2023
2 parents 416cf2f + 19fc3f9 commit 4de94b5
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 1 deletion.
9 changes: 8 additions & 1 deletion glue/viewers/table/qt/data_viewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ def _update_visible(self):
visible = np.zeros(self.order.shape, dtype=bool)
for layer_artist in self._table_viewer.layers:
if layer_artist.visible:
mask = layer_artist.layer.to_mask()
mask = layer_artist.layer.to_mask()[self.order]
if DASK_INSTALLED and isinstance(mask, da.Array):
mask = mask.compute()
visible |= mask
Expand Down Expand Up @@ -295,6 +295,13 @@ def _on_layers_changed(self, *args):
break
else:
return

# If we aren't changing the data layer, we don't need to
# reset the model, just update visible rows
if layer_state.layer == self.data:
self.model._update_visible()
return

self.data = layer_state.layer
self.setUpdatesEnabled(False)
self.model = DataTableModel(self)
Expand Down
62 changes: 62 additions & 0 deletions glue/viewers/table/qt/tests/test_data_viewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 4de94b5

Please sign in to comment.