From 3dcbc9bc84636c716ae43f53d52f2f0558de167f Mon Sep 17 00:00:00 2001 From: Lan Zagar Date: Fri, 20 Oct 2017 11:00:55 +0200 Subject: [PATCH 1/2] owdatasets: Add filtering --- Orange/widgets/data/owdatasets.py | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/Orange/widgets/data/owdatasets.py b/Orange/widgets/data/owdatasets.py index 650716e6755..82df3327aa6 100644 --- a/Orange/widgets/data/owdatasets.py +++ b/Orange/widgets/data/owdatasets.py @@ -12,10 +12,9 @@ from typing import Optional, Dict, Tuple from AnyQt.QtWidgets import ( - QLabel, QTextBrowser, QSplitter, QTreeView, + QLabel, QLineEdit, QTextBrowser, QSplitter, QTreeView, QStyleOptionViewItem, QStyledItemDelegate, QApplication ) - from AnyQt.QtGui import QStandardItemModel, QStandardItem from AnyQt.QtCore import ( Qt, QSize, QObject, QThread, QModelIndex, QSortFilterProxyModel, @@ -26,12 +25,12 @@ from serverfiles import LocalFiles, ServerFiles, sizeformat import Orange.data - from Orange.misc.environ import data_dir from Orange.widgets import widget, settings, gui from Orange.widgets.utils.signals import Output from Orange.widgets.widget import Msg + INDEX_URL = "http://datasets.orange.biolab.si/" log = logging.getLogger(__name__) @@ -141,6 +140,12 @@ def __init__(self): self.infolabel = QLabel(text="Initializing...\n\n") box.layout().addWidget(self.infolabel) + gui.widgetLabel(self.mainArea, "Filter") + self.filterLineEdit = QLineEdit( + textChanged=self.filter + ) + self.mainArea.layout().addWidget(self.filterLineEdit) + self.splitter = QSplitter(orientation=Qt.Vertical) self.view = QTreeView( @@ -150,6 +155,7 @@ def __init__(self): rootIsDecorated=False, editTriggers=QTreeView.NoEditTriggers, ) + box = gui.widgetBox(self.splitter, "Description", addToLayout=False) self.descriptionlabel = QLabel( wordWrap=True, @@ -181,7 +187,9 @@ def __init__(self): model.setHorizontalHeaderLabels(HEADER) proxy = QSortFilterProxyModel() proxy.setSourceModel(model) - self.view.setModel(model) + proxy.setFilterKeyColumn(-1) + proxy.setFilterCaseSensitivity(False) + self.view.setModel(proxy) if self.splitter_state: self.splitter.restoreState(self.splitter_state) @@ -294,8 +302,8 @@ def info(prefix, filename): current_index = i hs = self.view.header().saveState() - model_ = self.view.model() - self.view.setModel(model) + model_ = self.view.model().sourceModel() + self.view.model().setSourceModel(model) self.view.header().restoreState(hs) model_.deleteLater() model_.setParent(None) @@ -344,6 +352,12 @@ def selected_dataset(self): info = None return info + def filter(self): + filter_string = self.filterLineEdit.text().strip() + proxyModel = self.view.model() + if proxyModel: + proxyModel.setFilterFixedString(filter_string) + def __on_selection(self): # Main data sets view selection has changed rows = self.view.selectionModel().selectedRows(0) From f23f0698a318c5fd0ae84795442fa98c4d6c0e52 Mon Sep 17 00:00:00 2001 From: Lan Zagar Date: Tue, 24 Oct 2017 15:13:58 +0200 Subject: [PATCH 2/2] test_owdatasets: Add tests --- Orange/widgets/data/tests/test_owdatasets.py | 29 +++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/Orange/widgets/data/tests/test_owdatasets.py b/Orange/widgets/data/tests/test_owdatasets.py index 56be763f847..003c6752896 100644 --- a/Orange/widgets/data/tests/test_owdatasets.py +++ b/Orange/widgets/data/tests/test_owdatasets.py @@ -9,7 +9,34 @@ class TestOWDataSets(WidgetTest): @patch("Orange.widgets.data.owdatasets.list_remote", Mock(side_effect=requests.exceptions.ConnectionError)) + @patch("Orange.widgets.data.owdatasets.list_local", Mock(return_value={})) @patch("Orange.widgets.data.owdatasets.log", Mock()) - def test_works_without_internet_connection(self): + def test_no_internet_connection(self): w = self.create_widget(OWDataSets) # type: OWDataSets self.assertTrue(w.Error.no_remote_datasets.is_shown()) + + @patch("Orange.widgets.data.owdatasets.list_remote", + Mock(side_effect=requests.exceptions.ConnectionError)) + @patch("Orange.widgets.data.owdatasets.list_local", + Mock(return_value={('core', 'foo.tab'): {}})) + @patch("Orange.widgets.data.owdatasets.log", Mock()) + def test_only_local(self): + w = self.create_widget(OWDataSets) # type: OWDataSets + self.assertTrue(w.Warning.only_local_datasets.is_shown()) + self.assertEqual(w.view.model().rowCount(), 1) + + @patch("Orange.widgets.data.owdatasets.list_remote", + Mock(side_effect=requests.exceptions.ConnectionError)) + @patch("Orange.widgets.data.owdatasets.list_local", + Mock(return_value={('core', 'foo.tab'): {}, + ('core', 'bar.tab'): {}})) + @patch("Orange.widgets.data.owdatasets.log", Mock()) + def test_filtering(self): + w = self.create_widget(OWDataSets) # type: OWDataSets + self.assertEqual(w.view.model().rowCount(), 2) + w.filterLineEdit.setText("foo") + self.assertEqual(w.view.model().rowCount(), 1) + w.filterLineEdit.setText("baz") + self.assertEqual(w.view.model().rowCount(), 0) + w.filterLineEdit.setText("") + self.assertEqual(w.view.model().rowCount(), 2)