From 87db6cfd047e2c1d247dbfb45cad8b03e5dd6248 Mon Sep 17 00:00:00 2001 From: Matt Ranostay Date: Fri, 29 Jun 2018 12:01:11 -0700 Subject: libqtappfw: mediaplayer: change from JSON objects to proper Qt model Switch to using proper Qt model versus JSON objects being passed to QML Bug-AGL: SPEC-1430 Change-Id: Ic07a355fee3292d113bc62bee9acb0764a0a5458 Signed-off-by: Matt Ranostay --- mediaplayer/mediaplayer.cpp | 35 ++++++++++++++++++++++++++++-- mediaplayer/mediaplayer.h | 52 +++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 83 insertions(+), 4 deletions(-) (limited to 'mediaplayer') diff --git a/mediaplayer/mediaplayer.cpp b/mediaplayer/mediaplayer.cpp index 5755f37..384badf 100644 --- a/mediaplayer/mediaplayer.cpp +++ b/mediaplayer/mediaplayer.cpp @@ -19,11 +19,26 @@ #include "mediaplayer.h" #include "mediaplayermessage.h" -Mediaplayer::Mediaplayer (QUrl &url, QObject * parent) : +Playlist::Playlist(QVariantMap &item) +{ + m_index = item["index"].toInt(); + m_duration = item["duration"].toInt(); + m_genre = item["genre"].toString(); + m_path = item["path"].toString(); + m_title = item["title"].toString(); + m_album = item["album"].toString(); + m_artist = item["artist"].toString(); +} + +Playlist::~Playlist() {} + +Mediaplayer::Mediaplayer (QUrl &url, QQmlContext *context, QObject * parent) : QObject(parent), m_mloop(nullptr) { m_mloop = new MessageEngine(url); + m_context = context; + m_context->setContextProperty("MediaplayerModel", QVariant::fromValue(m_playlist)); QObject::connect(m_mloop, &MessageEngine::connected, this, &Mediaplayer::onConnected); QObject::connect(m_mloop, &MessageEngine::disconnected, this, &Mediaplayer::onDisconnected); QObject::connect(m_mloop, &MessageEngine::messageReceived, this, &Mediaplayer::onMessageReceived); @@ -34,6 +49,22 @@ Mediaplayer::~Mediaplayer() delete m_mloop; } +// Qt UI Context + +void Mediaplayer::updatePlaylist(QVariantMap playlist) +{ + QVariantList list = playlist["list"].toList(); + + m_playlist.clear(); + + for (auto i : list) { + QVariantMap item = qvariant_cast(i); + m_playlist.append(new Playlist(item)); + } + + // Refresh model + m_context->setContextProperty("MediaplayerModel", QVariant::fromValue(m_playlist)); +} // Control Verb methods @@ -161,7 +192,7 @@ void Mediaplayer::onMessageReceived(MessageType type, Message *message) if (tmsg->isEvent()) { if (tmsg->isPlaylistEvent()) { - emit playlistChanged(tmsg->eventData().toVariantMap()); + updatePlaylist(tmsg->eventData().toVariantMap()); } else if (tmsg->isMetadataEvent()) { emit metadataChanged(tmsg->eventData().toVariantMap()); } diff --git a/mediaplayer/mediaplayer.h b/mediaplayer/mediaplayer.h index bf0b8bf..36c8164 100644 --- a/mediaplayer/mediaplayer.h +++ b/mediaplayer/mediaplayer.h @@ -20,14 +20,59 @@ #include #include +#include +#include #include "messageengine.h" +class Playlist : public QObject +{ + Q_OBJECT + + Q_PROPERTY(int index READ index NOTIFY indexChanged) + Q_PROPERTY(int duration READ duration NOTIFY durationChanged) + Q_PROPERTY(QString path READ path NOTIFY pathChanged) + + // METADATA FIELDS + Q_PROPERTY(QString title READ title NOTIFY titleChanged) + Q_PROPERTY(QString album READ album NOTIFY albumChanged) + Q_PROPERTY(QString artist READ artist NOTIFY artistChanged) + Q_PROPERTY(QString genre READ genre NOTIFY genreChanged) + + public: + explicit Playlist(QVariantMap& item); + virtual ~Playlist(); + + bool operator<(Playlist& c) { return ((this->m_index < c.m_index)); }; + int index() { return m_index; }; + int duration() { return m_duration; }; + QString path() { return m_path; }; + + // METADATA FIELDS + QString title() { return m_title; }; + QString album() { return m_album; }; + QString artist() { return m_artist; }; + QString genre() { return m_genre; }; + + signals: + void indexChanged(); + void durationChanged(); + void pathChanged(); + void titleChanged(); + void albumChanged(); + void artistChanged(); + void genreChanged(); + + private: + int m_index, m_duration; + QString m_path, m_title, m_album, m_artist, m_genre; +}; + class Mediaplayer : public QObject { Q_OBJECT public: - explicit Mediaplayer(QUrl &url, QObject * parent = Q_NULLPTR); + explicit Mediaplayer(QUrl &url, QQmlContext *context, QObject * parent = Q_NULLPTR); virtual ~Mediaplayer(); // controls @@ -42,12 +87,15 @@ class Mediaplayer : public QObject Q_INVOKABLE void volume(int); Q_INVOKABLE void loop(int); + void updatePlaylist(QVariantMap playlist); + signals: - void playlistChanged(QVariantMap playlist); void metadataChanged(QVariantMap metadata); private: MessageEngine *m_mloop; + QQmlContext *m_context; + QList m_playlist; void control(QString, QJsonObject); void control(QString); -- cgit 1.2.3-korg