Skip to content

Commit

Permalink
Data sets: Disable combos when filter overrides them
Browse files Browse the repository at this point in the history
  • Loading branch information
janezd committed Nov 15, 2024
1 parent 3ce867f commit 3353676
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 13 deletions.
48 changes: 37 additions & 11 deletions Orange/widgets/data/owdatasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@
GENERAL_DOMAIN = None
ALL_DOMAINS = "" # The setting is Optional[str], so don't use other types here

# The number of characters at which filter overrides the domain and language
FILTER_OVERRIDE_LENGTH = 4


def ensure_local(index_url, file_path, local_cache_path,
force=False, progress_advance=None):
Expand Down Expand Up @@ -180,7 +183,7 @@ def filterAcceptsRow(self, row, parent):
data = source.index(row, 0).data(Qt.UserRole)
in_filter = (
self.__filter is not None
and len(self.__filter) >= 4
and len(self.__filter) >= FILTER_OVERRIDE_LENGTH
and self.__filter in data.title.casefold()
)
published_ok = data.publication_status == Namespace.PUBLISHED
Expand All @@ -190,7 +193,6 @@ def filterAcceptsRow(self, row, parent):
and (published_ok and domain_ok and language_ok
or in_filter))


class OWDataSets(OWWidget):
name = "Datasets"
description = "Load a dataset from an online repository"
Expand Down Expand Up @@ -276,8 +278,13 @@ def __init__(self):
"Typing four letters or more overrides domain and language filters")
layout.addWidget(self.filterLineEdit)

self.combo_elements = []

layout.addSpacing(20)
layout.addWidget(QLabel("Show data sets in "))
label = QLabel("Show data sets in ")
layout.addWidget(label)
self.combo_elements.append(label)

lang_combo = self.language_combo = QComboBox()
languages = [self.DEFAULT_LANG, self.ALL_LANGUAGES]
if self.language is not None and self.language not in languages:
Expand All @@ -289,14 +296,19 @@ def __init__(self):
lang_combo.setCurrentText(self.language)
lang_combo.activated.connect(self._on_language_changed)
layout.addWidget(lang_combo)
self.combo_elements.append(lang_combo)

layout.addSpacing(20)
layout.addWidget(QLabel("Domain:"))
label = QLabel("Domain:")
layout.addWidget(label)
self.combo_elements.append(label)

domain_combo = self.domain_combo = QComboBox()
domain_combo.addItem(self.GENERAL_DOMAIN_LABEL)
domain_combo.activated.connect(self._on_domain_changed)
if self.core_widget:
layout.addWidget(domain_combo)
self.combo_elements.append(domain_combo)

self.mainArea.layout().addLayout(layout)

Expand Down Expand Up @@ -494,17 +506,20 @@ def update_model(self):
self.domain = datainfo.domain
if self.domain == "sc": # domain from the list of ignored domain
self.domain = ALL_DOMAINS
combo = self.domain_combo
if self.domain == GENERAL_DOMAIN:
combo.setCurrentIndex(0)
elif self.domain == ALL_DOMAINS:
combo.setCurrentIndex(combo.count() - 1)
else:
combo.setCurrentText(self.domain)
self.__update_domain_combo()
self._on_domain_changed()

return model, current_index

def __update_domain_combo(self):
combo = self.domain_combo
if self.domain == GENERAL_DOMAIN:
combo.setCurrentIndex(0)
elif self.domain == ALL_DOMAINS:
combo.setCurrentIndex(combo.count() - 1)
else:
combo.setCurrentText(self.domain)

def _on_language_changed(self):
combo = self.language_combo
if combo.currentIndex() == combo.count() - 1:
Expand Down Expand Up @@ -622,6 +637,17 @@ def selected_dataset(self):

def filter(self):
filter_string = self.filterLineEdit.text().strip()
enable_combos = len(filter_string) < FILTER_OVERRIDE_LENGTH
if enable_combos is not self.domain_combo.isEnabled():
for element in self.combo_elements:
element.setEnabled(enable_combos)
if enable_combos:
self.__update_domain_combo()
self.language_combo.setCurrentText(self.language)
else:
self.domain_combo.setCurrentText(self.ALL_DOMAINS_LABEL)
self.language_combo.setCurrentText(self.ALL_LANGUAGES)

self.filter_hint = filter_string
proxyModel = self.view.model()
if proxyModel:
Expand Down
19 changes: 17 additions & 2 deletions Orange/widgets/data/tests/test_owdatasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,13 +182,27 @@ def test_filter_overrides_language_and_domain(self):
self.wait_until_stop_blocking(w)
w.language_combo.setCurrentText("Slovenščina")
w.language_combo.activated.emit(w.language_combo.currentIndex())
w.domain_combo.setCurrentText(w.GENERAL_DOMAIN_LABEL)
w.domain_combo.activated.emit(w.domain_combo.currentIndex())

self.assertEqual(self.__titles(w), {"Bax data set"})

w.filterLineEdit.setText("data ")
self.assertEqual(self.__titles(w), {"Foo data set",
"Bar data set",
"Bax data set"})
self.assertEqual(w.language_combo.currentText(), w.ALL_LANGUAGES)
self.assertFalse(w.language_combo.isEnabled())
self.assertEqual(w.domain_combo.currentText(), w.ALL_DOMAINS_LABEL)
self.assertFalse(w.domain_combo.isEnabled())

w.filterLineEdit.setText("da")
self.assertEqual(self.__titles(w), {"Bax data set"})
self.assertEqual(w.language_combo.currentText(), "Slovenščina")
self.assertTrue(w.language_combo.isEnabled())
self.assertEqual(w.domain_combo.currentText(), w.GENERAL_DOMAIN_LABEL)
self.assertTrue(w.domain_combo.isEnabled())


w.filterLineEdit.setText("bar d")
self.assertEqual(self.__titles(w), {"Bar data set"})
Expand All @@ -211,7 +225,8 @@ def test_filter_overrides_language_and_domain(self):
settings = w.settingsHandler.pack_data(w)
w2 = self.create_widget(OWDataSets, stored_settings=settings)
self.wait_until_stop_blocking(w2)
self.assertEqual(w2.language_combo.currentText(), "English")
self.assertEqual(w2.language_combo.currentText(), w2.ALL_LANGUAGES)
self.assertFalse(w2.language_combo.isEnabled())
self.assertEqual(w2.filterLineEdit.text(), "bax d")
self.assertEqual(self.__titles(w2), {"Bax data set"})

Expand Down Expand Up @@ -266,8 +281,8 @@ def test_download_iris(self):
# select the only dataset
sel_type = QItemSelectionModel.ClearAndSelect | QItemSelectionModel.Rows
w.view.selectionModel().select(w.view.model().index(0, 0), sel_type)
self.assertEqual(w.selected_id, "iris.tab")
w.commit()
self.assertEqual(w.selected_id, "iris.tab")
iris = self.get_output(w.Outputs.data, w)
self.assertEqual(len(iris), 150)

Expand Down

0 comments on commit 3353676

Please sign in to comment.