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 <QtCore/QList>
30#include <QtCore/QModelIndex>
31#include <QtCore/QStringList>
32
33
34#ifndef QT_NO_PHONON_OBJECTDESCRIPTIONMODEL
35
36namespace 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
141and existing builds. */
142#if defined(Q_OS_SYMBIAN) && defined(Q_CC_RVCT) || defined(Q_CC_CLANG)
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// Clang also requires the export declaration to be on the class to export vtables
147#define PHONON_TEMPLATE_CLASS_EXPORT PHONON_EXPORT
148#define PHONON_TEMPLATE_CLASS_MEMBER_EXPORT
149#else
150// Windows builds (at least) do not support export declaration on templated class
151// But if you export a member function, the vtable is implicitly exported
152#define PHONON_TEMPLATE_CLASS_EXPORT
153#define PHONON_TEMPLATE_CLASS_MEMBER_EXPORT PHONON_EXPORT
154#endif
155
156 /** \class ObjectDescriptionModel objectdescriptionmodel.h Phonon/ObjectDescriptionModel
157 * \short The ObjectDescriptionModel class provides a model from
158 * a list of ObjectDescription objects.
159 *
160 * ObjectDescriptionModel is a readonly model that supplies a list
161 * using ObjectDescription::name() for the text and
162 * ObjectDescription::description() for the tooltip. If set the properties
163 * "icon" and "available" are used to set the decoration and disable the
164 * item (disabled only visually, you can still select and drag it).
165 *
166 * It also provides the methods moveUp() and moveDown() to order the list.
167 * Additionally drag and drop is possible so that
168 * QAbstractItemView::InternalMove can be used.
169 * The resulting order of the ObjectDescription::index() values can then be
170 * retrieved using tupleIndexOrder().
171 *
172 * An example use case would be to give the user a QComboBox to select
173 * the output device:
174 * \code
175 * QComboBox *cb = new QComboBox(parentWidget);
176 * ObjectDescriptionModel *model = new ObjectDescriptionModel(cb);
177 * model->setModelData(BackendCapabilities::availableAudioOutputDevices());
178 * cb->setModel(model);
179 * cb->setCurrentIndex(0); // select first entry
180 * \endcode
181 *
182 * And to retrieve the selected AudioOutputDevice:
183 * \code
184 * int cbIndex = cb->currentIndex();
185 * AudioOutputDevice selectedDevice = model->modelData(cbIndex);
186 * \endcode
187 *
188 * \ingroup Frontend
189 * \author Matthias Kretz <kretz@kde.org>
190 */
191 template<ObjectDescriptionType type>
192 class PHONON_TEMPLATE_CLASS_EXPORT ObjectDescriptionModel : public QAbstractListModel
193 {
194 public:
195 Q_OBJECT_CHECK
196
197 /** \internal */
198 static PHONON_TEMPLATE_CLASS_MEMBER_EXPORT const QMetaObject staticMetaObject;
199 /** \internal */
200 PHONON_TEMPLATE_CLASS_MEMBER_EXPORT const QMetaObject *metaObject() const;
201 /** \internal */
202 PHONON_TEMPLATE_CLASS_MEMBER_EXPORT void *qt_metacast(const char *_clname);
203 //int qt_metacall(QMetaObject::Call _c, int _id, void **_a);
204
205 /**
206 * Returns the number of rows in the model. This value corresponds
207 * to the size of the list passed through setModelData.
208 *
209 * \param parent The optional \p parent argument is used in most models to specify
210 * the parent of the rows to be counted. Because this is a list if a
211 * valid parent is specified the result will always be 0.
212 *
213 * Reimplemented from QAbstractItemModel.
214 *
215 * \see QAbstractItemModel::rowCount
216 */
217 inline int rowCount(const QModelIndex &parent = QModelIndex()) const { return d->rowCount(parent); } //krazy:exclude=inline
218
219 /**
220 * Returns data from the item with the given \p index for the specified
221 * \p role.
222 * If the view requests an invalid index, an invalid variant is
223 * returned.
224 *
225 * Reimplemented from QAbstractItemModel.
226 *
227 * \see QAbstractItemModel::data
228 * \see Qt::ItemDataRole
229 */
230 inline QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const { return d->data(index, role); } //krazy:exclude=inline
231
232 /**
233 * Reimplemented to show unavailable devices as disabled (but still
234 * selectable).
235 */
236 inline Qt::ItemFlags flags(const QModelIndex &index) const { return d->flags(index); } //krazy:exclude=inline
237
238 /**
239 * Returns a list of indexes in the same order as they are in the
240 * model. The indexes come from the ObjectDescription::index
241 * method.
242 *
243 * This is useful to let the user define a list of preference.
244 */
245 inline QList<int> tupleIndexOrder() const { return d->tupleIndexOrder(); } //krazy:exclude=inline
246
247 /**
248 * Returns the ObjectDescription::index for the tuple
249 * at the given position \p positionIndex. For example a
250 * QComboBox will give you the currentIndex as the
251 * position in the list. But to select the according
252 * AudioOutputDevice using AudioOutputDevice::fromIndex
253 * you can use this method.
254 *
255 * \param positionIndex The position in the list.
256 */
257 inline int tupleIndexAtPositionIndex(int positionIndex) const { return d->tupleIndexAtPositionIndex(positionIndex); } //krazy:exclude=inline
258
259 /**
260 * Returns the MIME data that dropMimeData() can use to create new
261 * items.
262 */
263 inline QMimeData *mimeData(const QModelIndexList &indexes) const { return d->mimeData(type, indexes); } //krazy:exclude=inline
264
265 /**
266 * Moves the item at the given \p index up. In the resulting list
267 * the items at index.row() and index.row() - 1 are swapped.
268 *
269 * Connected views are updated automatically.
270 */
271 inline void moveUp(const QModelIndex &index) { d->moveUp(index); } //krazy:exclude=inline
272
273 /**
274 * Moves the item at the given \p index down. In the resulting list
275 * the items at index.row() and index.row() + 1 are swapped.
276 *
277 * Connected views are updated automatically.
278 */
279 inline void moveDown(const QModelIndex &index) { d->moveDown(index); } //krazy:exclude=inline
280
281 /**
282 * Constructs a ObjectDescription model with the
283 * given \p parent.
284 */
285 explicit inline ObjectDescriptionModel(QObject *parent = 0) : QAbstractListModel(parent), d(new ObjectDescriptionModelData(this)) {} //krazy:exclude=inline
286
287 /**
288 * Constructs a ObjectDescription model with the
289 * given \p parent and the given \p data.
290 */
291 explicit inline ObjectDescriptionModel(const QList<ObjectDescription<type> > &data, QObject *parent = 0) //krazy:exclude=inline
292 : QAbstractListModel(parent), d(new ObjectDescriptionModelData(this)) { setModelData(data); }
293
294 /**
295 * Sets the model data using the list provided by \p data.
296 *
297 * All previous model data is cleared.
298 */
299 inline void setModelData(const QList<ObjectDescription<type> > &data) { //krazy:exclude=inline
300 QList<QExplicitlySharedDataPointer<ObjectDescriptionData> > list;
301 for (int i = 0; i < data.count(); ++i) {
302 list += data.at(i).d;
303 }
304 d->setModelData(list);
305 }
306
307 /**
308 * Returns the model data.
309 *
310 * As the order of the list might have changed this can be different
311 * to what was set using setModelData().
312 */
313 inline QList<ObjectDescription<type> > modelData() const { //krazy:exclude=inline
314 QList<ObjectDescription<type> > ret;
315 QList<QExplicitlySharedDataPointer<ObjectDescriptionData> > list = d->modelData();
316 for (int i = 0; i < list.count(); ++i) {
317 ret << ObjectDescription<type>(list.at(i));
318 }
319 return ret;
320 }
321
322 /**
323 * Returns one ObjectDescription of the model data for the given \p index.
324 */
325 inline ObjectDescription<type> modelData(const QModelIndex &index) const { return ObjectDescription<type>(d->modelData(index)); } //krazy:exclude=inline
326
327 /**
328 * This model supports drag and drop to copy or move
329 * items.
330 */
331 inline Qt::DropActions supportedDropActions() const { return d->supportedDropActions(); } //krazy:exclude=inline
332
333 /**
334 * Accept drops from other models of the same ObjectDescriptionType.
335 *
336 * If a valid \p parent is given the dropped items will be inserted
337 * above that item.
338 */
339 inline bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) { //krazy:exclude=inline
340 return d->dropMimeData(type, data, action, row, column, parent);
341 }
342
343 /**
344 * Removes count rows starting with the given row.
345 *
346 * If a valid \p parent is given no rows are removed since this is a
347 * list model.
348 *
349 * Returns true if the rows were successfully removed; otherwise returns false.
350 */
351 inline bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex()) { //krazy:exclude=inline
352 return d->removeRows(row, count, parent);
353 }
354
355 /**
356 * Returns a list of supported drag and drop MIME types. Currently
357 * it only supports one type used internally.
358 */
359 inline QStringList mimeTypes() const { return d->mimeTypes(type); } //krazy:exclude=inline
360
361 protected:
362 ObjectDescriptionModelData *const d;
363 };
364
365 typedef ObjectDescriptionModel<AudioOutputDeviceType> AudioOutputDeviceModel;
366 typedef ObjectDescriptionModel<AudioCaptureDeviceType> AudioCaptureDeviceModel;
367 typedef ObjectDescriptionModel<VideoCaptureDeviceType> VideoCaptureDeviceModel;
368 typedef ObjectDescriptionModel<EffectType> EffectDescriptionModel;
369 typedef ObjectDescriptionModel<AudioChannelType> AudioChannelDescriptionModel;
370 typedef ObjectDescriptionModel<SubtitleType> SubtitleDescriptionModel;
371/*
372 typedef ObjectDescriptionModel<VideoOutputDeviceType> VideoOutputDeviceModel;
373 typedef ObjectDescriptionModel<AudioCodecType> AudioCodecDescriptionModel;
374 typedef ObjectDescriptionModel<VideoCodecType> VideoCodecDescriptionModel;
375 typedef ObjectDescriptionModel<ContainerFormatType> ContainerFormatDescriptionModel;
376 typedef ObjectDescriptionModel<VisualizationType> VisualizationDescriptionModel;*/
377
378}
379
380#endif //QT_NO_PHONON_OBJECTDESCRIPTIONMODEL
381
382
383#endif // PHONON_OBJECTDESCRIPTIONMODEL_H
384// vim: sw=4 ts=4 tw=80
385