Skip to content

Commit e00056b

Browse files
committed
owtable: output sorted data
1 parent 3a27881 commit e00056b

File tree

2 files changed

+29
-35
lines changed

2 files changed

+29
-35
lines changed

Orange/widgets/data/owtable.py

Lines changed: 3 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -602,6 +602,7 @@ def _setup_table_view(self, view, data):
602602
header = view.horizontalHeader()
603603
header.setSectionsClickable(is_sortable(data))
604604
header.setSortIndicatorShown(is_sortable(data))
605+
header.sortIndicatorChanged.connect(self.update_selection)
605606

606607
view.setModel(datamodel)
607608

@@ -856,7 +857,6 @@ def get_selection(view):
856857
rows = numpy.array(rows, dtype=numpy.intp)
857858
# map the rows through the applied sorting (if any)
858859
rows = model.mapToSourceRows(rows)
859-
rows.sort()
860860
rows = rows.tolist()
861861
return rows, cols
862862

@@ -887,21 +887,6 @@ def commit(self):
887887
rowsel, colsel = self.get_selection(view)
888888
self.selected_rows, self.selected_cols = rowsel, colsel
889889

890-
def select(data, rows, domain):
891-
"""
892-
Select the data subset with specified rows and domain subsets.
893-
894-
If either rows or domain is None they mean select all.
895-
"""
896-
if rows is not None and domain is not None:
897-
return data.from_table(domain, data, rows)
898-
elif rows is not None:
899-
return data.from_table(data.domain, rows)
900-
elif domain is not None:
901-
return data.from_table(domain, data)
902-
else:
903-
return data
904-
905890
domain = table.domain
906891

907892
if len(colsel) < len(domain) + len(domain.metas):
@@ -924,13 +909,11 @@ def select_vars(role):
924909
metas = select_vars(TableModel.Meta)
925910
domain = Orange.data.Domain(attrs, class_vars, metas)
926911

927-
# Avoid a copy if all/none rows are selected.
912+
# Avoid a copy if none rows are selected.
928913
if not rowsel:
929914
selected_data = None
930-
elif len(rowsel) == len(table):
931-
selected_data = select(table, None, domain)
932915
else:
933-
selected_data = select(table, rowsel, domain)
916+
selected_data = table.from_table(domain, table, rowsel)
934917

935918
self.Outputs.selected_data.send(selected_data)
936919
self.Outputs.annotated_data.send(create_annotated_table(table, rowsel))
@@ -1097,21 +1080,6 @@ def is_sortable(table):
10971080
return False
10981081

10991082

1100-
def test_model():
1101-
app = QApplication([])
1102-
view = QTableView(
1103-
sortingEnabled=True
1104-
)
1105-
data = Orange.data.Table("lenses")
1106-
model = TableModel(data)
1107-
1108-
view.setModel(model)
1109-
1110-
view.show()
1111-
view.raise_()
1112-
return app.exec()
1113-
1114-
11151083
if __name__ == "__main__": # pragma: no cover
11161084
WidgetPreview(OWDataTable).run(
11171085
[(Table("iris"), "iris"),

Orange/widgets/data/tests/test_owtable.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import unittest
2+
3+
24
from unittest.mock import Mock, patch
5+
from AnyQt.QtCore import Qt
36

47
from Orange.widgets.data.owtable import OWDataTable
58
from Orange.widgets.tests.base import WidgetTest, WidgetOutputsTestMixin
@@ -75,6 +78,29 @@ def test_pending_selection(self):
7578
output = self.get_output(widget.Outputs.selected_data)
7679
self.assertEqual(5, len(output))
7780

81+
def test_sorting(self):
82+
self.send_signal(self.widget.Inputs.data, self.data)
83+
self.widget.selected_rows = [0, 1, 2, 3, 4]
84+
self.widget.selected_cols = list(range(len(self.data.domain)))
85+
self.widget.set_selection()
86+
87+
output = self.get_output(self.widget.Outputs.selected_data)
88+
output, _ = output.get_column_view(0)
89+
output_original = output.tolist()
90+
91+
self.widget.tabs.currentWidget().sortByColumn(1, Qt.AscendingOrder)
92+
93+
output = self.get_output(self.widget.Outputs.selected_data)
94+
output, _ = output.get_column_view(0)
95+
output_sorted = output.tolist()
96+
97+
# the two outputs should not be the same.
98+
self.assertTrue(output_original != output_sorted)
99+
100+
# check if output after sorting is actually sorted.
101+
self.assertTrue(sorted(output_original) == output_sorted)
102+
self.assertTrue(sorted(output_sorted) == output_sorted)
103+
78104

79105
if __name__ == "__main__":
80106
unittest.main()

0 commit comments

Comments
 (0)