1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:LGPL$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** GNU Lesser General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
19 | ** General Public License version 3 as published by the Free Software |
20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
21 | ** packaging of this file. Please review the following information to |
22 | ** ensure the GNU Lesser General Public License version 3 requirements |
23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
24 | ** |
25 | ** GNU General Public License Usage |
26 | ** Alternatively, this file may be used under the terms of the GNU |
27 | ** General Public License version 2.0 or (at your option) the GNU General |
28 | ** Public license version 3 or any later version approved by the KDE Free |
29 | ** Qt Foundation. The licenses are as published by the Free Software |
30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
31 | ** included in the packaging of this file. Please review the following |
32 | ** information to ensure the GNU General Public License requirements will |
33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
35 | ** |
36 | ** $QT_END_LICENSE$ |
37 | ** |
38 | ****************************************************************************/ |
39 | |
40 | #ifndef QDECLARATIVEPLAYLIST_P_H |
41 | #define QDECLARATIVEPLAYLIST_P_H |
42 | |
43 | // |
44 | // W A R N I N G |
45 | // ------------- |
46 | // |
47 | // This file is not part of the Qt API. It exists for the convenience |
48 | // of other Qt classes. This header file may change from version to |
49 | // version without notice, or even be removed. |
50 | // |
51 | // We mean it. |
52 | // |
53 | |
54 | #include <QtCore/QAbstractListModel> |
55 | #include <QtQml/qqmlparserstatus.h> |
56 | #include <QtQml/qqml.h> |
57 | |
58 | #include <qmediaplaylist.h> |
59 | |
60 | QT_BEGIN_NAMESPACE |
61 | |
62 | class QDeclarativePlaylistItem : public QObject |
63 | { |
64 | Q_OBJECT |
65 | Q_PROPERTY(QUrl source READ source WRITE setSource) |
66 | |
67 | public: |
68 | QDeclarativePlaylistItem(QObject *parent = 0); |
69 | |
70 | QUrl source() const; |
71 | void setSource(const QUrl &source); |
72 | |
73 | private: |
74 | QUrl m_source; |
75 | }; |
76 | |
77 | class QDeclarativePlaylist : public QAbstractListModel, public QQmlParserStatus |
78 | { |
79 | Q_OBJECT |
80 | Q_PROPERTY(PlaybackMode playbackMode READ playbackMode WRITE setPlaybackMode NOTIFY playbackModeChanged) |
81 | Q_PROPERTY(QUrl currentItemSource READ currentItemSource NOTIFY currentItemSourceChanged) |
82 | Q_PROPERTY(int currentIndex READ currentIndex WRITE setCurrentIndex NOTIFY currentIndexChanged) |
83 | Q_PROPERTY(int itemCount READ itemCount NOTIFY itemCountChanged) |
84 | Q_PROPERTY(bool readOnly READ readOnly NOTIFY readOnlyChanged) |
85 | Q_PROPERTY(Error error READ error NOTIFY errorChanged) |
86 | Q_PROPERTY(QString errorString READ errorString NOTIFY errorChanged) |
87 | Q_PROPERTY(QQmlListProperty<QDeclarativePlaylistItem> items READ items DESIGNABLE false) |
88 | Q_ENUMS(PlaybackMode) |
89 | Q_ENUMS(Error) |
90 | Q_INTERFACES(QQmlParserStatus) |
91 | Q_CLASSINFO("DefaultProperty" , "items" ) |
92 | |
93 | public: |
94 | enum PlaybackMode |
95 | { |
96 | CurrentItemOnce = QMediaPlaylist::CurrentItemOnce, |
97 | CurrentItemInLoop = QMediaPlaylist::CurrentItemInLoop, |
98 | Sequential = QMediaPlaylist::Sequential, |
99 | Loop = QMediaPlaylist::Loop, |
100 | Random = QMediaPlaylist::Random |
101 | }; |
102 | enum Error |
103 | { |
104 | NoError = QMediaPlaylist::NoError, |
105 | FormatError = QMediaPlaylist::FormatError, |
106 | FormatNotSupportedError = QMediaPlaylist::FormatNotSupportedError, |
107 | NetworkError = QMediaPlaylist::NetworkError, |
108 | AccessDeniedError = QMediaPlaylist::AccessDeniedError |
109 | }; |
110 | enum Roles |
111 | { |
112 | SourceRole = Qt::UserRole + 1 |
113 | }; |
114 | |
115 | QDeclarativePlaylist(QObject *parent = 0); |
116 | ~QDeclarativePlaylist(); |
117 | |
118 | PlaybackMode playbackMode() const; |
119 | void setPlaybackMode(PlaybackMode playbackMode); |
120 | QUrl currentItemSource() const; |
121 | int currentIndex() const; |
122 | void setCurrentIndex(int currentIndex); |
123 | int itemCount() const; |
124 | bool readOnly() const; |
125 | Error error() const; |
126 | QString errorString() const; |
127 | QMediaPlaylist *mediaPlaylist() const { return m_playlist; } |
128 | |
129 | int rowCount(const QModelIndex &parent = QModelIndex()) const override; |
130 | QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; |
131 | QHash<int, QByteArray> roleNames() const override; |
132 | |
133 | void classBegin() override; |
134 | void componentComplete() override; |
135 | |
136 | QQmlListProperty<QDeclarativePlaylistItem> items() { |
137 | return QQmlListProperty<QDeclarativePlaylistItem>( |
138 | this, 0, item_append, item_count, 0, item_clear); |
139 | } |
140 | static void item_append(QQmlListProperty<QDeclarativePlaylistItem> *list, |
141 | QDeclarativePlaylistItem* item) { |
142 | static_cast<QDeclarativePlaylist*>(list->object)->addItem(source: item->source()); |
143 | } |
144 | static int item_count(QQmlListProperty<QDeclarativePlaylistItem> *list) { |
145 | return static_cast<QDeclarativePlaylist*>(list->object)->itemCount(); |
146 | } |
147 | static void item_clear(QQmlListProperty<QDeclarativePlaylistItem> *list) { |
148 | static_cast<QDeclarativePlaylist*>(list->object)->clear(); |
149 | } |
150 | |
151 | public Q_SLOTS: |
152 | QUrl itemSource(int index); |
153 | int nextIndex(int steps = 1); |
154 | int previousIndex(int steps = 1); |
155 | void next(); |
156 | void previous(); |
157 | void shuffle(); |
158 | void load(const QUrl &location, const QString &format = QString()); |
159 | bool save(const QUrl &location, const QString &format = QString()); |
160 | bool addItem(const QUrl &source); |
161 | Q_REVISION(1) bool addItems(const QList<QUrl> &sources); |
162 | bool insertItem(int index, const QUrl &source); |
163 | Q_REVISION(1) bool insertItems(int index, const QList<QUrl> &sources); |
164 | Q_REVISION(1) bool moveItem(int from, int to); |
165 | bool removeItem(int index); |
166 | Q_REVISION(1) bool removeItems(int start, int end); |
167 | bool clear(); |
168 | |
169 | Q_SIGNALS: |
170 | void playbackModeChanged(); |
171 | void currentItemSourceChanged(); |
172 | void currentIndexChanged(); |
173 | void itemCountChanged(); |
174 | void readOnlyChanged(); |
175 | void errorChanged(); |
176 | |
177 | void itemAboutToBeInserted(int start, int end); |
178 | void itemInserted(int start, int end); |
179 | void itemAboutToBeRemoved(int start, int end); |
180 | void itemRemoved(int start, int end); |
181 | void itemChanged(int start, int end); |
182 | void loaded(); |
183 | void loadFailed(); |
184 | |
185 | void error(QDeclarativePlaylist::Error error, const QString &errorString); |
186 | |
187 | private Q_SLOTS: |
188 | void _q_mediaAboutToBeInserted(int start, int end); |
189 | void _q_mediaInserted(int start, int end); |
190 | void _q_mediaAboutToBeRemoved(int start, int end); |
191 | void _q_mediaRemoved(int start, int end); |
192 | void _q_mediaChanged(int start, int end); |
193 | void _q_loadFailed(); |
194 | |
195 | private: |
196 | Q_DISABLE_COPY(QDeclarativePlaylist) |
197 | |
198 | QMediaPlaylist *m_playlist; |
199 | QString m_errorString; |
200 | QMediaPlaylist::Error m_error; |
201 | bool m_readOnly; |
202 | }; |
203 | |
204 | QT_END_NAMESPACE |
205 | |
206 | QML_DECLARE_TYPE(QT_PREPEND_NAMESPACE(QDeclarativePlaylistItem)) |
207 | QML_DECLARE_TYPE(QT_PREPEND_NAMESPACE(QDeclarativePlaylist)) |
208 | |
209 | #endif |
210 | |