[vlc-devel] [RFC 50/82] qt: add medialibrary album model

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


---
 modules/gui/qt/Makefile.am                    |   6 +
 .../gui/qt/components/mediacenter/mlalbum.cpp | 126 +++++++++++++
 .../gui/qt/components/mediacenter/mlalbum.hpp |  80 +++++++++
 .../components/mediacenter/mlalbummodel.cpp   | 170 ++++++++++++++++++
 .../components/mediacenter/mlalbummodel.hpp   |  54 ++++++
 5 files changed, 436 insertions(+)
 create mode 100644 modules/gui/qt/components/mediacenter/mlalbum.cpp
 create mode 100644 modules/gui/qt/components/mediacenter/mlalbum.hpp
 create mode 100644 modules/gui/qt/components/mediacenter/mlalbummodel.cpp
 create mode 100644 modules/gui/qt/components/mediacenter/mlalbummodel.hpp

diff --git a/modules/gui/qt/Makefile.am b/modules/gui/qt/Makefile.am
index 12a21ec7d6..a7b5cb5d30 100644
--- a/modules/gui/qt/Makefile.am
+++ b/modules/gui/qt/Makefile.am
@@ -124,6 +124,10 @@ libqt_plugin_la_SOURCES = \
 	gui/qt/components/mediacenter/mlqmltypes.hpp \
 	gui/qt/components/mediacenter/mlbasemodel.cpp \
 	gui/qt/components/mediacenter/mlbasemodel.hpp \
+	gui/qt/components/mediacenter/mlalbum.cpp \
+	gui/qt/components/mediacenter/mlalbum.hpp \
+	gui/qt/components/mediacenter/mlalbummodel.cpp \
+	gui/qt/components/mediacenter/mlalbummodel.hpp \
 	gui/qt/components/playlist/media.hpp \
 	gui/qt/components/playlist/playlist_common.cpp \
 	gui/qt/components/playlist/playlist_common.hpp \
@@ -247,6 +251,8 @@ nodist_libqt_plugin_la_SOURCES = \
 	gui/qt/components/mediacenter/mcmedialib.moc.cpp \
 	gui/qt/components/mediacenter/mlqmltypes.moc.cpp \
 	gui/qt/components/mediacenter/mlbasemodel.moc.cpp \
+	gui/qt/components/mediacenter/mlalbum.moc.cpp \
+	gui/qt/components/mediacenter/mlalbummodel.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/mlalbum.cpp b/modules/gui/qt/components/mediacenter/mlalbum.cpp
new file mode 100644
index 0000000000..9dd7fdc2a4
--- /dev/null
+++ b/modules/gui/qt/components/mediacenter/mlalbum.cpp
@@ -0,0 +1,126 @@
+/*****************************************************************************
+ * 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 "mlalbum.hpp"
+
+MLAlbum::MLAlbum(vlc_medialibrary_t* _ml, const vlc_ml_album_t *_data, QObject *_parent)
+    : QObject( _parent )
+    , m_ml          ( _ml )
+    , m_id          ( _data->i_id, VLC_ML_PARENT_ALBUM )
+    , m_title       ( QString::fromUtf8( _data->psz_title ) )
+    , m_releaseYear ( _data->i_year )
+    , m_shortSummary( QString::fromUtf8( _data->psz_summary ) )
+    , m_cover       ( QString::fromUtf8( _data->psz_artwork_mrl ) )
+    , m_mainArtist  ( QString::fromUtf8( _data->psz_artist ) )
+    , m_nbTracks    ( _data->i_nb_tracks )
+{
+    assert( _data );
+    assert( _ml );
+
+    int t_sec = _data->i_duration / 1000000;
+    int sec = t_sec % 60;
+    int min = (t_sec / 60) % 60;
+    int hour = t_sec / 3600;
+    if (hour == 0)
+        m_duration = QString("%1:%2")
+                .arg(min, 2, 10, QChar('0'))
+                .arg(sec, 2, 10, QChar('0'));
+    else
+        m_duration = QString("%1:%2:%3")
+                .arg(hour, 2, 10, QChar('0'))
+                .arg(min, 2, 10, QChar('0'))
+                .arg(sec, 2, 10, QChar('0'));
+}
+
+//private ctor for cloning
+MLAlbum::MLAlbum(const MLAlbum& _album, QObject *_parent)
+    : QObject( _parent )
+    , m_ml          ( _album.m_ml )
+    , m_id          ( _album.m_id )
+    , m_title       ( _album.m_title )
+    , m_releaseYear ( _album.m_releaseYear )
+    , m_shortSummary( _album.m_shortSummary )
+    , m_cover       ( _album.m_cover )
+    , m_mainArtist  ( _album.m_mainArtist )
+    , m_nbTracks    ( _album.m_nbTracks )
+    , m_duration    ( _album.m_duration )
+{
+}
+
+MLParentId MLAlbum::getParentId() const
+{
+    return m_id;
+}
+
+QString MLAlbum::getTitle() const
+{
+    return m_title;
+}
+
+unsigned int MLAlbum::getReleaseYear() const
+{
+    return  m_releaseYear;
+}
+
+QString MLAlbum::getShortSummary() const
+{
+    return m_shortSummary;
+}
+
+QString MLAlbum::getCover() const
+{
+    return m_cover;
+}
+
+
+QString MLAlbum::getArtist() const
+{
+    return m_mainArtist;
+}
+
+unsigned int MLAlbum::getNbTracks() const
+{
+    return m_nbTracks;
+}
+
+QString MLAlbum::getDuration() const
+{
+    return m_duration;
+}
+
+MLAlbum *MLAlbum::clone(QObject *parent) const
+{
+    return new MLAlbum(*this, parent);
+}
+
+QString MLAlbum::getPresName() const
+{
+    return m_title;
+}
+
+QString MLAlbum::getPresImage() const
+{
+    return m_cover;
+}
+
+QString MLAlbum::getPresInfo() const
+{
+    return m_shortSummary;
+}
+
+
diff --git a/modules/gui/qt/components/mediacenter/mlalbum.hpp b/modules/gui/qt/components/mediacenter/mlalbum.hpp
new file mode 100644
index 0000000000..be14d0a990
--- /dev/null
+++ b/modules/gui/qt/components/mediacenter/mlalbum.hpp
@@ -0,0 +1,80 @@
+/*****************************************************************************
+ * 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.
+ *****************************************************************************/
+
+#pragma once
+
+#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 "mlhelper.hpp"
+#include "mlqmltypes.hpp"
+
+class MLAlbum : public QObject
+{
+    Q_OBJECT
+
+    Q_PROPERTY(MLParentId id READ getParentId CONSTANT)
+    Q_PROPERTY(QString title READ getTitle CONSTANT)
+    Q_PROPERTY(unsigned int releaseyear READ getReleaseYear CONSTANT)
+    Q_PROPERTY(QString shortsummary READ getShortSummary CONSTANT)
+    Q_PROPERTY(QString cover READ getCover CONSTANT)
+    Q_PROPERTY(QString artist READ getArtist CONSTANT)
+    Q_PROPERTY(unsigned int nbtracks READ getNbTracks CONSTANT)
+    Q_PROPERTY(QString duration READ getDuration CONSTANT)
+
+public:
+    MLAlbum(vlc_medialibrary_t* _ml, const vlc_ml_album_t *_data, QObject *_parent = nullptr);
+
+    MLParentId getParentId() const;
+    QString getTitle() const;
+    unsigned int getReleaseYear() const;
+    QString getShortSummary() const;
+    QString getCover() const;
+    QString getArtist() const;
+    unsigned int getNbTracks() const;
+    QString getDuration() const;
+
+    MLAlbum* clone(QObject *parent = nullptr) const;
+
+    Q_INVOKABLE QString getPresName() const;
+    Q_INVOKABLE QString getPresImage() const;
+    Q_INVOKABLE QString getPresInfo() const;
+
+private:
+    //private ctor for cloning
+    MLAlbum(const MLAlbum &_album, QObject *_parent = nullptr);
+
+    vlc_medialibrary_t* m_ml;
+
+    MLParentId m_id;
+    QString m_title;
+    unsigned int m_releaseYear;
+    QString m_shortSummary;
+    QString m_cover;
+    QString m_mainArtist;
+    QList<QString> m_otherArtists;
+    unsigned int m_nbTracks;
+    QString m_duration;
+};
diff --git a/modules/gui/qt/components/mediacenter/mlalbummodel.cpp b/modules/gui/qt/components/mediacenter/mlalbummodel.cpp
new file mode 100644
index 0000000000..4a585ac7f4
--- /dev/null
+++ b/modules/gui/qt/components/mediacenter/mlalbummodel.cpp
@@ -0,0 +1,170 @@
+/*****************************************************************************
+ * 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 "mlalbummodel.hpp"
+
+namespace {
+    enum Roles
+    {
+        ALBUM_ID = Qt::UserRole + 1,
+        ALBUM_TITLE,
+        ALBUM_RELEASE_YEAR,
+        ALBUM_SHORT_SUMMARY,
+        ALBUM_COVER,
+        ALBUM_MAIN_ARTIST,
+        ALBUM_NB_TRACKS,
+        ALBUM_DURATION
+    };
+}
+
+QHash<QByteArray, vlc_ml_sorting_criteria_t> MLAlbumModel::M_names_to_criteria = {
+    {"id", VLC_ML_SORTING_DEFAULT},
+    {"title", VLC_ML_SORTING_ALBUM},
+    {"release_year", VLC_ML_SORTING_RELEASEDATE},
+    {"main_artist", VLC_ML_SORTING_ARTIST},
+    //{"nb_tracks"},
+    {"duration", VLC_ML_SORTING_DURATION}
+};
+
+MLAlbumModel::MLAlbumModel(QObject *parent)
+    : MLSlidingWindowModel<MLAlbum>(parent)
+{
+}
+
+QVariant MLAlbumModel::data(const QModelIndex &index, int role) const
+{
+    if (!index.isValid() || index.row() < 0)
+        return QVariant();
+
+    const MLAlbum* ml_item = item(static_cast<unsigned int>(index.row()));
+    if ( ml_item == NULL )
+        return QVariant();
+
+    switch (role)
+    {
+    case ALBUM_ID :
+        return QVariant::fromValue( ml_item->getParentId() );
+    case ALBUM_TITLE :
+        return QVariant::fromValue( ml_item->getTitle() );
+    case ALBUM_RELEASE_YEAR :
+        return QVariant::fromValue( ml_item->getReleaseYear() );
+    case ALBUM_SHORT_SUMMARY :
+        return QVariant::fromValue( ml_item->getShortSummary() );
+    case ALBUM_COVER :
+        return QVariant::fromValue( ml_item->getCover() );
+    case ALBUM_MAIN_ARTIST :
+        return QVariant::fromValue( ml_item->getArtist() );
+    case ALBUM_NB_TRACKS :
+        return QVariant::fromValue( ml_item->getNbTracks() );
+    case ALBUM_DURATION:
+        return QVariant::fromValue( ml_item->getDuration() );
+    default:
+        return QVariant();
+    }
+}
+
+QHash<int, QByteArray> MLAlbumModel::roleNames() const
+{
+    return {
+        {ALBUM_ID,"id"},
+        {ALBUM_TITLE, "title"},
+        {ALBUM_RELEASE_YEAR, "release_year"},
+        {ALBUM_SHORT_SUMMARY, "shortsummary"},
+        {ALBUM_COVER, "cover"},
+        {ALBUM_MAIN_ARTIST, "main_artist"},
+        {ALBUM_NB_TRACKS, "nb_tracks"},
+        {ALBUM_DURATION, "duration"}
+    };
+}
+
+std::vector<std::unique_ptr<MLAlbum>> MLAlbumModel::fetch( )
+{
+    ml_unique_ptr<vlc_ml_album_list_t> album_list;
+    if ( m_parent.id <= 0 )
+        album_list.reset( vlc_ml_list_albums(m_ml, &m_query_param ) );
+    else
+        album_list.reset( vlc_ml_list_albums_of(m_ml, &m_query_param, m_parent.type, m_parent.id ) );
+    if ( album_list == nullptr )
+        return {};
+    std::vector<std::unique_ptr<MLAlbum>> res;
+    for( const vlc_ml_album_t& album: ml_range_iterate<vlc_ml_album_t>( album_list ) )
+        res.emplace_back( new MLAlbum( m_ml, &album ) );
+    return res;
+}
+
+vlc_ml_sorting_criteria_t MLAlbumModel::nameToCriteria(QByteArray name) const
+{
+    return M_names_to_criteria.value(name, VLC_ML_SORTING_DEFAULT);
+}
+
+QByteArray MLAlbumModel::criteriaToName(vlc_ml_sorting_criteria_t criteria) const
+{
+    return M_names_to_criteria.key(criteria, "");
+}
+
+void MLAlbumModel::onVlcMlEvent(const vlc_ml_event_t* event)
+{
+    switch( event->i_type )
+    {
+        case VLC_ML_EVENT_ALBUM_ADDED:
+        case VLC_ML_EVENT_ALBUM_DELETED:
+        case VLC_ML_EVENT_ALBUM_UPDATED:
+            m_need_reset = true;
+            break;
+        case VLC_ML_EVENT_ARTIST_DELETED:
+            if ( m_parent.id != 0 && m_parent.type == VLC_ML_PARENT_ARTIST &&
+                 event->deletion.i_entity_id == m_parent.id )
+                    m_need_reset = true;
+            break;
+        case VLC_ML_EVENT_GENRE_DELETED:
+            if ( m_parent.id != 0 && m_parent.type == VLC_ML_PARENT_GENRE &&
+                 event->deletion.i_entity_id == m_parent.id )
+                    m_need_reset = true;
+            break;
+        default:
+            break;
+    }
+    MLBaseModel::onVlcMlEvent( event );
+}
+
+vlc_ml_sorting_criteria_t MLAlbumModel::roleToCriteria(int role) const
+{
+    switch (role)
+    {
+    case ALBUM_TITLE :
+        return VLC_ML_SORTING_ALPHA;
+    case ALBUM_RELEASE_YEAR :
+        return VLC_ML_SORTING_RELEASEDATE;
+    case ALBUM_MAIN_ARTIST :
+        return VLC_ML_SORTING_ARTIST;
+    case ALBUM_DURATION:
+        return VLC_ML_SORTING_DURATION;
+    default:
+        return VLC_ML_SORTING_DEFAULT;
+    }
+}
+
+size_t MLAlbumModel::countTotalElements() const
+{
+    auto queryParams = m_query_param;
+    queryParams.i_offset = 0;
+    queryParams.i_nbResults = 0;
+    if ( m_parent.id <= 0 )
+        return vlc_ml_count_albums(m_ml, &queryParams);
+    return vlc_ml_count_albums_of(m_ml, &queryParams, m_parent.type, m_parent.id);
+}
diff --git a/modules/gui/qt/components/mediacenter/mlalbummodel.hpp b/modules/gui/qt/components/mediacenter/mlalbummodel.hpp
new file mode 100644
index 0000000000..ab267e376d
--- /dev/null
+++ b/modules/gui/qt/components/mediacenter/mlalbummodel.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 MLABLUMMODEL_H
+#define MLABLUMMODEL_H
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include <QObject>
+#include "mlbasemodel.hpp"
+#include "mlalbum.hpp"
+#include "mcmedialib.hpp"
+
+class MLAlbumModel : public MLSlidingWindowModel<MLAlbum>
+{
+    Q_OBJECT
+
+public:
+    explicit MLAlbumModel(QObject *parent = nullptr);
+    virtual ~MLAlbumModel() = default;
+
+    Q_INVOKABLE QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
+    Q_INVOKABLE QHash<int, QByteArray> roleNames() const override;
+
+private:
+    std::vector<std::unique_ptr<MLAlbum>> 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 // MLABLUMMODEL_H
-- 
2.19.1



More information about the vlc-devel mailing list