Skip to content

Commit

Permalink
itemmodels.AbstractSortTableModel: Fix mapFromSourceRows
Browse files Browse the repository at this point in the history
  • Loading branch information
janezd committed Nov 2, 2017
1 parent 9225e8d commit 911f556
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
14 changes: 10 additions & 4 deletions Orange/widgets/utils/itemmodels.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ def mapToSourceRows(self, rows):
Parameters
----------
rows : int or list of int or numpy.ndarray or Ellipsis
rows : int or list of int or numpy.ndarray of dtype=int or Ellipsis
View (sorted) rows.
Returns
Expand All @@ -127,7 +127,10 @@ def mapToSourceRows(self, rows):
Source rows matching input rows. If they are the same,
simply input `rows` is returned.
"""
if self.__sortInd is not None:
# self.__sortInd[rows] fails if `rows` is an empty list or array
if self.__sortInd is not None \
and (isinstance(rows, (int, type(Ellipsis)))
or len(rows)):
new_rows = self.__sortInd[rows]
if rows is Ellipsis:
new_rows.setflags(write=False)
Expand All @@ -139,7 +142,7 @@ def mapFromSourceRows(self, rows):
Parameters
----------
rows : int or list of int or numpy.ndarray or Ellipsis
rows : int or list of int or numpy.ndarray of dtype=int or Ellipsis
Source model rows.
Returns
Expand All @@ -148,7 +151,10 @@ def mapFromSourceRows(self, rows):
ModelIndex (sorted) rows matching input source rows.
If they are the same, simply input `rows` is returned.
"""
if self.__sortIndInv is not None:
# self.__sortInd[rows] fails if `rows` is an empty list or array
if self.__sortIndInv is not None \
and (isinstance(rows, (int, type(Ellipsis)))
or len(rows)):
new_rows = self.__sortIndInv[rows]
if rows is Ellipsis:
new_rows.setflags(write=False)
Expand Down
26 changes: 26 additions & 0 deletions Orange/widgets/utils/tests/test_itemmodels.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

from unittest import TestCase

import numpy as np

from AnyQt.QtCore import Qt

from Orange.data import Domain, ContinuousVariable, DiscreteVariable
Expand Down Expand Up @@ -133,6 +135,30 @@ def test_sorting(self):
self.model.sort(1, Qt.DescendingOrder)
self.assertSequenceEqual(self.model.mapToSourceRows(...).tolist(), [0, 1])

def test_mapToSourceRows(self):
model = PyTableModel([[1, 4],
[2, 2],
[3, 3]])
model.sort(1, Qt.AscendingOrder)
self.assertSequenceEqual(model.mapToSourceRows(...).tolist(), [1, 2, 0])
self.assertEqual(model.mapToSourceRows(1).tolist(), 2)
self.assertSequenceEqual(model.mapToSourceRows([1, 2]).tolist(), [2, 0])
self.assertSequenceEqual(model.mapToSourceRows([]), [])
self.assertSequenceEqual(
model.mapToSourceRows(np.array([], dtype=int)).tolist(), [])

def test_mapFromSourceRows(self):
model = PyTableModel([[1, 4],
[2, 2],
[3, 3]])
model.sort(1, Qt.AscendingOrder)
self.assertSequenceEqual(model.mapFromSourceRows(...).tolist(), [2, 0, 1])
self.assertEqual(model.mapFromSourceRows(1).tolist(), 0)
self.assertSequenceEqual(model.mapFromSourceRows([1, 2]).tolist(), [0, 1])
self.assertSequenceEqual(model.mapFromSourceRows([]), [])
self.assertSequenceEqual(
model.mapFromSourceRows(np.array([], dtype=int)).tolist(), [])


class TestPyListModel(TestCase):
@classmethod
Expand Down

0 comments on commit 911f556

Please sign in to comment.