1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
|
/****************************************************************************
**
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the QtDeclarative module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** No Commercial Usage
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the Technology Preview License Agreement accompanying
** this package.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
**
**
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include "qdeclarativevmemetaobject_p.h"
#include "qdeclarative.h"
#include "qdeclarativerefcount_p.h"
#include "qdeclarativeexpression.h"
#include "qdeclarativeexpression_p.h"
#include "qdeclarativecontext_p.h"
#include <QColor>
#include <QDate>
#include <QtCore/qlist.h>
#include <QtCore/qdebug.h>
QT_BEGIN_NAMESPACE
QDeclarativeVMEMetaObject::QDeclarativeVMEMetaObject(QObject *obj,
const QMetaObject *other,
const QDeclarativeVMEMetaData *meta,
QDeclarativeCompiledData *cdata)
: object(obj), compiledData(cdata), ctxt(QDeclarativeDeclarativeData::get(obj)->outerContext),
metaData(meta), methods(0), parent(0)
{
compiledData->addref();
*static_cast<QMetaObject *>(this) = *other;
this->d.superdata = obj->metaObject();
QObjectPrivate *op = QObjectPrivate::get(obj);
if (op->metaObject)
parent = static_cast<QAbstractDynamicMetaObject*>(op->metaObject);
op->metaObject = this;
propOffset = QAbstractDynamicMetaObject::propertyOffset();
methodOffset = QAbstractDynamicMetaObject::methodOffset();
data = new QVariant[metaData->propertyCount];
aConnected.resize(metaData->aliasCount);
int list_type = qMetaTypeId<QDeclarativeListProperty<QObject> >();
// ### Optimize
for (int ii = 0; ii < metaData->propertyCount; ++ii) {
int t = (metaData->propertyData() + ii)->propertyType;
if (t == list_type) {
listProperties.append(new List(methodOffset + ii));
data[ii] = QVariant::fromValue(QDeclarativeListProperty<QObject>(obj, listProperties.last(), list_append,
list_count, list_at, list_clear));
} else if (t != -1) {
data[ii] = QVariant((QVariant::Type)t);
}
}
}
QDeclarativeVMEMetaObject::~QDeclarativeVMEMetaObject()
{
compiledData->release();
delete parent;
qDeleteAll(listProperties);
delete [] data;
delete [] methods;
}
int QDeclarativeVMEMetaObject::metaCall(QMetaObject::Call c, int _id, void **a)
{
int id = _id;
if(c == QMetaObject::WriteProperty) {
int flags = *reinterpret_cast<int*>(a[3]);
if (!(flags & QDeclarativePropertyPrivate::BypassInterceptor)
&& !aInterceptors.isEmpty()
&& aInterceptors.testBit(id)) {
QPair<int, QDeclarativePropertyValueInterceptor*> pair = interceptors.value(id);
int valueIndex = pair.first;
QDeclarativePropertyValueInterceptor *vi = pair.second;
int type = property(id).userType();
if (type != QVariant::Invalid) {
if (valueIndex != -1) {
QDeclarativeEnginePrivate *ep = ctxt?QDeclarativeEnginePrivate::get(ctxt->engine):0;
QDeclarativeValueType *valueType = 0;
if (ep) valueType = ep->valueTypes[type];
else valueType = QDeclarativeValueTypeFactory::valueType(type);
Q_ASSERT(valueType);
valueType->setValue(QVariant(type, a[0]));
QMetaProperty valueProp = valueType->metaObject()->property(valueIndex);
vi->write(valueProp.read(valueType));
if (!ep) delete valueType;
return -1;
} else {
vi->write(QVariant(type, a[0]));
return -1;
}
}
}
}
if(c == QMetaObject::ReadProperty || c == QMetaObject::WriteProperty) {
if (id >= propOffset) {
id -= propOffset;
if (id < metaData->propertyCount) {
int t = (metaData->propertyData() + id)->propertyType;
bool needActivate = false;
if (t == -1) {
if (c == QMetaObject::ReadProperty) {
*reinterpret_cast<QVariant *>(a[0]) = data[id];
} else if (c == QMetaObject::WriteProperty) {
needActivate =
(data[id] != *reinterpret_cast<QVariant *>(a[0]));
data[id] = *reinterpret_cast<QVariant *>(a[0]);
}
} else {
if (c == QMetaObject::ReadProperty) {
switch(t) {
case QVariant::Int:
*reinterpret_cast<int *>(a[0]) = data[id].toInt();
break;
case QVariant::Bool:
*reinterpret_cast<bool *>(a[0]) = data[id].toBool();
break;
case QVariant::Double:
*reinterpret_cast<double *>(a[0]) = data[id].toDouble();
break;
case QVariant::String:
*reinterpret_cast<QString *>(a[0]) = data[id].toString();
break;
case QVariant::Url:
*reinterpret_cast<QUrl *>(a[0]) = data[id].toUrl();
break;
case QVariant::Color:
*reinterpret_cast<QColor *>(a[0]) = data[id].value<QColor>();
break;
case QVariant::Date:
*reinterpret_cast<QDate *>(a[0]) = data[id].toDate();
break;
case QMetaType::QObjectStar:
*reinterpret_cast<QObject **>(a[0]) = data[id].value<QObject*>();
break;
default:
break;
}
if (t == qMetaTypeId<QDeclarativeListProperty<QObject> >()) {
*reinterpret_cast<QDeclarativeListProperty<QObject> *>(a[0]) =
data[id].value<QDeclarativeListProperty<QObject> >();
}
} else if (c == QMetaObject::WriteProperty) {
QVariant value = QVariant((QVariant::Type)data[id].type(), a[0]);
needActivate = (data[id] != value);
data[id] = value;
}
}
if (c == QMetaObject::WriteProperty && needActivate) {
activate(object, methodOffset + id, 0);
}
return -1;
}
id -= metaData->propertyCount;
if (id < metaData->aliasCount) {
QDeclarativeVMEMetaData::AliasData *d = metaData->aliasData() + id;
if (d->flags & QML_ALIAS_FLAG_PTR && c == QMetaObject::ReadProperty)
*reinterpret_cast<void **>(a[0]) = 0;
if (!ctxt) return -1;
QDeclarativeContext *context = ctxt->asQDeclarativeContext();
QDeclarativeContextPrivate *ctxtPriv = QDeclarativeContextPrivate::get(context);
QObject *target = ctxtPriv->data->idValues[d->contextIdx].data();
if (!target)
return -1;
if (c == QMetaObject::ReadProperty && !aConnected.testBit(id)) {
int sigIdx = methodOffset + id + metaData->propertyCount;
QMetaObject::connect(context, d->contextIdx + ctxtPriv->notifyIndex, object, sigIdx);
if (d->propertyIdx != -1) {
QMetaProperty prop =
target->metaObject()->property(d->propertyIdx);
if (prop.hasNotifySignal())
QMetaObject::connect(target, prop.notifySignalIndex(),
object, sigIdx);
}
aConnected.setBit(id);
}
if (d->propertyIdx == -1) {
*reinterpret_cast<QObject **>(a[0]) = target;
return -1;
} else {
return QMetaObject::metacall(target, c, d->propertyIdx, a);
}
}
return -1;
}
} else if(c == QMetaObject::InvokeMetaMethod) {
if (id >= methodOffset) {
id -= methodOffset;
int plainSignals = metaData->signalCount + metaData->propertyCount +
metaData->aliasCount;
if (id < plainSignals) {
QMetaObject::activate(object, _id, a);
return -1;
}
id -= plainSignals;
if (id < metaData->methodCount) {
if (!ctxt->engine)
return -1; // We can't run the method
QDeclarativeEnginePrivate *ep = QDeclarativeEnginePrivate::get(ctxt->engine);
QScriptValue function = method(id);
QScriptValueList args;
QDeclarativeVMEMetaData::MethodData *data = metaData->methodData() + id;
if (data->parameterCount) {
for (int ii = 0; ii < data->parameterCount; ++ii) {
args << ep->scriptValueFromVariant(*(QVariant *)a[ii + 1]);
}
}
QScriptValue rv = function.call(ep->objectClass->newQObject(object), args);
if (a[0]) *reinterpret_cast<QVariant *>(a[0]) = ep->scriptValueToVariant(rv);
return -1;
}
return -1;
}
}
if (parent)
return parent->metaCall(c, _id, a);
else
return object->qt_metacall(c, _id, a);
}
QScriptValue QDeclarativeVMEMetaObject::method(int index)
{
if (!methods)
methods = new QScriptValue[metaData->methodCount];
if (!methods[index].isValid()) {
QDeclarativeVMEMetaData::MethodData *data = metaData->methodData() + index;
const QChar *body =
(const QChar *)(((const char*)metaData) + data->bodyOffset);
QString code = QString::fromRawData(body, data->bodyLength);
// XXX Use QScriptProgram
// XXX We should evaluate all methods in a single big script block to
// improve the call time between dynamic methods defined on the same
// object
methods[index] = QDeclarativeExpressionPrivate::evalInObjectScope(ctxt, object, code);
}
return methods[index];
}
void QDeclarativeVMEMetaObject::listChanged(int id)
{
activate(object, methodOffset + id, 0);
}
void QDeclarativeVMEMetaObject::list_append(QDeclarativeListProperty<QObject> *prop, QObject *o)
{
List *list = static_cast<List *>(prop->data);
list->append(o);
QMetaObject::activate(prop->object, list->notifyIndex, 0);
}
int QDeclarativeVMEMetaObject::list_count(QDeclarativeListProperty<QObject> *prop)
{
return static_cast<List *>(prop->data)->count();
}
QObject *QDeclarativeVMEMetaObject::list_at(QDeclarativeListProperty<QObject> *prop, int index)
{
return static_cast<List *>(prop->data)->at(index);
}
void QDeclarativeVMEMetaObject::list_clear(QDeclarativeListProperty<QObject> *prop)
{
List *list = static_cast<List *>(prop->data);
list->clear();
QMetaObject::activate(prop->object, list->notifyIndex, 0);
}
void QDeclarativeVMEMetaObject::registerInterceptor(int index, int valueIndex, QDeclarativePropertyValueInterceptor *interceptor)
{
if (aInterceptors.isEmpty())
aInterceptors.resize(propertyCount() + metaData->propertyCount);
aInterceptors.setBit(index);
interceptors.insert(index, qMakePair(valueIndex, interceptor));
}
QScriptValue QDeclarativeVMEMetaObject::vmeMethod(int index)
{
if (index < methodOffset) {
Q_ASSERT(parent);
return static_cast<QDeclarativeVMEMetaObject *>(parent)->vmeMethod(index);
}
int plainSignals = metaData->signalCount + metaData->propertyCount + metaData->aliasCount;
Q_ASSERT(index >= (methodOffset + plainSignals) && index < (methodOffset + plainSignals + metaData->methodCount));
return method(index - methodOffset - plainSignals);
}
QT_END_NAMESPACE
|