Implement filter row instead of tabs, allows for more expressive filters
[novacoin.git] / src / qt / transactionfilterproxy.cpp
1 #include "transactionfilterproxy.h"
2 #include "transactiontablemodel.h"
3
4 #include <QDateTime>
5 #include <QDebug>
6
7 // Earliest date that can be represented (far in the past)
8 const QDateTime TransactionFilterProxy::MIN_DATE = QDateTime::fromTime_t(0);
9 // Last date that can be represented (far in the future)
10 const QDateTime TransactionFilterProxy::MAX_DATE = QDateTime::fromTime_t(0xFFFFFFFF);
11
12 TransactionFilterProxy::TransactionFilterProxy(QObject *parent) :
13     QSortFilterProxyModel(parent),
14     dateFrom(MIN_DATE),
15     dateTo(MAX_DATE),
16     addrPrefix(),
17     typeFilter(ALL_TYPES),
18     minAmount(0)
19 {
20 }
21
22 bool TransactionFilterProxy::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const
23 {
24     QModelIndex index = sourceModel()->index(sourceRow, 0, sourceParent);
25
26     int type = index.data(TransactionTableModel::TypeRole).toInt();
27     QDateTime datetime = index.data(TransactionTableModel::DateRole).toDateTime();
28     QString address = index.data(TransactionTableModel::AddressRole).toString();
29     QString label = index.data(TransactionTableModel::LabelRole).toString();
30     qint64 amount = index.data(TransactionTableModel::AbsoluteAmountRole).toLongLong();
31
32     if(!(TYPE(type) & typeFilter))
33         return false;
34     if(datetime < dateFrom || datetime > dateTo)
35         return false;
36     if(!address.startsWith(addrPrefix) && !label.startsWith(addrPrefix))
37         return false;
38     if(amount < minAmount)
39         return false;
40
41     return true;
42 }
43
44 void TransactionFilterProxy::setDateRange(const QDateTime &from, const QDateTime &to)
45 {
46     this->dateFrom = from;
47     this->dateTo = to;
48     invalidateFilter();
49 }
50
51 void TransactionFilterProxy::setAddressPrefix(const QString &addrPrefix)
52 {
53     this->addrPrefix = addrPrefix;
54     invalidateFilter();
55 }
56
57 void TransactionFilterProxy::setTypeFilter(quint32 modes)
58 {
59     this->typeFilter = modes;
60     invalidateFilter();
61 }
62
63 void TransactionFilterProxy::setMinAmount(qint64 minimum)
64 {
65     this->minAmount = minimum;
66     invalidateFilter();
67 }