[vlc-devel] [PATCH 35/39] qt: support sorting from SortFilterProxyModel

Prince Gupta guptaprince8832 at gmail.com
Thu Jan 7 11:50:31 UTC 2021


---
 modules/gui/qt/util/sortfilterproxymodel.cpp | 35 ++++++++++++++++++++
 modules/gui/qt/util/sortfilterproxymodel.hpp | 13 ++++++++
 2 files changed, 48 insertions(+)

diff --git a/modules/gui/qt/util/sortfilterproxymodel.cpp b/modules/gui/qt/util/sortfilterproxymodel.cpp
index 8106f9d089..ab70bb9833 100644
--- a/modules/gui/qt/util/sortfilterproxymodel.cpp
+++ b/modules/gui/qt/util/sortfilterproxymodel.cpp
@@ -33,6 +33,7 @@ SortFilterProxyModel::SortFilterProxyModel( QObject *parent )
     connect( this, &QAbstractItemModel::layoutChanged, this, &SortFilterProxyModel::countChanged );
 
     connect( this, &QAbstractProxyModel::sourceModelChanged, this, &SortFilterProxyModel::updateFilterRole );
+    connect( this, &QAbstractProxyModel::sourceModelChanged, this, &SortFilterProxyModel::updateSortRole );
 }
 
 QString SortFilterProxyModel::searchPattern() const
@@ -57,6 +58,35 @@ void SortFilterProxyModel::setSearchRole( const QByteArray &searchRole )
     updateFilterRole();
 }
 
+QString SortFilterProxyModel::sortCriteria() const
+{
+    return m_sortCriteria;
+}
+
+void SortFilterProxyModel::setSortCriteria(const QString &sortCriteria)
+{
+    if (m_sortCriteria == sortCriteria)
+        return;
+
+    m_sortCriteria = sortCriteria;
+    updateSortRole();
+    emit sortCriteriaChanged();
+}
+
+Qt::SortOrder SortFilterProxyModel::sortOrder() const
+{
+    return QSortFilterProxyModel::sortOrder();
+}
+
+void SortFilterProxyModel::setSortOrder(Qt::SortOrder sortOrder)
+{
+    if (this->sortOrder() == sortOrder)
+        return;
+
+    this->sort(0, sortOrder);
+    emit sortOrderChanged();
+}
+
 int SortFilterProxyModel::count() const
 {
     return rowCount();
@@ -91,3 +121,8 @@ void SortFilterProxyModel::updateFilterRole()
 {
     setFilterRole( roleNames().key( m_searchRole ) );
 }
+
+void SortFilterProxyModel::updateSortRole()
+{
+    setSortRole( roleNames().key( m_sortCriteria.toUtf8() ) );
+}
diff --git a/modules/gui/qt/util/sortfilterproxymodel.hpp b/modules/gui/qt/util/sortfilterproxymodel.hpp
index d2fba63f41..d574505304 100644
--- a/modules/gui/qt/util/sortfilterproxymodel.hpp
+++ b/modules/gui/qt/util/sortfilterproxymodel.hpp
@@ -33,6 +33,8 @@ class SortFilterProxyModel : public QSortFilterProxyModel
 
     Q_PROPERTY( QByteArray searchRole READ searchRole WRITE setSearchRole NOTIFY searchRoleChanged )
     Q_PROPERTY( QString searchPattern READ searchPattern WRITE setSearchPattern NOTIFY searchPatternChanged )
+    Q_PROPERTY( QString sortCriteria READ sortCriteria WRITE setSortCriteria NOTIFY sortCriteriaChanged )
+    Q_PROPERTY( Qt::SortOrder sortOrder READ sortOrder WRITE setSortOrder NOTIFY sortOrderChanged )
     Q_PROPERTY( int count READ count NOTIFY countChanged )
 
 public:
@@ -44,6 +46,12 @@ public:
     QByteArray searchRole() const;
     void setSearchRole( const QByteArray &searchRole );
 
+    QString sortCriteria() const;
+    void setSortCriteria( const QString &sortCriteria );
+
+    Qt::SortOrder sortOrder() const;
+    void setSortOrder(Qt::SortOrder sortOrder);
+
     int count() const;
 
     Q_INVOKABLE QMap<QString, QVariant> getDataAt( int idx );
@@ -54,12 +62,17 @@ signals:
     void searchPatternChanged();
     void searchRoleChanged();
     void countChanged();
+    void sortCriteriaChanged();
+    void sortOrderChanged();
 
 private slots:
     void updateFilterRole();
+    void updateSortRole();
 
 private:
     QByteArray m_searchRole;
+    QString m_sortCriteria;
+    Qt::SortOrder m_sortOrder;
 };
 
 #endif // SORT_FILTER_PROXY_MODEL
-- 
2.25.1



More information about the vlc-devel mailing list