1 | /* This file is part of the KDE project |
2 | Copyright (C) 2006-2007 Matthias Kretz <kretz@kde.org> |
3 | |
4 | This library is free software; you can redistribute it and/or |
5 | modify it under the terms of the GNU Lesser General Public |
6 | License as published by the Free Software Foundation; either |
7 | version 2.1 of the License, or (at your option) version 3, or any |
8 | later version accepted by the membership of KDE e.V. (or its |
9 | successor approved by the membership of KDE e.V.), Nokia Corporation |
10 | (or its successors, if any) and the KDE Free Qt Foundation, which shall |
11 | act as a proxy defined in Section 6 of version 3 of the license. |
12 | |
13 | This library is distributed in the hope that it will be useful, |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
16 | Lesser General Public License for more details. |
17 | |
18 | You should have received a copy of the GNU Lesser General Public |
19 | License along with this library. If not, see <http://www.gnu.org/licenses/>. |
20 | |
21 | */ |
22 | |
23 | #ifndef PHONON_OBJECTDESCRIPTIONMODEL_H |
24 | #define PHONON_OBJECTDESCRIPTIONMODEL_H |
25 | |
26 | #include "phonon_export.h" |
27 | #include "phonondefs.h" |
28 | #include "objectdescription.h" |
29 | #include <QList> |
30 | #include <QModelIndex> |
31 | #include <QStringList> |
32 | |
33 | |
34 | #ifndef QT_NO_PHONON_OBJECTDESCRIPTIONMODEL |
35 | |
36 | namespace Phonon |
37 | { |
38 | class ObjectDescriptionModelDataPrivate; |
39 | |
40 | /** \internal |
41 | * \class ObjectDescriptionModelData objectdescriptionmodel.h phonon/ObjectDescriptionModelData |
42 | * \brief Data class for models for ObjectDescription objects. |
43 | * |
44 | * \author Matthias Kretz <kretz@kde.org> |
45 | */ |
46 | class PHONON_EXPORT ObjectDescriptionModelData |
47 | { |
48 | public: |
49 | /** |
50 | * Returns the number of rows in the model. This value corresponds |
51 | * to the size of the list passed through setModelData. |
52 | * |
53 | * \param parent The optional \p parent argument is used in most models to specify |
54 | * the parent of the rows to be counted. Because this is a list if a |
55 | * valid parent is specified the result will always be 0. |
56 | * |
57 | * Reimplemented from QAbstractItemModel. |
58 | * |
59 | * \see QAbstractItemModel::rowCount |
60 | */ |
61 | int rowCount(const QModelIndex &parent = QModelIndex()) const; |
62 | |
63 | /** |
64 | * Returns data from the item with the given \p index for the specified |
65 | * \p role. |
66 | * If the view requests an invalid index, an invalid variant is |
67 | * returned. |
68 | * |
69 | * Reimplemented from QAbstractItemModel. |
70 | * |
71 | * \see QAbstractItemModel::data |
72 | * \see Qt::ItemDataRole |
73 | */ |
74 | QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const; |
75 | |
76 | /** |
77 | * Reimplemented to show unavailable devices as disabled (but still |
78 | * selectable). |
79 | */ |
80 | Qt::ItemFlags flags(const QModelIndex &index) const; |
81 | |
82 | /** |
83 | * Returns a list of indexes in the same order as they are in the |
84 | * model. The indexes come from the ObjectDescription::index |
85 | * method. |
86 | * |
87 | * This is useful to let the user define a list of preference. |
88 | */ |
89 | QList<int> tupleIndexOrder() const; |
90 | |
91 | /** |
92 | * Returns the ObjectDescription::index for the tuple |
93 | * at the given position \p positionIndex. For example a |
94 | * QComboBox will give you the currentIndex as the |
95 | * position in the list. But to select the according |
96 | * AudioOutputDevice using AudioOutputDevice::fromIndex |
97 | * you can use this method. |
98 | * |
99 | * \param positionIndex The position in the list. |
100 | */ |
101 | int tupleIndexAtPositionIndex(int positionIndex) const; |
102 | |
103 | /** |
104 | * Returns the MIME data that dropMimeData() can use to create new |
105 | * items. |
106 | */ |
107 | QMimeData *mimeData(ObjectDescriptionType type, const QModelIndexList &indexes) const; |
108 | |
109 | /** |
110 | * Moves the item at the given \p index up. In the resulting list |
111 | * the items at index.row() and index.row() - 1 are swapped. |
112 | * |
113 | * Connected views are updated automatically. |
114 | */ |
115 | void moveUp(const QModelIndex &index); |
116 | |
117 | /** |
118 | * Moves the item at the given \p index down. In the resulting list |
119 | * the items at index.row() and index.row() + 1 are swapped. |
120 | * |
121 | * Connected views are updated automatically. |
122 | */ |
123 | void moveDown(const QModelIndex &index); |
124 | |
125 | void setModelData(const QList<QExplicitlySharedDataPointer<ObjectDescriptionData> > &data); |
126 | QList<QExplicitlySharedDataPointer<ObjectDescriptionData> > modelData() const; |
127 | QExplicitlySharedDataPointer<ObjectDescriptionData> modelData(const QModelIndex &index) const; |
128 | Qt::DropActions supportedDropActions() const; |
129 | bool dropMimeData(ObjectDescriptionType type, const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent); |
130 | bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex()); |
131 | QStringList mimeTypes(ObjectDescriptionType type) const; |
132 | |
133 | ObjectDescriptionModelData(QAbstractListModel *); |
134 | protected: |
135 | ~ObjectDescriptionModelData(); |
136 | //ObjectDescriptionModelData(ObjectDescriptionModelDataPrivate *dd); |
137 | ObjectDescriptionModelDataPrivate *const d; |
138 | }; |
139 | |
140 | /* Required to ensure template class vtables are exported on both symbian |
141 | and existing builds. */ |
142 | #if defined(Q_OS_SYMBIAN) && defined(Q_CC_RVCT) |
143 | // RVCT compiler (2.2.686) requires the export declaration to be on the class to export vtables |
144 | // MWC compiler works both ways |
145 | // GCCE compiler is unknown (it can't compile QtCore yet) |
146 | #define PHONON_TEMPLATE_CLASS_EXPORT PHONON_EXPORT |
147 | #define PHONON_TEMPLATE_CLASS_MEMBER_EXPORT |
148 | #else |
149 | // Windows builds (at least) do not support export declaration on templated class |
150 | // But if you export a member function, the vtable is implicitly exported |
151 | #define PHONON_TEMPLATE_CLASS_EXPORT |
152 | #define PHONON_TEMPLATE_CLASS_MEMBER_EXPORT PHONON_EXPORT |
153 | #endif |
154 | |
155 | /** \class ObjectDescriptionModel objectdescriptionmodel.h Phonon/ObjectDescriptionModel |
156 | * \short The ObjectDescriptionModel class provides a model from |
157 | * a list of ObjectDescription objects. |
158 | * |
159 | * ObjectDescriptionModel is a readonly model that supplies a list |
160 | * using ObjectDescription::name() for the text and |
161 | * ObjectDescription::description() for the tooltip. If set the properties |
162 | * "icon" and "available" are used to set the decoration and disable the |
163 | * item (disabled only visually, you can still select and drag it). |
164 | * |
165 | * It also provides the methods moveUp() and moveDown() to order the list. |
166 | * Additionally drag and drop is possible so that |
167 | * QAbstractItemView::InternalMove can be used. |
168 | * The resulting order of the ObjectDescription::index() values can then be |
169 | * retrieved using tupleIndexOrder(). |
170 | * |
171 | * An example use case would be to give the user a QComboBox to select |
172 | * the output device: |
173 | * \code |
174 | * QComboBox *cb = new QComboBox(parentWidget); |
175 | * ObjectDescriptionModel *model = new ObjectDescriptionModel(cb); |
176 | * model->setModelData(BackendCapabilities::availableAudioOutputDevices()); |
177 | * cb->setModel(model); |
178 | * cb->setCurrentIndex(0); // select first entry |
179 | * \endcode |
180 | * |
181 | * And to retrieve the selected AudioOutputDevice: |
182 | * \code |
183 | * int cbIndex = cb->currentIndex(); |
184 | * AudioOutputDevice selectedDevice = model->modelData(cbIndex); |
185 | * \endcode |
186 | * |
187 | * \ingroup Frontend |
188 | * \author Matthias Kretz <kretz@kde.org> |
189 | */ |
190 | template<ObjectDescriptionType type> |
191 | class PHONON_TEMPLATE_CLASS_EXPORT ObjectDescriptionModel : public QAbstractListModel |
192 | { |
193 | public: |
194 | /** \internal */ |
195 | static PHONON_TEMPLATE_CLASS_MEMBER_EXPORT const QMetaObject staticMetaObject; |
196 | /** \internal */ |
197 | PHONON_TEMPLATE_CLASS_MEMBER_EXPORT const QMetaObject *metaObject() const override; |
198 | /** \internal */ |
199 | PHONON_TEMPLATE_CLASS_MEMBER_EXPORT void *qt_metacast(const char *_clname) override; |
200 | //int qt_metacall(QMetaObject::Call _c, int _id, void **_a); |
201 | |
202 | /** |
203 | * Returns the number of rows in the model. This value corresponds |
204 | * to the size of the list passed through setModelData. |
205 | * |
206 | * \param parent The optional \p parent argument is used in most models to specify |
207 | * the parent of the rows to be counted. Because this is a list if a |
208 | * valid parent is specified the result will always be 0. |
209 | * |
210 | * Reimplemented from QAbstractItemModel. |
211 | * |
212 | * \see QAbstractItemModel::rowCount |
213 | */ |
214 | inline int rowCount(const QModelIndex &parent = QModelIndex()) const override { return d->rowCount(parent); } //krazy:exclude=inline |
215 | |
216 | /** |
217 | * Returns data from the item with the given \p index for the specified |
218 | * \p role. |
219 | * If the view requests an invalid index, an invalid variant is |
220 | * returned. |
221 | * |
222 | * Reimplemented from QAbstractItemModel. |
223 | * |
224 | * \see QAbstractItemModel::data |
225 | * \see Qt::ItemDataRole |
226 | */ |
227 | inline QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override { return d->data(index, role); } //krazy:exclude=inline |
228 | |
229 | /** |
230 | * Reimplemented to show unavailable devices as disabled (but still |
231 | * selectable). |
232 | */ |
233 | inline Qt::ItemFlags flags(const QModelIndex &index) const override { return d->flags(index); } //krazy:exclude=inline |
234 | |
235 | /** |
236 | * Returns a list of indexes in the same order as they are in the |
237 | * model. The indexes come from the ObjectDescription::index |
238 | * method. |
239 | * |
240 | * This is useful to let the user define a list of preference. |
241 | */ |
242 | inline QList<int> tupleIndexOrder() const { return d->tupleIndexOrder(); } //krazy:exclude=inline |
243 | |
244 | /** |
245 | * Returns the ObjectDescription::index for the tuple |
246 | * at the given position \p positionIndex. For example a |
247 | * QComboBox will give you the currentIndex as the |
248 | * position in the list. But to select the according |
249 | * AudioOutputDevice using AudioOutputDevice::fromIndex |
250 | * you can use this method. |
251 | * |
252 | * \param positionIndex The position in the list. |
253 | */ |
254 | inline int tupleIndexAtPositionIndex(int positionIndex) const { return d->tupleIndexAtPositionIndex(positionIndex); } //krazy:exclude=inline |
255 | |
256 | /** |
257 | * Returns the MIME data that dropMimeData() can use to create new |
258 | * items. |
259 | */ |
260 | inline QMimeData *mimeData(const QModelIndexList &indexes) const override { return d->mimeData(type, indexes); } //krazy:exclude=inline |
261 | |
262 | /** |
263 | * Moves the item at the given \p index up. In the resulting list |
264 | * the items at index.row() and index.row() - 1 are swapped. |
265 | * |
266 | * Connected views are updated automatically. |
267 | */ |
268 | inline void moveUp(const QModelIndex &index) { d->moveUp(index); } //krazy:exclude=inline |
269 | |
270 | /** |
271 | * Moves the item at the given \p index down. In the resulting list |
272 | * the items at index.row() and index.row() + 1 are swapped. |
273 | * |
274 | * Connected views are updated automatically. |
275 | */ |
276 | inline void moveDown(const QModelIndex &index) { d->moveDown(index); } //krazy:exclude=inline |
277 | |
278 | /** |
279 | * Constructs a ObjectDescription model with the |
280 | * given \p parent. |
281 | */ |
282 | explicit inline ObjectDescriptionModel(QObject *parent = nullptr) : QAbstractListModel(parent), d(new ObjectDescriptionModelData(this)) {} //krazy:exclude=inline |
283 | |
284 | /** |
285 | * Constructs a ObjectDescription model with the |
286 | * given \p parent and the given \p data. |
287 | */ |
288 | explicit inline ObjectDescriptionModel(const QList<ObjectDescription<type> > &data, QObject *parent = nullptr) //krazy:exclude=inline |
289 | : QAbstractListModel(parent), d(new ObjectDescriptionModelData(this)) { setModelData(data); } |
290 | |
291 | /** |
292 | * Sets the model data using the list provided by \p data. |
293 | * |
294 | * All previous model data is cleared. |
295 | */ |
296 | inline void setModelData(const QList<ObjectDescription<type> > &data) { //krazy:exclude=inline |
297 | QList<QExplicitlySharedDataPointer<ObjectDescriptionData> > list; |
298 | for (int i = 0; i < data.count(); ++i) { |
299 | list += data.at(i).d; |
300 | } |
301 | d->setModelData(list); |
302 | } |
303 | |
304 | /** |
305 | * Returns the model data. |
306 | * |
307 | * As the order of the list might have changed this can be different |
308 | * to what was set using setModelData(). |
309 | */ |
310 | inline QList<ObjectDescription<type> > modelData() const { //krazy:exclude=inline |
311 | QList<ObjectDescription<type> > ret; |
312 | QList<QExplicitlySharedDataPointer<ObjectDescriptionData> > list = d->modelData(); |
313 | for (int i = 0; i < list.count(); ++i) { |
314 | ret << ObjectDescription<type>(list.at(i)); |
315 | } |
316 | return ret; |
317 | } |
318 | |
319 | /** |
320 | * Returns one ObjectDescription of the model data for the given \p index. |
321 | */ |
322 | inline ObjectDescription<type> modelData(const QModelIndex &index) const { return ObjectDescription<type>(d->modelData(index)); } //krazy:exclude=inline |
323 | |
324 | /** |
325 | * This model supports drag and drop to copy or move |
326 | * items. |
327 | */ |
328 | inline Qt::DropActions supportedDropActions() const override { return d->supportedDropActions(); } //krazy:exclude=inline |
329 | |
330 | /** |
331 | * Accept drops from other models of the same ObjectDescriptionType. |
332 | * |
333 | * If a valid \p parent is given the dropped items will be inserted |
334 | * above that item. |
335 | */ |
336 | inline bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) override { //krazy:exclude=inline |
337 | return d->dropMimeData(type, data, action, row, column, parent); |
338 | } |
339 | |
340 | /** |
341 | * Removes count rows starting with the given row. |
342 | * |
343 | * If a valid \p parent is given no rows are removed since this is a |
344 | * list model. |
345 | * |
346 | * Returns true if the rows were successfully removed; otherwise returns false. |
347 | */ |
348 | inline bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex()) override { //krazy:exclude=inline |
349 | return d->removeRows(row, count, parent); |
350 | } |
351 | |
352 | /** |
353 | * Returns a list of supported drag and drop MIME types. Currently |
354 | * it only supports one type used internally. |
355 | */ |
356 | inline QStringList mimeTypes() const override { return d->mimeTypes(type); } //krazy:exclude=inline |
357 | |
358 | protected: |
359 | ObjectDescriptionModelData *const d; |
360 | }; |
361 | |
362 | typedef ObjectDescriptionModel<AudioOutputDeviceType> AudioOutputDeviceModel; |
363 | typedef ObjectDescriptionModel<AudioCaptureDeviceType> AudioCaptureDeviceModel; |
364 | typedef ObjectDescriptionModel<VideoCaptureDeviceType> VideoCaptureDeviceModel; |
365 | typedef ObjectDescriptionModel<EffectType> EffectDescriptionModel; |
366 | typedef ObjectDescriptionModel<AudioChannelType> AudioChannelDescriptionModel; |
367 | typedef ObjectDescriptionModel<SubtitleType> SubtitleDescriptionModel; |
368 | /* |
369 | typedef ObjectDescriptionModel<VideoOutputDeviceType> VideoOutputDeviceModel; |
370 | typedef ObjectDescriptionModel<AudioCodecType> AudioCodecDescriptionModel; |
371 | typedef ObjectDescriptionModel<VideoCodecType> VideoCodecDescriptionModel; |
372 | typedef ObjectDescriptionModel<ContainerFormatType> ContainerFormatDescriptionModel; |
373 | typedef ObjectDescriptionModel<VisualizationType> VisualizationDescriptionModel;*/ |
374 | |
375 | } |
376 | |
377 | #endif //QT_NO_PHONON_OBJECTDESCRIPTIONMODEL |
378 | |
379 | |
380 | #endif // PHONON_OBJECTDESCRIPTIONMODEL_H |
381 | // vim: sw=4 ts=4 tw=80 |
382 | |