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

Filter by resources type using checkbox #29

Merged
merged 2 commits into from
May 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
58 changes: 47 additions & 11 deletions qgis_hub_plugin/gui/resource_browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -35,13 +35,18 @@ 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)

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))

Expand All @@ -50,6 +55,12 @@ def __init__(self, parent=None, iface=None):

self.pushButtonDownload.clicked.connect(self.download_resource)

self.checkBoxGeopackage.stateChanged.connect(
Copy link
Collaborator

Choose a reason for hiding this comment

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

If we check in the hub API, there are 5 resource types
image

It seems the API doesn't return all five resource types. Could you check it? Add the missing 2 check boxes after that.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I checked the API URL used, it only returns three types of resources.
Is there any other API I can use, or can you direct me to the place where I can find the API's.

Copy link
Collaborator

Choose a reason for hiding this comment

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

It's not really documented, but it's from this https://github.com/qgis/QGIS-Django/blob/master/qgis-app/api/urls.py

Copy link
Collaborator

Choose a reason for hiding this comment

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

I will create an issue in that repo to include the other resource type also.

Copy link
Collaborator

Choose a reason for hiding this comment

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

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")
)
Expand All @@ -60,19 +71,38 @@ 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", {})

self.resource_model.clear()
for resource in resources:
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()

filter_exp = ["NONE"]
if geopackage_checked:
filter_exp.append("Geopackage")
if style_checked:
filter_exp.append("Style")
if model_checked:
filter_exp.append("Model")

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:
Expand All @@ -81,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

Expand Down Expand Up @@ -178,6 +210,8 @@ def shorten_string(text: str) -> str:


class ResourceItem(QStandardItem):
ResourceTypeRole = Qt.UserRole + 1

def __init__(self, params: dict):
super().__init__()

Expand All @@ -202,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)
207 changes: 137 additions & 70 deletions qgis_hub_plugin/gui/resource_browser.ui
Original file line number Diff line number Diff line change
Expand Up @@ -14,78 +14,9 @@
<string>QGIS Hub Explorer</string>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0" rowspan="2">
<widget class="QWidget" name="widget_3" native="true">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="layoutDirection">
<enum>Qt::LeftToRight</enum>
</property>
<layout class="QFormLayout" name="formLayout">
<item row="0" column="0">
<widget class="QLabel" name="labelSearch">
<property name="text">
<string>Search</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QWidget" name="widget" native="true">
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QLineEdit" name="lineEditSearch"/>
</item>
<item>
<widget class="QToolButton" name="toolButtonSearch">
<property name="text">
<string>...</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="labelResourceType">
<property name="text">
<string>Resource Type</string>
</property>
</widget>
</item>
<item row="2" column="0">
<widget class="QLabel" name="labelReloadResources">
<property name="text">
<string>Reload Resources</string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QComboBox" name="comboBox">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="QToolButton" name="reloadToolButton">
<property name="text">
<string>...</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item row="3" column="0">
<widget class="QWidget" name="widget_4" native="true">
<layout class="QHBoxLayout" name="horizontalLayout_2">
<layout class="QHBoxLayout">
<item>
<widget class="QListView" name="listViewResources">
<property name="sizePolicy">
Expand Down Expand Up @@ -336,6 +267,142 @@
</property>
</widget>
</item>
<item row="0" column="0" rowspan="2">
<widget class="QWidget" name="widget_3" native="true">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="layoutDirection">
<enum>Qt::LeftToRight</enum>
</property>
<layout class="QFormLayout" name="formLayout">
<item row="0" column="0">
<widget class="QLabel" name="labelSearch">
<property name="text">
<string>Search</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QWidget" name="widget" native="true">
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QLineEdit" name="lineEditSearch"/>
</item>
<item>
<widget class="QToolButton" name="toolButtonSearch">
<property name="text">
<string>...</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item row="3" column="0">
<widget class="QLabel" name="labelResourceType">
<property name="text">
<string>Resource Type</string>
</property>
</widget>
</item>
<item row="6" column="0">
<widget class="QLabel" name="labelReloadResources">
<property name="text">
<string>Reload Resources</string>
</property>
</widget>
</item>
<item row="6" column="1">
<widget class="QToolButton" name="reloadToolButton">
<property name="text">
<string>...</string>
</property>
</widget>
</item>
<item row="3" column="1">
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QWidget" name="widget_2" native="true">
<widget class="QCheckBox" name="checkBoxStyle">
<property name="geometry">
<rect>
<x>10</x>
<y>-8</y>
<width>58</width>
<height>31</height>
</rect>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Style</string>
</property>
<property name="checked">
<bool>true</bool>
</property>
</widget>
<widget class="QCheckBox" name="checkBoxGeopackage">
<property name="geometry">
<rect>
<x>90</x>
<y>-8</y>
<width>106</width>
<height>31</height>
</rect>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Geopackage</string>
</property>
<property name="checked">
<bool>true</bool>
</property>
<property name="tristate">
<bool>false</bool>
</property>
</widget>
<widget class="QCheckBox" name="checkBoxModel">
<property name="geometry">
<rect>
<x>210</x>
<y>-8</y>
<width>90</width>
<height>31</height>
</rect>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Model</string>
</property>
<property name="checked">
<bool>true</bool>
</property>
</widget>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
</item>
</layout>
</widget>
<resources/>
Expand Down