Skip to content

Commit

Permalink
OWRank: slightly better settings migration for headerState and selection
Browse files Browse the repository at this point in the history
  • Loading branch information
kernc committed Sep 20, 2017
1 parent edc0d03 commit 61fcbad
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
32 changes: 26 additions & 6 deletions Orange/widgets/data/owrank.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ class Outputs:
nSelected = Setting(5)
auto_apply = Setting(True)

headerState = Setting(['', 1])
sorting = Setting((0, 1))
selected_methods = Setting(set())

settings_version = 2
Expand Down Expand Up @@ -421,8 +421,9 @@ def updateScores(self):

# Re-apply sort
try:
sort_column, sort_order = self.headerState
self.ranksModel.sort(labels.index(sort_column) + 1, sort_order)
sort_column, sort_order = self.sorting
if sort_column < len(labels):
self.ranksModel.sort(sort_column + 1, sort_order) # +1 for '#' (discrete count) column
except ValueError:
pass

Expand Down Expand Up @@ -471,8 +472,8 @@ def headerClick(self, index):

# Store the header states
sort_order = self.ranksModel.sortOrder()
sort_column = self.ranksModel.headerData(self.ranksModel.sortColumn(), Qt.Horizontal)
self.headerState = (sort_column, sort_order)
sort_column = self.ranksModel.sortColumn() - 1 # -1 for '#' (discrete count) column
self.sorting = (sort_column, sort_order)

def methodSelectionChanged(self, state, method_name):
if state == Qt.Checked:
Expand Down Expand Up @@ -539,7 +540,26 @@ def migrate_settings(cls, settings, version):
# If older settings, restore sort header to default
# Saved selected_rows will likely be incorrect
if version is None or version < 2:
settings['headerState'] = ('', 1)
column, order = 0, Qt.DescendingOrder
headerState = settings.pop("headerState")

# Lacking knowledge of last problemType, use discrete ranks view's ordering
if isinstance(headerState, (tuple, list)):
headerState = headerState[0]

if isinstance(headerState, bytes):
hview = QHeaderView(Qt.Horizontal)
hview.restoreState(headerState)
column, order = hview.sortIndicatorSection() - 1, hview.sortIndicatorOrder()
settings["sorting"] = (column, order)

@classmethod
def migrate_context(cls, context, version):
if version is None or version < 2:
# Old selection was saved as sorted indices. New selection is original indices.
# Since we can't devise the latter without first computing the ranks,
# just reset the selection to avoid confusion.
context.values['selected_rows'] = []


if __name__ == "__main__":
Expand Down
4 changes: 2 additions & 2 deletions Orange/widgets/data/tests/test_owrank.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ def test_data_which_make_scorer_nan(self):
self.widget.selected_methods.add('ANOVA')
self.send_signal(self.widget.Inputs.data, table)

def test_setting_migration_extends_header_state(self):
def test_setting_migration_fixes_header_state(self):
# Settings as of version 3.3.5
settings = {
'auto_apply': True,
Expand All @@ -266,4 +266,4 @@ def test_setting_migration_extends_header_state(self):

w = self.create_widget(OWRank, stored_settings=settings)

self.assertEqual(len(w.headerState), 3)
self.assertEqual(w.headerState, (1, Qt.AscendingOrder))

0 comments on commit 61fcbad

Please sign in to comment.