transactionfilterproxy.cpp raw

   1  // Copyright (c) 2011-2021 The Limenka developers
   2  // Distributed under the MIT software license, see the accompanying
   3  // file COPYING or http://www.opensource.org/licenses/mit-license.php.
   4  
   5  #include <qt/transactionfilterproxy.h>
   6  
   7  #include <qt/transactiontablemodel.h>
   8  #include <qt/transactionrecord.h>
   9  
  10  #include <algorithm>
  11  #include <cstdlib>
  12  #include <optional>
  13  
  14  TransactionFilterProxy::TransactionFilterProxy(QObject* parent)
  15      : QSortFilterProxyModel(parent),
  16        m_search_string(),
  17        typeFilter(ALL_TYPES)
  18  {
  19  }
  20  
  21  bool TransactionFilterProxy::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const
  22  {
  23      QModelIndex index = sourceModel()->index(sourceRow, 0, sourceParent);
  24  
  25      int status = index.data(TransactionTableModel::StatusRole).toInt();
  26      if (!showInactive && status == TransactionStatus::Conflicted)
  27          return false;
  28  
  29      int type = index.data(TransactionTableModel::TypeRole).toInt();
  30      if (!(TYPE(type) & typeFilter))
  31          return false;
  32  
  33      bool involvesWatchAddress = index.data(TransactionTableModel::WatchonlyRole).toBool();
  34      if (involvesWatchAddress && watchOnlyFilter == WatchOnlyFilter_No)
  35          return false;
  36      if (!involvesWatchAddress && watchOnlyFilter == WatchOnlyFilter_Yes)
  37          return false;
  38  
  39      QDateTime datetime = index.data(TransactionTableModel::DateRole).toDateTime();
  40      if (dateFrom && datetime < *dateFrom) return false;
  41      if (dateTo && datetime > *dateTo) return false;
  42  
  43      QString address = index.data(TransactionTableModel::AddressRole).toString();
  44      QString label = index.data(TransactionTableModel::LabelRole).toString();
  45      QString txid = index.data(TransactionTableModel::TxHashRole).toString();
  46      if (!address.contains(m_search_string, Qt::CaseInsensitive) &&
  47          !  label.contains(m_search_string, Qt::CaseInsensitive) &&
  48          !   txid.contains(m_search_string, Qt::CaseInsensitive)) {
  49          return false;
  50      }
  51  
  52      qint64 amount = llabs(index.data(TransactionTableModel::AmountRole).toLongLong());
  53      if (amount < minAmount)
  54          return false;
  55  
  56      return true;
  57  }
  58  
  59  void TransactionFilterProxy::setDateRange(const std::optional<QDateTime>& from, const std::optional<QDateTime>& to)
  60  {
  61  #if QT_VERSION >= QT_VERSION_CHECK(6, 10, 0)
  62      beginFilterChange();
  63  #endif
  64  
  65      dateFrom = from;
  66      dateTo = to;
  67  
  68  #if QT_VERSION >= QT_VERSION_CHECK(6, 10, 0)
  69      endFilterChange(QSortFilterProxyModel::Direction::Rows);
  70  #else
  71      invalidateFilter();
  72  #endif
  73  }
  74  
  75  void TransactionFilterProxy::setSearchString(const QString &search_string)
  76  {
  77      if (m_search_string == search_string) return;
  78  
  79  #if QT_VERSION >= QT_VERSION_CHECK(6, 10, 0)
  80      beginFilterChange();
  81  #endif
  82  
  83      m_search_string = search_string;
  84  
  85  #if QT_VERSION >= QT_VERSION_CHECK(6, 10, 0)
  86      endFilterChange(QSortFilterProxyModel::Direction::Rows);
  87  #else
  88      invalidateFilter();
  89  #endif
  90  }
  91  
  92  void TransactionFilterProxy::setTypeFilter(quint32 modes)
  93  {
  94  #if QT_VERSION >= QT_VERSION_CHECK(6, 10, 0)
  95      beginFilterChange();
  96  #endif
  97  
  98      this->typeFilter = modes;
  99  
 100  #if QT_VERSION >= QT_VERSION_CHECK(6, 10, 0)
 101      endFilterChange(QSortFilterProxyModel::Direction::Rows);
 102  #else
 103      invalidateFilter();
 104  #endif
 105  }
 106  
 107  void TransactionFilterProxy::setMinAmount(const CAmount& minimum)
 108  {
 109  #if QT_VERSION >= QT_VERSION_CHECK(6, 10, 0)
 110      beginFilterChange();
 111  #endif
 112  
 113      this->minAmount = minimum;
 114  
 115  #if QT_VERSION >= QT_VERSION_CHECK(6, 10, 0)
 116      endFilterChange(QSortFilterProxyModel::Direction::Rows);
 117  #else
 118      invalidateFilter();
 119  #endif
 120  }
 121  
 122  void TransactionFilterProxy::setWatchOnlyFilter(WatchOnlyFilter filter)
 123  {
 124  #if QT_VERSION >= QT_VERSION_CHECK(6, 10, 0)
 125      beginFilterChange();
 126  #endif
 127  
 128      this->watchOnlyFilter = filter;
 129  
 130  #if QT_VERSION >= QT_VERSION_CHECK(6, 10, 0)
 131      endFilterChange(QSortFilterProxyModel::Direction::Rows);
 132  #else
 133      invalidateFilter();
 134  #endif
 135  }
 136  
 137  void TransactionFilterProxy::setShowInactive(bool _showInactive)
 138  {
 139  #if QT_VERSION >= QT_VERSION_CHECK(6, 10, 0)
 140      beginFilterChange();
 141  #endif
 142  
 143      this->showInactive = _showInactive;
 144  
 145  #if QT_VERSION >= QT_VERSION_CHECK(6, 10, 0)
 146      endFilterChange(QSortFilterProxyModel::Direction::Rows);
 147  #else
 148      invalidateFilter();
 149  #endif
 150  }
 151