[vlc-devel] [RFC 3/8] qt: playlist: make PlaylistListModel selectable

Romain Vimont rom1v at videolabs.io
Tue Jul 9 18:30:27 CEST 2019


Make it inherit SelectableListModel, and implement required functions.
The actual selection state is stored in the playlist items themselves.
---
 .../qt/components/playlist/playlist_item.cpp  | 10 ++++++++
 .../qt/components/playlist/playlist_item.hpp  |  5 ++++
 .../qt/components/playlist/playlist_model.cpp | 24 +++++++++++++++++--
 .../qt/components/playlist/playlist_model.hpp | 11 +++++++--
 4 files changed, 46 insertions(+), 4 deletions(-)

diff --git a/modules/gui/qt/components/playlist/playlist_item.cpp b/modules/gui/qt/components/playlist/playlist_item.cpp
index 8281daa059..c9bb3d2995 100644
--- a/modules/gui/qt/components/playlist/playlist_item.cpp
+++ b/modules/gui/qt/components/playlist/playlist_item.cpp
@@ -30,6 +30,16 @@ PlaylistItem::PlaylistItem(vlc_playlist_item_t* item)
     }
 }
 
+bool PlaylistItem::isSelected() const
+{
+    return d->selected;
+}
+
+void PlaylistItem::setSelected(bool selected)
+{
+    d->selected = selected;
+}
+
 QString PlaylistItem::getTitle() const
 {
     return d->title;
diff --git a/modules/gui/qt/components/playlist/playlist_item.hpp b/modules/gui/qt/components/playlist/playlist_item.hpp
index 88ab12ffa0..88cca497fd 100644
--- a/modules/gui/qt/components/playlist/playlist_item.hpp
+++ b/modules/gui/qt/components/playlist/playlist_item.hpp
@@ -63,6 +63,9 @@ public:
         return d ? d->item.get() : nullptr;
     }
 
+    bool isSelected() const;
+    void setSelected(bool selected);
+
     QString getTitle() const;
 
     QString getArtist() const;
@@ -80,6 +83,8 @@ private:
     struct Data : public QSharedData {
         PlaylistItemPtr item;
 
+        bool selected = false;
+
         /* cached values */
         QString title;
         QString artist;
diff --git a/modules/gui/qt/components/playlist/playlist_model.cpp b/modules/gui/qt/components/playlist/playlist_model.cpp
index 12edef2296..983319abd9 100644
--- a/modules/gui/qt/components/playlist/playlist_model.cpp
+++ b/modules/gui/qt/components/playlist/playlist_model.cpp
@@ -233,13 +233,13 @@ PlaylistListModelPrivate::notifyItemsChanged(int idx, int count, const QVector<i
 // public API
 
 PlaylistListModel::PlaylistListModel(QObject *parent)
-    : QAbstractListModel(parent)
+    : SelectableListModel(parent)
     , d_ptr(new PlaylistListModelPrivate(this))
 {
 }
 
 PlaylistListModel::PlaylistListModel(vlc_playlist_t *raw_playlist, QObject *parent)
-    : QAbstractListModel(parent)
+    : SelectableListModel(parent)
     , d_ptr(new PlaylistListModelPrivate(this))
 {
     setPlaylistId(PlaylistPtr(raw_playlist));
@@ -249,6 +249,23 @@ PlaylistListModel::~PlaylistListModel()
 {
 }
 
+bool PlaylistListModel::isRowSelected(int row) const
+{
+    Q_D(const PlaylistListModel);
+    return d->m_items[row].isSelected();
+}
+
+void PlaylistListModel::setRowSelected(int row, bool selected)
+{
+    Q_D(PlaylistListModel);
+    return d->m_items[row].setSelected(selected);
+}
+
+int PlaylistListModel::getSelectedRole() const
+{
+    return SelectedRole;
+}
+
 QHash<int, QByteArray>
 PlaylistListModel::roleNames() const
 {
@@ -259,6 +276,7 @@ PlaylistListModel::roleNames() const
         { ArtistRole , "artist" },
         { AlbumRole  , "album" },
         { ArtworkRole, "artwork" },
+        { SelectedRole, "selected" },
     };
 }
 
@@ -440,6 +458,8 @@ PlaylistListModel::data(const QModelIndex &index, int role) const
         return d->m_items[row].getAlbum();
     case ArtworkRole:
         return d->m_items[row].getArtwork();
+    case SelectedRole:
+        return d->m_items[row].isSelected();
     default:
         return {};
     }
diff --git a/modules/gui/qt/components/playlist/playlist_model.hpp b/modules/gui/qt/components/playlist/playlist_model.hpp
index a1215496ac..ff6f0a46f6 100644
--- a/modules/gui/qt/components/playlist/playlist_model.hpp
+++ b/modules/gui/qt/components/playlist/playlist_model.hpp
@@ -27,12 +27,13 @@
 #include <QVector>
 #include "playlist_common.hpp"
 #include "playlist_item.hpp"
+#include "../selectable_list_model.hpp"
 
 namespace vlc {
 namespace playlist {
 
 class PlaylistListModelPrivate;
-class PlaylistListModel : public QAbstractListModel
+class PlaylistListModel : public SelectableListModel
 {
     Q_OBJECT
     Q_PROPERTY(PlaylistPtr playlistId READ getPlaylistId WRITE setPlaylistId NOTIFY playlistIdChanged)
@@ -46,7 +47,8 @@ public:
         IsCurrentRole,
         ArtistRole,
         AlbumRole,
-        ArtworkRole
+        ArtworkRole,
+        SelectedRole,
     };
 
     PlaylistListModel(QObject *parent = nullptr);
@@ -67,6 +69,11 @@ public:
 
     int getCurrentIndex() const;
 
+protected:
+    bool isRowSelected(int row) const override;
+    void setRowSelected(int row, bool selected) override;
+    int getSelectedRole() const override;
+
 public slots:
     PlaylistPtr getPlaylistId() const;
     void setPlaylistId(PlaylistPtr id);
-- 
2.20.1



More information about the vlc-devel mailing list