Skip to content

Commit

Permalink
Merge pull request #2695 from lanzagar/datasets_filter
Browse files Browse the repository at this point in the history
[ENH] Data Sets: Add filter
  • Loading branch information
astaric authored Oct 24, 2017
2 parents b069855 + f23f069 commit 8b94e4c
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 7 deletions.
26 changes: 20 additions & 6 deletions Orange/widgets/data/owdatasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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__)

Expand Down Expand Up @@ -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(
Expand All @@ -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,
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
29 changes: 28 additions & 1 deletion Orange/widgets/data/tests/test_owdatasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 8b94e4c

Please sign in to comment.