[vlc-devel] [PATCH 05/17] qt/medialibrary: Create MLPlaylistModel

Benjamin Arnaud benjamin.arnaud at videolabs.io
Fri Feb 19 10:25:29 UTC 2021


---
 modules/gui/qt/Makefile.am                    |   3 +
 .../gui/qt/medialibrary/mlplaylistmodel.cpp   | 203 ++++++++++++++++++
 .../gui/qt/medialibrary/mlplaylistmodel.hpp   |  79 +++++++
 po/POTFILES.in                                |   2 +
 4 files changed, 287 insertions(+)
 create mode 100644 modules/gui/qt/medialibrary/mlplaylistmodel.cpp
 create mode 100644 modules/gui/qt/medialibrary/mlplaylistmodel.hpp

diff --git a/modules/gui/qt/Makefile.am b/modules/gui/qt/Makefile.am
index 4365fda0f8..6057040f99 100644
--- a/modules/gui/qt/Makefile.am
+++ b/modules/gui/qt/Makefile.am
@@ -174,6 +174,8 @@ libqt_plugin_la_SOURCES = \
 	gui/qt/medialibrary/mlplaylistlistmodel.hpp \
 	gui/qt/medialibrary/mlplaylistmedia.cpp \
 	gui/qt/medialibrary/mlplaylistmedia.hpp \
+	gui/qt/medialibrary/mlplaylistmodel.cpp \
+	gui/qt/medialibrary/mlplaylistmodel.hpp \
 	gui/qt/menus/custom_menus.cpp \
 	gui/qt/menus/custom_menus.hpp \
 	gui/qt/menus/qml_menu_wrapper.cpp \
@@ -337,6 +339,7 @@ nodist_libqt_plugin_la_SOURCES = \
 	gui/qt/medialibrary/mlvideomodel.moc.cpp \
 	gui/qt/medialibrary/mlplaylist.moc.cpp \
 	gui/qt/medialibrary/mlplaylistlistmodel.moc.cpp \
+	gui/qt/medialibrary/mlplaylistmodel.moc.cpp \
 	gui/qt/menus/custom_menus.moc.cpp \
 	gui/qt/menus/qml_menu_wrapper.moc.cpp \
 	gui/qt/menus/menus.moc.cpp \
diff --git a/modules/gui/qt/medialibrary/mlplaylistmodel.cpp b/modules/gui/qt/medialibrary/mlplaylistmodel.cpp
new file mode 100644
index 0000000000..7f00210fd8
--- /dev/null
+++ b/modules/gui/qt/medialibrary/mlplaylistmodel.cpp
@@ -0,0 +1,203 @@
+/*****************************************************************************
+ * Copyright (C) 2021 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.
+ *****************************************************************************/
+
+#if HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include "mlplaylistmodel.hpp"
+
+// MediaLibrary includes
+#include "mlhelper.hpp"
+#include "mlplaylistmedia.hpp"
+
+//-------------------------------------------------------------------------------------------------
+// Static variables
+
+static const QHash<QByteArray, vlc_ml_sorting_criteria_t> criterias =
+{
+    {"id",             VLC_ML_SORTING_DEFAULT},
+    {"title",          VLC_ML_SORTING_ALPHA},
+    {"duration",       VLC_ML_SORTING_DURATION},
+    {"duration_short", VLC_ML_SORTING_DURATION},
+    {"playcount",      VLC_ML_SORTING_PLAYCOUNT},
+};
+
+//=================================================================================================
+// MLPlaylistModel
+//=================================================================================================
+
+/* explicit */ MLPlaylistModel::MLPlaylistModel(QObject * parent)
+    : MLBaseModel(parent) {}
+
+
+//-------------------------------------------------------------------------------------------------
+// QAbstractItemModel implementation
+//-------------------------------------------------------------------------------------------------
+
+QHash<int, QByteArray> MLPlaylistModel::roleNames() const /* override */
+{
+    return
+    {
+        { MEDIA_ID,                 "id"                 },
+        { MEDIA_TITLE,              "title"              },
+        { MEDIA_THUMBNAIL,          "thumbnail"          },
+        { MEDIA_DURATION,           "duration"           },
+        { MEDIA_DURATION_SHORT,     "duration_short"     },
+        { MEDIA_PROGRESS,           "progress"           },
+        { MEDIA_PLAYCOUNT,          "playcount"          },
+        { MEDIA_RESOLUTION,         "resolution_name"    },
+        { MEDIA_CHANNEL,            "channel"            },
+        { MEDIA_MRL,                "mrl"                },
+        { MEDIA_DISPLAY_MRL,        "display_mrl"        },
+        { MEDIA_AUDIO_TRACK,        "audioDesc"          },
+        { MEDIA_VIDEO_TRACK,        "videoDesc"          },
+        { MEDIA_TITLE_FIRST_SYMBOL, "title_first_symbol" }
+    };
+}
+
+QVariant MLPlaylistModel::data(const QModelIndex & index, int role) const /* override */
+{
+    MLPlaylistMedia * media = static_cast<MLPlaylistMedia *>(item(index.row()));
+
+    if (media == nullptr)
+        return QVariant();
+
+    switch (role)
+    {
+        case MEDIA_ID:
+            return QVariant::fromValue(media->getId());
+        case MEDIA_TITLE:
+            return QVariant::fromValue(media->getTitle());
+        case MEDIA_THUMBNAIL:
+            return QVariant::fromValue(media->getThumbnail());
+        case MEDIA_DURATION:
+            return QVariant::fromValue(media->getDuration());
+        case MEDIA_DURATION_SHORT:
+            return QVariant::fromValue(media->getDurationShort());
+        case MEDIA_PROGRESS:
+            return QVariant::fromValue(media->getProgress());
+        case MEDIA_PLAYCOUNT:
+            return QVariant::fromValue(media->getPlayCount());
+        case MEDIA_RESOLUTION:
+            return QVariant::fromValue(media->getResolutionName());
+        case MEDIA_CHANNEL:
+            return QVariant::fromValue(media->getChannel());
+        case MEDIA_MRL:
+            return QVariant::fromValue(media->getMRL());
+        case MEDIA_DISPLAY_MRL:
+            return QVariant::fromValue(media->getMRLDisplay());
+        case MEDIA_VIDEO_TRACK:
+            return QVariant::fromValue(media->getVideo());
+        case MEDIA_AUDIO_TRACK:
+            return QVariant::fromValue(media->getAudio());
+        case MEDIA_TITLE_FIRST_SYMBOL:
+            return QVariant::fromValue(getFirstSymbol(media->getTitle()));
+        default:
+            return QVariant();
+    }
+}
+
+//-------------------------------------------------------------------------------------------------
+// Protected MLBaseModel implementation
+//-------------------------------------------------------------------------------------------------
+
+vlc_ml_sorting_criteria_t MLPlaylistModel::roleToCriteria(int role) const /* override */
+{
+    switch (role)
+    {
+        case MEDIA_TITLE:
+            return VLC_ML_SORTING_ALPHA;
+        case MEDIA_DURATION:
+        case MEDIA_DURATION_SHORT:
+            return VLC_ML_SORTING_DURATION;
+        case MEDIA_PLAYCOUNT:
+            return VLC_ML_SORTING_PLAYCOUNT;
+        default:
+            return VLC_ML_SORTING_DEFAULT;
+    }
+}
+
+vlc_ml_sorting_criteria_t MLPlaylistModel::nameToCriteria(QByteArray name) const /* override */
+{
+    return criterias.value(name, VLC_ML_SORTING_DEFAULT);
+}
+
+QByteArray MLPlaylistModel::criteriaToName(vlc_ml_sorting_criteria_t criteria) const /* override */
+{
+    return criterias.key(criteria, "");
+}
+
+//-------------------------------------------------------------------------------------------------
+
+ListCacheLoader<std::unique_ptr<MLItem>> * MLPlaylistModel::createLoader() const /* override */
+{
+    return new Loader(*this);
+}
+
+//-------------------------------------------------------------------------------------------------
+// Private MLBaseModel reimplementation
+//-------------------------------------------------------------------------------------------------
+
+void MLPlaylistModel::onVlcMlEvent(const MLEvent & event) /* override */
+{
+    if (event.i_type == VLC_ML_EVENT_PLAYLIST_UPDATED)
+    {
+        m_need_reset = true;
+
+        // NOTE: Maybe we should call this from MLBaseModel ?
+        emit resetRequested();
+    }
+
+    MLBaseModel::onVlcMlEvent(event);
+}
+
+//=================================================================================================
+// Loader
+//=================================================================================================
+
+MLPlaylistModel::Loader::Loader(const MLPlaylistModel & model) : MLBaseModel::BaseLoader(model) {}
+
+size_t MLPlaylistModel::Loader::count() const /* override */
+{
+    vlc_ml_query_params_t params = getParams().toCQueryParams();
+
+    return vlc_ml_count_playlist_media(m_ml, &params, m_parent.id);
+}
+
+std::vector<std::unique_ptr<MLItem>>
+MLPlaylistModel::Loader::load(size_t index, size_t count) const /* override */
+{
+    vlc_ml_query_params_t params = getParams(index, count).toCQueryParams();
+
+    ml_unique_ptr<vlc_ml_media_list_t> list {
+        vlc_ml_list_playlist_media(m_ml, &params, m_parent.id)
+    };
+
+    if (list == nullptr)
+        return {};
+
+    std::vector<std::unique_ptr<MLItem>> result;
+
+    for (const vlc_ml_media_t & media : ml_range_iterate<vlc_ml_media_t> (list))
+    {
+        result.emplace_back(std::make_unique<MLPlaylistMedia>(m_ml, &media));
+    }
+
+    return result;
+}
diff --git a/modules/gui/qt/medialibrary/mlplaylistmodel.hpp b/modules/gui/qt/medialibrary/mlplaylistmodel.hpp
new file mode 100644
index 0000000000..85b47cc825
--- /dev/null
+++ b/modules/gui/qt/medialibrary/mlplaylistmodel.hpp
@@ -0,0 +1,79 @@
+/*****************************************************************************
+ * Copyright (C) 2021 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 MLPLAYLISTMODEL_HPP
+#define MLPLAYLISTMODEL_HPP
+
+// MediaLibrary includes
+#include "mlbasemodel.hpp"
+
+class MLPlaylistModel : public MLBaseModel
+{
+    Q_OBJECT
+
+public:
+    enum Role
+    {
+        MEDIA_ID = Qt::UserRole + 1,
+        MEDIA_TITLE,
+        MEDIA_THUMBNAIL,
+        MEDIA_DURATION,
+        MEDIA_DURATION_SHORT,
+        MEDIA_PROGRESS,
+        MEDIA_PLAYCOUNT,
+        MEDIA_RESOLUTION,
+        MEDIA_CHANNEL,
+        MEDIA_MRL,
+        MEDIA_DISPLAY_MRL,
+        MEDIA_VIDEO_TRACK,
+        MEDIA_AUDIO_TRACK,
+        MEDIA_TITLE_FIRST_SYMBOL,
+    };
+
+public:
+    explicit MLPlaylistModel(QObject * parent = nullptr);
+
+public: // QAbstractItemModel implementation
+    QHash<int, QByteArray> roleNames() const override;
+
+    QVariant data(const QModelIndex & index, int role = Qt::DisplayRole) const override;
+
+protected: // MLBaseModel implementation    
+    vlc_ml_sorting_criteria_t roleToCriteria(int role) const override;
+
+    vlc_ml_sorting_criteria_t nameToCriteria(QByteArray name) const override;
+
+    QByteArray criteriaToName(vlc_ml_sorting_criteria_t criteria) const override;
+
+    ListCacheLoader<std::unique_ptr<MLItem>> * createLoader() const override;
+
+private: // MLBaseModel implementation
+    void onVlcMlEvent(const MLEvent & event) override;
+
+private:
+    struct Loader : public MLBaseModel::BaseLoader
+    {
+        Loader(const MLPlaylistModel & model);
+
+        size_t count() const override;
+
+        std::vector<std::unique_ptr<MLItem>> load(size_t index, size_t count) const override;
+    };
+};
+
+#endif // MLPLAYLISTMODEL_HPP
diff --git a/po/POTFILES.in b/po/POTFILES.in
index d765d78991..a7870b93d0 100644
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -774,6 +774,8 @@ modules/gui/qt/medialibrary/mlplaylistlistmodel.cpp
 modules/gui/qt/medialibrary/mlplaylistlistmodel.hpp
 modules/gui/qt/medialibrary/mlplaylistmedia.cpp
 modules/gui/qt/medialibrary/mlplaylistmedia.hpp
+modules/gui/qt/medialibrary/mlplaylistmodel.cpp
+modules/gui/qt/medialibrary/mlplaylistmodel.hpp
 modules/gui/qt/menus/menus.cpp
 modules/gui/qt/menus/menus.hpp
 modules/gui/qt/qt.cpp
-- 
2.25.1



More information about the vlc-devel mailing list