1 #ifndef PLAYLISTLISTMODEL_H
2 #define PLAYLISTLISTMODEL_H
3 
4 #include <QStandardItemModel>
5 #include "core/song.h"
6 class PlaylistListModel : public QStandardItemModel {
7   Q_OBJECT
8 
9  public:
10   PlaylistListModel(QObject* parent = nullptr);
11 
12   enum Types { Type_Folder, Type_Playlist, Type_Track };
13 
14   enum Roles { Role_Type = Qt::UserRole, Role_PlaylistId, Role_TrackId };
15 
16   bool dropMimeData(const QMimeData* data, Qt::DropAction action, int row,
17                     int column, const QModelIndex& parent);
18 
19   // These icons will be used for newly created playlists and folders.
20   // The caller will need to set these icons on existing items if there are any.
21   void SetIcons(const QIcon& playlist_icon, const QIcon& folder_icon);
track_icon()22   const QIcon& track_icon() const { return track_icon_; }
playlist_icon()23   const QIcon& playlist_icon() const { return playlist_icon_; }
folder_icon()24   const QIcon& folder_icon() const { return folder_icon_; }
25 
26   // Walks from the given item to the root, returning the / separated path of
27   // all the parent folders.  The path includes this item if it is a folder.
28   QString ItemPath(const QStandardItem* item) const;
29 
30   // Finds the playlist with the given ID, returns 0 if it doesn't exist.
31   QStandardItem* PlaylistById(int id) const;
32 
33   // Finds the folder with the given path, creating it (and its parents) if they
34   // do not exist.  Returns invisibleRootItem() if path is empty.
35   QStandardItem* FolderByPath(const QString& path);
36 
37   // Returns a new folder item with the given name.  The item isn't added to
38   // the model yet.
39   QStandardItem* NewFolder(const QString& name) const;
40 
41   // Returns a new playlist item with the given name and ID.  The item isn't
42   // added to the model yet.
43   QStandardItem* NewPlaylist(const QString& name, int id) const;
44 
45   // Returns a new track item. The item isn't added to the model yet.
46   QStandardItem* NewTrack(const Song& song) const;
47 
48   // QStandardItemModel
49   bool setData(const QModelIndex& index, const QVariant& value, int role);
50 
51 signals:
52   void PlaylistPathChanged(int id, const QString& new_path);
53   void PlaylistRenamed(int id, const QString& new_name);
54 
55  private slots:
56   void RowsChanged(const QModelIndex& begin, const QModelIndex& end);
57   void RowsAboutToBeRemoved(const QModelIndex& parent, int start, int end);
58   void RowsInserted(const QModelIndex& parent, int start, int end);
59 
60  private:
61   void AddRowMappings(const QModelIndex& begin, const QModelIndex& end);
62   void AddRowItem(QStandardItem* item, const QString& parent_path);
63   void UpdatePathsRecursive(const QModelIndex& parent);
64 
65  private:
66   bool dropping_rows_;
67 
68   QIcon track_icon_;
69   QIcon playlist_icon_;
70   QIcon folder_icon_;
71 
72   QMap<int, QStandardItem*> playlists_by_id_;
73   QMap<QString, QStandardItem*> folders_by_path_;
74 };
75 
76 #endif  // PLAYLISTLISTMODEL_H
77