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

Implement popular/recent panels #199

Merged
merged 8 commits into from
May 11, 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
3 changes: 2 additions & 1 deletion koordinates/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
AccessType,
SortOrder,
Capability,
PublicAccessType
PublicAccessType,
ExplorePanel
)
from .layer_utils import LayerUtils # NOQA
from .repo import Repo # NOQA
Expand Down
37 changes: 36 additions & 1 deletion koordinates/api/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@
from .data_browser import DataBrowserQuery
from .utils import ApiUtils
from .repo import Repo
from .enums import DataType
from .enums import (
DataType,
ExplorePanel
)
from .dataset import Dataset

PAGE_SIZE = 20
Expand Down Expand Up @@ -130,6 +133,27 @@ def _build_datasets_request(self,

return endpoint, headers, params

def _build_explore_request(self,
panel: ExplorePanel,
context=None) -> Tuple[str, Dict[str, str], dict]:
"""
Builds the parameters used for a datasets request
"""
headers = {"Expand": "list,list.publisher,list.styles,list.data.source_summary"}

params = {}
params.update({"page_size": PAGE_SIZE})

endpoint = "explore-sections/"
if panel == ExplorePanel.Popular:
endpoint += "popular/"
elif panel == ExplorePanel.Recent:
endpoint += "recent/"

params["item_types"] = 'layer.*,publisher.*'

return endpoint, headers, params

def datasets_async(self,
page=1,
query: Optional[DataBrowserQuery] = None,
Expand All @@ -155,6 +179,17 @@ def facets_async(self,

return QgsNetworkAccessManager.instance().get(network_request)

def explore_async(self,
panel: ExplorePanel,
context=None) -> QNetworkReply:
"""
Retrieve datasets asynchronously
"""
endpoint, headers, params = self._build_explore_request(panel, context)
network_request = self._build_request(endpoint, headers, params)

return QgsNetworkAccessManager.instance().get(network_request)

def datasets(self, page=1, query: Optional[DataBrowserQuery] = None, context=None):
"""
Retrieve datasets blocking
Expand Down
13 changes: 12 additions & 1 deletion koordinates/api/enums.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
from enum import Enum
from enum import (
Enum,
auto
)
from typing import (
Set,
List
Expand Down Expand Up @@ -183,3 +186,11 @@ def to_text(order: 'SortOrder'):
SortOrder.AlphabeticalAZ: 'Alphabetical (A-Z)',
SortOrder.AlphabeticalZA: 'Alphabetical (Z-A)',
SortOrder.Oldest: 'Oldest'}[order]


class ExplorePanel(Enum):
"""
Explore panels
"""
Popular = auto()
Recent = auto()
50 changes: 28 additions & 22 deletions koordinates/gui/datasets_browser_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
import math
import os
from functools import partial
from typing import Optional
from typing import (
Optional,
List,
Dict
)

from qgis.PyQt import sip
from qgis.PyQt.QtCore import (
Expand All @@ -19,10 +23,8 @@
QLabel,
QToolButton,
QVBoxLayout,
QSizePolicy,
QWidget
)
from qgis.gui import QgsScrollArea

from .response_table_layout import ResponsiveTableWidget
from ..api import (
Expand All @@ -35,29 +37,21 @@


class DatasetsBrowserWidget(QWidget):
datasetDetailsRequested = pyqtSignal(dict)
total_count_changed = pyqtSignal(int)
visible_count_changed = pyqtSignal(int)

def __init__(self):
super().__init__()

self.scroll_area = QgsScrollArea()
self.scroll_area.setSizePolicy(QSizePolicy.Preferred, QSizePolicy.Preferred)
self.scroll_area.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
self.scroll_area.setWidgetResizable(True)
layout = QVBoxLayout()
layout.setContentsMargins(0, 0, 0, 0)

self.table_widget = ResponsiveTableWidget()
self.scroll_area.setWidget(self.table_widget)

layout.addWidget(self.scroll_area)
layout.addWidget(self.table_widget)
self.setLayout(layout)

self.setObjectName('DatasetsBrowserWidget')
self.scroll_area.setFrameShape(QFrame.NoFrame)
self.scroll_area.setStyleSheet("#qt_scrollarea_viewport{ background: transparent; }")

self._current_query: Optional[DataBrowserQuery] = None
self._current_reply: Optional[QNetworkReply] = None
Expand All @@ -77,8 +71,8 @@ def cancel_active_requests(self):

self._current_reply = None

def _create_temporary_items_for_page(self):
for i in range(PAGE_SIZE):
def _create_temporary_items_for_page(self, count=PAGE_SIZE):
for i in range(count):
self.table_widget.push_empty_widget()

def populate(self, query: DataBrowserQuery, context):
Expand All @@ -99,14 +93,11 @@ def _fetch_records(self,
query: Optional[DataBrowserQuery] = None,
context: Optional[str] = None,
page: int = 1):
if self._current_reply is not None and not sip.isdeleted(self._current_reply):
if self._current_reply is not None and not sip.isdeleted(
self._current_reply):
self._current_reply.abort()
self._current_reply = None

if page == 1:
# scroll to top on new search
self.scroll_area.verticalScrollBar().setValue(0)

if query is not None:
self._current_query = query
if context is not None:
Expand All @@ -117,7 +108,8 @@ def _fetch_records(self,
context=self._current_context,
page=page
)
self._current_reply.finished.connect(partial(self._reply_finished, self._current_reply))
self._current_reply.finished.connect(
partial(self._reply_finished, self._current_reply))
self.setCursor(Qt.WaitCursor)

def _reply_finished(self, reply: QNetworkReply):
Expand All @@ -138,8 +130,15 @@ def _reply_finished(self, reply: QNetworkReply):
return
# self.error_occurred.emit(request.reply().errorString())

datasets = json.loads(reply.readAll().data().decode())
tokens = reply.rawHeader(b"X-Resource-Range").data().decode().split("/")
result = json.loads(reply.readAll().data().decode())
if 'panels' in result:
datasets = [item['content'] for item in
result['panels'][0]['items']]
else:
datasets = result

tokens = reply.rawHeader(b"X-Resource-Range").data().decode().split(
"/")
total = tokens[-1]
self.total_count_changed.emit(int(total))
last = tokens[0].split("-")[-1]
Expand Down Expand Up @@ -172,6 +171,13 @@ def _reply_finished(self, reply: QNetworkReply):

self.table_widget.setUpdatesEnabled(True)

def set_datasets(self, datasets: List[Dict]):
self.table_widget.setUpdatesEnabled(False)
self._add_datasets(datasets)
self._datasets.extend(datasets)
self.visible_count_changed.emit(len(self._datasets))
self.table_widget.setUpdatesEnabled(True)

def _add_datasets(self, datasets):
for i, dataset in enumerate(datasets):
self.table_widget.push_dataset(dataset)
Expand Down
7 changes: 7 additions & 0 deletions koordinates/gui/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,10 @@ class TabStyle(Enum):
class FilterWidgetAppearance(Enum):
Horizontal = auto()
Vertical = auto()


class ExploreMode:
Popular = auto()
Browse = auto()
Publishers = auto()
Recent = auto()
112 changes: 88 additions & 24 deletions koordinates/gui/filter_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
from .advanced_filter_widget import AdvancedFilterWidget
from .enums import (
TabStyle,
FilterWidgetAppearance
FilterWidgetAppearance,
ExploreMode
)
from .explore_tab_bar import (
ExploreTabBar,
Expand All @@ -27,12 +28,16 @@
from .gui_utils import GuiUtils
from ..api import (
DataBrowserQuery,
SortOrder
SortOrder,
DataType,
AccessType,
ExplorePanel
)


class FilterWidget(QWidget):
filters_changed = pyqtSignal()
explore = pyqtSignal(ExplorePanel)
clear_all = pyqtSignal()

def __init__(self, parent=None):
Expand Down Expand Up @@ -166,43 +171,83 @@ def set_wide_mode(self, wide_mode: bool):

def set_search_line_edit(self, widget):
self.search_line_edit = widget
self.search_line_edit.textChanged.connect(self._filter_widget_changed)
self.search_line_edit.textChanged.connect(self._search_text_changed)

def _search_text_changed(self):
if self.search_line_edit.text().strip():
self.set_explore_mode(ExploreMode.Browse)
self._filter_widget_changed()

def _explore_tab_changed(self, tab_index: int):
"""
Called when the active explore tab is changed
"""
show_filters = tab_index == 1
self.advanced_filter_widget.setVisible(
show_filters
)
if show_filters:
self.advanced_filter_widget.updateGeometry()

if tab_index == 0:
self.popular_button.setChecked(True)
self.set_explore_mode(ExploreMode.Popular)
elif tab_index == 1:
self.browse_button.setChecked(True)
self.set_explore_mode(ExploreMode.Browse)
elif tab_index == 2:
self.publishers_button.setChecked(True)
self.set_explore_mode(ExploreMode.Publishers)
elif tab_index == 3:
self.recent_button.setChecked(True)
self.updateGeometry()
self.set_explore_mode(ExploreMode.Recent)

def _explore_button_toggled(self, button, checked):
if not checked:
return

if button == self.popular_button:
self.explore_tab_bar.setCurrentIndex(0)
self.set_explore_mode(ExploreMode.Popular)
elif button == self.browse_button:
self.explore_tab_bar.setCurrentIndex(1)
self.set_explore_mode(ExploreMode.Browse)
elif button == self.publishers_button:
self.explore_tab_bar.setCurrentIndex(2)
self.set_explore_mode(ExploreMode.Publishers)
elif button == self.recent_button:
self.set_explore_mode(ExploreMode.Recent)

def explore_mode(self) -> ExploreMode:
tab_index = self.explore_tab_bar.currentIndex()
if tab_index == 0:
return ExploreMode.Popular
elif tab_index == 1:
return ExploreMode.Browse
elif tab_index == 2:
return ExploreMode.Publishers
else:
return ExploreMode.Recent

def set_explore_mode(self, mode: ExploreMode):
show_filters = mode == ExploreMode.Browse
self.advanced_filter_widget.setVisible(
show_filters
)
if show_filters:
self.advanced_filter_widget.updateGeometry()

if mode in (
ExploreMode.Popular,
ExploreMode.Recent) and self.search_line_edit:
self.search_line_edit.clear()

if mode == ExploreMode.Popular:
self.explore_tab_bar.setCurrentIndex(0)
self.popular_button.setChecked(True)
elif mode == ExploreMode.Browse:
self.explore_tab_bar.setCurrentIndex(1)
self.browse_button.setChecked(True)
elif mode == ExploreMode.Publishers:
self.explore_tab_bar.setCurrentIndex(2)
self.publishers_button.setChecked(True)
elif mode == ExploreMode.Recent:
self.explore_tab_bar.setCurrentIndex(3)
self.recent_button.setChecked(True)

self.updateGeometry()
if mode == ExploreMode.Popular:
self.explore.emit(ExplorePanel.Popular)
elif mode == ExploreMode.Recent:
self.explore.emit(ExplorePanel.Recent)
else:
self._update_query()

def _clear_all(self):
self.search_line_edit.clear()
Expand All @@ -229,13 +274,32 @@ def build_query(self) -> DataBrowserQuery:
Returns a query representing the current widget state
"""
query = DataBrowserQuery()
query.starred = self._starred
query.order = self.sort_order

if self.search_line_edit.text().strip():
query.search = self.search_line_edit.text().strip()
mode = self.explore_mode()
if mode == ExploreMode.Browse:
query.starred = self._starred
query.order = self.sort_order

if self.search_line_edit.text().strip():
query.search = self.search_line_edit.text().strip()

self.advanced_filter_widget.apply_constraints_to_query(query)
elif mode == ExploreMode.Popular:
query.order = SortOrder.Popularity
query.access_type = AccessType.Public
query.data_types = {DataType.Tables,
DataType.Vectors,
DataType.Rasters,
DataType.Grids,
DataType.PointClouds}
elif mode == ExploreMode.Recent:
query.order = SortOrder.RecentlyUpdated
query.access_type = AccessType.Public
query.data_types = {DataType.Tables,
DataType.Vectors,
DataType.Rasters,
DataType.Grids,
DataType.PointClouds}

self.advanced_filter_widget.apply_constraints_to_query(query)
return query

def _update_query(self):
Expand Down
Loading