merge bitcoin-core/gui#354: Refactor open date range to use std::optional

This commit is contained in:
Kittywhiskers Van Gogh 2021-06-03 09:54:25 +01:00
parent 17a7e9ba85
commit 0a5481cf44
No known key found for this signature in database
GPG Key ID: 30CD0C065E5C4AAD
3 changed files with 21 additions and 26 deletions

View File

@ -9,15 +9,8 @@
#include <cstdlib> #include <cstdlib>
// Earliest date that can be represented (far in the past)
const QDateTime TransactionFilterProxy::MIN_DATE = QDateTime::fromTime_t(0);
// Last date that can be represented (far in the future)
const QDateTime TransactionFilterProxy::MAX_DATE = QDateTime::fromTime_t(0xFFFFFFFF);
TransactionFilterProxy::TransactionFilterProxy(QObject *parent) : TransactionFilterProxy::TransactionFilterProxy(QObject *parent) :
QSortFilterProxyModel(parent), QSortFilterProxyModel(parent),
dateFrom(MIN_DATE.toTime_t()),
dateTo(MAX_DATE.toTime_t()),
m_search_string(), m_search_string(),
typeFilter(COMMON_TYPES), typeFilter(COMMON_TYPES),
watchOnlyFilter(WatchOnlyFilter_All), watchOnlyFilter(WatchOnlyFilter_All),
@ -44,9 +37,10 @@ bool TransactionFilterProxy::filterAcceptsRow(int sourceRow, const QModelIndex &
return false; return false;
if (!involvesWatchAddress && watchOnlyFilter == WatchOnlyFilter_Yes) if (!involvesWatchAddress && watchOnlyFilter == WatchOnlyFilter_Yes)
return false; return false;
qint64 datetime = index.data(TransactionTableModel::DateRoleInt).toLongLong();
if (datetime < dateFrom || datetime > dateTo) QDateTime datetime = index.data(TransactionTableModel::DateRole).toDateTime();
return false; if (dateFrom && datetime < *dateFrom) return false;
if (dateTo && datetime > *dateTo) return false;
QString address = index.data(TransactionTableModel::AddressRole).toString(); QString address = index.data(TransactionTableModel::AddressRole).toString();
QString label = index.data(TransactionTableModel::LabelRole).toString(); QString label = index.data(TransactionTableModel::LabelRole).toString();
@ -64,10 +58,10 @@ bool TransactionFilterProxy::filterAcceptsRow(int sourceRow, const QModelIndex &
return true; return true;
} }
void TransactionFilterProxy::setDateRange(const QDateTime &from, const QDateTime &to) void TransactionFilterProxy::setDateRange(const std::optional<QDateTime>& from, const std::optional<QDateTime>& to)
{ {
this->dateFrom = from.toTime_t(); dateFrom = from;
this->dateTo = to.toTime_t(); dateTo = to;
invalidateFilter(); invalidateFilter();
} }

View File

@ -10,6 +10,8 @@
#include <QDateTime> #include <QDateTime>
#include <QSortFilterProxyModel> #include <QSortFilterProxyModel>
#include <optional>
/** Filter the transaction list according to pre-specified rules. */ /** Filter the transaction list according to pre-specified rules. */
class TransactionFilterProxy : public QSortFilterProxyModel class TransactionFilterProxy : public QSortFilterProxyModel
{ {
@ -18,10 +20,6 @@ class TransactionFilterProxy : public QSortFilterProxyModel
public: public:
explicit TransactionFilterProxy(QObject *parent = nullptr); explicit TransactionFilterProxy(QObject *parent = nullptr);
/** Earliest date that can be represented (far in the past) */
static const QDateTime MIN_DATE;
/** Last date that can be represented (far in the future) */
static const QDateTime MAX_DATE;
/** Type filter bit field (all types) */ /** Type filter bit field (all types) */
static const quint32 ALL_TYPES = 0xFFFFFFFF; static const quint32 ALL_TYPES = 0xFFFFFFFF;
/** Type filter bit field (all types but Darksend-SPAM) */ /** Type filter bit field (all types but Darksend-SPAM) */
@ -36,7 +34,8 @@ public:
WatchOnlyFilter_No WatchOnlyFilter_No
}; };
void setDateRange(const QDateTime &from, const QDateTime &to); /** Filter transactions between date range. Use std::nullopt for open range. */
void setDateRange(const std::optional<QDateTime>& from, const std::optional<QDateTime>& to);
void setSearchString(const QString &); void setSearchString(const QString &);
/** /**
@note Type filter takes a bit field created with TYPE() or ALL_TYPES @note Type filter takes a bit field created with TYPE() or ALL_TYPES
@ -57,8 +56,8 @@ protected:
bool filterAcceptsRow(int source_row, const QModelIndex & source_parent) const override; bool filterAcceptsRow(int source_row, const QModelIndex & source_parent) const override;
private: private:
qint64 dateFrom; std::optional<QDateTime> dateFrom;
qint64 dateTo; std::optional<QDateTime> dateTo;
QString m_search_string; QString m_search_string;
quint32 typeFilter; quint32 typeFilter;
WatchOnlyFilter watchOnlyFilter; WatchOnlyFilter watchOnlyFilter;

View File

@ -20,6 +20,8 @@
#include <interfaces/node.h> #include <interfaces/node.h>
#include <node/ui_interface.h> #include <node/ui_interface.h>
#include <optional>
#include <QCalendarWidget> #include <QCalendarWidget>
#include <QComboBox> #include <QComboBox>
#include <QDateTimeEdit> #include <QDateTimeEdit>
@ -265,26 +267,26 @@ void TransactionView::chooseDate(int idx)
{ {
case All: case All:
transactionProxyModel->setDateRange( transactionProxyModel->setDateRange(
TransactionFilterProxy::MIN_DATE, std::nullopt,
TransactionFilterProxy::MAX_DATE); std::nullopt);
break; break;
case Today: case Today:
transactionProxyModel->setDateRange( transactionProxyModel->setDateRange(
GUIUtil::StartOfDay(current), GUIUtil::StartOfDay(current),
TransactionFilterProxy::MAX_DATE); std::nullopt);
break; break;
case ThisWeek: { case ThisWeek: {
// Find last Monday // Find last Monday
QDate startOfWeek = current.addDays(-(current.dayOfWeek()-1)); QDate startOfWeek = current.addDays(-(current.dayOfWeek()-1));
transactionProxyModel->setDateRange( transactionProxyModel->setDateRange(
GUIUtil::StartOfDay(startOfWeek), GUIUtil::StartOfDay(startOfWeek),
TransactionFilterProxy::MAX_DATE); std::nullopt);
} break; } break;
case ThisMonth: case ThisMonth:
transactionProxyModel->setDateRange( transactionProxyModel->setDateRange(
GUIUtil::StartOfDay(QDate(current.year(), current.month(), 1)), GUIUtil::StartOfDay(QDate(current.year(), current.month(), 1)),
TransactionFilterProxy::MAX_DATE); std::nullopt);
break; break;
case LastMonth: case LastMonth:
transactionProxyModel->setDateRange( transactionProxyModel->setDateRange(
@ -294,7 +296,7 @@ void TransactionView::chooseDate(int idx)
case ThisYear: case ThisYear:
transactionProxyModel->setDateRange( transactionProxyModel->setDateRange(
GUIUtil::StartOfDay(QDate(current.year(), 1, 1)), GUIUtil::StartOfDay(QDate(current.year(), 1, 1)),
TransactionFilterProxy::MAX_DATE); std::nullopt);
break; break;
case Range: case Range:
dateRangeWidget->setVisible(true); dateRangeWidget->setVisible(true);