[vlc-devel] [RFC 52/82] qt: add medialibrary artist model

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


---
 modules/gui/qt/Makefile.am                    |   6 +
 .../qt/components/mediacenter/mlartist.cpp    |  95 ++++++++++++
 .../qt/components/mediacenter/mlartist.hpp    |  75 ++++++++++
 .../components/mediacenter/mlartistmodel.cpp  | 141 ++++++++++++++++++
 .../components/mediacenter/mlartistmodel.hpp  |  52 +++++++
 5 files changed, 369 insertions(+)
 create mode 100644 modules/gui/qt/components/mediacenter/mlartist.cpp
 create mode 100644 modules/gui/qt/components/mediacenter/mlartist.hpp
 create mode 100644 modules/gui/qt/components/mediacenter/mlartistmodel.cpp
 create mode 100644 modules/gui/qt/components/mediacenter/mlartistmodel.hpp

diff --git a/modules/gui/qt/Makefile.am b/modules/gui/qt/Makefile.am
index 0277acdf4a..958f62fabf 100644
--- a/modules/gui/qt/Makefile.am
+++ b/modules/gui/qt/Makefile.am
@@ -132,6 +132,10 @@ libqt_plugin_la_SOURCES = \
 	gui/qt/components/mediacenter/mlalbumtrack.hpp \
 	gui/qt/components/mediacenter/mlalbumtrackmodel.cpp \
 	gui/qt/components/mediacenter/mlalbumtrackmodel.hpp \
+	gui/qt/components/mediacenter/mlartist.cpp \
+	gui/qt/components/mediacenter/mlartist.hpp \
+	gui/qt/components/mediacenter/mlartistmodel.cpp \
+	gui/qt/components/mediacenter/mlartistmodel.hpp \
 	gui/qt/components/playlist/media.hpp \
 	gui/qt/components/playlist/playlist_common.cpp \
 	gui/qt/components/playlist/playlist_common.hpp \
@@ -259,6 +263,8 @@ nodist_libqt_plugin_la_SOURCES = \
 	gui/qt/components/mediacenter/mlalbummodel.moc.cpp \
 	gui/qt/components/mediacenter/mlalbumtrack.moc.cpp \
 	gui/qt/components/mediacenter/mlalbumtrackmodel.moc.cpp \
+	gui/qt/components/mediacenter/mlartist.moc.cpp \
+	gui/qt/components/mediacenter/mlartistmodel.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/mlartist.cpp b/modules/gui/qt/components/mediacenter/mlartist.cpp
new file mode 100644
index 0000000000..bf4e2ee2a7
--- /dev/null
+++ b/modules/gui/qt/components/mediacenter/mlartist.cpp
@@ -0,0 +1,95 @@
+/*****************************************************************************
+ * 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 "mlartist.hpp"
+
+MLArtist::MLArtist(const vlc_ml_artist_t* _data, QObject *_parent)
+    : QObject(_parent)
+    , m_id      ( _data->i_id, VLC_ML_PARENT_ARTIST )
+    , m_name    ( QString::fromUtf8( _data->psz_name ) )
+    , m_shortBio( QString::fromUtf8( _data->psz_shortbio ) )
+    , m_cover   ( QString::fromUtf8( _data->psz_artwork_mrl ) )
+    , m_nbAlbums( _data->i_nb_album )
+{
+    assert( _data );
+}
+
+MLArtist::MLArtist(const MLArtist &artist, QObject *_parent)
+    : QObject(_parent)
+    , m_id      ( artist.m_id )
+    , m_name    ( artist.m_name )
+    , m_shortBio( artist.m_shortBio )
+    , m_cover   ( artist.m_cover )
+    , m_nbAlbums( artist.m_nbAlbums )
+{
+
+}
+
+MLParentId MLArtist::getId() const
+{
+    return m_id;
+}
+
+QString MLArtist::getName() const
+{
+    return m_name;
+}
+
+QString MLArtist::getShortBio() const
+{
+    return m_shortBio;
+}
+
+QString MLArtist::getCover() const
+{
+    return m_cover;
+}
+
+unsigned int MLArtist::getNbAlbums() const
+{
+    return m_nbAlbums;
+}
+
+
+unsigned int MLArtist::getNbTracks() const
+{
+    return m_nbTracks;
+}
+
+MLArtist *MLArtist::clone(QObject *parent) const
+{
+    return new MLArtist(*this, parent);
+}
+
+
+QString MLArtist::getPresName() const
+{
+    return m_name;
+}
+
+QString MLArtist::getPresImage() const
+{
+    return m_cover;
+}
+
+QString MLArtist::getPresInfo() const
+{
+    return m_shortBio;
+}
+
diff --git a/modules/gui/qt/components/mediacenter/mlartist.hpp b/modules/gui/qt/components/mediacenter/mlartist.hpp
new file mode 100644
index 0000000000..f9a9e5a31b
--- /dev/null
+++ b/modules/gui/qt/components/mediacenter/mlartist.hpp
@@ -0,0 +1,75 @@
+/*****************************************************************************
+ * 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 MLARTIST_HPP
+#define MLARTIST_HPP
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+#include "vlc_common.h"
+
+#include <QObject>
+#include <QString>
+#include <QList>
+#include <memory>
+#include <vlc_media_library.h>
+
+#include "mlalbum.hpp"
+#include "mlhelper.hpp"
+#include "mlqmltypes.hpp"
+
+class MLArtist : public QObject
+{
+    Q_OBJECT
+
+    Q_PROPERTY(MLParentId id READ getId CONSTANT)
+    Q_PROPERTY(QString name READ getName CONSTANT)
+    Q_PROPERTY(QString shortbio READ getShortBio CONSTANT)
+    Q_PROPERTY(QString cover READ getCover CONSTANT)
+    Q_PROPERTY(unsigned int nbalbums READ getNbAlbums CONSTANT)
+    Q_PROPERTY(unsigned int nbtracks READ getNbTracks CONSTANT)
+
+public:
+    MLArtist(const vlc_ml_artist_t *_data, QObject *_parent = nullptr);
+
+    MLParentId getId() const;
+    QString getName() const;
+    QString getShortBio() const;
+    QString getCover() const;
+    unsigned int getNbAlbums() const;
+    unsigned int getNbTracks() const;
+
+    MLArtist* clone(QObject *parent = nullptr) const;
+
+    Q_INVOKABLE QString getPresName() const;
+    Q_INVOKABLE QString getPresImage() const;
+    Q_INVOKABLE QString getPresInfo() const;
+
+private:
+    MLArtist(const MLArtist &artist, QObject *_parent = nullptr);
+
+    MLParentId m_id;
+    QString m_name;
+    QString m_shortBio;
+    QString m_cover;
+    unsigned int m_nbAlbums;
+    unsigned int m_nbTracks;
+};
+
+#endif
diff --git a/modules/gui/qt/components/mediacenter/mlartistmodel.cpp b/modules/gui/qt/components/mediacenter/mlartistmodel.cpp
new file mode 100644
index 0000000000..f60638c512
--- /dev/null
+++ b/modules/gui/qt/components/mediacenter/mlartistmodel.cpp
@@ -0,0 +1,141 @@
+/*****************************************************************************
+ * 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 "mlartistmodel.hpp"
+
+namespace {
+    enum Roles
+    {
+        ARTIST_ID = Qt::UserRole + 1,
+        ARTIST_NAME,
+        ARTIST_SHORT_BIO,
+        ARTIST_COVER,
+        ARTIST_NB_ALBUMS
+    };
+}
+
+QHash<QByteArray, vlc_ml_sorting_criteria_t> MLArtistModel::M_names_to_criteria = {
+    {"title", VLC_ML_SORTING_ALPHA},
+};
+
+MLArtistModel::MLArtistModel(QObject *parent)
+    : MLSlidingWindowModel(parent)
+{
+}
+
+QVariant MLArtistModel::data(const QModelIndex &index, int role) const
+{
+    if (!index.isValid() || index.row() < 0)
+        return QVariant();
+
+    const MLArtist* ml_artist = item(static_cast<unsigned int>(index.row()));
+    if ( !ml_artist )
+        return QVariant();
+
+    switch (role)
+    {
+    case ARTIST_ID :
+        return QVariant::fromValue( ml_artist->getId() );
+    case ARTIST_NAME :
+        return QVariant::fromValue( ml_artist->getName() );
+    case ARTIST_SHORT_BIO :
+        return QVariant::fromValue( ml_artist->getShortBio() );
+    case ARTIST_COVER :
+        return QVariant::fromValue( ml_artist->getCover() );
+    case ARTIST_NB_ALBUMS :
+        return QVariant::fromValue( ml_artist->getNbAlbums() );
+    default :
+        return QVariant();
+    }
+}
+
+QHash<int, QByteArray> MLArtistModel::roleNames() const
+{
+    return {
+        { ARTIST_ID, "id" },
+        { ARTIST_NAME, "name" },
+        { ARTIST_SHORT_BIO, "short_bio" },
+        { ARTIST_COVER, "cover" },
+        { ARTIST_NB_ALBUMS, "nb_albums" },
+    };
+}
+
+std::vector<std::unique_ptr<MLArtist>> MLArtistModel::fetch()
+{
+    ml_unique_ptr<vlc_ml_artist_list_t> artist_list;
+    if ( m_parent.id <= 0 )
+        artist_list.reset( vlc_ml_list_artists(m_ml, &m_query_param, false) );
+    else
+        artist_list.reset( vlc_ml_list_artist_of(m_ml, &m_query_param, m_parent.type, m_parent.id) );
+    if ( artist_list == nullptr )
+        return {};
+    std::vector<std::unique_ptr<MLArtist>> res;
+    for( const vlc_ml_artist_t& artist: ml_range_iterate<vlc_ml_artist_t>( artist_list ) )
+        res.emplace_back( new MLArtist( &artist ) );
+    return res;
+}
+
+size_t MLArtistModel::countTotalElements() const
+{
+    auto queryParams = m_query_param;
+    queryParams.i_offset = 0;
+    queryParams.i_nbResults = 0;
+
+    if ( m_parent.id <= 0 )
+        return vlc_ml_count_artists(m_ml, &queryParams, false);
+    return vlc_ml_count_artists_of(m_ml, &queryParams, m_parent.type, m_parent.id );
+}
+
+vlc_ml_sorting_criteria_t MLArtistModel::roleToCriteria(int role) const
+{
+    switch (role)
+    {
+    case ARTIST_NAME :
+        return VLC_ML_SORTING_ALPHA;
+    default :
+        return VLC_ML_SORTING_DEFAULT;
+    }
+}
+
+vlc_ml_sorting_criteria_t MLArtistModel::nameToCriteria(QByteArray name) const
+{
+    return M_names_to_criteria.value(name, VLC_ML_SORTING_DEFAULT);
+}
+
+QByteArray MLArtistModel::criteriaToName(vlc_ml_sorting_criteria_t criteria) const
+{
+    return M_names_to_criteria.key(criteria, "");
+}
+
+void MLArtistModel::onVlcMlEvent(const vlc_ml_event_t* event)
+{
+    switch (event->i_type)
+    {
+        case VLC_ML_EVENT_ARTIST_ADDED:
+        case VLC_ML_EVENT_ARTIST_UPDATED:
+        case VLC_ML_EVENT_ARTIST_DELETED:
+            m_need_reset = true;
+            break;
+        case VLC_ML_EVENT_GENRE_DELETED:
+            if ( m_parent.id != 0 && m_parent.type == VLC_ML_PARENT_GENRE &&
+                 m_parent.id == event->deletion.i_entity_id )
+                m_need_reset = true;
+            break;
+    }
+    MLBaseModel::onVlcMlEvent(event);
+}
diff --git a/modules/gui/qt/components/mediacenter/mlartistmodel.hpp b/modules/gui/qt/components/mediacenter/mlartistmodel.hpp
new file mode 100644
index 0000000000..e3e0a2433d
--- /dev/null
+++ b/modules/gui/qt/components/mediacenter/mlartistmodel.hpp
@@ -0,0 +1,52 @@
+/*****************************************************************************
+ * 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 MLARTISTMODEL_HPP
+#define MLARTISTMODEL_HPP
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include <QObject>
+#include "mlbasemodel.hpp"
+#include "mlartist.hpp"
+
+class MLArtistModel : public MLSlidingWindowModel<MLArtist>
+{
+    Q_OBJECT
+
+public:
+    explicit MLArtistModel(QObject *parent = nullptr);
+    virtual ~MLArtistModel() = default;
+
+    QVariant data(const QModelIndex &index, int role) const override;
+    QHash<int, QByteArray> roleNames() const override;
+
+private:
+    std::vector<std::unique_ptr<MLArtist>> fetch() override;
+    size_t countTotalElements() const override;
+    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;
+    virtual void onVlcMlEvent(const vlc_ml_event_t* event) override;
+
+    static QHash<QByteArray, vlc_ml_sorting_criteria_t> M_names_to_criteria;
+};
+
+#endif // MLARTISTMODEL_HPP
-- 
2.19.1



More information about the vlc-devel mailing list