[844] | 1 | /****************************************************************************
|
---|
| 2 | **
|
---|
| 3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
| 4 | ** All rights reserved.
|
---|
| 5 | ** Contact: Nokia Corporation (qt-info@nokia.com)
|
---|
| 6 | **
|
---|
| 7 | ** This file is part of the QtDeclarative module of the Qt Toolkit.
|
---|
| 8 | **
|
---|
| 9 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
| 10 | ** Commercial Usage
|
---|
| 11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
| 12 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
| 13 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
| 14 | ** a written agreement between you and Nokia.
|
---|
| 15 | **
|
---|
| 16 | ** GNU Lesser General Public License Usage
|
---|
| 17 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
---|
| 18 | ** General Public License version 2.1 as published by the Free Software
|
---|
| 19 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
---|
| 20 | ** packaging of this file. Please review the following information to
|
---|
| 21 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
---|
| 22 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
---|
| 23 | **
|
---|
| 24 | ** In addition, as a special exception, Nokia gives you certain additional
|
---|
| 25 | ** rights. These rights are described in the Nokia Qt LGPL Exception
|
---|
| 26 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
---|
| 27 | **
|
---|
| 28 | ** GNU General Public License Usage
|
---|
| 29 | ** Alternatively, this file may be used under the terms of the GNU
|
---|
| 30 | ** General Public License version 3.0 as published by the Free Software
|
---|
| 31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
| 32 | ** packaging of this file. Please review the following information to
|
---|
| 33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
---|
| 34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
---|
| 35 | **
|
---|
| 36 | ** If you have questions regarding the use of this file, please contact
|
---|
| 37 | ** Nokia at qt-info@nokia.com.
|
---|
| 38 | ** $QT_END_LICENSE$
|
---|
| 39 | **
|
---|
| 40 | ****************************************************************************/
|
---|
| 41 |
|
---|
| 42 | #include "private/qdeclarativepackage_p.h"
|
---|
| 43 |
|
---|
| 44 | #include <private/qobject_p.h>
|
---|
| 45 | #include <private/qdeclarativeguard_p.h>
|
---|
| 46 |
|
---|
| 47 | QT_BEGIN_NAMESPACE
|
---|
| 48 |
|
---|
| 49 | /*!
|
---|
| 50 | \qmlclass Package QDeclarativePackage
|
---|
| 51 | \ingroup qml-working-with-data
|
---|
| 52 | \brief Package provides a collection of named items.
|
---|
| 53 |
|
---|
| 54 | The Package class is used in conjunction with
|
---|
| 55 | VisualDataModel to enable delegates with a shared context
|
---|
| 56 | to be provided to multiple views.
|
---|
| 57 |
|
---|
| 58 | Any item within a Package may be assigned a name via the
|
---|
| 59 | \l{Package::name}{Package.name} attached property.
|
---|
| 60 |
|
---|
| 61 | The example below creates a Package containing two named items;
|
---|
| 62 | \e list and \e grid. The third element in the package (the \l Rectangle) is parented to whichever
|
---|
| 63 | delegate it should appear in. This allows an item to move
|
---|
| 64 | between views.
|
---|
| 65 |
|
---|
| 66 | \snippet examples/declarative/modelviews/package/Delegate.qml 0
|
---|
| 67 |
|
---|
| 68 | These named items are used as the delegates by the two views who
|
---|
| 69 | reference the special \l{VisualDataModel::parts} property to select
|
---|
| 70 | a model which provides the chosen delegate.
|
---|
| 71 |
|
---|
| 72 | \snippet examples/declarative/modelviews/package/view.qml 0
|
---|
| 73 |
|
---|
| 74 | \sa {declarative/modelviews/package}{Package example}, {demos/declarative/photoviewer}{Photo Viewer demo}, QtDeclarative
|
---|
| 75 | */
|
---|
| 76 |
|
---|
| 77 | /*!
|
---|
| 78 | \qmlattachedproperty string Package::name
|
---|
| 79 | This attached property holds the name of an item within a Package.
|
---|
| 80 | */
|
---|
| 81 |
|
---|
| 82 |
|
---|
| 83 | class QDeclarativePackagePrivate : public QObjectPrivate
|
---|
| 84 | {
|
---|
| 85 | public:
|
---|
| 86 | QDeclarativePackagePrivate() {}
|
---|
| 87 |
|
---|
| 88 | struct DataGuard : public QDeclarativeGuard<QObject>
|
---|
| 89 | {
|
---|
| 90 | DataGuard(QObject *obj, QList<DataGuard> *l) : list(l) { (QDeclarativeGuard<QObject>&)*this = obj; }
|
---|
| 91 | QList<DataGuard> *list;
|
---|
| 92 | void objectDestroyed(QObject *) {
|
---|
| 93 | // we assume priv will always be destroyed after objectDestroyed calls
|
---|
| 94 | list->removeOne(*this);
|
---|
| 95 | }
|
---|
| 96 | };
|
---|
| 97 |
|
---|
| 98 | QList<DataGuard> dataList;
|
---|
| 99 | static void data_append(QDeclarativeListProperty<QObject> *prop, QObject *o) {
|
---|
| 100 | QList<DataGuard> *list = static_cast<QList<DataGuard> *>(prop->data);
|
---|
| 101 | list->append(DataGuard(o, list));
|
---|
| 102 | }
|
---|
| 103 | static void data_clear(QDeclarativeListProperty<QObject> *prop) {
|
---|
| 104 | QList<DataGuard> *list = static_cast<QList<DataGuard> *>(prop->data);
|
---|
| 105 | list->clear();
|
---|
| 106 | }
|
---|
| 107 | static QObject *data_at(QDeclarativeListProperty<QObject> *prop, int index) {
|
---|
| 108 | QList<DataGuard> *list = static_cast<QList<DataGuard> *>(prop->data);
|
---|
| 109 | return list->at(index);
|
---|
| 110 | }
|
---|
| 111 | static int data_count(QDeclarativeListProperty<QObject> *prop) {
|
---|
| 112 | QList<DataGuard> *list = static_cast<QList<DataGuard> *>(prop->data);
|
---|
| 113 | return list->count();
|
---|
| 114 | }
|
---|
| 115 | };
|
---|
| 116 |
|
---|
| 117 | QHash<QObject *, QDeclarativePackageAttached *> QDeclarativePackageAttached::attached;
|
---|
| 118 |
|
---|
| 119 | QDeclarativePackageAttached::QDeclarativePackageAttached(QObject *parent)
|
---|
| 120 | : QObject(parent)
|
---|
| 121 | {
|
---|
| 122 | attached.insert(parent, this);
|
---|
| 123 | }
|
---|
| 124 |
|
---|
| 125 | QDeclarativePackageAttached::~QDeclarativePackageAttached()
|
---|
| 126 | {
|
---|
| 127 | attached.remove(parent());
|
---|
| 128 | }
|
---|
| 129 |
|
---|
| 130 | QString QDeclarativePackageAttached::name() const
|
---|
| 131 | {
|
---|
| 132 | return _name;
|
---|
| 133 | }
|
---|
| 134 |
|
---|
| 135 | void QDeclarativePackageAttached::setName(const QString &n)
|
---|
| 136 | {
|
---|
| 137 | _name = n;
|
---|
| 138 | }
|
---|
| 139 |
|
---|
| 140 | QDeclarativePackage::QDeclarativePackage(QObject *parent)
|
---|
| 141 | : QObject(*(new QDeclarativePackagePrivate), parent)
|
---|
| 142 | {
|
---|
| 143 | }
|
---|
| 144 |
|
---|
| 145 | QDeclarativePackage::~QDeclarativePackage()
|
---|
| 146 | {
|
---|
| 147 | Q_D(QDeclarativePackage);
|
---|
| 148 | for (int ii = 0; ii < d->dataList.count(); ++ii) {
|
---|
| 149 | QObject *obj = d->dataList.at(ii);
|
---|
| 150 | obj->setParent(this);
|
---|
| 151 | }
|
---|
| 152 | }
|
---|
| 153 |
|
---|
| 154 | QDeclarativeListProperty<QObject> QDeclarativePackage::data()
|
---|
| 155 | {
|
---|
| 156 | Q_D(QDeclarativePackage);
|
---|
| 157 | return QDeclarativeListProperty<QObject>(this, &d->dataList, QDeclarativePackagePrivate::data_append,
|
---|
| 158 | QDeclarativePackagePrivate::data_count,
|
---|
| 159 | QDeclarativePackagePrivate::data_at,
|
---|
| 160 | QDeclarativePackagePrivate::data_clear);
|
---|
| 161 | }
|
---|
| 162 |
|
---|
| 163 | bool QDeclarativePackage::hasPart(const QString &name)
|
---|
| 164 | {
|
---|
| 165 | Q_D(QDeclarativePackage);
|
---|
| 166 | for (int ii = 0; ii < d->dataList.count(); ++ii) {
|
---|
| 167 | QObject *obj = d->dataList.at(ii);
|
---|
| 168 | QDeclarativePackageAttached *a = QDeclarativePackageAttached::attached.value(obj);
|
---|
| 169 | if (a && a->name() == name)
|
---|
| 170 | return true;
|
---|
| 171 | }
|
---|
| 172 | return false;
|
---|
| 173 | }
|
---|
| 174 |
|
---|
| 175 | QObject *QDeclarativePackage::part(const QString &name)
|
---|
| 176 | {
|
---|
| 177 | Q_D(QDeclarativePackage);
|
---|
| 178 | if (name.isEmpty() && !d->dataList.isEmpty())
|
---|
| 179 | return d->dataList.at(0);
|
---|
| 180 |
|
---|
| 181 | for (int ii = 0; ii < d->dataList.count(); ++ii) {
|
---|
| 182 | QObject *obj = d->dataList.at(ii);
|
---|
| 183 | QDeclarativePackageAttached *a = QDeclarativePackageAttached::attached.value(obj);
|
---|
| 184 | if (a && a->name() == name)
|
---|
| 185 | return obj;
|
---|
| 186 | }
|
---|
| 187 |
|
---|
| 188 | if (name == QLatin1String("default") && !d->dataList.isEmpty())
|
---|
| 189 | return d->dataList.at(0);
|
---|
| 190 |
|
---|
| 191 | return 0;
|
---|
| 192 | }
|
---|
| 193 |
|
---|
| 194 | QDeclarativePackageAttached *QDeclarativePackage::qmlAttachedProperties(QObject *o)
|
---|
| 195 | {
|
---|
| 196 | return new QDeclarativePackageAttached(o);
|
---|
| 197 | }
|
---|
| 198 |
|
---|
| 199 |
|
---|
| 200 |
|
---|
| 201 | QT_END_NAMESPACE
|
---|