History: filter by amount

This commit is contained in:
Ilya Kitaev 2016-10-08 03:26:45 +03:00
parent 10c2786fca
commit 0ac27e13a6
5 changed files with 48 additions and 52 deletions

View file

@ -84,6 +84,9 @@ int main(int argc, char *argv[])
qmlRegisterUncreatableType<TransactionHistory>("moneroComponents.TransactionHistory", 1, 0, "TransactionHistory",
"TransactionHistory can't be instantiated directly");
qmlRegisterUncreatableType<TransactionInfo>("moneroComponents.TransactionInfo", 1, 0, "TransactionInfo",
"TransactionHistory can't be instantiated directly");
qRegisterMetaType<PendingTransaction::Priority>();
qRegisterMetaType<TransactionInfo::Direction>();
qRegisterMetaType<TransactionHistoryModel::TransactionInfoRole>();

View file

@ -31,6 +31,7 @@ import QtQuick 2.0
import moneroComponents.Wallet 1.0
import moneroComponents.WalletManager 1.0
import moneroComponents.TransactionHistory 1.0
import moneroComponents.TransactionInfo 1.0
import moneroComponents.TransactionHistoryModel 1.0
import "../components"
@ -206,6 +207,10 @@ Rectangle {
if (amountToLine.text.length) {
model.amountToFilter = parseFloat(amountToLine.text)
}
var directionFilter = transactionsModel.get(transactionTypeDropdown.currentIndex).value
console.log("Direction filter: " + directionFilter)
model.directionFilter = directionFilter
}
@ -240,9 +245,9 @@ Rectangle {
ListModel {
id: transactionsModel
ListElement { column1: "ALL"; column2: "" }
ListElement { column1: "SENT"; column2: "" }
ListElement { column1: "RECEIVED"; column2: "" }
ListElement { column1: "ALL"; column2: ""; value: TransactionInfo.Direction_Both }
ListElement { column1: "SENT"; column2: ""; value: TransactionInfo.Direction_Out }
ListElement { column1: "RECEIVED"; column2: ""; value: TransactionInfo.Direction_In }
}

View file

@ -24,7 +24,8 @@ class TransactionInfo : public QObject
public:
enum Direction {
Direction_In = Bitmonero::TransactionInfo::Direction_In,
Direction_Out = Bitmonero::TransactionInfo::Direction_Out
Direction_Out = Bitmonero::TransactionInfo::Direction_Out,
Direction_Both // invalid direction value, used for filtering
};
Q_ENUM(Direction)

View file

@ -112,6 +112,20 @@ void TransactionHistorySortFilterModel::setAmountToFilter(double value)
}
}
int TransactionHistorySortFilterModel::directionFilter() const
{
return m_filterValues.value(TransactionHistoryModel::TransactionDirectionRole).value<TransactionInfo::Direction>();
}
void TransactionHistorySortFilterModel::setDirectionFilter(int value)
{
if (value != directionFilter()) {
m_filterValues[TransactionHistoryModel::TransactionDirectionRole] = QVariant::fromValue(value);
emit directionFilterChanged();
invalidateFilter();
}
}
void TransactionHistorySortFilterModel::sort(int column, Qt::SortOrder order)
{
@ -150,6 +164,7 @@ bool TransactionHistorySortFilterModel::filterAcceptsRow(int source_row, const Q
{
QDateTime from = QDateTime(dateFromFilter());
QDateTime to = QDateTime(dateToFilter());
to = to.addDays(1); // including upperbound
QDateTime timestamp = data.toDateTime();
bool matchFrom = from.isNull() || timestamp.isNull() || timestamp >= from;
bool matchTo = to.isNull() || timestamp.isNull() || timestamp <= to;
@ -162,19 +177,28 @@ bool TransactionHistorySortFilterModel::filterAcceptsRow(int source_row, const Q
double to = amountToFilter();
double amount = data.toDouble();
bool matchFrom = from < 0 || amount >= from;
bool matchTo = to < 0 || amount <= to;
bool matchFrom = from <= 0 || amount >= from;
bool matchTo = to <= 0 || amount <= to;
result = matchFrom && matchTo;
}
break;
case TransactionHistoryModel::TransactionDirectionRole:
result = directionFilter() == TransactionInfo::Direction_Both ? true
: data.toInt() == directionFilter();
break;
default:
break;
}
if (!result) // stop the loop once filter doesn't match
if (!result) { // stop the loop once filter doesn't match
break;
}
}
}
return result;
}
@ -183,39 +207,3 @@ bool TransactionHistorySortFilterModel::lessThan(const QModelIndex &source_left,
{
return QSortFilterProxyModel::lessThan(source_left, source_right);
}
QVariant TransactionHistorySortFilterModel::filterValue(int role)
{
return m_filterValues.value(role);
}
void TransactionHistorySortFilterModel::setFilterValue(int role, const QVariant &filterValue)
{
m_filterValues[role] = filterValue;
}
QDate TransactionHistorySortFilterModel::dateFromToFilter(TransactionHistorySortFilterModel::ScopeIndex index) const
{
int role = TransactionHistoryModel::TransactionTimeStampRole;
if (!m_filterValues.contains(role)) {
return QDate();
}
return m_filterValues.value(role).toList().at(index).toDate();
}
void TransactionHistorySortFilterModel::setDateFromToFilter(TransactionHistorySortFilterModel::ScopeIndex index, const QDate &value)
{
QVariantList scopeFilter;
int role = TransactionHistoryModel::TransactionTimeStampRole;
if (m_filterValues.contains(role)) {
scopeFilter = m_filterValues.value(role).toList();
}
while (scopeFilter.size() < 2) {
scopeFilter.append(QDate());
}
scopeFilter[index] = QVariant::fromValue(value);
m_filterValues[role] = scopeFilter;
}

View file

@ -1,12 +1,14 @@
#ifndef TRANSACTIONHISTORYSORTFILTERMODEL_H
#define TRANSACTIONHISTORYSORTFILTERMODEL_H
#include "TransactionInfo.h"
#include <QSortFilterProxyModel>
#include <QMap>
#include <QVariant>
#include <QDate>
class TransactionHistory;
class TransactionHistorySortFilterModel: public QSortFilterProxyModel
@ -17,6 +19,8 @@ class TransactionHistorySortFilterModel: public QSortFilterProxyModel
Q_PROPERTY(QDate dateToFilter READ dateToFilter WRITE setDateToFilter NOTIFY dateToFilterChanged)
Q_PROPERTY(double amountFromFilter READ amountFromFilter WRITE setAmountFromFilter NOTIFY amountFromFilterChanged)
Q_PROPERTY(double amountToFilter READ amountToFilter WRITE setAmountToFilter NOTIFY amountToFilterChanged)
Q_PROPERTY(int directionFilter READ directionFilter WRITE setDirectionFilter NOTIFY directionFilterChanged)
Q_PROPERTY(TransactionHistory * transactionHistory READ transactionHistory)
public:
@ -41,6 +45,9 @@ public:
double amountToFilter() const;
void setAmountToFilter(double value);
//! filtering by direction
int directionFilter() const;
void setDirectionFilter(int value);
Q_INVOKABLE void sort(int column, Qt::SortOrder order);
TransactionHistory * transactionHistory() const;
@ -51,6 +58,7 @@ signals:
void dateToFilterChanged();
void amountFromFilterChanged();
void amountToFilterChanged();
void directionFilterChanged();
protected:
// QSortFilterProxyModel overrides
@ -64,15 +72,6 @@ private:
To = 1
};
QVariant filterValue(int role);
void setFilterValue(int role, const QVariant &filterValue);
QDate dateFromToFilter(ScopeIndex index) const;
void setDateFromToFilter(ScopeIndex index, const QDate &value);
// double amountFromToFilter(ScopeIndex index) const;
// void setAmountFromToFilter(ScopeIndex index, double value);
private:
QMap<int, QVariant> m_filterValues;
};