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

Added filter and sorting functionalities to the searched items #47

Merged
merged 1 commit into from
Dec 9, 2021
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
46 changes: 39 additions & 7 deletions src/qgis_stac/gui/qgis_stac_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand Down Expand Up @@ -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
Expand Down
17 changes: 16 additions & 1 deletion src/qgis_stac/gui/result_item_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
11 changes: 7 additions & 4 deletions src/qgis_stac/ui/qgis_stac_widget.ui
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,9 @@
<property name="flat">
<bool>true</bool>
</property>
<property name="checkable">
<bool>false</bool>
</property>
<property name="collapsed">
<bool>false</bool>
</property>
Expand Down Expand Up @@ -487,7 +490,7 @@
<item>
<widget class="QLineEdit" name="items_filter">
<property name="toolTip">
<string>Filter by item title</string>
<string>Filter by item id</string>
</property>
</widget>
</item>
Expand All @@ -510,12 +513,12 @@
<string>Sort by</string>
</property>
<property name="buddy">
<cstring>sort_field_cmb</cstring>
<cstring>sort_cmb</cstring>
</property>
</widget>
</item>
<item>
<widget class="QComboBox" name="sort_field_cmb">
<widget class="QComboBox" name="sort_cmb">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Maximum">
<horstretch>0</horstretch>
Expand All @@ -531,7 +534,7 @@
</widget>
</item>
<item>
<widget class="QCheckBox" name="reverse_order_chb">
<widget class="QCheckBox" name="reverse_order_box">
<property name="minimumSize">
<size>
<width>0</width>
Expand Down