feat: limit the number of results
introduce a new property, `limit` that allows for narrowing down the number of returned results usecase: give me list of 5 most valuable assets ```qml tagsModel: SortFilterProxyModel { id: walletAccountAssetsModel sourceModel: assets limit: 5 function filterPredicate(modelData) { return d.uniquePermissionTokenKeys.includes(modelData.symbol.toUpperCase()) } filters: [ ExpressionFilter { expression: walletAccountAssetsModel.filterPredicate(model) } ] sorters: ExpressionSorter { expression: { return modelLeft.enabledNetworkBalance.amount > modelRight.enabledNetworkBalance.amount // descending, biggest first } } } ```
This commit is contained in:
parent
c32b851264
commit
285a86d492
|
@ -52,6 +52,30 @@ int QQmlSortFilterProxyModel::count() const
|
|||
return rowCount();
|
||||
}
|
||||
|
||||
/*!
|
||||
\qmlproperty int SortFilterProxyModel::limit
|
||||
|
||||
Limit the total number of results
|
||||
*/
|
||||
int QQmlSortFilterProxyModel::limit() const
|
||||
{
|
||||
return m_limit;
|
||||
}
|
||||
|
||||
void QQmlSortFilterProxyModel::setLimit(int newLimit)
|
||||
{
|
||||
if (m_limit == newLimit)
|
||||
return;
|
||||
|
||||
const auto prevCount = count();
|
||||
m_limit = newLimit;
|
||||
emit limitChanged();
|
||||
|
||||
if (prevCount != count()) {
|
||||
emit countChanged();
|
||||
}
|
||||
}
|
||||
|
||||
/*!
|
||||
\qmlproperty bool SortFilterProxyModel::delayed
|
||||
|
||||
|
@ -234,6 +258,14 @@ QVariant QQmlSortFilterProxyModel::sourceData(const QModelIndex &sourceIndex, in
|
|||
return sourceModel()->data(sourceIndex, role);
|
||||
}
|
||||
|
||||
int QQmlSortFilterProxyModel::rowCount(const QModelIndex& parent) const
|
||||
{
|
||||
const auto origLimit = QSortFilterProxyModel::rowCount(parent);
|
||||
if (m_limit == -1)
|
||||
return origLimit;
|
||||
return qMin(m_limit, origLimit);
|
||||
}
|
||||
|
||||
QVariant QQmlSortFilterProxyModel::data(const QModelIndex &index, int role) const
|
||||
{
|
||||
return sourceData(mapToSource(index), role);
|
||||
|
|
|
@ -23,6 +23,7 @@ class QQmlSortFilterProxyModel : public QSortFilterProxyModel,
|
|||
|
||||
Q_PROPERTY(int count READ count NOTIFY countChanged)
|
||||
Q_PROPERTY(bool delayed READ delayed WRITE setDelayed NOTIFY delayedChanged)
|
||||
Q_PROPERTY(int limit READ limit WRITE setLimit NOTIFY limitChanged FINAL)
|
||||
|
||||
Q_PROPERTY(QString filterRoleName READ filterRoleName WRITE setFilterRoleName NOTIFY filterRoleNameChanged)
|
||||
Q_PROPERTY(QString filterPattern READ filterPattern WRITE setFilterPattern NOTIFY filterPatternChanged)
|
||||
|
@ -93,6 +94,7 @@ public:
|
|||
void setSourceModel(QAbstractItemModel *sourceModel) override;
|
||||
|
||||
Q_SIGNALS:
|
||||
void limitChanged();
|
||||
void countChanged();
|
||||
void delayedChanged();
|
||||
|
||||
|
@ -105,6 +107,7 @@ Q_SIGNALS:
|
|||
void ascendingSortOrderChanged();
|
||||
|
||||
protected:
|
||||
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
|
||||
bool filterAcceptsRow(int source_row, const QModelIndex& source_parent) const override;
|
||||
bool lessThan(const QModelIndex& source_left, const QModelIndex& source_right) const override;
|
||||
|
||||
|
@ -126,6 +129,9 @@ private Q_SLOTS:
|
|||
void invalidateProxyRoles();
|
||||
|
||||
private:
|
||||
int limit() const;
|
||||
void setLimit(int newLimit);
|
||||
|
||||
QVariantMap modelDataMap(const QModelIndex& modelIndex) const;
|
||||
|
||||
void onFilterAppended(Filter* filter) override;
|
||||
|
@ -153,6 +159,7 @@ private:
|
|||
bool m_invalidateFilterQueued = false;
|
||||
bool m_invalidateQueued = false;
|
||||
bool m_invalidateProxyRolesQueued = false;
|
||||
int m_limit{-1};
|
||||
};
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue