File indexing completed on 2024-04-28 16:01:49

0001 /*  This file is part of the KDE project
0002     Copyright (C) 2006-2007 Matthias Kretz <kretz@kde.org>
0003 
0004     This library is free software; you can redistribute it and/or
0005     modify it under the terms of the GNU Lesser General Public
0006     License as published by the Free Software Foundation; either
0007     version 2.1 of the License, or (at your option) version 3, or any
0008     later version accepted by the membership of KDE e.V. (or its
0009     successor approved by the membership of KDE e.V.), Nokia Corporation 
0010     (or its successors, if any) and the KDE Free Qt Foundation, which shall
0011     act as a proxy defined in Section 6 of version 3 of the license.
0012 
0013     This library is distributed in the hope that it will be useful,
0014     but WITHOUT ANY WARRANTY; without even the implied warranty of
0015     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0016     Lesser General Public License for more details.
0017 
0018     You should have received a copy of the GNU Lesser General Public 
0019     License along with this library.  If not, see <http://www.gnu.org/licenses/>.
0020 
0021 */
0022 
0023 #ifndef PHONON_OBJECTDESCRIPTIONMODEL_H
0024 #define PHONON_OBJECTDESCRIPTIONMODEL_H
0025 
0026 #include "phonon_export.h"
0027 #include "phonondefs.h"
0028 #include "objectdescription.h"
0029 #include <QList>
0030 #include <QModelIndex>
0031 #include <QStringList>
0032 
0033 
0034 #ifndef QT_NO_PHONON_OBJECTDESCRIPTIONMODEL
0035 
0036 namespace Phonon
0037 {
0038     class ObjectDescriptionModelDataPrivate;
0039 
0040     /** \internal
0041      * \class ObjectDescriptionModelData objectdescriptionmodel.h phonon/ObjectDescriptionModelData
0042      * \brief Data class for models for ObjectDescription objects.
0043      *
0044      * \author Matthias Kretz <kretz@kde.org>
0045      */
0046     class PHONON_EXPORT ObjectDescriptionModelData
0047     {
0048         public:
0049             /**
0050              * Returns the number of rows in the model. This value corresponds
0051              * to the size of the list passed through setModelData.
0052              *
0053              * \param parent The optional \p parent argument is used in most models to specify
0054              * the parent of the rows to be counted. Because this is a list if a
0055              * valid parent is specified the result will always be 0.
0056              *
0057              * Reimplemented from QAbstractItemModel.
0058              *
0059              * \see QAbstractItemModel::rowCount
0060              */
0061             int rowCount(const QModelIndex &parent = QModelIndex()) const;
0062 
0063             /**
0064              * Returns data from the item with the given \p index for the specified
0065              * \p role.
0066              * If the view requests an invalid index, an invalid variant is
0067              * returned.
0068              *
0069              * Reimplemented from QAbstractItemModel.
0070              *
0071              * \see QAbstractItemModel::data
0072              * \see Qt::ItemDataRole
0073              */
0074             QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
0075 
0076             /**
0077              * Reimplemented to show unavailable devices as disabled (but still
0078              * selectable).
0079              */
0080             Qt::ItemFlags flags(const QModelIndex &index) const;
0081 
0082             /**
0083              * Returns a list of indexes in the same order as they are in the
0084              * model. The indexes come from the ObjectDescription::index
0085              * method.
0086              *
0087              * This is useful to let the user define a list of preference.
0088              */
0089             QList<int> tupleIndexOrder() const;
0090 
0091             /**
0092              * Returns the ObjectDescription::index for the tuple
0093              * at the given position \p positionIndex. For example a
0094              * QComboBox will give you the currentIndex as the
0095              * position in the list. But to select the according
0096              * AudioOutputDevice using AudioOutputDevice::fromIndex
0097              * you can use this method.
0098              *
0099              * \param positionIndex The position in the list.
0100              */
0101             int tupleIndexAtPositionIndex(int positionIndex) const;
0102 
0103             /**
0104              * Returns the MIME data that dropMimeData() can use to create new
0105              * items.
0106              */
0107             QMimeData *mimeData(ObjectDescriptionType type, const QModelIndexList &indexes) const;
0108 
0109             /**
0110              * Moves the item at the given \p index up. In the resulting list
0111              * the items at index.row() and index.row() - 1 are swapped.
0112              *
0113              * Connected views are updated automatically.
0114              */
0115             void moveUp(const QModelIndex &index);
0116 
0117             /**
0118              * Moves the item at the given \p index down. In the resulting list
0119              * the items at index.row() and index.row() + 1 are swapped.
0120              *
0121              * Connected views are updated automatically.
0122              */
0123             void moveDown(const QModelIndex &index);
0124 
0125             void setModelData(const QList<QExplicitlySharedDataPointer<ObjectDescriptionData> > &data);
0126             QList<QExplicitlySharedDataPointer<ObjectDescriptionData> > modelData() const;
0127             QExplicitlySharedDataPointer<ObjectDescriptionData> modelData(const QModelIndex &index) const;
0128             Qt::DropActions supportedDropActions() const;
0129             bool dropMimeData(ObjectDescriptionType type, const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent);
0130             bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex());
0131             QStringList mimeTypes(ObjectDescriptionType type) const;
0132 
0133             ObjectDescriptionModelData(QAbstractListModel *);
0134         protected:
0135             ~ObjectDescriptionModelData();
0136             //ObjectDescriptionModelData(ObjectDescriptionModelDataPrivate *dd);
0137             ObjectDescriptionModelDataPrivate *const d;
0138     };
0139 
0140 /* Required to ensure template class vtables are exported on both symbian
0141 and existing builds. */
0142 #if defined(Q_OS_SYMBIAN) && defined(Q_CC_RVCT)
0143 // RVCT compiler (2.2.686) requires the export declaration to be on the class to export vtables
0144 // MWC compiler works both ways
0145 // GCCE compiler is unknown (it can't compile QtCore yet)
0146 #define PHONON_TEMPLATE_CLASS_EXPORT PHONON_EXPORT
0147 #define PHONON_TEMPLATE_CLASS_MEMBER_EXPORT
0148 #else
0149 // Windows builds (at least) do not support export declaration on templated class
0150 // But if you export a member function, the vtable is implicitly exported
0151 #define PHONON_TEMPLATE_CLASS_EXPORT
0152 #define PHONON_TEMPLATE_CLASS_MEMBER_EXPORT PHONON_EXPORT
0153 #endif
0154 
0155     /** \class ObjectDescriptionModel objectdescriptionmodel.h Phonon/ObjectDescriptionModel
0156      * \short The ObjectDescriptionModel class provides a model from
0157      * a list of ObjectDescription objects.
0158      *
0159      * ObjectDescriptionModel is a readonly model that supplies a list
0160      * using ObjectDescription::name() for the text and
0161      * ObjectDescription::description() for the tooltip. If set the properties
0162      * "icon" and "available" are used to set the decoration and disable the
0163      * item (disabled only visually, you can still select and drag it).
0164      *
0165      * It also provides the methods moveUp() and moveDown() to order the list.
0166      * Additionally drag and drop is possible so that
0167      * QAbstractItemView::InternalMove can be used.
0168      * The resulting order of the ObjectDescription::index() values can then be
0169      * retrieved using tupleIndexOrder().
0170      *
0171      * An example use case would be to give the user a QComboBox to select
0172      * the output device:
0173      * \code
0174      * QComboBox *cb = new QComboBox(parentWidget);
0175      * ObjectDescriptionModel *model = new ObjectDescriptionModel(cb);
0176      * model->setModelData(BackendCapabilities::availableAudioOutputDevices());
0177      * cb->setModel(model);
0178      * cb->setCurrentIndex(0); // select first entry
0179      * \endcode
0180      *
0181      * And to retrieve the selected AudioOutputDevice:
0182      * \code
0183      * int cbIndex = cb->currentIndex();
0184      * AudioOutputDevice selectedDevice = model->modelData(cbIndex);
0185      * \endcode
0186      *
0187      * \ingroup Frontend
0188      * \author Matthias Kretz <kretz@kde.org>
0189      */
0190     template<ObjectDescriptionType type>
0191     class PHONON_TEMPLATE_CLASS_EXPORT ObjectDescriptionModel : public QAbstractListModel
0192     {
0193         public:
0194             /** \internal */
0195             static PHONON_TEMPLATE_CLASS_MEMBER_EXPORT const QMetaObject staticMetaObject;
0196             /** \internal */
0197             PHONON_TEMPLATE_CLASS_MEMBER_EXPORT const QMetaObject *metaObject() const override;
0198             /** \internal */
0199             PHONON_TEMPLATE_CLASS_MEMBER_EXPORT void *qt_metacast(const char *_clname) override;
0200             //int qt_metacall(QMetaObject::Call _c, int _id, void **_a);
0201 
0202             /**
0203              * Returns the number of rows in the model. This value corresponds
0204              * to the size of the list passed through setModelData.
0205              *
0206              * \param parent The optional \p parent argument is used in most models to specify
0207              * the parent of the rows to be counted. Because this is a list if a
0208              * valid parent is specified the result will always be 0.
0209              *
0210              * Reimplemented from QAbstractItemModel.
0211              *
0212              * \see QAbstractItemModel::rowCount
0213              */
0214             inline int rowCount(const QModelIndex &parent = QModelIndex()) const override { return d->rowCount(parent); } //krazy:exclude=inline
0215 
0216             /**
0217              * Returns data from the item with the given \p index for the specified
0218              * \p role.
0219              * If the view requests an invalid index, an invalid variant is
0220              * returned.
0221              *
0222              * Reimplemented from QAbstractItemModel.
0223              *
0224              * \see QAbstractItemModel::data
0225              * \see Qt::ItemDataRole
0226              */
0227             inline QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override { return d->data(index, role); } //krazy:exclude=inline
0228 
0229             /**
0230              * Reimplemented to show unavailable devices as disabled (but still
0231              * selectable).
0232              */
0233             inline Qt::ItemFlags flags(const QModelIndex &index) const override { return d->flags(index); } //krazy:exclude=inline
0234 
0235             /**
0236              * Returns a list of indexes in the same order as they are in the
0237              * model. The indexes come from the ObjectDescription::index
0238              * method.
0239              *
0240              * This is useful to let the user define a list of preference.
0241              */
0242             inline QList<int> tupleIndexOrder() const { return d->tupleIndexOrder(); } //krazy:exclude=inline
0243 
0244             /**
0245              * Returns the ObjectDescription::index for the tuple
0246              * at the given position \p positionIndex. For example a
0247              * QComboBox will give you the currentIndex as the
0248              * position in the list. But to select the according
0249              * AudioOutputDevice using AudioOutputDevice::fromIndex
0250              * you can use this method.
0251              *
0252              * \param positionIndex The position in the list.
0253              */
0254             inline int tupleIndexAtPositionIndex(int positionIndex) const { return d->tupleIndexAtPositionIndex(positionIndex); } //krazy:exclude=inline
0255 
0256             /**
0257              * Returns the MIME data that dropMimeData() can use to create new
0258              * items.
0259              */
0260             inline QMimeData *mimeData(const QModelIndexList &indexes) const override { return d->mimeData(type, indexes); } //krazy:exclude=inline
0261 
0262             /**
0263              * Moves the item at the given \p index up. In the resulting list
0264              * the items at index.row() and index.row() - 1 are swapped.
0265              *
0266              * Connected views are updated automatically.
0267              */
0268             inline void moveUp(const QModelIndex &index) { d->moveUp(index); } //krazy:exclude=inline
0269 
0270             /**
0271              * Moves the item at the given \p index down. In the resulting list
0272              * the items at index.row() and index.row() + 1 are swapped.
0273              *
0274              * Connected views are updated automatically.
0275              */
0276             inline void moveDown(const QModelIndex &index) { d->moveDown(index); } //krazy:exclude=inline
0277 
0278             /**
0279              * Constructs a ObjectDescription model with the
0280              * given \p parent.
0281              */
0282             explicit inline ObjectDescriptionModel(QObject *parent = nullptr) : QAbstractListModel(parent), d(new ObjectDescriptionModelData(this)) {} //krazy:exclude=inline
0283 
0284             /**
0285              * Constructs a ObjectDescription model with the
0286              * given \p parent and the given \p data.
0287              */
0288             explicit inline ObjectDescriptionModel(const QList<ObjectDescription<type> > &data, QObject *parent = nullptr) //krazy:exclude=inline
0289                 : QAbstractListModel(parent), d(new ObjectDescriptionModelData(this)) { setModelData(data); }
0290 
0291             /**
0292              * Sets the model data using the list provided by \p data.
0293              *
0294              * All previous model data is cleared.
0295              */
0296             inline void setModelData(const QList<ObjectDescription<type> > &data) { //krazy:exclude=inline
0297                 QList<QExplicitlySharedDataPointer<ObjectDescriptionData> > list;
0298                 for (int i = 0; i < data.count(); ++i) {
0299                     list += data.at(i).d;
0300                 }
0301                 d->setModelData(list);
0302             }
0303 
0304             /**
0305              * Returns the model data.
0306              *
0307              * As the order of the list might have changed this can be different
0308              * to what was set using setModelData().
0309              */
0310             inline QList<ObjectDescription<type> > modelData() const { //krazy:exclude=inline
0311                 QList<ObjectDescription<type> > ret;
0312                 QList<QExplicitlySharedDataPointer<ObjectDescriptionData> > list = d->modelData();
0313                 for (int i = 0; i < list.count(); ++i) {
0314                     ret << ObjectDescription<type>(list.at(i));
0315                 }
0316                 return ret;
0317             }
0318 
0319             /**
0320              * Returns one ObjectDescription of the model data for the given \p index.
0321              */
0322             inline ObjectDescription<type> modelData(const QModelIndex &index) const { return ObjectDescription<type>(d->modelData(index)); } //krazy:exclude=inline
0323 
0324             /**
0325              * This model supports drag and drop to copy or move
0326              * items.
0327              */
0328             inline Qt::DropActions supportedDropActions() const override { return d->supportedDropActions(); } //krazy:exclude=inline
0329 
0330             /**
0331              * Accept drops from other models of the same ObjectDescriptionType.
0332              *
0333              * If a valid \p parent is given the dropped items will be inserted
0334              * above that item.
0335              */
0336             inline bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) override { //krazy:exclude=inline
0337                 return d->dropMimeData(type, data, action, row, column, parent);
0338             }
0339 
0340             /**
0341              * Removes count rows starting with the given row.
0342              *
0343              * If a valid \p parent is given no rows are removed since this is a
0344              * list model.
0345              *
0346              * Returns true if the rows were successfully removed; otherwise returns false.
0347              */
0348             inline bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex()) override { //krazy:exclude=inline
0349                 return d->removeRows(row, count, parent);
0350             }
0351 
0352             /**
0353              * Returns a list of supported drag and drop MIME types. Currently
0354              * it only supports one type used internally.
0355              */
0356             inline QStringList mimeTypes() const override { return d->mimeTypes(type); } //krazy:exclude=inline
0357 
0358         protected:
0359             ObjectDescriptionModelData *const d;
0360     };
0361 
0362     typedef ObjectDescriptionModel<AudioOutputDeviceType> AudioOutputDeviceModel;
0363     typedef ObjectDescriptionModel<AudioCaptureDeviceType> AudioCaptureDeviceModel;
0364     typedef ObjectDescriptionModel<VideoCaptureDeviceType> VideoCaptureDeviceModel;
0365     typedef ObjectDescriptionModel<EffectType> EffectDescriptionModel;
0366     typedef ObjectDescriptionModel<AudioChannelType> AudioChannelDescriptionModel;
0367     typedef ObjectDescriptionModel<SubtitleType> SubtitleDescriptionModel;
0368 /*
0369     typedef ObjectDescriptionModel<VideoOutputDeviceType> VideoOutputDeviceModel;
0370     typedef ObjectDescriptionModel<AudioCodecType> AudioCodecDescriptionModel;
0371     typedef ObjectDescriptionModel<VideoCodecType> VideoCodecDescriptionModel;
0372     typedef ObjectDescriptionModel<ContainerFormatType> ContainerFormatDescriptionModel;
0373     typedef ObjectDescriptionModel<VisualizationType> VisualizationDescriptionModel;*/
0374 
0375 }
0376 
0377 #endif //QT_NO_PHONON_OBJECTDESCRIPTIONMODEL
0378 
0379 
0380 #endif // PHONON_OBJECTDESCRIPTIONMODEL_H
0381 // vim: sw=4 ts=4 tw=80