|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2010 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 ** No Commercial Usage |
|
11 ** This file contains pre-release code and may not be distributed. |
|
12 ** You may use this file in accordance with the terms and conditions |
|
13 ** contained in the Technology Preview License Agreement accompanying |
|
14 ** this package. |
|
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 ** If you have questions regarding the use of this file, please contact |
|
29 ** Nokia at qt-info@nokia.com. |
|
30 ** |
|
31 ** |
|
32 ** |
|
33 ** |
|
34 ** |
|
35 ** |
|
36 ** |
|
37 ** |
|
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 \brief Package provides a collection of named items |
|
52 |
|
53 The Package class is currently used in conjunction with |
|
54 VisualDataModel to enable delegates with a shared context |
|
55 to be provided to multiple views. |
|
56 |
|
57 Any item within a Package may be assigned a name via the |
|
58 \e {Package.name} attached property. |
|
59 |
|
60 The example below creates a Package containing two named items; |
|
61 \e list and \e grid. The third element in the package is parented to whichever |
|
62 delegate it should appear in. This allows an item to move |
|
63 between views. |
|
64 |
|
65 \snippet examples/declarative/modelviews/package/Delegate.qml 0 |
|
66 |
|
67 These named items are used as the delegates by the two views who |
|
68 reference the special VisualDataModel.parts property to select |
|
69 a model which provides the chosen delegate. |
|
70 |
|
71 \snippet examples/declarative/modelviews/package/view.qml 0 |
|
72 |
|
73 \sa QtDeclarative |
|
74 */ |
|
75 |
|
76 |
|
77 class QDeclarativePackagePrivate : public QObjectPrivate |
|
78 { |
|
79 public: |
|
80 QDeclarativePackagePrivate() {} |
|
81 |
|
82 struct DataGuard : public QDeclarativeGuard<QObject> |
|
83 { |
|
84 DataGuard(QObject *obj, QList<DataGuard> *l) : list(l) { (QDeclarativeGuard<QObject>&)*this = obj; } |
|
85 QList<DataGuard> *list; |
|
86 void objectDestroyed(QObject *) { |
|
87 // we assume priv will always be destroyed after objectDestroyed calls |
|
88 list->removeOne(*this); |
|
89 } |
|
90 }; |
|
91 |
|
92 QList<DataGuard> dataList; |
|
93 static void data_append(QDeclarativeListProperty<QObject> *prop, QObject *o) { |
|
94 QList<DataGuard> *list = static_cast<QList<DataGuard> *>(prop->data); |
|
95 list->append(DataGuard(o, list)); |
|
96 } |
|
97 static void data_clear(QDeclarativeListProperty<QObject> *prop) { |
|
98 QList<DataGuard> *list = static_cast<QList<DataGuard> *>(prop->data); |
|
99 list->clear(); |
|
100 } |
|
101 static QObject *data_at(QDeclarativeListProperty<QObject> *prop, int index) { |
|
102 QList<DataGuard> *list = static_cast<QList<DataGuard> *>(prop->data); |
|
103 return list->at(index); |
|
104 } |
|
105 static int data_count(QDeclarativeListProperty<QObject> *prop) { |
|
106 QList<DataGuard> *list = static_cast<QList<DataGuard> *>(prop->data); |
|
107 return list->count(); |
|
108 } |
|
109 }; |
|
110 |
|
111 QHash<QObject *, QDeclarativePackageAttached *> QDeclarativePackageAttached::attached; |
|
112 |
|
113 QDeclarativePackageAttached::QDeclarativePackageAttached(QObject *parent) |
|
114 : QObject(parent) |
|
115 { |
|
116 attached.insert(parent, this); |
|
117 } |
|
118 |
|
119 QDeclarativePackageAttached::~QDeclarativePackageAttached() |
|
120 { |
|
121 attached.remove(parent()); |
|
122 } |
|
123 |
|
124 QString QDeclarativePackageAttached::name() const |
|
125 { |
|
126 return _name; |
|
127 } |
|
128 |
|
129 void QDeclarativePackageAttached::setName(const QString &n) |
|
130 { |
|
131 _name = n; |
|
132 } |
|
133 |
|
134 QDeclarativePackage::QDeclarativePackage(QObject *parent) |
|
135 : QObject(*(new QDeclarativePackagePrivate), parent) |
|
136 { |
|
137 } |
|
138 |
|
139 QDeclarativePackage::~QDeclarativePackage() |
|
140 { |
|
141 Q_D(QDeclarativePackage); |
|
142 for (int ii = 0; ii < d->dataList.count(); ++ii) { |
|
143 QObject *obj = d->dataList.at(ii); |
|
144 obj->setParent(this); |
|
145 } |
|
146 } |
|
147 |
|
148 QDeclarativeListProperty<QObject> QDeclarativePackage::data() |
|
149 { |
|
150 Q_D(QDeclarativePackage); |
|
151 return QDeclarativeListProperty<QObject>(this, &d->dataList, QDeclarativePackagePrivate::data_append, |
|
152 QDeclarativePackagePrivate::data_count, |
|
153 QDeclarativePackagePrivate::data_at, |
|
154 QDeclarativePackagePrivate::data_clear); |
|
155 } |
|
156 |
|
157 bool QDeclarativePackage::hasPart(const QString &name) |
|
158 { |
|
159 Q_D(QDeclarativePackage); |
|
160 for (int ii = 0; ii < d->dataList.count(); ++ii) { |
|
161 QObject *obj = d->dataList.at(ii); |
|
162 QDeclarativePackageAttached *a = QDeclarativePackageAttached::attached.value(obj); |
|
163 if (a && a->name() == name) |
|
164 return true; |
|
165 } |
|
166 return false; |
|
167 } |
|
168 |
|
169 QObject *QDeclarativePackage::part(const QString &name) |
|
170 { |
|
171 Q_D(QDeclarativePackage); |
|
172 if (name.isEmpty() && !d->dataList.isEmpty()) |
|
173 return d->dataList.at(0); |
|
174 |
|
175 for (int ii = 0; ii < d->dataList.count(); ++ii) { |
|
176 QObject *obj = d->dataList.at(ii); |
|
177 QDeclarativePackageAttached *a = QDeclarativePackageAttached::attached.value(obj); |
|
178 if (a && a->name() == name) |
|
179 return obj; |
|
180 } |
|
181 |
|
182 if (name == QLatin1String("default") && !d->dataList.isEmpty()) |
|
183 return d->dataList.at(0); |
|
184 |
|
185 return 0; |
|
186 } |
|
187 |
|
188 QDeclarativePackageAttached *QDeclarativePackage::qmlAttachedProperties(QObject *o) |
|
189 { |
|
190 return new QDeclarativePackageAttached(o); |
|
191 } |
|
192 |
|
193 |
|
194 |
|
195 QT_END_NAMESPACE |