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