[vlc-commits] qt, qml: add SortFilterProxyModel to filter models in QML

Prince Gupta git at videolan.org
Mon Oct 5 18:10:33 CEST 2020


vlc | branch: master | Prince Gupta <guptaprince8832 at gmail.com> | Wed Sep 30 00:22:53 2020 +0530| [3ca5b632005fe24ab2307d696717656f8d8440b1] | committer: Pierre Lamot

qt, qml: add SortFilterProxyModel to filter models in QML

> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=3ca5b632005fe24ab2307d696717656f8d8440b1
---

 modules/gui/qt/Makefile.am                   |  3 +
 modules/gui/qt/maininterface/mainui.cpp      |  2 +
 modules/gui/qt/util/sortfilterproxymodel.cpp | 93 ++++++++++++++++++++++++++++
 modules/gui/qt/util/sortfilterproxymodel.hpp | 65 +++++++++++++++++++
 4 files changed, 163 insertions(+)

diff --git a/modules/gui/qt/Makefile.am b/modules/gui/qt/Makefile.am
index f775bbb5d1..6ff173323e 100644
--- a/modules/gui/qt/Makefile.am
+++ b/modules/gui/qt/Makefile.am
@@ -212,6 +212,8 @@ libqt_plugin_la_SOURCES = \
 	gui/qt/util/selectable_list_model.cpp \
 	gui/qt/util/selectable_list_model.hpp \
 	gui/qt/util/singleton.hpp \
+	gui/qt/util/sortfilterproxymodel.cpp \
+	gui/qt/util/sortfilterproxymodel.hpp \
 	gui/qt/util/soutchain.cpp gui/qt/util/soutchain.hpp \
 	gui/qt/util/systempalette.cpp gui/qt/util/systempalette.hpp \
 	gui/qt/util/validators.cpp gui/qt/util/validators.hpp \
@@ -345,6 +347,7 @@ nodist_libqt_plugin_la_SOURCES = \
 	gui/qt/util/recents.moc.cpp \
 	gui/qt/util/renderer_manager.moc.cpp \
 	gui/qt/util/selectable_list_model.moc.cpp \
+	gui/qt/util/sortfilterproxymodel.moc.cpp \
 	gui/qt/util/systempalette.moc.cpp \
 	gui/qt/util/validators.moc.cpp \
 	gui/qt/util/varchoicemodel.moc.cpp \
diff --git a/modules/gui/qt/maininterface/mainui.cpp b/modules/gui/qt/maininterface/mainui.cpp
index a44f2da909..83d81c4ab8 100644
--- a/modules/gui/qt/maininterface/mainui.cpp
+++ b/modules/gui/qt/maininterface/mainui.cpp
@@ -24,6 +24,7 @@
 #include "util/i18n.hpp"
 #include "util/systempalette.hpp"
 #include "util/recent_media_model.hpp"
+#include "util/sortfilterproxymodel.hpp"
 #include "util/navigation_history.hpp"
 
 #include "dialogs/help/aboutmodel.hpp"
@@ -215,6 +216,7 @@ void MainUI::registerQMLTypes()
     qmlRegisterType<NetworkMediaContextMenu>( "org.videolan.vlc", 0, 1, "NetworkMediaContextMenu" );
     qmlRegisterType<NetworkDeviceContextMenu>( "org.videolan.vlc", 0, 1, "NetworkDeviceContextMenu" );
     qmlRegisterType<PlaylistContextMenu>( "org.videolan.vlc", 0, 1, "PlaylistContextMenu" );
+    qmlRegisterType<SortFilterProxyModel>( "org.videolan.vlc", 0, 1, "SortFilterProxyModel" );
 }
 
 void MainUI::onQmlWarning(const QList<QQmlError>& qmlErrors)
diff --git a/modules/gui/qt/util/sortfilterproxymodel.cpp b/modules/gui/qt/util/sortfilterproxymodel.cpp
new file mode 100644
index 0000000000..8106f9d089
--- /dev/null
+++ b/modules/gui/qt/util/sortfilterproxymodel.cpp
@@ -0,0 +1,93 @@
+/*****************************************************************************
+ * Copyright (C) 2020 VLC authors and VideoLAN
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * ( at your option ) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ *****************************************************************************/
+
+#include "sortfilterproxymodel.hpp"
+
+#include <QItemSelection>
+
+#include <cassert>
+
+SortFilterProxyModel::SortFilterProxyModel( QObject *parent )
+    : QSortFilterProxyModel( parent )
+{
+    setFilterCaseSensitivity(Qt::CaseInsensitive);
+
+    connect( this, &QAbstractListModel::rowsInserted, this, &SortFilterProxyModel::countChanged );
+    connect( this, &QAbstractListModel::rowsRemoved, this, &SortFilterProxyModel::countChanged );
+    connect( this, &QAbstractItemModel::modelReset, this, &SortFilterProxyModel::countChanged );
+    connect( this, &QAbstractItemModel::layoutChanged, this, &SortFilterProxyModel::countChanged );
+
+    connect( this, &QAbstractProxyModel::sourceModelChanged, this, &SortFilterProxyModel::updateFilterRole );
+}
+
+QString SortFilterProxyModel::searchPattern() const
+{
+    return filterRegExp().pattern();
+}
+
+void SortFilterProxyModel::setSearchPattern( const QString &searchPattern )
+{
+    setFilterRegExp(searchPattern);
+}
+
+QByteArray SortFilterProxyModel::searchRole() const
+{
+    return m_searchRole;
+}
+
+void SortFilterProxyModel::setSearchRole( const QByteArray &searchRole )
+{
+    m_searchRole = searchRole;
+    emit searchRoleChanged();
+    updateFilterRole();
+}
+
+int SortFilterProxyModel::count() const
+{
+    return rowCount();
+}
+
+QMap<QString, QVariant> SortFilterProxyModel::getDataAt( int idx )
+{
+    QMap<QString, QVariant> dataDict;
+    QHash<int,QByteArray> roles = roleNames();
+    for( const auto role: roles.keys() ) {
+        dataDict[roles[role]] = data( index( idx, 0 ), role );
+    }
+    return dataDict;
+}
+
+QModelIndexList SortFilterProxyModel::mapIndexesToSource( const QModelIndexList &indexes )
+{
+    QModelIndexList sourceIndexes;
+    sourceIndexes.reserve( indexes.size() );
+    for( const auto &proxyIndex : indexes ) {
+        sourceIndexes.push_back( mapToSource(proxyIndex) );
+    }
+    return sourceIndexes;
+}
+
+int SortFilterProxyModel::mapIndexToSource(int idx)
+{
+    return mapToSource( index( idx, 0 ) ).row();
+}
+
+void SortFilterProxyModel::updateFilterRole()
+{
+    setFilterRole( roleNames().key( m_searchRole ) );
+}
diff --git a/modules/gui/qt/util/sortfilterproxymodel.hpp b/modules/gui/qt/util/sortfilterproxymodel.hpp
new file mode 100644
index 0000000000..d2fba63f41
--- /dev/null
+++ b/modules/gui/qt/util/sortfilterproxymodel.hpp
@@ -0,0 +1,65 @@
+/*****************************************************************************
+ * Copyright (C) 2020 VLC authors and VideoLAN
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * ( at your option ) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ *****************************************************************************/
+
+#ifndef SORT_FILTER_PROXY_MODEL
+#define SORT_FILTER_PROXY_MODEL
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include "qt.hpp"
+#include <QObject>
+#include <QSortFilterProxyModel>
+
+class SortFilterProxyModel : public QSortFilterProxyModel
+{
+    Q_OBJECT
+
+    Q_PROPERTY( QByteArray searchRole READ searchRole WRITE setSearchRole NOTIFY searchRoleChanged )
+    Q_PROPERTY( QString searchPattern READ searchPattern WRITE setSearchPattern NOTIFY searchPatternChanged )
+    Q_PROPERTY( int count READ count NOTIFY countChanged )
+
+public:
+    SortFilterProxyModel( QObject * parent = nullptr );
+
+    QString searchPattern() const;
+    void setSearchPattern( const QString &searchPattern );
+
+    QByteArray searchRole() const;
+    void setSearchRole( const QByteArray &searchRole );
+
+    int count() const;
+
+    Q_INVOKABLE QMap<QString, QVariant> getDataAt( int idx );
+    Q_INVOKABLE QModelIndexList mapIndexesToSource( const QModelIndexList &indexes );
+    Q_INVOKABLE int mapIndexToSource( int idx );
+
+signals:
+    void searchPatternChanged();
+    void searchRoleChanged();
+    void countChanged();
+
+private slots:
+    void updateFilterRole();
+
+private:
+    QByteArray m_searchRole;
+};
+
+#endif // SORT_FILTER_PROXY_MODEL



More information about the vlc-commits mailing list