Merge PR #506: Add sorting capability to generic sortfilter proxy model

This commit is contained in:
Jenkins nymea 2021-01-14 00:38:22 +01:00
commit ad5d1f222b
2 changed files with 41 additions and 0 deletions

View File

@ -42,6 +42,26 @@ void SortFilterProxyModel::setFilterList(const QStringList &filterList)
} }
} }
QString SortFilterProxyModel::sortRoleName() const
{
return m_sortRoleName;
}
void SortFilterProxyModel::setSortRoleName(const QString &sortRoleName)
{
if (m_sortRoleName != sortRoleName) {
m_sortRoleName = sortRoleName;
emit sortRoleNameChanged();
sort(0, sortOrder());
}
}
void SortFilterProxyModel::setSortOrder(Qt::SortOrder sortOrder)
{
sort(0, sortOrder);
emit sortOrderChanged();
}
QVariant SortFilterProxyModel::data(int row, const QString &role) const QVariant SortFilterProxyModel::data(int row, const QString &role) const
{ {
int roleId = roleNames().key(role.toUtf8()); int roleId = roleNames().key(role.toUtf8());
@ -60,3 +80,13 @@ bool SortFilterProxyModel::filterAcceptsRow(int source_row, const QModelIndex &s
} }
return QSortFilterProxyModel::filterAcceptsRow(source_row, source_parent); return QSortFilterProxyModel::filterAcceptsRow(source_row, source_parent);
} }
bool SortFilterProxyModel::lessThan(const QModelIndex &source_left, const QModelIndex &source_right) const
{
int sortRole = sourceModel()->roleNames().key(m_sortRoleName.toUtf8());
QVariant left = sourceModel()->data(source_left, sortRole);
QVariant right = sourceModel()->data(source_right, sortRole);
return left <= right;
}

View File

@ -8,6 +8,8 @@ class SortFilterProxyModel : public QSortFilterProxyModel
Q_OBJECT Q_OBJECT
Q_PROPERTY(QString filterRoleName READ filterRoleName WRITE setFilterRoleName NOTIFY filterRoleNameChanged) Q_PROPERTY(QString filterRoleName READ filterRoleName WRITE setFilterRoleName NOTIFY filterRoleNameChanged)
Q_PROPERTY(QStringList filterList READ filterList WRITE setFilterList NOTIFY filterListChanged) Q_PROPERTY(QStringList filterList READ filterList WRITE setFilterList NOTIFY filterListChanged)
Q_PROPERTY(QString sortRoleName READ sortRoleName WRITE setSortRoleName NOTIFY sortRoleNameChanged)
Q_PROPERTY(Qt::SortOrder sortOrder READ sortOrder WRITE setSortOrder NOTIFY sortOrderChanged)
Q_PROPERTY(int count READ rowCount NOTIFY countChanged) Q_PROPERTY(int count READ rowCount NOTIFY countChanged)
public: public:
@ -19,19 +21,28 @@ public:
QStringList filterList() const; QStringList filterList() const;
void setFilterList(const QStringList &filterList); void setFilterList(const QStringList &filterList);
QString sortRoleName() const;
void setSortRoleName(const QString &sortRoleName);
void setSortOrder(Qt::SortOrder sortOrder);
Q_INVOKABLE QVariant data(int row, const QString &role) const; Q_INVOKABLE QVariant data(int row, const QString &role) const;
signals: signals:
void filterRoleNameChanged(); void filterRoleNameChanged();
void filterListChanged(); void filterListChanged();
void sortRoleNameChanged();
void sortOrderChanged();
void countChanged(); void countChanged();
protected: protected:
bool filterAcceptsRow(int source_row, const QModelIndex &source_parent) 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;
private: private:
QString m_filterRoleName; QString m_filterRoleName;
QStringList m_filterList; QStringList m_filterList;
QString m_sortRoleName;
}; };
#endif // SORTFILTERPROXYMODEL_H #endif // SORTFILTERPROXYMODEL_H