Skip to content

Commit

Permalink
Merge pull request #842 from Samweli/datasets_date_filtering
Browse files Browse the repository at this point in the history
Enable date filtering in the datasets items
  • Loading branch information
azvoleff authored Dec 16, 2024
2 parents ce574ac + d994917 commit 0c12b77
Show file tree
Hide file tree
Showing 7 changed files with 334 additions and 8 deletions.
7 changes: 7 additions & 0 deletions LDMP/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ class Setting(enum.Enum):
UNKNOWN_AREA_OF_INTEREST = "private/unknown_area_of_interest"
PRIOR_LOCALE = "private/prior_locale"

DATE_FILTER_ENABLED = "filters/date_filter_enabled"
FILTER_START_DATE = "filters/start_date"
FILTER_END_DATE = "filters/end_date"

DEBUG = "advanced/debug"
BINARIES_ENABLED = "advanced/binaries_enabled"
BINARIES_DIR = "advanced/binaries_folder"
Expand Down Expand Up @@ -116,6 +120,9 @@ class SettingsManager:
Setting.CUSTOM_CRS: "epsg:4326",
Setting.POLL_REMOTE: True,
Setting.DOWNLOAD_RESULTS: True,
Setting.DATE_FILTER_ENABLED: False,
Setting.FILTER_START_DATE: "",
Setting.FILTER_END_DATE: "",
Setting.BUFFER_CHECKED: False,
Setting.AREA_FROM_OPTION: AreaSetting.COUNTRY_REGION.value,
Setting.POINT_X: 0.0,
Expand Down
27 changes: 27 additions & 0 deletions LDMP/dataset_additional_metadata.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import os
from pathlib import Path

from qgis.PyQt import QtWidgets, uic

DatasetAdditionalMetadataUi, _ = uic.loadUiType(
str(Path(__file__).parents[0] / "gui/DlgDatasetAdditionalMetadata.ui")
)

ICON_PATH = os.path.join(os.path.dirname(__file__), "icons")


class DataSetAdditionalMetadataDialog(QtWidgets.QDialog, DatasetAdditionalMetadataUi):
dataset: dict

le_title: QtWidgets.QLineEdit
te_author: QtWidgets.QTextEdit
le_source: QtWidgets.QLineEdit
te_citation: QtWidgets.QTextEdit

def __init__(self, dataset: dict, parent=None):
super().__init__(parent)
self.setupUi(self)
self.le_title.setText(dataset.get("title", ""))
self.te_author.setPlainText(dataset.get("Data source", ""))
self.le_source.setText(dataset.get("Source", ""))
self.te_citation.setPlainText(dataset.get("Citation", ""))
10 changes: 7 additions & 3 deletions LDMP/download_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

import json
import os
from functools import partial
from pathlib import Path

import qgis.gui
Expand All @@ -21,6 +22,7 @@

from . import calculate, conf
from .conf import Setting, settings_manager
from .dataset_additional_metadata import DataSetAdditionalMetadataDialog
from .jobs.manager import job_manager
from .logger import log

Expand Down Expand Up @@ -185,7 +187,8 @@ def update_data_table(self):
# Add "Notes" buttons in cell
for row in range(0, len(self.datasets)):
btn = QtWidgets.QPushButton(self.tr("Details"))
btn.clicked.connect(self.btn_details)
btn_details = partial(self.btn_details, self.datasets[row])
btn.clicked.connect(btn_details)
self.data_view.setIndexWidget(self.proxy_model.index(row, 8), btn)

# Category
Expand Down Expand Up @@ -227,11 +230,12 @@ def update_data_table(self):

self.data_view.setSelectionBehavior(QtWidgets.QAbstractItemView.SelectRows)

def btn_details(self):
def btn_details(self, dataset):
# button = self.sender()
# index = self.data_view.indexAt(button.pos())
# TODO: Code the details view
pass
dlg = DataSetAdditionalMetadataDialog(dataset)
dlg.exec()

def btn_calculate(self):
# Note that the super class has several tests in it - if they fail it
Expand Down
133 changes: 133 additions & 0 deletions LDMP/gui/DlgDatasetAdditionalMetadata.ui
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Dialog</class>
<widget class="QDialog" name="Dialog">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>706</width>
<height>577</height>
</rect>
</property>
<property name="windowTitle">
<string>Additional Metadata</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<property name="leftMargin">
<number>6</number>
</property>
<property name="topMargin">
<number>6</number>
</property>
<property name="rightMargin">
<number>6</number>
</property>
<property name="bottomMargin">
<number>6</number>
</property>
<item>
<widget class="QScrollArea" name="scrollArea">
<property name="frameShape">
<enum>QFrame::NoFrame</enum>
</property>
<property name="widgetResizable">
<bool>true</bool>
</property>
<widget class="QWidget" name="scrollAreaWidgetContents">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>694</width>
<height>565</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<widget class="QgsCollapsibleGroupBox" name="groupBox">
<property name="title">
<string>Metadata</string>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="8" column="0">
<widget class="QTextEdit" name="te_citation">
<property name="readOnly">
<bool>true</bool>
</property>
</widget>
</item>
<item row="2" column="0" colspan="3">
<widget class="QLineEdit" name="le_title">
<property name="readOnly">
<bool>true</bool>
</property>
</widget>
</item>
<item row="6" column="0">
<widget class="QLineEdit" name="le_source">
<property name="readOnly">
<bool>true</bool>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label">
<property name="text">
<string>Title</string>
</property>
</widget>
</item>
<item row="4" column="0" colspan="3">
<widget class="QTextEdit" name="te_author">
<property name="readOnly">
<bool>true</bool>
</property>
</widget>
</item>
<item row="7" column="0">
<widget class="QLabel" name="label_3">
<property name="text">
<string>Bibliographical Citation</string>
</property>
</widget>
</item>
<item row="3" column="0" colspan="2">
<widget class="QLabel" name="label_2">
<property name="text">
<string>Author or Publisher</string>
</property>
</widget>
</item>
<item row="5" column="0">
<widget class="QLabel" name="label_4">
<property name="text">
<string>Source Link or Identifier</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
</layout>
</widget>
</widget>
</item>
</layout>
</widget>
<customwidgets>
<customwidget>
<class>QgsCollapsibleGroupBox</class>
<extends>QGroupBox</extends>
<header>qgscollapsiblegroupbox.h</header>
<container>1</container>
</customwidget>
</customwidgets>
<tabstops>
<tabstop>scrollArea</tabstop>
<tabstop>le_title</tabstop>
<tabstop>te_author</tabstop>
</tabstops>
<resources/>
<connections/>
</ui>
92 changes: 88 additions & 4 deletions LDMP/gui/WidgetMain.ui
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<item>
<widget class="QTabWidget" name="tabWidget">
<property name="currentIndex">
<number>0</number>
<number>1</number>
</property>
<widget class="QWidget" name="tab_algorithms">
<attribute name="title">
Expand All @@ -41,7 +41,7 @@
<attribute name="title">
<string>Datasets</string>
</attribute>
<layout class="QVBoxLayout" name="verticalLayout_3" stretch="0,0,0">
<layout class="QVBoxLayout" name="verticalLayout_3">
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
Expand All @@ -65,7 +65,7 @@ for running algorithms and their results.</string>
<property name="placeholderText">
<string>Search...</string>
</property>
<property name="showSearchIcon" stdset="0">
<property name="showSearchIcon">
<bool>true</bool>
</property>
<property name="qgisRelation" stdset="0">
Expand All @@ -82,6 +82,79 @@ for running algorithms and their results.</string>
</item>
</layout>
</item>
<item>
<widget class="QgsCollapsibleGroupBox" name="date_filter_group">
<property name="toolTip">
<string/>
</property>
<property name="title">
<string>Filter by date</string>
</property>
<property name="checkable">
<bool>true</bool>
</property>
<property name="checked">
<bool>false</bool>
</property>
<property name="collapsed">
<bool>true</bool>
</property>
<layout class="QGridLayout" name="gridLayout_2">
<item row="0" column="0">
<widget class="QLabel" name="label_13">
<property name="text">
<string>Start</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QLabel" name="label_14">
<property name="text">
<string>End</string>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QgsDateTimeEdit" name="start_dte">
<property name="toolTip">
<string/>
</property>
<property name="dateTime">
<datetime>
<hour>0</hour>
<minute>0</minute>
<second>0</second>
<year>2021</year>
<month>1</month>
<day>1</day>
</datetime>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QgsDateTimeEdit" name="end_dte">
<property name="dateTime">
<datetime>
<hour>0</hour>
<minute>0</minute>
<second>0</second>
<year>2021</year>
<month>12</month>
<day>1</day>
</datetime>
</property>
<property name="date">
<date>
<year>2021</year>
<month>12</month>
<day>1</day>
</date>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item>
<widget class="QTreeView" name="datasets_tv">
<property name="verticalScrollMode">
Expand Down Expand Up @@ -154,10 +227,21 @@ for running algorithms and their results.</string>
</widget>
</widget>
<customwidgets>
<customwidget>
<class>QgsCollapsibleGroupBox</class>
<extends>QGroupBox</extends>
<header>qgscollapsiblegroupbox.h</header>
<container>1</container>
</customwidget>
<customwidget>
<class>QgsDateTimeEdit</class>
<extends>QDateTimeEdit</extends>
<header>qgsdatetimeedit.h</header>
</customwidget>
<customwidget>
<class>QgsFilterLineEdit</class>
<extends>QLineEdit</extends>
<header>qgis.gui</header>
<header>qgsfilterlineedit.h</header>
</customwidget>
</customwidgets>
<resources/>
Expand Down
22 changes: 21 additions & 1 deletion LDMP/jobs/mvc.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,13 @@ class JobsSortFilterProxyModel(QtCore.QSortFilterProxyModel):
def __init__(self, current_sort_field: SortField, *args, **kwargs):
super().__init__(*args, **kwargs)
self.current_sort_field = current_sort_field
self.start_date = None
self.end_date = None

def set_date_filter(self, start_date, end_date):
self.start_date = start_date
self.end_date = end_date
self.invalidateFilter()

def filterAcceptsRow(self, source_row: int, source_parent: QtCore.QModelIndex):
jobs_model = self.sourceModel()
Expand All @@ -118,7 +125,20 @@ def filterAcceptsRow(self, source_row: int, source_parent: QtCore.QModelIndex):
elif self.type_filter == TypeFilter.VECTOR:
matches_type = job.is_vector()

return matches_filter and matches_type
# Date filtering logic
matches_date = True
if self.start_date and self.end_date:
job_start_date = QtCore.QDateTime.fromString(
job.start_date.strftime("%Y-%m-%d %H:%M:%S"), "yyyy-MM-dd HH:mm:ss"
)
job_end_date = QtCore.QDateTime.fromString(
job.end_date.strftime("%Y-%m-%d %H:%M:%S"), "yyyy-MM-dd HH:mm:ss"
)
matches_date = (
job_start_date >= self.start_date and job_end_date <= self.end_date
)

return matches_filter and matches_type and matches_date

def lessThan(self, left: QtCore.QModelIndex, right: QtCore.QModelIndex) -> bool:
model = self.sourceModel()
Expand Down
Loading

0 comments on commit 0c12b77

Please sign in to comment.