Skip to content

Commit

Permalink
Re-order widgets in the SourceList on update.
Browse files Browse the repository at this point in the history
  • Loading branch information
ntoll authored and Allie Crevier committed Mar 27, 2020
1 parent 5ec5c7c commit 807783f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
25 changes: 23 additions & 2 deletions securedrop_client/gui/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -889,6 +889,22 @@ def show_no_source_selected_message(self):
self.no_source_selected.show()


class SourceListWidgetItem(QListWidgetItem):

def __lt__(self, other):
"""
Used for ordering widgets by timestamp of last interaction.
"""
lw = self.listWidget()
me = lw.itemWidget(self)
them = lw.itemWidget(other)
if me and them:
my_ts = arrow.get(me.source.last_updated)
other_ts = arrow.get(them.source.last_updated)
return my_ts < other_ts
return True


class SourceList(QListWidget):
"""
Displays the list of sources.
Expand Down Expand Up @@ -921,6 +937,9 @@ def __init__(self):
layout = QVBoxLayout(self)
self.setLayout(layout)

# Enable ordering.
self.setSortingEnabled(True)

# To hold references to SourceWidget instances indexed by source UUID.
self.source_widgets = {}

Expand Down Expand Up @@ -975,11 +994,13 @@ def update(self, sources: List[Source]) -> List[str]:
new_source = SourceWidget(self.controller, source)
self.source_widgets[source.uuid] = new_source

list_item = QListWidgetItem()
list_item = SourceListWidgetItem()
self.insertItem(0, list_item)
list_item.setSizeHint(new_source.sizeHint())
self.setItemWidget(list_item, new_source)

# Sort..!
self.sortItems(Qt.DescendingOrder)
return deleted_uuids

def initial_update(self, sources: List[Source]):
Expand All @@ -1003,7 +1024,7 @@ def schedule_source_management(slice_size=slice_size):
for source in sources_slice:
new_source = SourceWidget(self.controller, source)
self.source_widgets[source.uuid] = new_source
list_item = QListWidgetItem(self)
list_item = SourceListWidgetItem(self)
list_item.setSizeHint(new_source.sizeHint())

self.insertItem(0, list_item)
Expand Down
6 changes: 3 additions & 3 deletions tests/gui/test_widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -769,7 +769,7 @@ def test_SourceList_update_adds_new_sources(mocker):
mock_sw = mocker.MagicMock()
mock_lwi = mocker.MagicMock()
mocker.patch('securedrop_client.gui.widgets.SourceWidget', mock_sw)
mocker.patch('securedrop_client.gui.widgets.QListWidgetItem', mock_lwi)
mocker.patch('securedrop_client.gui.widgets.SourceListWidgetItem', mock_lwi)

sources = [mocker.MagicMock(), mocker.MagicMock(), mocker.MagicMock(), ]
sl.update(sources)
Expand Down Expand Up @@ -1009,7 +1009,7 @@ def test_SourceList_add_source_closure_adds_sources(mocker):
mock_sw = mocker.MagicMock()
mock_lwi = mocker.MagicMock()
mocker.patch('securedrop_client.gui.widgets.SourceWidget', mock_sw)
mocker.patch('securedrop_client.gui.widgets.QListWidgetItem', mock_lwi)
mocker.patch('securedrop_client.gui.widgets.SourceListWidgetItem', mock_lwi)
sources = [mocker.MagicMock(), mocker.MagicMock(), mocker.MagicMock(), ]
mock_timer = mocker.MagicMock()
with mocker.patch("securedrop_client.gui.widgets.QTimer", mock_timer):
Expand Down Expand Up @@ -1044,7 +1044,7 @@ def test_SourceList_add_source_closure_exits_on_no_more_sources(mocker):
mock_sw = mocker.MagicMock()
mock_lwi = mocker.MagicMock()
mocker.patch('securedrop_client.gui.widgets.SourceWidget', mock_sw)
mocker.patch('securedrop_client.gui.widgets.QListWidgetItem', mock_lwi)
mocker.patch('securedrop_client.gui.widgets.SourceListWidgetItem', mock_lwi)
sources = []
mock_timer = mocker.MagicMock()
with mocker.patch("securedrop_client.gui.widgets.QTimer", mock_timer):
Expand Down

0 comments on commit 807783f

Please sign in to comment.