History: filtering by paymentId and date

This commit is contained in:
Ilya Kitaev 2016-10-08 01:24:18 +03:00
parent e3cea8582f
commit 9b09e83ba0
4 changed files with 125 additions and 22 deletions

View file

@ -42,7 +42,11 @@ Rectangle {
color: "#F0EEEE"
onModelChanged: {
console.log("model.rowCount: " + model.rowCount())
if (typeof model !== 'undefined') {
// setup date filter scope according to real transactions
fromDatePicker.currentDate = model.transactionHistory.firstDateTime
toDatePicker.currentDate = model.transactionHistory.lastDateTime
}
}
@ -107,6 +111,8 @@ Rectangle {
anchors.leftMargin: 17
anchors.rightMargin: 17
anchors.topMargin: 5
}
// Filter by description input (not implemented yet)
@ -154,6 +160,9 @@ Rectangle {
anchors.leftMargin: 17
anchors.topMargin: 5
z: 2
onCurrentDateChanged: {
console.log("CurrentDate: " + currentDate)
}
}
// DateTo picker
@ -188,6 +197,12 @@ Rectangle {
shadowPressedColor: "#2D002F"
releasedColor: "#6B0072"
pressedColor: "#4D0051"
onClicked: {
// Apply filter here;
model.paymentIdFilter = paymentIdLine.text
model.dateFromFilter = fromDatePicker.currentDate
model.dateToFilter = toDatePicker.currentDate
}
}
CheckBox {
@ -373,21 +388,6 @@ Rectangle {
model.sort(0, desc ? Qt.DescendingOrder : Qt.AscendingOrder)
}
}
/*
ListModel {
id: testModel
ListElement { paymentId: "faef56b9acf67a7dba75ec01f403497049d7cff111628edfe7b57278554dc798"; address: "faef56b9acf67a7dba75ec01f403497049d7cff111628edfe7b57278554dc798"; date: "Jan 12, 2014"; time: "12:23 <font size='2'>AM</font>"; amount: "0.<font size='2'>000709159241</font>"; balance: "19301.<font size='2'>870709159241</font>"; description: "Client from Australia"; out: false }
ListElement { paymentId: "faef56b9acf67a7dba75ec01f403497049d7cff111628edfe7b57278554dc798"; address: "faef56b9acf67a7dba75ec01f403497049d7cff111628edfe7b57278554dc798"; date: "Jan 12, 2014"; time: "12:23 <font size='2'>AM</font>"; amount: "0.<font size='2'>000709159241</font>"; balance: "19301.<font size='2'>870709159241</font>"; description: ""; out: true }
ListElement { paymentId: "faef56b9acf67a7dba75ec01f403497049d7cff111628edfe7b57278554dc798"; address: "faef56b9acf67a7dba75ec01f403497049d7cff111628edfe7b57278554dc798"; date: "Jan 12, 2014"; time: "12:23 <font size='2'>AM</font>"; amount: "0.<font size='2'>000709159241</font>"; balance: "19301.<font size='2'>870709159241</font>"; description: ""; out: true }
ListElement { paymentId: ""; address: "faef56b9acf67a7dba75ec01f403497049d7cff111628edfe7b57278554dc798"; date: "Jan 12, 2014"; time: "12:23 <font size='2'>AM</font>"; amount: "0.<font size='2'>000709159241</font>"; balance: "19301.<font size='2'>870709159241</font>"; description: ""; out: false }
ListElement { paymentId: ""; address: "faef56b9acf67a7dba75ec01f403497049d7cff111628edfe7b57278554dc798"; date: "Jan 12, 2014"; time: "12:23 <font size='2'>AM</font>"; amount: "0.<font size='2'>000709159241</font>"; balance: "19301.<font size='2'>870709159241</font>"; description: "Client from Australia"; out: false }
ListElement { paymentId: "faef56b9acf67a7dba75ec01f403497049d7cff111628edfe7b57278554dc798"; address: "faef56b9acf67a7dba75ec01f403497049d7cff111628edfe7b57278554dc798"; date: "Jan 12, 2014"; time: "12:23 <font size='2'>AM</font>"; amount: "0.<font size='2'>000709159241</font>"; balance: "19301.<font size='2'>870709159241</font>"; description: ""; out: false }
ListElement { paymentId: ""; address: "faef56b9acf67a7dba75ec01f403497049d7cff111628edfe7b57278554dc798"; date: "Jan 12, 2014"; time: "12:23 <font size='2'>AM</font>"; amount: "0.<font size='2'>000709159241</font>"; balance: "19301.<font size='2'>870709159241</font>"; description: ""; out: false }
ListElement { paymentId: "faef56b9acf67a7dba75ec01f403497049d7cff111628edfe7b57278554dc798"; address: "faef56b9acf67a7dba75ec01f403497049d7cff111628edfe7b57278554dc798"; date: "Jan 12, 2014"; time: "12:23 <font size='2'>AM</font>"; amount: "0.<font size='2'>000709159241</font>"; balance: "19301.<font size='2'>870709159241</font>"; description: ""; out: false }
ListElement { paymentId: "faef56b9acf67a7dba75ec01f403497049d7cff111628edfe7b57278554dc798"; address: "faef56b9acf67a7dba75ec01f403497049d7cff111628edfe7b57278554dc798"; date: "Jan 12, 2014"; time: "12:23 <font size='2'>AM</font>"; amount: "0.<font size='2'>000709159241</font>"; balance: "19301.<font size='2'>870709159241</font>"; description: "Client from Australia"; out: false }
ListElement { paymentId: "faef56b9acf67a7dba75ec01f403497049d7cff111628edfe7b57278554dc798"; address: "faef56b9acf67a7dba75ec01f403497049d7cff111628edfe7b57278554dc798"; date: "Jan 12, 2014"; time: "12:23 <font size='2'>AM</font>"; amount: "0.<font size='2'>000709159241</font>"; balance: "19301.<font size='2'>870709159241</font>"; description: ""; out: false }
}
*/
Scroll {
id: flickableScroll

View file

@ -30,7 +30,7 @@ class Wallet : public QObject
Q_PROPERTY(quint64 unlockedBalance READ unlockedBalance)
Q_PROPERTY(TransactionHistory * history READ history)
Q_PROPERTY(QString paymentId READ paymentId WRITE setPaymentId)
Q_PROPERTY(TransactionHistorySortFilterModel * historyModel READ historyModel)
Q_PROPERTY(TransactionHistorySortFilterModel * historyModel READ historyModel NOTIFY historyModelChanged)
public:
@ -151,6 +151,7 @@ signals:
void moneySpent(const QString &txId, quint64 amount);
void moneyReceived(const QString &txId, quint64 amount);
void newBlock(quint64 height);
void historyModelChanged() const;
private:

View file

@ -6,17 +6,49 @@
TransactionHistorySortFilterModel::TransactionHistorySortFilterModel(QObject *parent)
: QSortFilterProxyModel(parent)
{
setDynamicSortFilter(true);
}
QString TransactionHistorySortFilterModel::paymentIdFilter() const
{
return m_filterValues.value(TransactionHistoryModel::TransactionPaymentIdRole).toString();
}
void TransactionHistorySortFilterModel::setPaymentIdFilter(const QString &arg)
{
if (paymentIdFilter() != arg) {
m_filterValues[TransactionHistoryModel::TransactionPaymentIdRole] = arg;
emit paymentIdFilterChanged();
invalidateFilter();
}
}
QDate TransactionHistorySortFilterModel::dateFromFilter() const
{
return dateFromToFilter(From);
}
void TransactionHistorySortFilterModel::setDateFromFilter(const QDate &date)
{
if (date != dateFromFilter()) {
setDateFromToFilter(From, date);
emit dateFromFilterChanged();
invalidateFilter();
}
}
QDate TransactionHistorySortFilterModel::dateToFilter() const
{
return dateFromToFilter(To);
}
void TransactionHistorySortFilterModel::setDateToFilter(const QDate &date)
{
if (date != dateToFilter()) {
setDateFromToFilter(To, date);
emit dateFromFilterChanged();
invalidateFilter();
}
}
void TransactionHistorySortFilterModel::sort(int column, Qt::SortOrder order)
@ -24,6 +56,12 @@ void TransactionHistorySortFilterModel::sort(int column, Qt::SortOrder order)
QSortFilterProxyModel::sort(column, order);
}
TransactionHistory *TransactionHistorySortFilterModel::transactionHistory() const
{
const TransactionHistoryModel * model = static_cast<const TransactionHistoryModel*> (sourceModel());
return model->transactionHistory();
}
bool TransactionHistorySortFilterModel::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const
{
@ -38,11 +76,27 @@ bool TransactionHistorySortFilterModel::filterAcceptsRow(int source_row, const Q
bool result = true;
// iterating through filters
for (int role : m_filterValues.keys()) {
if (m_filterValues.contains(role)) {
QVariant data = sourceModel()->data(index, role);
result = data.toString().contains(m_filterValues.value(role).toString());
if (result)
switch (role) {
case TransactionHistoryModel::TransactionPaymentIdRole:
result = data.toString().contains(paymentIdFilter());
break;
case TransactionHistoryModel::TransactionTimeStampRole:
{
QDateTime from = QDateTime(dateFromFilter());
QDateTime to = QDateTime(dateToFilter());
QDateTime timestamp = data.toDateTime();
bool matchFrom = from.isNull() || timestamp.isNull() || timestamp >= from;
bool matchTo = to.isNull() || timestamp.isNull() || timestamp <= to;
result = matchFrom && matchTo;
}
default:
break;
}
if (!result) // stop the loop once filter doesn't match
break;
}
}
@ -64,3 +118,26 @@ void TransactionHistorySortFilterModel::setFilterValue(int role, const QVariant
{
m_filterValues[role] = filterValue;
}
QDate TransactionHistorySortFilterModel::dateFromToFilter(TransactionHistorySortFilterModel::DateScopeIndex 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::DateScopeIndex 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

@ -5,18 +5,36 @@
#include <QSortFilterProxyModel>
#include <QMap>
#include <QVariant>
#include <QDate>
class TransactionHistory;
class TransactionHistorySortFilterModel: public QSortFilterProxyModel
{
Q_OBJECT
Q_OBJECT
Q_PROPERTY(QString paymentIdFilter READ paymentIdFilter WRITE setPaymentIdFilter NOTIFY paymentIdFilterChanged)
Q_PROPERTY(QDate dateFromFilter READ dateFromFilter WRITE setDateFromFilter NOTIFY dateFromFilterChanged)
Q_PROPERTY(QDate dateToFilter READ dateToFilter WRITE setDateToFilter NOTIFY dateToFilterChanged)
Q_PROPERTY(TransactionHistory * transactionHistory READ transactionHistory)
public:
TransactionHistorySortFilterModel(QObject * parent = nullptr);
QString paymentIdFilter() const;
void setPaymentIdFilter(const QString &arg);
QDate dateFromFilter() const;
void setDateFromFilter(const QDate &date);
QDate dateToFilter() const;
void setDateToFilter(const QDate &date);
Q_INVOKABLE void sort(int column, Qt::SortOrder order);
TransactionHistory * transactionHistory() const;
signals:
void paymentIdFilterChanged();
void dateFromFilterChanged();
void dateToFilterChanged();
protected:
// QSortFilterProxyModel overrides
virtual bool filterAcceptsRow(int source_row, const QModelIndex &source_parent) const;
@ -24,8 +42,15 @@ protected:
private:
enum DateScopeIndex {
From = 0,
To = 1
};
QVariant filterValue(int role);
void setFilterValue(int role, const QVariant &filterValue);
QDate dateFromToFilter(DateScopeIndex index) const;
void setDateFromToFilter(DateScopeIndex index, const QDate &value);
private:
QMap<int, QVariant> m_filterValues;