From 22329353b226a45bf1ef4fdddfb7feb92e29da67 Mon Sep 17 00:00:00 2001 From: Samweli Date: Thu, 9 Dec 2021 15:12:23 +0300 Subject: [PATCH] added filter and sorting functionalities --- src/qgis_stac/gui/qgis_stac_widget.py | 46 ++++++++++++++++++++++---- src/qgis_stac/gui/result_item_model.py | 17 +++++++++- src/qgis_stac/ui/qgis_stac_widget.ui | 11 +++--- 3 files changed, 62 insertions(+), 12 deletions(-) diff --git a/src/qgis_stac/gui/qgis_stac_widget.py b/src/qgis_stac/gui/qgis_stac_widget.py index bd1fd76..c968b55 100644 --- a/src/qgis_stac/gui/qgis_stac_widget.py +++ b/src/qgis_stac/gui/qgis_stac_widget.py @@ -109,8 +109,7 @@ def __init__( self.standard_model = QtGui.QStandardItemModel() self.items_tree.setItemDelegate(self.items_delegate) - - self.items_proxy_model = ItemsSortFilterProxyModel(SortField.DATE) + self.items_proxy_model = ItemsSortFilterProxyModel(SortField.ID) # initialize page self.page = 1 @@ -122,6 +121,9 @@ def __init__( self.end_dte.valueChanged.connect(self.save_filters) self.extent_box.extentChanged.connect(self.save_filters) + self.populate_sorting_field() + self.sort_cmb.activated.connect(self.sort_items) + def add_connection(self): """ Adds a new connection into the plugin, then updates the connections combo box list to show the added connection. @@ -408,12 +410,42 @@ def items_filter_changed(self, filter_text): :param filter_text: Filter text :type: str """ - exp_reg = QtCore.QRegExp( - filter_text, - QtCore.Qt.CaseInsensitive, - QtCore.QRegExp.FixedString + # TODO update to user QtCore.QRegExp, QRegularExpression will be + # deprecated. + options = QtCore.QRegularExpression.NoPatternOption + options |= QtCore.QRegularExpression.CaseInsensitiveOption + regular_expression = QtCore.QRegularExpression(filter_text, options) + self.items_proxy_model.setFilterRegularExpression(regular_expression) + + def populate_sorting_field(self): + """" Initializes sorting field combo box list items""" + default = SortField.ID + labels = { + SortField.ID: tr("Name"), + SortField.COLLECTION: tr("Collection"), + # SortField.DATE: tr("Date") + } + for ordering_type, item_text in labels.items(): + self.sort_cmb.addItem(item_text, ordering_type) + self.sort_cmb.setCurrentIndex( + self.sort_cmb.findData(default, role=QtCore.Qt.UserRole) ) - self.items_proxy_model.setFilterRegExp(exp_reg) + + def sort_items(self, index): + """ Sort the tree view items based on the current selected sort field + in the sort fields combo box + + :param index: Current index of the sort fields list + :type index: int + """ + sort_field = self.sort_cmb.itemData(index) + self.items_proxy_model.set_sort_field(sort_field) + order = QtCore.Qt.AscendingOrder \ + if not self.reverse_order_box.isChecked() \ + else QtCore.Qt.DescendingOrder + self.items_proxy_model.sort(index, order) + # Force refresh of the tree view items + self.items_tree.setModel(self.items_proxy_model) def get_selected_collections(self): """ Gets the currently selected collections ids from the collection diff --git a/src/qgis_stac/gui/result_item_model.py b/src/qgis_stac/gui/result_item_model.py index 7da5c95..3536076 100644 --- a/src/qgis_stac/gui/result_item_model.py +++ b/src/qgis_stac/gui/result_item_model.py @@ -76,13 +76,28 @@ def __init__(self, current_sort_field, *args, **kwargs): super().__init__(*args, **kwargs) self.current_sort_field = current_sort_field + def set_sort_field(self, field): + self.current_sort_field = field + + def filterAcceptsRow(self, source_row: int, source_parent: QtCore.QModelIndex): + index = self.sourceModel().index(source_row, 0, source_parent) + + match = self.filterRegularExpression().match( + self.sourceModel().data(index).id + ) + return match.hasMatch() + def lessThan(self, left: QtCore.QModelIndex, right: QtCore.QModelIndex) -> bool: model = self.sourceModel() left_item = model.data(left) right_item = model.data(right) - if self.current_sort_field == SortField.DATE: + if self.current_sort_field == SortField.ID: result = left_item.id < right_item.id + elif self.current_sort_field == SortField.COLLECTION: + result = left_item.collection < right_item.collection + elif self.current_sort_field == SortField.DATE: + pass else: raise NotImplementedError diff --git a/src/qgis_stac/ui/qgis_stac_widget.ui b/src/qgis_stac/ui/qgis_stac_widget.ui index 0aeebef..6669908 100644 --- a/src/qgis_stac/ui/qgis_stac_widget.ui +++ b/src/qgis_stac/ui/qgis_stac_widget.ui @@ -325,6 +325,9 @@ true + + false + false @@ -487,7 +490,7 @@ - Filter by item title + Filter by item id @@ -510,12 +513,12 @@ Sort by - sort_field_cmb + sort_cmb - + 0 @@ -531,7 +534,7 @@ - + 0