From 5bba42ef808cfafe4c0dea73745198e9553f85d4 Mon Sep 17 00:00:00 2001 From: Ronit Jadhav Date: Mon, 8 May 2023 14:21:03 +0200 Subject: [PATCH 1/2] added resource type filter by checkbox --- qgis_hub_plugin/gui/resource_browser.py | 37 ++++- qgis_hub_plugin/gui/resource_browser.ui | 207 ++++++++++++++++-------- 2 files changed, 167 insertions(+), 77 deletions(-) diff --git a/qgis_hub_plugin/gui/resource_browser.py b/qgis_hub_plugin/gui/resource_browser.py index 42cc3d4..2022df1 100644 --- a/qgis_hub_plugin/gui/resource_browser.py +++ b/qgis_hub_plugin/gui/resource_browser.py @@ -35,6 +35,7 @@ def __init__(self, parent=None, iface=None): self.graphicsViewPreview.setScene(self.graphicsScene) # Resources + self.resources = [] self.resource_model = QStandardItemModel(self.listViewResources) self.listViewResources.setModel(self.resource_model) @@ -50,6 +51,16 @@ def __init__(self, parent=None, iface=None): self.pushButtonDownload.clicked.connect(self.download_resource) + self.checkBoxGeopackage.stateChanged.connect( + lambda: self.populate_resources(force_update=False) + ) + self.checkBoxStyle.stateChanged.connect( + lambda: self.populate_resources(force_update=False) + ) + self.checkBoxModel.stateChanged.connect( + lambda: self.populate_resources(force_update=False) + ) + self.reloadToolButton.setIcon( QIcon(":/images/themes/default/mActionRefresh.svg") ) @@ -60,15 +71,27 @@ def __init__(self, parent=None, iface=None): self.hide_preview() def populate_resources(self, force_update=False): - response = get_all_resources(force_update=force_update) - # total = response.get("total") - # previous_url = response.get("previous") - # next_url = response.get("next") - resources = response.get("results", {}) + if force_update or not self.resources: + response = get_all_resources(force_update=force_update) + # total = response.get("total") + # previous_url = response.get("previous") + # next_url = response.get("next") + self.resources = response.get("results", {}) + + geopackage_checked = self.checkBoxGeopackage.isChecked() + style_checked = self.checkBoxStyle.isChecked() + model_checked = self.checkBoxModel.isChecked() + + filtered_resources = [ + ResourceItem(r) + for r in self.resources + if (geopackage_checked and r["resource_type"] == "Geopackage") + or (style_checked and r["resource_type"] == "Style") + or (model_checked and r["resource_type"] == "Model") + ] self.resource_model.clear() - for resource in resources: - item = ResourceItem(resource) + for item in filtered_resources: self.resource_model.appendRow(item) @pyqtSlot("QItemSelection", "QItemSelection") diff --git a/qgis_hub_plugin/gui/resource_browser.ui b/qgis_hub_plugin/gui/resource_browser.ui index 0f60e3c..efa8c2d 100644 --- a/qgis_hub_plugin/gui/resource_browser.ui +++ b/qgis_hub_plugin/gui/resource_browser.ui @@ -14,78 +14,9 @@ QGIS Hub Explorer - - - - - 0 - 0 - - - - Qt::LeftToRight - - - - - - Search - - - - - - - - - - - - - ... - - - - - - - - - - Resource Type - - - - - - - Reload Resources - - - - - - - - 0 - 0 - - - - - - - - ... - - - - - - - + @@ -336,6 +267,142 @@ + + + + + 0 + 0 + + + + Qt::LeftToRight + + + + + + Search + + + + + + + + + + + + + ... + + + + + + + + + + Resource Type + + + + + + + Reload Resources + + + + + + + ... + + + + + + + + + + + 10 + -8 + 58 + 31 + + + + + 0 + 0 + + + + Style + + + true + + + + + + 90 + -8 + 106 + 31 + + + + + 0 + 0 + + + + Geopackage + + + true + + + false + + + + + + 210 + -8 + 90 + 31 + + + + + 0 + 0 + + + + Model + + + true + + + + + + + + + From ead92fd62fcb9e90bd0788e37ccd55007183942d Mon Sep 17 00:00:00 2001 From: Ronit Jadhav Date: Fri, 19 May 2023 07:57:04 +0200 Subject: [PATCH 2/2] Implementing proxy model for filter --- qgis_hub_plugin/gui/resource_browser.py | 57 +++++++++++++++---------- 1 file changed, 35 insertions(+), 22 deletions(-) diff --git a/qgis_hub_plugin/gui/resource_browser.py b/qgis_hub_plugin/gui/resource_browser.py index 2022df1..3bc791e 100644 --- a/qgis_hub_plugin/gui/resource_browser.py +++ b/qgis_hub_plugin/gui/resource_browser.py @@ -4,7 +4,7 @@ from qgis.core import Qgis, QgsApplication from qgis.PyQt import uic -from qgis.PyQt.QtCore import QSize, Qt, QUrl, pyqtSlot +from qgis.PyQt.QtCore import QRegExp, QSize, QSortFilterProxyModel, Qt, QUrl, pyqtSlot from qgis.PyQt.QtGui import ( QDesktopServices, QIcon, @@ -37,12 +37,16 @@ def __init__(self, parent=None, iface=None): # Resources self.resources = [] self.resource_model = QStandardItemModel(self.listViewResources) - self.listViewResources.setModel(self.resource_model) + + self.proxy_model = QSortFilterProxyModel() + self.proxy_model.setSourceModel(self.resource_model) + self.listViewResources.setModel(self.proxy_model) self.resource_selection_model = self.listViewResources.selectionModel() self.resource_selection_model.selectionChanged.connect( - self.on_resource_selecton_changed + self.on_resource_selection_changed ) + # TODO(IS): make user able to change the icon size self.listViewResources.setIconSize(QSize(96, 96)) @@ -52,14 +56,10 @@ def __init__(self, parent=None, iface=None): self.pushButtonDownload.clicked.connect(self.download_resource) self.checkBoxGeopackage.stateChanged.connect( - lambda: self.populate_resources(force_update=False) - ) - self.checkBoxStyle.stateChanged.connect( - lambda: self.populate_resources(force_update=False) - ) - self.checkBoxModel.stateChanged.connect( - lambda: self.populate_resources(force_update=False) + lambda: self.update_resource_filter() ) + self.checkBoxStyle.stateChanged.connect(lambda: self.update_resource_filter()) + self.checkBoxModel.stateChanged.connect(lambda: self.update_resource_filter()) self.reloadToolButton.setIcon( QIcon(":/images/themes/default/mActionRefresh.svg") @@ -78,24 +78,31 @@ def populate_resources(self, force_update=False): # next_url = response.get("next") self.resources = response.get("results", {}) + self.resource_model.clear() + for resource in self.resources: + item = ResourceItem(resource) + self.resource_model.appendRow(item) + + def update_resource_filter(self): geopackage_checked = self.checkBoxGeopackage.isChecked() style_checked = self.checkBoxStyle.isChecked() model_checked = self.checkBoxModel.isChecked() - filtered_resources = [ - ResourceItem(r) - for r in self.resources - if (geopackage_checked and r["resource_type"] == "Geopackage") - or (style_checked and r["resource_type"] == "Style") - or (model_checked and r["resource_type"] == "Model") - ] + filter_exp = ["NONE"] + if geopackage_checked: + filter_exp.append("Geopackage") + if style_checked: + filter_exp.append("Style") + if model_checked: + filter_exp.append("Model") - self.resource_model.clear() - for item in filtered_resources: - self.resource_model.appendRow(item) + filter_regexp = QRegExp("|".join(filter_exp), Qt.CaseInsensitive) + + self.proxy_model.setFilterRegExp(filter_regexp) + self.proxy_model.setFilterRole(ResourceItem.ResourceTypeRole) @pyqtSlot("QItemSelection", "QItemSelection") - def on_resource_selecton_changed(self, selected, deselected): + def on_resource_selection_changed(self, selected, deselected): if self.selected_resource(): self.update_preview() else: @@ -104,7 +111,9 @@ def on_resource_selecton_changed(self, selected, deselected): def selected_resource(self): selected_indexes = self.listViewResources.selectionModel().selectedIndexes() if len(selected_indexes) > 0: - return self.resource_model.itemFromIndex(selected_indexes[0]) + proxy_index = selected_indexes[0] + source_index = self.proxy_model.mapToSource(proxy_index) + return self.resource_model.itemFromIndex(source_index) else: return None @@ -201,6 +210,8 @@ def shorten_string(text: str) -> str: class ResourceItem(QStandardItem): + ResourceTypeRole = Qt.UserRole + 1 + def __init__(self, params: dict): super().__init__() @@ -225,3 +236,5 @@ def __init__(self, params: dict): self.setIcon(QIcon(str(thumbnail_path))) else: self.setIcon(get_icon("qbrowser_icon.svg")) + + self.setData(self.resource_type, self.ResourceTypeRole)