Skip to content

Commit 0a5481c

Browse files
committed
merge bitcoin-core/gui#354: Refactor open date range to use std::optional
1 parent 17a7e9b commit 0a5481c

File tree

3 files changed

+21
-26
lines changed

3 files changed

+21
-26
lines changed

src/qt/transactionfilterproxy.cpp

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,8 @@
99

1010
#include <cstdlib>
1111

12-
// Earliest date that can be represented (far in the past)
13-
const QDateTime TransactionFilterProxy::MIN_DATE = QDateTime::fromTime_t(0);
14-
// Last date that can be represented (far in the future)
15-
const QDateTime TransactionFilterProxy::MAX_DATE = QDateTime::fromTime_t(0xFFFFFFFF);
16-
1712
TransactionFilterProxy::TransactionFilterProxy(QObject *parent) :
1813
QSortFilterProxyModel(parent),
19-
dateFrom(MIN_DATE.toTime_t()),
20-
dateTo(MAX_DATE.toTime_t()),
2114
m_search_string(),
2215
typeFilter(COMMON_TYPES),
2316
watchOnlyFilter(WatchOnlyFilter_All),
@@ -44,9 +37,10 @@ bool TransactionFilterProxy::filterAcceptsRow(int sourceRow, const QModelIndex &
4437
return false;
4538
if (!involvesWatchAddress && watchOnlyFilter == WatchOnlyFilter_Yes)
4639
return false;
47-
qint64 datetime = index.data(TransactionTableModel::DateRoleInt).toLongLong();
48-
if (datetime < dateFrom || datetime > dateTo)
49-
return false;
40+
41+
QDateTime datetime = index.data(TransactionTableModel::DateRole).toDateTime();
42+
if (dateFrom && datetime < *dateFrom) return false;
43+
if (dateTo && datetime > *dateTo) return false;
5044

5145
QString address = index.data(TransactionTableModel::AddressRole).toString();
5246
QString label = index.data(TransactionTableModel::LabelRole).toString();
@@ -64,10 +58,10 @@ bool TransactionFilterProxy::filterAcceptsRow(int sourceRow, const QModelIndex &
6458
return true;
6559
}
6660

67-
void TransactionFilterProxy::setDateRange(const QDateTime &from, const QDateTime &to)
61+
void TransactionFilterProxy::setDateRange(const std::optional<QDateTime>& from, const std::optional<QDateTime>& to)
6862
{
69-
this->dateFrom = from.toTime_t();
70-
this->dateTo = to.toTime_t();
63+
dateFrom = from;
64+
dateTo = to;
7165
invalidateFilter();
7266
}
7367

src/qt/transactionfilterproxy.h

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
#include <QDateTime>
1111
#include <QSortFilterProxyModel>
1212

13+
#include <optional>
14+
1315
/** Filter the transaction list according to pre-specified rules. */
1416
class TransactionFilterProxy : public QSortFilterProxyModel
1517
{
@@ -18,10 +20,6 @@ class TransactionFilterProxy : public QSortFilterProxyModel
1820
public:
1921
explicit TransactionFilterProxy(QObject *parent = nullptr);
2022

21-
/** Earliest date that can be represented (far in the past) */
22-
static const QDateTime MIN_DATE;
23-
/** Last date that can be represented (far in the future) */
24-
static const QDateTime MAX_DATE;
2523
/** Type filter bit field (all types) */
2624
static const quint32 ALL_TYPES = 0xFFFFFFFF;
2725
/** Type filter bit field (all types but Darksend-SPAM) */
@@ -36,7 +34,8 @@ class TransactionFilterProxy : public QSortFilterProxyModel
3634
WatchOnlyFilter_No
3735
};
3836

39-
void setDateRange(const QDateTime &from, const QDateTime &to);
37+
/** Filter transactions between date range. Use std::nullopt for open range. */
38+
void setDateRange(const std::optional<QDateTime>& from, const std::optional<QDateTime>& to);
4039
void setSearchString(const QString &);
4140
/**
4241
@note Type filter takes a bit field created with TYPE() or ALL_TYPES
@@ -57,8 +56,8 @@ class TransactionFilterProxy : public QSortFilterProxyModel
5756
bool filterAcceptsRow(int source_row, const QModelIndex & source_parent) const override;
5857

5958
private:
60-
qint64 dateFrom;
61-
qint64 dateTo;
59+
std::optional<QDateTime> dateFrom;
60+
std::optional<QDateTime> dateTo;
6261
QString m_search_string;
6362
quint32 typeFilter;
6463
WatchOnlyFilter watchOnlyFilter;

src/qt/transactionview.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
#include <interfaces/node.h>
2121
#include <node/ui_interface.h>
2222

23+
#include <optional>
24+
2325
#include <QCalendarWidget>
2426
#include <QComboBox>
2527
#include <QDateTimeEdit>
@@ -265,26 +267,26 @@ void TransactionView::chooseDate(int idx)
265267
{
266268
case All:
267269
transactionProxyModel->setDateRange(
268-
TransactionFilterProxy::MIN_DATE,
269-
TransactionFilterProxy::MAX_DATE);
270+
std::nullopt,
271+
std::nullopt);
270272
break;
271273
case Today:
272274
transactionProxyModel->setDateRange(
273275
GUIUtil::StartOfDay(current),
274-
TransactionFilterProxy::MAX_DATE);
276+
std::nullopt);
275277
break;
276278
case ThisWeek: {
277279
// Find last Monday
278280
QDate startOfWeek = current.addDays(-(current.dayOfWeek()-1));
279281
transactionProxyModel->setDateRange(
280282
GUIUtil::StartOfDay(startOfWeek),
281-
TransactionFilterProxy::MAX_DATE);
283+
std::nullopt);
282284

283285
} break;
284286
case ThisMonth:
285287
transactionProxyModel->setDateRange(
286288
GUIUtil::StartOfDay(QDate(current.year(), current.month(), 1)),
287-
TransactionFilterProxy::MAX_DATE);
289+
std::nullopt);
288290
break;
289291
case LastMonth:
290292
transactionProxyModel->setDateRange(
@@ -294,7 +296,7 @@ void TransactionView::chooseDate(int idx)
294296
case ThisYear:
295297
transactionProxyModel->setDateRange(
296298
GUIUtil::StartOfDay(QDate(current.year(), 1, 1)),
297-
TransactionFilterProxy::MAX_DATE);
299+
std::nullopt);
298300
break;
299301
case Range:
300302
dateRangeWidget->setVisible(true);

0 commit comments

Comments
 (0)