source: trunk/src/declarative/qml/qdeclarativepropertycache.cpp@ 1147

Last change on this file since 1147 was 846, checked in by Dmitry A. Kuminov, 14 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

File size: 14.4 KB
Line 
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/qdeclarativepropertycache_p.h"
43
44#include "private/qdeclarativeengine_p.h"
45#include "private/qdeclarativebinding_p.h"
46#include <QtCore/qdebug.h>
47
48Q_DECLARE_METATYPE(QScriptValue)
49
50QT_BEGIN_NAMESPACE
51
52QDeclarativePropertyCache::Data::Flags QDeclarativePropertyCache::Data::flagsForProperty(const QMetaProperty &p, QDeclarativeEngine *engine)
53{
54 int propType = p.userType();
55
56 Flags flags;
57
58 if (p.isConstant())
59 flags |= Data::IsConstant;
60 if (p.isWritable())
61 flags |= Data::IsWritable;
62 if (p.isResettable())
63 flags |= Data::IsResettable;
64
65 if (propType == qMetaTypeId<QDeclarativeBinding *>()) {
66 flags |= Data::IsQmlBinding;
67 } else if (propType == qMetaTypeId<QScriptValue>()) {
68 flags |= Data::IsQScriptValue;
69 } else if (p.isEnumType()) {
70 flags |= Data::IsEnumType;
71 } else {
72 QDeclarativeMetaType::TypeCategory cat = engine ? QDeclarativeEnginePrivate::get(engine)->typeCategory(propType)
73 : QDeclarativeMetaType::typeCategory(propType);
74 if (cat == QDeclarativeMetaType::Object)
75 flags |= Data::IsQObjectDerived;
76 else if (cat == QDeclarativeMetaType::List)
77 flags |= Data::IsQList;
78 }
79
80 return flags;
81}
82
83void QDeclarativePropertyCache::Data::load(const QMetaProperty &p, QDeclarativeEngine *engine)
84{
85 propType = p.userType();
86 if (QVariant::Type(propType) == QVariant::LastType)
87 propType = qMetaTypeId<QVariant>();
88 coreIndex = p.propertyIndex();
89 notifyIndex = p.notifySignalIndex();
90 flags = flagsForProperty(p, engine);
91}
92
93void QDeclarativePropertyCache::Data::load(const QMetaMethod &m)
94{
95 coreIndex = m.methodIndex();
96 relatedIndex = -1;
97 flags |= Data::IsFunction;
98 if (m.methodType() == QMetaMethod::Signal)
99 flags |= Data::IsSignal;
100 propType = QVariant::Invalid;
101
102 const char *returnType = m.typeName();
103 if (returnType)
104 propType = QMetaType::type(returnType);
105
106 QList<QByteArray> params = m.parameterTypes();
107 if (!params.isEmpty())
108 flags |= Data::HasArguments;
109}
110
111
112/*!
113Creates a new empty QDeclarativePropertyCache.
114*/
115QDeclarativePropertyCache::QDeclarativePropertyCache(QDeclarativeEngine *e)
116: QDeclarativeCleanup(e), engine(e)
117{
118 Q_ASSERT(engine);
119}
120
121/*!
122Creates a new QDeclarativePropertyCache of \a metaObject.
123*/
124QDeclarativePropertyCache::QDeclarativePropertyCache(QDeclarativeEngine *e, const QMetaObject *metaObject)
125: QDeclarativeCleanup(e), engine(e)
126{
127 Q_ASSERT(engine);
128 Q_ASSERT(metaObject);
129
130 update(engine, metaObject);
131}
132
133QDeclarativePropertyCache::~QDeclarativePropertyCache()
134{
135 clear();
136}
137
138void QDeclarativePropertyCache::clear()
139{
140 for (int ii = 0; ii < indexCache.count(); ++ii) {
141 if (indexCache.at(ii)) indexCache.at(ii)->release();
142 }
143
144 for (int ii = 0; ii < methodIndexCache.count(); ++ii) {
145 RData *data = methodIndexCache.at(ii);
146 if (data) data->release();
147 }
148
149 for (StringCache::ConstIterator iter = stringCache.begin();
150 iter != stringCache.end(); ++iter) {
151 RData *data = (*iter);
152 data->release();
153 }
154
155 for (IdentifierCache::ConstIterator iter = identifierCache.begin();
156 iter != identifierCache.end(); ++iter) {
157 RData *data = (*iter);
158 data->release();
159 }
160
161 indexCache.clear();
162 methodIndexCache.clear();
163 stringCache.clear();
164 identifierCache.clear();
165}
166
167QDeclarativePropertyCache::Data QDeclarativePropertyCache::create(const QMetaObject *metaObject,
168 const QString &property)
169{
170 Q_ASSERT(metaObject);
171
172 QDeclarativePropertyCache::Data rv;
173 {
174 const QMetaObject *cmo = metaObject;
175 while (cmo) {
176 int idx = metaObject->indexOfProperty(property.toUtf8());
177 if (idx != -1) {
178 QMetaProperty p = metaObject->property(idx);
179 if (p.isScriptable()) {
180 rv.load(metaObject->property(idx));
181 return rv;
182 } else {
183 while (cmo && cmo->propertyOffset() >= idx)
184 cmo = cmo->superClass();
185 }
186 } else {
187 cmo = 0;
188 }
189 }
190 }
191
192 int methodCount = metaObject->methodCount();
193 for (int ii = methodCount - 1; ii >= 3; --ii) { // >=3 to block the destroyed signal and deleteLater() slot
194 QMetaMethod m = metaObject->method(ii);
195 if (m.access() == QMetaMethod::Private)
196 continue;
197 QString methodName = QString::fromUtf8(m.signature());
198
199 int parenIdx = methodName.indexOf(QLatin1Char('('));
200 Q_ASSERT(parenIdx != -1);
201 QStringRef methodNameRef = methodName.leftRef(parenIdx);
202
203 if (methodNameRef == property) {
204 rv.load(m);
205 return rv;
206 }
207 }
208
209 return rv;
210}
211
212QDeclarativePropertyCache *QDeclarativePropertyCache::copy() const
213{
214 QDeclarativePropertyCache *cache = new QDeclarativePropertyCache(engine);
215 cache->indexCache = indexCache;
216 cache->methodIndexCache = methodIndexCache;
217 cache->stringCache = stringCache;
218 cache->identifierCache = identifierCache;
219
220 for (int ii = 0; ii < indexCache.count(); ++ii) {
221 if (indexCache.at(ii)) indexCache.at(ii)->addref();
222 }
223 for (int ii = 0; ii < methodIndexCache.count(); ++ii) {
224 if (methodIndexCache.at(ii)) methodIndexCache.at(ii)->addref();
225 }
226 for (StringCache::ConstIterator iter = stringCache.begin(); iter != stringCache.end(); ++iter)
227 (*iter)->addref();
228 for (IdentifierCache::ConstIterator iter = identifierCache.begin(); iter != identifierCache.end(); ++iter)
229 (*iter)->addref();
230
231 return cache;
232}
233
234void QDeclarativePropertyCache::append(QDeclarativeEngine *engine, const QMetaObject *metaObject,
235 Data::Flag propertyFlags, Data::Flag methodFlags, Data::Flag signalFlags)
236{
237 QDeclarativeEnginePrivate *enginePriv = QDeclarativeEnginePrivate::get(engine);
238
239 int methodCount = metaObject->methodCount();
240 // 3 to block the destroyed signal and the deleteLater() slot
241 int methodOffset = qMax(3, metaObject->methodOffset());
242
243 methodIndexCache.resize(methodCount);
244 for (int ii = methodOffset; ii < methodCount; ++ii) {
245 QMetaMethod m = metaObject->method(ii);
246 if (m.access() == QMetaMethod::Private)
247 continue;
248 QString methodName = QString::fromUtf8(m.signature());
249
250 int parenIdx = methodName.indexOf(QLatin1Char('('));
251 Q_ASSERT(parenIdx != -1);
252 methodName = methodName.left(parenIdx);
253
254 RData *data = new RData;
255 data->identifier = enginePriv->objectClass->createPersistentIdentifier(methodName);
256 methodIndexCache[ii] = data;
257
258 data->load(m);
259 if (m.methodType() == QMetaMethod::Slot || m.methodType() == QMetaMethod::Method)
260 data->flags |= methodFlags;
261 else if (m.methodType() == QMetaMethod::Signal)
262 data->flags |= signalFlags;
263
264 if (stringCache.contains(methodName)) {
265 RData *old = stringCache[methodName];
266 // We only overload methods in the same class, exactly like C++
267 if (old->flags & Data::IsFunction && old->coreIndex >= methodOffset)
268 data->relatedIndex = old->coreIndex;
269 stringCache[methodName]->release();
270 identifierCache[data->identifier.identifier]->release();
271 }
272
273 stringCache.insert(methodName, data);
274 identifierCache.insert(data->identifier.identifier, data);
275 data->addref();
276 data->addref();
277 }
278
279 int propCount = metaObject->propertyCount();
280 int propOffset = metaObject->propertyOffset();
281
282 indexCache.resize(propCount);
283 for (int ii = propOffset; ii < propCount; ++ii) {
284 QMetaProperty p = metaObject->property(ii);
285 if (!p.isScriptable())
286 continue;
287
288 QString propName = QString::fromUtf8(p.name());
289
290 RData *data = new RData;
291 data->identifier = enginePriv->objectClass->createPersistentIdentifier(propName);
292 indexCache[ii] = data;
293
294 data->load(p, engine);
295 data->flags |= propertyFlags;
296
297 if (stringCache.contains(propName)) {
298 stringCache[propName]->release();
299 identifierCache[data->identifier.identifier]->release();
300 }
301
302 stringCache.insert(propName, data);
303 identifierCache.insert(data->identifier.identifier, data);
304 data->addref();
305 data->addref();
306 }
307}
308
309void QDeclarativePropertyCache::updateRecur(QDeclarativeEngine *engine, const QMetaObject *metaObject)
310{
311 if (!metaObject)
312 return;
313
314 updateRecur(engine, metaObject->superClass());
315
316 append(engine, metaObject);
317}
318
319void QDeclarativePropertyCache::update(QDeclarativeEngine *engine, const QMetaObject *metaObject)
320{
321 Q_ASSERT(engine);
322 Q_ASSERT(metaObject);
323
324 clear();
325
326 // Optimization to prevent unnecessary reallocation of lists
327 indexCache.reserve(metaObject->propertyCount());
328 methodIndexCache.reserve(metaObject->methodCount());
329
330 updateRecur(engine,metaObject);
331}
332
333QDeclarativePropertyCache::Data *
334QDeclarativePropertyCache::property(int index) const
335{
336 if (index < 0 || index >= indexCache.count())
337 return 0;
338
339 return indexCache.at(index);
340}
341
342QDeclarativePropertyCache::Data *
343QDeclarativePropertyCache::method(int index) const
344{
345 if (index < 0 || index >= methodIndexCache.count())
346 return 0;
347
348 return methodIndexCache.at(index);
349}
350
351QDeclarativePropertyCache::Data *
352QDeclarativePropertyCache::property(const QString &str) const
353{
354 return stringCache.value(str);
355}
356
357QString QDeclarativePropertyCache::Data::name(QObject *object)
358{
359 if (!object)
360 return QString();
361
362 return name(object->metaObject());
363}
364
365QString QDeclarativePropertyCache::Data::name(const QMetaObject *metaObject)
366{
367 if (!metaObject || coreIndex == -1)
368 return QString();
369
370 if (flags & IsFunction) {
371 QMetaMethod m = metaObject->method(coreIndex);
372
373 QString name = QString::fromUtf8(m.signature());
374 int parenIdx = name.indexOf(QLatin1Char('('));
375 if (parenIdx != -1)
376 name = name.left(parenIdx);
377 return name;
378 } else {
379 QMetaProperty p = metaObject->property(coreIndex);
380 return QString::fromUtf8(p.name());
381 }
382}
383
384QStringList QDeclarativePropertyCache::propertyNames() const
385{
386 return stringCache.keys();
387}
388
389QDeclarativePropertyCache::Data *QDeclarativePropertyCache::property(QDeclarativeEngine *engine, QObject *obj,
390 const QScriptDeclarativeClass::Identifier &name, Data &local)
391{
392 QDeclarativePropertyCache::Data *rv = 0;
393
394 QDeclarativeEnginePrivate *enginePrivate = QDeclarativeEnginePrivate::get(engine);
395
396 QDeclarativePropertyCache *cache = 0;
397 QDeclarativeData *ddata = QDeclarativeData::get(obj);
398 if (ddata && ddata->propertyCache && ddata->propertyCache->qmlEngine() == engine)
399 cache = ddata->propertyCache;
400 if (!cache) {
401 cache = enginePrivate->cache(obj);
402 if (cache && ddata && !ddata->propertyCache) { cache->addref(); ddata->propertyCache = cache; }
403 }
404
405 if (cache) {
406 rv = cache->property(name);
407 } else {
408 local = QDeclarativePropertyCache::create(obj->metaObject(), enginePrivate->objectClass->toString(name));
409 if (local.isValid())
410 rv = &local;
411 }
412
413 return rv;
414}
415
416QDeclarativePropertyCache::Data *QDeclarativePropertyCache::property(QDeclarativeEngine *engine, QObject *obj,
417 const QString &name, Data &local)
418{
419 QDeclarativePropertyCache::Data *rv = 0;
420
421 if (!engine) {
422 local = QDeclarativePropertyCache::create(obj->metaObject(), name);
423 if (local.isValid())
424 rv = &local;
425 } else {
426 QDeclarativeEnginePrivate *enginePrivate = QDeclarativeEnginePrivate::get(engine);
427
428 QDeclarativePropertyCache *cache = 0;
429 QDeclarativeData *ddata = QDeclarativeData::get(obj);
430 if (ddata && ddata->propertyCache && ddata->propertyCache->qmlEngine() == engine)
431 cache = ddata->propertyCache;
432 if (!cache) {
433 cache = enginePrivate->cache(obj);
434 if (cache && ddata && !ddata->propertyCache) { cache->addref(); ddata->propertyCache = cache; }
435 }
436
437 if (cache) {
438 rv = cache->property(name);
439 } else {
440 local = QDeclarativePropertyCache::create(obj->metaObject(), name);
441 if (local.isValid())
442 rv = &local;
443 }
444 }
445
446 return rv;
447}
448
449QT_END_NAMESPACE
Note: See TracBrowser for help on using the repository browser.