Skip to content

Commit 4830f49

Browse files
committed
qt: Refactor open date range to use std::optional
1 parent a9435e3 commit 4830f49

File tree

3 files changed

+19
-25
lines changed

3 files changed

+19
-25
lines changed

src/qt/transactionfilterproxy.cpp

Lines changed: 5 additions & 12 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),
20-
dateTo(MAX_DATE),
2114
m_search_string(),
2215
typeFilter(ALL_TYPES),
2316
watchOnlyFilter(WatchOnlyFilter_All),
@@ -46,8 +39,8 @@ bool TransactionFilterProxy::filterAcceptsRow(int sourceRow, const QModelIndex &
4639
return false;
4740

4841
QDateTime datetime = index.data(TransactionTableModel::DateRole).toDateTime();
49-
if (datetime < dateFrom || datetime > dateTo)
50-
return false;
42+
if (dateFrom && datetime < *dateFrom) return false;
43+
if (dateTo && datetime > *dateTo) return false;
5144

5245
QString address = index.data(TransactionTableModel::AddressRole).toString();
5346
QString label = index.data(TransactionTableModel::LabelRole).toString();
@@ -65,10 +58,10 @@ bool TransactionFilterProxy::filterAcceptsRow(int sourceRow, const QModelIndex &
6558
return true;
6659
}
6760

68-
void TransactionFilterProxy::setDateRange(const QDateTime &from, const QDateTime &to)
61+
void TransactionFilterProxy::setDateRange(const std::optional<QDateTime>& from, const std::optional<QDateTime>& to)
6962
{
70-
this->dateFrom = from;
71-
this->dateTo = to;
63+
dateFrom = from;
64+
dateTo = to;
7265
invalidateFilter();
7366
}
7467

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

@@ -34,7 +32,8 @@ class TransactionFilterProxy : public QSortFilterProxyModel
3432
WatchOnlyFilter_No
3533
};
3634

37-
void setDateRange(const QDateTime &from, const QDateTime &to);
35+
/** Filter transactions between date range. Use std::nullopt for open range. */
36+
void setDateRange(const std::optional<QDateTime>& from, const std::optional<QDateTime>& to);
3837
void setSearchString(const QString &);
3938
/**
4039
@note Type filter takes a bit field created with TYPE() or ALL_TYPES
@@ -55,8 +54,8 @@ class TransactionFilterProxy : public QSortFilterProxyModel
5554
bool filterAcceptsRow(int source_row, const QModelIndex & source_parent) const override;
5655

5756
private:
58-
QDateTime dateFrom;
59-
QDateTime dateTo;
57+
std::optional<QDateTime> dateFrom;
58+
std::optional<QDateTime> dateTo;
6059
QString m_search_string;
6160
quint32 typeFilter;
6261
WatchOnlyFilter watchOnlyFilter;

src/qt/transactionview.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919

2020
#include <node/ui_interface.h>
2121

22+
#include <optional>
23+
2224
#include <QApplication>
2325
#include <QComboBox>
2426
#include <QDateTimeEdit>
@@ -266,26 +268,26 @@ void TransactionView::chooseDate(int idx)
266268
{
267269
case All:
268270
transactionProxyModel->setDateRange(
269-
TransactionFilterProxy::MIN_DATE,
270-
TransactionFilterProxy::MAX_DATE);
271+
std::nullopt,
272+
std::nullopt);
271273
break;
272274
case Today:
273275
transactionProxyModel->setDateRange(
274276
GUIUtil::StartOfDay(current),
275-
TransactionFilterProxy::MAX_DATE);
277+
std::nullopt);
276278
break;
277279
case ThisWeek: {
278280
// Find last Monday
279281
QDate startOfWeek = current.addDays(-(current.dayOfWeek()-1));
280282
transactionProxyModel->setDateRange(
281283
GUIUtil::StartOfDay(startOfWeek),
282-
TransactionFilterProxy::MAX_DATE);
284+
std::nullopt);
283285

284286
} break;
285287
case ThisMonth:
286288
transactionProxyModel->setDateRange(
287289
GUIUtil::StartOfDay(QDate(current.year(), current.month(), 1)),
288-
TransactionFilterProxy::MAX_DATE);
290+
std::nullopt);
289291
break;
290292
case LastMonth:
291293
transactionProxyModel->setDateRange(
@@ -295,7 +297,7 @@ void TransactionView::chooseDate(int idx)
295297
case ThisYear:
296298
transactionProxyModel->setDateRange(
297299
GUIUtil::StartOfDay(QDate(current.year(), 1, 1)),
298-
TransactionFilterProxy::MAX_DATE);
300+
std::nullopt);
299301
break;
300302
case Range:
301303
dateRangeWidget->setVisible(true);

0 commit comments

Comments
 (0)