[vlc-commits] qt/mlbasemodel: Add the 'getItemsForIndexes' function

Benjamin Arnaud git at videolan.org
Mon Mar 15 10:34:52 UTC 2021


vlc | branch: master | Benjamin Arnaud <benjamin.arnaud at videolabs.io> | Thu Mar 11 10:16:50 2021 +0100| [539f902507759765a485f8c48c3fe53c63e7b3ac] | committer: Pierre Lamot

qt/mlbasemodel: Add the 'getItemsForIndexes' function

And update the implementation

Signed-off-by: Pierre Lamot <pierre at videolabs.io>

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

 modules/gui/qt/medialibrary/mlbasemodel.cpp | 64 +++++++++++++++++++++++++++--
 modules/gui/qt/medialibrary/mlbasemodel.hpp | 29 +++++++++----
 2 files changed, 80 insertions(+), 13 deletions(-)

diff --git a/modules/gui/qt/medialibrary/mlbasemodel.cpp b/modules/gui/qt/medialibrary/mlbasemodel.cpp
index 68eb28dac9..91527c7828 100644
--- a/modules/gui/qt/medialibrary/mlbasemodel.cpp
+++ b/modules/gui/qt/medialibrary/mlbasemodel.cpp
@@ -17,10 +17,15 @@
  *****************************************************************************/
 
 #include <cassert>
-#include "mlbasemodel.hpp"
 #include "medialib.hpp"
 #include <vlc_cxx_helpers.hpp>
+
 #include "util/listcache.hpp"
+#include "util/qmlinputitem.hpp"
+
+// MediaLibrary includes
+#include "mlbasemodel.hpp"
+#include "mlhelper.hpp"
 
 static constexpr ssize_t COUNT_UNINITIALIZED =
     ListCache<std::unique_ptr<MLItem>>::COUNT_UNINITIALIZED;
@@ -265,8 +270,7 @@ QVariant MLBaseModel::getIdForIndex(QVariant index) const
     return QVariant::fromValue(obj->getId());
 }
 
-
-QVariantList MLBaseModel::getIdsForIndexes(QModelIndexList indexes) const
+QVariantList MLBaseModel::getIdsForIndexes(const QModelIndexList & indexes) const
 {
     QVariantList idList;
     idList.reserve(indexes.length());
@@ -279,7 +283,7 @@ QVariantList MLBaseModel::getIdsForIndexes(QModelIndexList indexes) const
     return idList;
 }
 
-QVariantList MLBaseModel::getIdsForIndexes(QVariantList indexes) const
+QVariantList MLBaseModel::getIdsForIndexes(const QVariantList & indexes) const
 {
     QVariantList idList;
 
@@ -300,6 +304,58 @@ QVariantList MLBaseModel::getIdsForIndexes(QVariantList indexes) const
     return idList;
 }
 
+//-------------------------------------------------------------------------------------------------
+
+/* Q_INVOKABLE virtual */
+QVariantList MLBaseModel::getItemsForIndexes(const QModelIndexList & indexes) const
+{
+    assert(m_ml);
+
+    QVariantList items;
+
+    vlc_ml_query_params_t query;
+
+    memset(&query, 0, sizeof(vlc_ml_query_params_t));
+
+    for (const QModelIndex & index : indexes)
+    {
+        MLItem * item = this->item(index.row());
+
+        if (item == nullptr)
+            continue;
+
+        const MLItemId & itemId = item->getId();
+
+        // NOTE: When we have a parent it's a collection of media(s).
+        if (itemId.type == VLC_ML_PARENT_UNKNOWN)
+        {
+            QmlInputItem input(vlc_ml_get_input_item(m_ml, itemId.id), false);
+
+            items.append(QVariant::fromValue(input));
+        }
+        else
+        {
+            ml_unique_ptr<vlc_ml_media_list_t> list;
+
+            list.reset(vlc_ml_list_media_of(m_ml, &query, itemId.type, itemId.id));
+
+            if (list == nullptr)
+                continue;
+
+            for (const vlc_ml_media_t & media : ml_range_iterate<vlc_ml_media_t>(list))
+            {
+                QmlInputItem input(vlc_ml_get_input_item(m_ml, media.i_id), false);
+
+                items.append(QVariant::fromValue(input));
+            }
+        }
+    }
+
+    return items;
+}
+
+//-------------------------------------------------------------------------------------------------
+
 unsigned MLBaseModel::getCount() const
 {
     if (!m_mediaLib)
diff --git a/modules/gui/qt/medialibrary/mlbasemodel.hpp b/modules/gui/qt/medialibrary/mlbasemodel.hpp
index d1057ac6b1..349a75a3fc 100644
--- a/modules/gui/qt/medialibrary/mlbasemodel.hpp
+++ b/modules/gui/qt/medialibrary/mlbasemodel.hpp
@@ -45,23 +45,34 @@ class MLBaseModel : public QAbstractListModel
 {
     Q_OBJECT
 
+    Q_PROPERTY(MLItemId parentId READ parentId WRITE setParentId NOTIFY parentIdChanged
+               RESET unsetParentId)
+
+    Q_PROPERTY(MediaLib * ml READ ml WRITE setMl)
+
+    Q_PROPERTY(QString searchPattern READ searchPattern WRITE setSearchPattern)
+
+    Q_PROPERTY(Qt::SortOrder sortOrder READ getSortOrder WRITE setSortOder NOTIFY sortOrderChanged)
+
+    Q_PROPERTY(QString sortCriteria READ getSortCriteria WRITE setSortCriteria
+               NOTIFY sortCriteriaChanged RESET unsetSortCriteria)
+
+    Q_PROPERTY(unsigned int count READ getCount NOTIFY countChanged)
+
 public:
     explicit MLBaseModel(QObject *parent = nullptr);
+
     virtual ~MLBaseModel();
 
+public: // Interface
     Q_INVOKABLE void sortByColumn(QByteArray name, Qt::SortOrder order);
 
-    Q_PROPERTY( MLItemId parentId READ parentId WRITE setParentId NOTIFY parentIdChanged RESET unsetParentId )
-    Q_PROPERTY( MediaLib* ml READ ml WRITE setMl )
-    Q_PROPERTY( QString searchPattern READ searchPattern WRITE setSearchPattern )
+    Q_INVOKABLE virtual QVariant getIdForIndex(QVariant index) const;
 
-    Q_PROPERTY( Qt::SortOrder sortOrder READ getSortOrder WRITE setSortOder NOTIFY sortOrderChanged )
-    Q_PROPERTY( QString sortCriteria READ getSortCriteria WRITE setSortCriteria NOTIFY sortCriteriaChanged RESET unsetSortCriteria )
-    Q_PROPERTY( unsigned int count READ getCount NOTIFY countChanged )
+    Q_INVOKABLE virtual QVariantList getIdsForIndexes(const QVariantList    & indexes) const;
+    Q_INVOKABLE virtual QVariantList getIdsForIndexes(const QModelIndexList & indexes) const;
 
-    Q_INVOKABLE virtual QVariant getIdForIndex( QVariant index) const;
-    Q_INVOKABLE virtual QVariantList getIdsForIndexes( QVariantList indexes ) const;
-    Q_INVOKABLE virtual QVariantList getIdsForIndexes( QModelIndexList indexes ) const;
+    Q_INVOKABLE virtual QVariantList getItemsForIndexes(const QModelIndexList & indexes) const;
 
     Q_INVOKABLE QMap<QString, QVariant> getDataAt(int index);
 



More information about the vlc-commits mailing list