PlayerFlat
 Tudo Classes Funções
playlistmodel.h
1 #ifndef PLAYLISTMODEL_H
2 #define PLAYLISTMODEL_H
3 
4 #include <QAbstractItemModel>
5 #include <QMediaPlaylist>
6 
7 /*
8  * This class implements a playlist model to store songs to be played
9  * it subclasses QAbstractItemModel to deal with song playlist using
10  * a model/view approach. This is very common in visual applications
11  * development since model implementation remains separated from
12  * visual exibition aspects. For example, if the playlist is stored
13  * in a database, it is possible execute commands to retrive the music
14  * path from such database. Therefore, where the data is extracted is
15  * responsability only from this class, and the viewing component does
16  * not have to know nothing about this.
17 */
18 
19 class PlaylistModel : public QAbstractItemModel
20 {
21  Q_OBJECT
22 private:
23  // store the media playlist
24  QMediaPlaylist *m_playlist;
25 
26  // maps the data index->variant
27  // the index contains the playlist position the user
28  // selected. The variant is a chunck of bytes used
29  // to store some information. In this case, the song url
30  // to be played
31  QMap<QModelIndex, QVariant> m_data;
32 public:
33  // class constructor
34  explicit PlaylistModel(QObject *parent = 0);
35  enum Column {
36  Title = 0,
37  ColumnCount
38  };
39 
40 signals:
41 
42 public slots:
43  // modelview assumes data is stored in a table structure
44  // some of the methods below MUST BE IMPLEMENTED so the
45  // data to be retrieved may be accessed correctly
46 
47  // returns the number of rows stored in model
48  int rowCount(const QModelIndex &parent = QModelIndex()) const;
49 
50  // returns the number of columns stored in this model
51  int columnCount(const QModelIndex &parent = QModelIndex()) const;
52 
53  // returns the model index of element at a given row/column
54  QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const;
55 
56  // returns the parent of a given item
57  QModelIndex parent(const QModelIndex &child) const;
58 
59  // returns the data associated with a given index.
60  // The role variable tells the method what information is to be retrieved according
61  // to a given role
62  // Qt::DisplayRole is usually associated to some text that is stored
63  QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
64 
65  // returns the playlist for the media player
66  QMediaPlaylist *playlist() const;
67 
68  // tells what playlist shall be filled...
69  void setPlaylist(QMediaPlaylist *playlist);
70 
71  // A setData is used to insert itens into the playlist
72  bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::DisplayRole);
73 
74 private slots:
75  // some convenience functions to organize the internal structure of the list
76  // they are used when a new playlist is to be defined
77  // old playlist must be deleted an the new one has to be started up
78  void beginInsertItems(int start, int end);
79  void endInsertItems();
80  void beginRemoveItems(int start, int end);
81  void endRemoveItems();
82  void changeItems(int start, int end);
83 
84 };
85 
86 #endif // PLAYLISTMODEL_H
Definition: playlistmodel.h:19