History: filter by amount

This commit is contained in:
Ilya Kitaev 2016-10-08 02:28:27 +03:00
parent 9b09e83ba0
commit 8b748bf25d
3 changed files with 123 additions and 16 deletions

View file

@ -160,9 +160,6 @@ Rectangle {
anchors.leftMargin: 17
anchors.topMargin: 5
z: 2
onCurrentDateChanged: {
console.log("CurrentDate: " + currentDate)
}
}
// DateTo picker
@ -202,11 +199,21 @@ Rectangle {
model.paymentIdFilter = paymentIdLine.text
model.dateFromFilter = fromDatePicker.currentDate
model.dateToFilter = toDatePicker.currentDate
if (advancedFilteringCheckBox.checked) {
if (amountFromLine.text.length) {
model.amountFromFilter = parseFloat(amountFromLine.text)
}
if (amountToLine.text.length) {
model.amountToFilter = parseFloat(amountToLine.text)
}
}
}
}
CheckBox {
id: checkBox
id: advancedFilteringCheckBox
text: qsTr("Advance filtering")
anchors.left: filterButton.right
anchors.bottom: filterButton.bottom
@ -233,9 +240,10 @@ Rectangle {
ListModel {
id: transactionsModel
ListElement { column1: "ALL"; column2: "" }
ListElement { column1: "SENT"; column2: "" }
ListElement { column1: "RECIVE"; column2: "" }
ListElement { column1: "ON HOLD"; column2: "" }
ListElement { column1: "RECEIVED"; column2: "" }
}
StandardDropdown {

View file

@ -3,6 +3,39 @@
#include <QDebug>
namespace {
/**
* helper to extract scope value from filter
*/
template <typename T>
T scopeFilterValue(const QMap<int, QVariant> &filters, int role, int scopeIndex)
{
if (!filters.contains(role)) {
return T();
}
return filters.value(role).toList().at(scopeIndex).value<T>();
}
/**
* helper to setup scope value to filter
*/
template <typename T>
void setScopeFilterValue(QMap<int, QVariant> &filters, int role, int scopeIndex, const T &value)
{
QVariantList scopeFilter;
if (filters.contains(role)) {
scopeFilter = filters.value(role).toList();
}
while (scopeFilter.size() < 2) {
scopeFilter.append(T());
}
scopeFilter[scopeIndex] = QVariant::fromValue(value);
filters[role] = scopeFilter;
}
}
TransactionHistorySortFilterModel::TransactionHistorySortFilterModel(QObject *parent)
: QSortFilterProxyModel(parent)
{
@ -25,13 +58,13 @@ void TransactionHistorySortFilterModel::setPaymentIdFilter(const QString &arg)
QDate TransactionHistorySortFilterModel::dateFromFilter() const
{
return dateFromToFilter(From);
return scopeFilterValue<QDate>(m_filterValues, TransactionHistoryModel::TransactionTimeStampRole, ScopeIndex::From);
}
void TransactionHistorySortFilterModel::setDateFromFilter(const QDate &date)
{
if (date != dateFromFilter()) {
setDateFromToFilter(From, date);
setScopeFilterValue(m_filterValues, TransactionHistoryModel::TransactionTimeStampRole, ScopeIndex::From, date);
emit dateFromFilterChanged();
invalidateFilter();
}
@ -39,18 +72,47 @@ void TransactionHistorySortFilterModel::setDateFromFilter(const QDate &date)
QDate TransactionHistorySortFilterModel::dateToFilter() const
{
return dateFromToFilter(To);
return scopeFilterValue<QDate>(m_filterValues, TransactionHistoryModel::TransactionTimeStampRole, ScopeIndex::To);
}
void TransactionHistorySortFilterModel::setDateToFilter(const QDate &date)
{
if (date != dateToFilter()) {
setDateFromToFilter(To, date);
emit dateFromFilterChanged();
setScopeFilterValue(m_filterValues, TransactionHistoryModel::TransactionTimeStampRole, ScopeIndex::To, date);
emit dateToFilterChanged();
invalidateFilter();
}
}
double TransactionHistorySortFilterModel::amountFromFilter() const
{
return scopeFilterValue<double>(m_filterValues, TransactionHistoryModel::TransactionAmountRole, ScopeIndex::From);
}
void TransactionHistorySortFilterModel::setAmountFromFilter(double value)
{
if (value != amountFromFilter()) {
setScopeFilterValue(m_filterValues, TransactionHistoryModel::TransactionAmountRole, ScopeIndex::From, value);
emit amountFromFilterChanged();
invalidateFilter();
}
}
double TransactionHistorySortFilterModel::amountToFilter() const
{
return scopeFilterValue<double>(m_filterValues, TransactionHistoryModel::TransactionAmountRole, ScopeIndex::To);
}
void TransactionHistorySortFilterModel::setAmountToFilter(double value)
{
if (value != amountToFilter()) {
setScopeFilterValue(m_filterValues, TransactionHistoryModel::TransactionAmountRole, ScopeIndex::To, value);
emit amountToFilterChanged();
invalidateFilter();
}
}
void TransactionHistorySortFilterModel::sort(int column, Qt::SortOrder order)
{
QSortFilterProxyModel::sort(column, order);
@ -93,9 +155,22 @@ bool TransactionHistorySortFilterModel::filterAcceptsRow(int source_row, const Q
bool matchTo = to.isNull() || timestamp.isNull() || timestamp <= to;
result = matchFrom && matchTo;
}
break;
case TransactionHistoryModel::TransactionAmountRole:
{
double from = amountFromFilter();
double to = amountToFilter();
double amount = data.toDouble();
bool matchFrom = from < 0 || amount >= from;
bool matchTo = to < 0 || amount <= to;
result = matchFrom && matchTo;
}
break;
default:
break;
}
if (!result) // stop the loop once filter doesn't match
break;
}
@ -119,7 +194,7 @@ void TransactionHistorySortFilterModel::setFilterValue(int role, const QVariant
m_filterValues[role] = filterValue;
}
QDate TransactionHistorySortFilterModel::dateFromToFilter(TransactionHistorySortFilterModel::DateScopeIndex index) const
QDate TransactionHistorySortFilterModel::dateFromToFilter(TransactionHistorySortFilterModel::ScopeIndex index) const
{
int role = TransactionHistoryModel::TransactionTimeStampRole;
if (!m_filterValues.contains(role)) {
@ -128,7 +203,7 @@ QDate TransactionHistorySortFilterModel::dateFromToFilter(TransactionHistorySort
return m_filterValues.value(role).toList().at(index).toDate();
}
void TransactionHistorySortFilterModel::setDateFromToFilter(TransactionHistorySortFilterModel::DateScopeIndex index, const QDate &value)
void TransactionHistorySortFilterModel::setDateFromToFilter(TransactionHistorySortFilterModel::ScopeIndex index, const QDate &value)
{
QVariantList scopeFilter;
int role = TransactionHistoryModel::TransactionTimeStampRole;
@ -141,3 +216,6 @@ void TransactionHistorySortFilterModel::setDateFromToFilter(TransactionHistorySo
scopeFilter[index] = QVariant::fromValue(value);
m_filterValues[role] = scopeFilter;
}

View file

@ -15,17 +15,32 @@ class TransactionHistorySortFilterModel: public QSortFilterProxyModel
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(double amountFromFilter READ amountFromFilter WRITE setAmountFromFilter NOTIFY amountFromFilterChanged)
Q_PROPERTY(double amountToFilter READ amountToFilter WRITE setAmountToFilter NOTIFY amountToFilterChanged)
Q_PROPERTY(TransactionHistory * transactionHistory READ transactionHistory)
public:
TransactionHistorySortFilterModel(QObject * parent = nullptr);
//! filtering by payment id
QString paymentIdFilter() const;
void setPaymentIdFilter(const QString &arg);
//! filtering by date (lower bound)
QDate dateFromFilter() const;
void setDateFromFilter(const QDate &date);
//! filtering by to date (upper bound)
QDate dateToFilter() const;
void setDateToFilter(const QDate &date);
//! filtering by amount (lower bound)
double amountFromFilter() const;
void setAmountFromFilter(double value);
//! filtering by amount (upper bound)
double amountToFilter() const;
void setAmountToFilter(double value);
Q_INVOKABLE void sort(int column, Qt::SortOrder order);
TransactionHistory * transactionHistory() const;
@ -34,6 +49,8 @@ signals:
void paymentIdFilterChanged();
void dateFromFilterChanged();
void dateToFilterChanged();
void amountFromFilterChanged();
void amountToFilterChanged();
protected:
// QSortFilterProxyModel overrides
@ -42,15 +59,19 @@ protected:
private:
enum DateScopeIndex {
enum ScopeIndex {
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);
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;