Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ListViewFilter: Retain selection when filtering #270

Merged
merged 2 commits into from
May 14, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion orangewidget/utils/listview.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from contextlib import contextmanager
from typing import Iterable, Optional
import warnings

Expand All @@ -10,9 +11,19 @@
QSortFilterProxyModel,
QItemSelection,
QSize,
QItemSelectionModel,
)


@contextmanager
def disconnected(signal, slot, connection_type=Qt.AutoConnection):
signal.disconnect(slot)
try:
yield
finally:
signal.connect(slot, connection_type)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps blocking signals (https://doc.qt.io/qt-6/qobject.html#blockSignals) would be better the temporarily disconnecting them?

Also: search the code for def blocked(. You'll find that a similar function already exists in two other places. Perhaps we should introduce one in orange-widget-base and then (after release...) use it wherever needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or even use an existing one at

def signal_blocking(obj):



class ListViewFilter(QListView):
"""
A QListView with implicit and transparent row filtering.
Expand All @@ -27,6 +38,7 @@ def __init__(
**kwargs
):
super().__init__(*args, **kwargs)
self.__selection = QItemSelection()
self.__search = QLineEdit(self, placeholderText="Filter...")
self.__search.textEdited.connect(self.__on_text_edited)
self.__preferred_size = preferred_size
Expand All @@ -40,9 +52,29 @@ def __init__(
assert isinstance(proxy, QSortFilterProxyModel)
super().setModel(proxy)
self.set_source_model(model)
self.selectionModel().selectionChanged.connect(self.__on_sel_changed)

def __on_sel_changed(
self,
selected: QItemSelection,
deselected: QItemSelection
):
selected = self.model().mapSelectionToSource(selected)
deselected = self.model().mapSelectionToSource(deselected)
self.__selection.merge(selected, QItemSelectionModel.Select)
self.__selection.merge(deselected, QItemSelectionModel.Deselect)
self.__select()

def __on_text_edited(self, string: str):
self.model().setFilterFixedString(string)
with disconnected(self.selectionModel().selectionChanged,
self.__on_sel_changed):
self.model().setFilterFixedString(string)
self.__select()

def __select(self):
selection = self.model().mapSelectionFromSource(self.__selection)
self.selectionModel().select(selection,
QItemSelectionModel.ClearAndSelect)

def setModel(self, _):
raise TypeError("The model cannot be changed. "
Expand Down
15 changes: 15 additions & 0 deletions orangewidget/utils/tests/test_listview.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,21 @@ def test_set_proxy(self):
def test_set_proxy_raises(self):
self.assertRaises(Exception, ListViewFilter, proxy=PyListModel())

def test_selection(self):
view = ListViewFilter()
view.model().setSourceModel(PyListModel(["one", "two", "three"]))
sel_model = view.selectionModel()
selected_row = 0
index = view.model().index(selected_row, 0)
sel_model.select(index, sel_model.ClearAndSelect)
self.assertEqual(sel_model.selectedRows(0)[0].row(), selected_row)

view._ListViewFilter__search.textEdited.emit("two")
self.assertEqual(len(sel_model.selectedRows(0)), 0)

view._ListViewFilter__search.textEdited.emit("")
self.assertEqual(sel_model.selectedRows(0)[0].row(), selected_row)


if __name__ == "__main__":
unittest.main()
Loading