[vlc-devel] [RFC 53/82] qt: add medialibrary genre model

Pierre Lamot pierre at videolabs.io
Fri Feb 1 14:01:57 CET 2019


---
 modules/gui/qt/Makefile.am                    |   6 +
 .../gui/qt/components/mediacenter/mlgenre.cpp |  58 ++++++++
 .../gui/qt/components/mediacenter/mlgenre.hpp |  60 +++++++++
 .../components/mediacenter/mlgenremodel.cpp   | 127 ++++++++++++++++++
 .../components/mediacenter/mlgenremodel.hpp   |  54 ++++++++
 5 files changed, 305 insertions(+)
 create mode 100644 modules/gui/qt/components/mediacenter/mlgenre.cpp
 create mode 100644 modules/gui/qt/components/mediacenter/mlgenre.hpp
 create mode 100644 modules/gui/qt/components/mediacenter/mlgenremodel.cpp
 create mode 100644 modules/gui/qt/components/mediacenter/mlgenremodel.hpp

diff --git a/modules/gui/qt/Makefile.am b/modules/gui/qt/Makefile.am
index 958f62fabf..0169955200 100644
--- a/modules/gui/qt/Makefile.am
+++ b/modules/gui/qt/Makefile.am
@@ -136,6 +136,10 @@ libqt_plugin_la_SOURCES = \
 	gui/qt/components/mediacenter/mlartist.hpp \
 	gui/qt/components/mediacenter/mlartistmodel.cpp \
 	gui/qt/components/mediacenter/mlartistmodel.hpp \
+	gui/qt/components/mediacenter/mlgenre.cpp \
+	gui/qt/components/mediacenter/mlgenre.hpp \
+	gui/qt/components/mediacenter/mlgenremodel.cpp \
+	gui/qt/components/mediacenter/mlgenremodel.hpp \
 	gui/qt/components/playlist/media.hpp \
 	gui/qt/components/playlist/playlist_common.cpp \
 	gui/qt/components/playlist/playlist_common.hpp \
@@ -265,6 +269,8 @@ nodist_libqt_plugin_la_SOURCES = \
 	gui/qt/components/mediacenter/mlalbumtrackmodel.moc.cpp \
 	gui/qt/components/mediacenter/mlartist.moc.cpp \
 	gui/qt/components/mediacenter/mlartistmodel.moc.cpp \
+	gui/qt/components/mediacenter/mlgenre.moc.cpp \
+	gui/qt/components/mediacenter/mlgenremodel.moc.cpp \
 	gui/qt/components/playlist/playlist_common.moc.cpp \
 	gui/qt/components/playlist/playlist_item.moc.cpp \
 	gui/qt/components/playlist/playlist_model.moc.cpp \
diff --git a/modules/gui/qt/components/mediacenter/mlgenre.cpp b/modules/gui/qt/components/mediacenter/mlgenre.cpp
new file mode 100644
index 0000000000..8bed1c1fc7
--- /dev/null
+++ b/modules/gui/qt/components/mediacenter/mlgenre.cpp
@@ -0,0 +1,58 @@
+/*****************************************************************************
+ * Copyright (C) 2019 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 <cassert>
+#include "mlgenre.hpp"
+
+MLGenre::MLGenre(const vlc_ml_genre_t *_data, QObject *_parent )
+    : QObject(_parent)
+    , m_id      ( _data->i_id, VLC_ML_PARENT_GENRE )
+    , m_name    ( QString::fromUtf8( _data->psz_name ) )
+
+{
+    assert(_data);
+}
+
+MLGenre::MLGenre(const MLGenre &genre, QObject *_parent)
+    : QObject(_parent)
+    , m_id      ( genre.m_id )
+    , m_name    ( genre.m_name )
+{
+
+}
+
+MLParentId MLGenre::getId() const
+{
+    return m_id;
+}
+
+QString MLGenre::getName() const
+{
+    return m_name;
+}
+
+unsigned int MLGenre::getNbTracks() const
+{
+    return m_nbTracks;
+}
+
+MLGenre *MLGenre::clone(QObject *parent) const
+{
+    return new MLGenre(*this, parent);
+}
+
diff --git a/modules/gui/qt/components/mediacenter/mlgenre.hpp b/modules/gui/qt/components/mediacenter/mlgenre.hpp
new file mode 100644
index 0000000000..61e44a9df3
--- /dev/null
+++ b/modules/gui/qt/components/mediacenter/mlgenre.hpp
@@ -0,0 +1,60 @@
+/*****************************************************************************
+ * Copyright (C) 2019 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 MLGENRE_HPP
+#define MLGENRE_HPP
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+#include "vlc_common.h"
+
+#include <memory>
+#include <QObject>
+#include <QString>
+#include <QList>
+#include <vlc_media_library.h>
+#include "mlhelper.hpp"
+#include "mlqmltypes.hpp"
+
+class MLGenre : public QObject
+{
+    Q_OBJECT
+
+    Q_PROPERTY(MLParentId id READ getId CONSTANT)
+    Q_PROPERTY(QString name READ getName CONSTANT)
+    Q_PROPERTY(unsigned int nbtracks READ getNbTracks CONSTANT)
+
+public:
+    MLGenre( const vlc_ml_genre_t *_data, QObject *_parent = nullptr);
+
+    MLParentId getId() const;
+    QString getName() const;
+    unsigned int getNbTracks() const;
+
+    MLGenre* clone(QObject *parent = nullptr) const;
+
+private:
+    MLGenre( const MLGenre& genre, QObject *_parent = nullptr);
+
+    MLParentId m_id;
+    QString m_name;
+    unsigned int m_nbTracks;
+};
+
+#endif
diff --git a/modules/gui/qt/components/mediacenter/mlgenremodel.cpp b/modules/gui/qt/components/mediacenter/mlgenremodel.cpp
new file mode 100644
index 0000000000..c4a23ecbae
--- /dev/null
+++ b/modules/gui/qt/components/mediacenter/mlgenremodel.cpp
@@ -0,0 +1,127 @@
+/*****************************************************************************
+ * Copyright (C) 2019 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 "mlgenremodel.hpp"
+
+#include "mlartistmodel.hpp"
+
+namespace {
+    enum Roles
+    {
+        GENRE_ID = Qt::UserRole + 1,
+        GENRE_NAME,
+        GENRE_NB_TRACKS,
+        GENRE_ARTISTS,
+        GENRE_TRACKS,
+        GENRE_ALBUMS,
+    };
+}
+
+QHash<QByteArray, vlc_ml_sorting_criteria_t> MLGenreModel::M_names_to_criteria = {
+    {"title", VLC_ML_SORTING_ALPHA}
+};
+
+MLGenreModel::MLGenreModel(QObject *parent)
+    : MLSlidingWindowModel<MLGenre>(parent)
+{
+}
+
+QVariant MLGenreModel::data(const QModelIndex &index, int role) const
+{
+    if (!index.isValid() || index.row() < 0)
+        return QVariant();
+
+    const MLGenre* ml_genre = item(static_cast<unsigned int>(index.row()));
+    if (!ml_genre)
+        return QVariant();
+
+    switch (role)
+    {
+        // Genres
+    case GENRE_ID:
+        return QVariant::fromValue( ml_genre->getId() );
+    case GENRE_NAME:
+        return QVariant::fromValue( ml_genre->getName() );
+    case GENRE_NB_TRACKS:
+        return QVariant::fromValue( ml_genre->getNbTracks() );
+    default :
+        return QVariant();
+    }
+}
+
+QHash<int, QByteArray> MLGenreModel::roleNames() const
+{
+    return {
+        { GENRE_ID, "id" },
+        { GENRE_NAME, "name" },
+        { GENRE_NB_TRACKS, "nb_tracks" },
+        { GENRE_ARTISTS, "artists" },
+        { GENRE_TRACKS, "tracks" },
+        { GENRE_ALBUMS, "albums" }
+    };
+}
+
+std::vector<std::unique_ptr<MLGenre>> MLGenreModel::fetch()
+{
+    ml_unique_ptr<vlc_ml_genre_list_t> genre_list(
+        vlc_ml_list_genres(m_ml, &m_query_param)
+    );
+    if ( genre_list == nullptr )
+        return {};
+    std::vector<std::unique_ptr<MLGenre>> res;
+    for( const vlc_ml_genre_t& genre: ml_range_iterate<vlc_ml_genre_t>( genre_list ) )
+        res.emplace_back( std::unique_ptr<MLGenre>{ new MLGenre( &genre ) } );
+    return res;
+}
+
+size_t MLGenreModel::countTotalElements() const
+{
+    auto queryParams = m_query_param;
+    queryParams.i_offset = 0;
+    queryParams.i_nbResults = 0;
+    return vlc_ml_count_genres( m_ml, &queryParams );
+}
+
+void MLGenreModel::onVlcMlEvent(const vlc_ml_event_t* event)
+{
+    switch (event->i_type)
+    {
+        case VLC_ML_EVENT_GENRE_ADDED:
+        case VLC_ML_EVENT_GENRE_UPDATED:
+        case VLC_ML_EVENT_GENRE_DELETED:
+            m_need_reset = true;
+            break;
+    }
+    MLBaseModel::onVlcMlEvent(event);
+}
+
+vlc_ml_sorting_criteria_t MLGenreModel::roleToCriteria(int role) const
+{
+    switch (role)
+    {
+    case GENRE_NAME:
+        return VLC_ML_SORTING_ALPHA;
+    default :
+        return VLC_ML_SORTING_DEFAULT;
+    }
+}
+
+vlc_ml_sorting_criteria_t MLGenreModel::nameToCriteria(QByteArray name) const
+{
+    return M_names_to_criteria.value(name, VLC_ML_SORTING_DEFAULT);
+}
diff --git a/modules/gui/qt/components/mediacenter/mlgenremodel.hpp b/modules/gui/qt/components/mediacenter/mlgenremodel.hpp
new file mode 100644
index 0000000000..c83ee01688
--- /dev/null
+++ b/modules/gui/qt/components/mediacenter/mlgenremodel.hpp
@@ -0,0 +1,54 @@
+/*****************************************************************************
+ * Copyright (C) 2019 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 MLGENREMODEL_HPP
+#define MLGENREMODEL_HPP
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include <memory>
+#include <QObject>
+#include "mlbasemodel.hpp"
+#include "mlgenre.hpp"
+
+class MLGenreModel : public MLSlidingWindowModel<MLGenre>
+{
+    Q_OBJECT
+
+public:
+    explicit MLGenreModel(QObject *parent = nullptr);
+    virtual ~MLGenreModel() = default;
+
+    QHash<int, QByteArray> roleNames() const override;
+    QVariant data(const QModelIndex &index, int role) const override;
+
+private:
+    std::vector<std::unique_ptr<MLGenre>> fetch() override;
+    size_t countTotalElements() const override;
+    void onVlcMlEvent(const vlc_ml_event_t* event) override;
+    vlc_ml_sorting_criteria_t roleToCriteria(int role) const override;
+    vlc_ml_sorting_criteria_t nameToCriteria(QByteArray name) const override;
+
+
+    static QHash<QByteArray, vlc_ml_sorting_criteria_t> M_names_to_criteria;
+};
+
+
+#endif // MLGENREMODEL_HPP
-- 
2.19.1



More information about the vlc-devel mailing list