|
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 "qdeclarativecomponent.h" |
|
43 #include "private/qdeclarativecomponent_p.h" |
|
44 |
|
45 #include "private/qdeclarativecompiler_p.h" |
|
46 #include "private/qdeclarativecontext_p.h" |
|
47 #include "private/qdeclarativecompositetypedata_p.h" |
|
48 #include "private/qdeclarativeengine_p.h" |
|
49 #include "private/qdeclarativevme_p.h" |
|
50 #include "qdeclarative.h" |
|
51 #include "qdeclarativeengine.h" |
|
52 #include "private/qdeclarativebinding_p.h" |
|
53 #include "private/qdeclarativebinding_p_p.h" |
|
54 #include "private/qdeclarativeglobal_p.h" |
|
55 #include "private/qdeclarativescriptparser_p.h" |
|
56 |
|
57 #include <QStack> |
|
58 #include <QStringList> |
|
59 #include <QFileInfo> |
|
60 #include <QtCore/qdebug.h> |
|
61 #include <QApplication> |
|
62 #include <QGraphicsObject> |
|
63 |
|
64 QT_BEGIN_NAMESPACE |
|
65 |
|
66 class QByteArray; |
|
67 |
|
68 /*! |
|
69 \class QDeclarativeComponent |
|
70 \since 4.7 |
|
71 \brief The QDeclarativeComponent class encapsulates a QML component description. |
|
72 \mainclass |
|
73 */ |
|
74 |
|
75 /*! |
|
76 \qmlclass Component QDeclarativeComponent |
|
77 \since 4.7 |
|
78 \brief The Component element encapsulates a QML component description. |
|
79 |
|
80 Components are reusable, encapsulated Qml element with a well-defined interface. |
|
81 They are often defined in \l {qdeclarativedocuments.html}{Component Files}. |
|
82 |
|
83 The \e Component element allows defining components within a QML file. |
|
84 This can be useful for reusing a small component within a single QML |
|
85 file, or for defining a component that logically belongs with the |
|
86 file containing it. |
|
87 |
|
88 \qml |
|
89 Item { |
|
90 Component { |
|
91 id: redSquare |
|
92 Rectangle { |
|
93 color: "red" |
|
94 width: 10 |
|
95 height: 10 |
|
96 } |
|
97 } |
|
98 Loader { sourceComponent: redSquare } |
|
99 Loader { sourceComponent: redSquare; x: 20 } |
|
100 } |
|
101 \endqml |
|
102 */ |
|
103 |
|
104 /*! |
|
105 \qmlattachedsignal Component::onCompleted() |
|
106 |
|
107 Emitted after component "startup" has completed. This can be used to |
|
108 execute script code at startup, once the full QML environment has been |
|
109 established. |
|
110 |
|
111 The \c {Component::onCompleted} attached property can be applied to |
|
112 any element. The order of running the \c onCompleted scripts is |
|
113 undefined. |
|
114 |
|
115 \qml |
|
116 Rectangle { |
|
117 Component.onCompleted: console.log("Completed Running!") |
|
118 Rectangle { |
|
119 Component.onCompleted: console.log("Nested Completed Running!") |
|
120 } |
|
121 } |
|
122 \endqml |
|
123 */ |
|
124 |
|
125 /*! |
|
126 \qmlattachedsignal Component::onDestruction() |
|
127 |
|
128 Emitted as the component begins destruction. This can be used to undo |
|
129 work done in the onCompleted signal, or other imperative code in your |
|
130 application. |
|
131 |
|
132 The \c {Component::onDestruction} attached property can be applied to |
|
133 any element. However, it applies to the destruction of the component as |
|
134 a whole, and not the destruction of the specific object. The order of |
|
135 running the \c onDestruction scripts is undefined. |
|
136 |
|
137 \qml |
|
138 Rectangle { |
|
139 Component.onDestruction: console.log("Destruction Beginning!") |
|
140 Rectangle { |
|
141 Component.onDestruction: console.log("Nested Destruction Beginning!") |
|
142 } |
|
143 } |
|
144 \endqml |
|
145 |
|
146 \sa QtDeclarative |
|
147 */ |
|
148 |
|
149 /*! |
|
150 \enum QDeclarativeComponent::Status |
|
151 |
|
152 Specifies the loading status of the QDeclarativeComponent. |
|
153 |
|
154 \value Null This QDeclarativeComponent has no data. Call loadUrl() or setData() to add QML content. |
|
155 \value Ready This QDeclarativeComponent is ready and create() may be called. |
|
156 \value Loading This QDeclarativeComponent is loading network data. |
|
157 \value Error An error has occured. Call errors() to retrieve a list of \{QDeclarativeError}{errors}. |
|
158 */ |
|
159 |
|
160 void QDeclarativeComponentPrivate::typeDataReady() |
|
161 { |
|
162 Q_Q(QDeclarativeComponent); |
|
163 |
|
164 Q_ASSERT(typeData); |
|
165 |
|
166 fromTypeData(typeData); |
|
167 typeData = 0; |
|
168 |
|
169 emit q->statusChanged(q->status()); |
|
170 } |
|
171 |
|
172 void QDeclarativeComponentPrivate::updateProgress(qreal p) |
|
173 { |
|
174 Q_Q(QDeclarativeComponent); |
|
175 |
|
176 progress = p; |
|
177 emit q->progressChanged(p); |
|
178 } |
|
179 |
|
180 void QDeclarativeComponentPrivate::fromTypeData(QDeclarativeCompositeTypeData *data) |
|
181 { |
|
182 url = data->imports.baseUrl(); |
|
183 QDeclarativeCompiledData *c = data->toCompiledComponent(engine); |
|
184 |
|
185 if (!c) { |
|
186 Q_ASSERT(data->status == QDeclarativeCompositeTypeData::Error); |
|
187 |
|
188 state.errors = data->errors; |
|
189 |
|
190 } else { |
|
191 |
|
192 cc = c; |
|
193 |
|
194 } |
|
195 |
|
196 data->release(); |
|
197 } |
|
198 |
|
199 void QDeclarativeComponentPrivate::clear() |
|
200 { |
|
201 if (typeData) { |
|
202 typeData->remWaiter(this); |
|
203 typeData->release(); |
|
204 typeData = 0; |
|
205 } |
|
206 |
|
207 if (cc) { |
|
208 cc->release(); |
|
209 cc = 0; |
|
210 } |
|
211 } |
|
212 |
|
213 /*! |
|
214 \internal |
|
215 */ |
|
216 QDeclarativeComponent::QDeclarativeComponent(QObject *parent) |
|
217 : QObject(*(new QDeclarativeComponentPrivate), parent) |
|
218 { |
|
219 } |
|
220 |
|
221 /*! |
|
222 Destruct the QDeclarativeComponent. |
|
223 */ |
|
224 QDeclarativeComponent::~QDeclarativeComponent() |
|
225 { |
|
226 Q_D(QDeclarativeComponent); |
|
227 |
|
228 if (d->state.completePending) { |
|
229 qWarning("QDeclarativeComponent: Component destroyed while completion pending"); |
|
230 d->completeCreate(); |
|
231 } |
|
232 |
|
233 if (d->typeData) { |
|
234 d->typeData->remWaiter(d); |
|
235 d->typeData->release(); |
|
236 } |
|
237 if (d->cc) |
|
238 d->cc->release(); |
|
239 } |
|
240 |
|
241 /*! |
|
242 \qmlproperty enumeration Component::status |
|
243 This property holds the status of component loading. It can be one of: |
|
244 \list |
|
245 \o Component.Null - no data is available for the component |
|
246 \o Component.Ready - the component has been loaded, and can be used to create instances. |
|
247 \o Component.Loading - the component is currently being loaded |
|
248 \o Component.Error - an error occurred while loading the component. |
|
249 Calling errorString() will provide a human-readable description of any errors. |
|
250 \endlist |
|
251 */ |
|
252 |
|
253 /*! |
|
254 \property QDeclarativeComponent::status |
|
255 The component's current \l{QDeclarativeComponent::Status} {status}. |
|
256 */ |
|
257 QDeclarativeComponent::Status QDeclarativeComponent::status() const |
|
258 { |
|
259 Q_D(const QDeclarativeComponent); |
|
260 |
|
261 if (d->typeData) |
|
262 return Loading; |
|
263 else if (!d->state.errors.isEmpty()) |
|
264 return Error; |
|
265 else if (d->engine && d->cc) |
|
266 return Ready; |
|
267 else |
|
268 return Null; |
|
269 } |
|
270 |
|
271 /*! |
|
272 Returns true if status() == QDeclarativeComponent::Null. |
|
273 */ |
|
274 bool QDeclarativeComponent::isNull() const |
|
275 { |
|
276 return status() == Null; |
|
277 } |
|
278 |
|
279 /*! |
|
280 Returns true if status() == QDeclarativeComponent::Ready. |
|
281 */ |
|
282 bool QDeclarativeComponent::isReady() const |
|
283 { |
|
284 return status() == Ready; |
|
285 } |
|
286 |
|
287 /*! |
|
288 Returns true if status() == QDeclarativeComponent::Error. |
|
289 */ |
|
290 bool QDeclarativeComponent::isError() const |
|
291 { |
|
292 return status() == Error; |
|
293 } |
|
294 |
|
295 /*! |
|
296 Returns true if status() == QDeclarativeComponent::Loading. |
|
297 */ |
|
298 bool QDeclarativeComponent::isLoading() const |
|
299 { |
|
300 return status() == Loading; |
|
301 } |
|
302 |
|
303 /*! |
|
304 \qmlproperty real Component::progress |
|
305 The progress of loading the component, from 0.0 (nothing loaded) |
|
306 to 1.0 (finished). |
|
307 */ |
|
308 |
|
309 /*! |
|
310 \property QDeclarativeComponent::progress |
|
311 The progress of loading the component, from 0.0 (nothing loaded) |
|
312 to 1.0 (finished). |
|
313 */ |
|
314 qreal QDeclarativeComponent::progress() const |
|
315 { |
|
316 Q_D(const QDeclarativeComponent); |
|
317 return d->progress; |
|
318 } |
|
319 |
|
320 /*! |
|
321 \fn void QDeclarativeComponent::progressChanged(qreal progress) |
|
322 |
|
323 Emitted whenever the component's loading progress changes. \a progress will be the |
|
324 current progress between 0.0 (nothing loaded) and 1.0 (finished). |
|
325 */ |
|
326 |
|
327 /*! |
|
328 \fn void QDeclarativeComponent::statusChanged(QDeclarativeComponent::Status status) |
|
329 |
|
330 Emitted whenever the component's status changes. \a status will be the |
|
331 new status. |
|
332 */ |
|
333 |
|
334 /*! |
|
335 Create a QDeclarativeComponent with no data and give it the specified |
|
336 \a engine and \a parent. Set the data with setData(). |
|
337 */ |
|
338 QDeclarativeComponent::QDeclarativeComponent(QDeclarativeEngine *engine, QObject *parent) |
|
339 : QObject(*(new QDeclarativeComponentPrivate), parent) |
|
340 { |
|
341 Q_D(QDeclarativeComponent); |
|
342 d->engine = engine; |
|
343 } |
|
344 |
|
345 /*! |
|
346 Create a QDeclarativeComponent from the given \a url and give it the |
|
347 specified \a parent and \a engine. |
|
348 |
|
349 Ensure that the URL provided is full and correct, in particular, use |
|
350 \l QUrl::fromLocalFile() when loading a file from the local filesystem. |
|
351 |
|
352 \sa loadUrl() |
|
353 */ |
|
354 QDeclarativeComponent::QDeclarativeComponent(QDeclarativeEngine *engine, const QUrl &url, QObject *parent) |
|
355 : QObject(*(new QDeclarativeComponentPrivate), parent) |
|
356 { |
|
357 Q_D(QDeclarativeComponent); |
|
358 d->engine = engine; |
|
359 loadUrl(url); |
|
360 } |
|
361 |
|
362 /*! |
|
363 Create a QDeclarativeComponent from the given \a fileName and give it the specified |
|
364 \a parent and \a engine. |
|
365 |
|
366 \sa loadUrl() |
|
367 */ |
|
368 QDeclarativeComponent::QDeclarativeComponent(QDeclarativeEngine *engine, const QString &fileName, |
|
369 QObject *parent) |
|
370 : QObject(*(new QDeclarativeComponentPrivate), parent) |
|
371 { |
|
372 Q_D(QDeclarativeComponent); |
|
373 d->engine = engine; |
|
374 loadUrl(d->engine->baseUrl().resolved(QUrl::fromLocalFile(fileName))); |
|
375 } |
|
376 |
|
377 /*! |
|
378 \internal |
|
379 */ |
|
380 QDeclarativeComponent::QDeclarativeComponent(QDeclarativeEngine *engine, QDeclarativeCompiledData *cc, int start, int count, QObject *parent) |
|
381 : QObject(*(new QDeclarativeComponentPrivate), parent) |
|
382 { |
|
383 Q_D(QDeclarativeComponent); |
|
384 d->engine = engine; |
|
385 d->cc = cc; |
|
386 cc->addref(); |
|
387 d->start = start; |
|
388 d->count = count; |
|
389 d->url = cc->url; |
|
390 d->progress = 1.0; |
|
391 } |
|
392 |
|
393 /*! |
|
394 Sets the QDeclarativeComponent to use the given QML \a data. If \a url |
|
395 is provided, it is used to set the component name and to provide |
|
396 a base path for items resolved by this component. |
|
397 */ |
|
398 void QDeclarativeComponent::setData(const QByteArray &data, const QUrl &url) |
|
399 { |
|
400 Q_D(QDeclarativeComponent); |
|
401 |
|
402 d->clear(); |
|
403 |
|
404 d->url = url; |
|
405 |
|
406 QDeclarativeCompositeTypeData *typeData = |
|
407 QDeclarativeEnginePrivate::get(d->engine)->typeManager.getImmediate(data, url); |
|
408 |
|
409 if (typeData->status == QDeclarativeCompositeTypeData::Waiting |
|
410 || typeData->status == QDeclarativeCompositeTypeData::WaitingResources) |
|
411 { |
|
412 d->typeData = typeData; |
|
413 d->typeData->addWaiter(d); |
|
414 |
|
415 } else { |
|
416 |
|
417 d->fromTypeData(typeData); |
|
418 |
|
419 } |
|
420 |
|
421 d->progress = 1.0; |
|
422 emit statusChanged(status()); |
|
423 emit progressChanged(d->progress); |
|
424 } |
|
425 |
|
426 /*! |
|
427 Returns the QDeclarativeContext the component was created in. This is only |
|
428 valid for components created directly from QML. |
|
429 */ |
|
430 QDeclarativeContext *QDeclarativeComponent::creationContext() const |
|
431 { |
|
432 Q_D(const QDeclarativeComponent); |
|
433 if(d->creationContext) |
|
434 return d->creationContext->asQDeclarativeContext(); |
|
435 |
|
436 return qmlContext(this); |
|
437 } |
|
438 |
|
439 /*! |
|
440 Load the QDeclarativeComponent from the provided \a url. |
|
441 |
|
442 Ensure that the URL provided is full and correct, in particular, use |
|
443 \l QUrl::fromLocalFile() when loading a file from the local filesystem. |
|
444 */ |
|
445 void QDeclarativeComponent::loadUrl(const QUrl &url) |
|
446 { |
|
447 Q_D(QDeclarativeComponent); |
|
448 |
|
449 d->clear(); |
|
450 |
|
451 if (url.isRelative() && !url.isEmpty()) |
|
452 d->url = d->engine->baseUrl().resolved(url); |
|
453 else |
|
454 d->url = url; |
|
455 |
|
456 if (url.isEmpty()) { |
|
457 QDeclarativeError error; |
|
458 error.setDescription(tr("Invalid empty URL")); |
|
459 d->state.errors << error; |
|
460 return; |
|
461 } |
|
462 |
|
463 QDeclarativeCompositeTypeData *data = |
|
464 QDeclarativeEnginePrivate::get(d->engine)->typeManager.get(d->url); |
|
465 |
|
466 if (data->status == QDeclarativeCompositeTypeData::Waiting |
|
467 || data->status == QDeclarativeCompositeTypeData::WaitingResources) |
|
468 { |
|
469 d->typeData = data; |
|
470 d->typeData->addWaiter(d); |
|
471 d->progress = data->progress; |
|
472 } else { |
|
473 d->fromTypeData(data); |
|
474 d->progress = 1.0; |
|
475 } |
|
476 |
|
477 emit statusChanged(status()); |
|
478 emit progressChanged(d->progress); |
|
479 } |
|
480 |
|
481 /*! |
|
482 Return the list of errors that occured during the last compile or create |
|
483 operation. An empty list is returned if isError() is not set. |
|
484 */ |
|
485 QList<QDeclarativeError> QDeclarativeComponent::errors() const |
|
486 { |
|
487 Q_D(const QDeclarativeComponent); |
|
488 if (isError()) |
|
489 return d->state.errors; |
|
490 else |
|
491 return QList<QDeclarativeError>(); |
|
492 } |
|
493 |
|
494 /*! |
|
495 \qmlmethod string Component::errorString() |
|
496 |
|
497 Returns a human-readable description of any errors. |
|
498 |
|
499 The string includes the file, location, and description of each error. |
|
500 If multiple errors are present they are separated by a newline character. |
|
501 |
|
502 If no errors are present, an empty string is returned. |
|
503 */ |
|
504 |
|
505 /*! |
|
506 \internal |
|
507 errorString is only meant as a way to get the errors in script |
|
508 */ |
|
509 QString QDeclarativeComponent::errorString() const |
|
510 { |
|
511 Q_D(const QDeclarativeComponent); |
|
512 QString ret; |
|
513 if(!isError()) |
|
514 return ret; |
|
515 foreach(const QDeclarativeError &e, d->state.errors) { |
|
516 ret += e.url().toString() + QLatin1Char(':') + |
|
517 QString::number(e.line()) + QLatin1Char(' ') + |
|
518 e.description() + QLatin1Char('\n'); |
|
519 } |
|
520 return ret; |
|
521 } |
|
522 |
|
523 /*! |
|
524 \qmlproperty url Component::url |
|
525 The component URL. This is the URL that was used to construct the component. |
|
526 */ |
|
527 |
|
528 /*! |
|
529 \property QDeclarativeComponent::url |
|
530 The component URL. This is the URL passed to either the constructor, |
|
531 or the loadUrl() or setData() methods. |
|
532 */ |
|
533 QUrl QDeclarativeComponent::url() const |
|
534 { |
|
535 Q_D(const QDeclarativeComponent); |
|
536 return d->url; |
|
537 } |
|
538 |
|
539 /*! |
|
540 \internal |
|
541 */ |
|
542 QDeclarativeComponent::QDeclarativeComponent(QDeclarativeComponentPrivate &dd, QObject *parent) |
|
543 : QObject(dd, parent) |
|
544 { |
|
545 } |
|
546 |
|
547 /*! |
|
548 \qmlmethod object Component::createObject(parent) |
|
549 Returns an object instance from this component, or null if object creation fails. |
|
550 |
|
551 The object will be created in the same context as the one in which the component |
|
552 was created. This function will always return null when called on components |
|
553 which were not created in QML. |
|
554 |
|
555 Note that if the returned object is to be displayed, its \c parent must be set to |
|
556 an existing item in a scene, or else the object will not be visible. The parent |
|
557 argument is required to help you avoid this, you must explicitly pass in null if |
|
558 you wish to create an object without setting a parent. |
|
559 */ |
|
560 |
|
561 /*! |
|
562 \internal |
|
563 A version of create which returns a scriptObject, for use in script. |
|
564 This function will only work on components created in QML. |
|
565 |
|
566 Sets graphics object parent because forgetting to do this is a frequent |
|
567 and serious problem. |
|
568 */ |
|
569 QScriptValue QDeclarativeComponent::createObject(QObject* parent) |
|
570 { |
|
571 Q_D(QDeclarativeComponent); |
|
572 QDeclarativeContext* ctxt = creationContext(); |
|
573 if(!ctxt) |
|
574 return QScriptValue(QScriptValue::NullValue); |
|
575 QObject* ret = create(ctxt); |
|
576 if (!ret) |
|
577 return QScriptValue(QScriptValue::NullValue); |
|
578 |
|
579 QGraphicsObject* gobj = qobject_cast<QGraphicsObject*>(ret); |
|
580 bool needParent = (gobj != 0); |
|
581 if(parent){ |
|
582 ret->setParent(parent); |
|
583 if (gobj) { |
|
584 QGraphicsObject* gparent = qobject_cast<QGraphicsObject*>(parent); |
|
585 if(gparent){ |
|
586 gobj->setParentItem(gparent); |
|
587 needParent = false; |
|
588 } |
|
589 } |
|
590 } |
|
591 if(needParent) |
|
592 qWarning("QDeclarativeComponent: Created graphical object was not placed in the graphics scene."); |
|
593 |
|
594 QDeclarativeEnginePrivate *priv = QDeclarativeEnginePrivate::get(d->engine); |
|
595 QDeclarativeData::get(ret, true)->setImplicitDestructible(); |
|
596 return priv->objectClass->newQObject(ret, QMetaType::QObjectStar); |
|
597 } |
|
598 |
|
599 /*! |
|
600 Create an object instance from this component. Returns 0 if creation |
|
601 failed. \a context specifies the context within which to create the object |
|
602 instance. |
|
603 |
|
604 If \a context is 0 (the default), it will create the instance in the |
|
605 engine' s \l {QDeclarativeEngine::rootContext()}{root context}. |
|
606 */ |
|
607 QObject *QDeclarativeComponent::create(QDeclarativeContext *context) |
|
608 { |
|
609 Q_D(QDeclarativeComponent); |
|
610 |
|
611 if (!context) |
|
612 context = d->engine->rootContext(); |
|
613 |
|
614 QObject *rv = beginCreate(context); |
|
615 completeCreate(); |
|
616 return rv; |
|
617 } |
|
618 |
|
619 QObject *QDeclarativeComponentPrivate::create(QDeclarativeContextData *context, |
|
620 const QBitField &bindings) |
|
621 { |
|
622 if (!context) |
|
623 context = QDeclarativeContextData::get(engine->rootContext()); |
|
624 |
|
625 QObject *rv = beginCreate(context, bindings); |
|
626 completeCreate(); |
|
627 return rv; |
|
628 } |
|
629 |
|
630 /*! |
|
631 This method provides more advanced control over component instance creation. |
|
632 In general, programmers should use QDeclarativeComponent::create() to create a |
|
633 component. |
|
634 |
|
635 Create an object instance from this component. Returns 0 if creation |
|
636 failed. \a context specifies the context within which to create the object |
|
637 instance. |
|
638 |
|
639 When QDeclarativeComponent constructs an instance, it occurs in three steps: |
|
640 \list 1 |
|
641 \i The object hierarchy is created, and constant values are assigned. |
|
642 \i Property bindings are evaluated for the the first time. |
|
643 \i If applicable, QDeclarativeParserStatus::componentComplete() is called on objects. |
|
644 \endlist |
|
645 QDeclarativeComponent::beginCreate() differs from QDeclarativeComponent::create() in that it |
|
646 only performs step 1. QDeclarativeComponent::completeCreate() must be called to |
|
647 complete steps 2 and 3. |
|
648 |
|
649 This breaking point is sometimes useful when using attached properties to |
|
650 communicate information to an instantiated component, as it allows their |
|
651 initial values to be configured before property bindings take effect. |
|
652 */ |
|
653 QObject *QDeclarativeComponent::beginCreate(QDeclarativeContext *context) |
|
654 { |
|
655 Q_D(QDeclarativeComponent); |
|
656 QObject *rv = d->beginCreate(context?QDeclarativeContextData::get(context):0, QBitField()); |
|
657 if (rv) { |
|
658 QDeclarativeData *ddata = QDeclarativeData::get(rv); |
|
659 Q_ASSERT(ddata); |
|
660 ddata->indestructible = true; |
|
661 } |
|
662 return rv; |
|
663 } |
|
664 |
|
665 QObject * |
|
666 QDeclarativeComponentPrivate::beginCreate(QDeclarativeContextData *context, const QBitField &bindings) |
|
667 { |
|
668 Q_Q(QDeclarativeComponent); |
|
669 if (!context) { |
|
670 qWarning("QDeclarativeComponent: Cannot create a component in a null context"); |
|
671 return 0; |
|
672 } |
|
673 |
|
674 if (!context->isValid()) { |
|
675 qWarning("QDeclarativeComponent: Cannot create a component in an invalid context"); |
|
676 return 0; |
|
677 } |
|
678 |
|
679 if (context->engine != engine) { |
|
680 qWarning("QDeclarativeComponent: Must create component in context from the same QDeclarativeEngine"); |
|
681 return 0; |
|
682 } |
|
683 |
|
684 if (state.completePending) { |
|
685 qWarning("QDeclarativeComponent: Cannot create new component instance before completing the previous"); |
|
686 return 0; |
|
687 } |
|
688 |
|
689 if (!q->isReady()) { |
|
690 qWarning("QDeclarativeComponent: Component is not ready"); |
|
691 return 0; |
|
692 } |
|
693 |
|
694 QDeclarativeEnginePrivate *ep = QDeclarativeEnginePrivate::get(engine); |
|
695 |
|
696 QDeclarativeContextData *ctxt = new QDeclarativeContextData; |
|
697 ctxt->isInternal = true; |
|
698 ctxt->url = cc->url; |
|
699 ctxt->imports = cc->importCache; |
|
700 |
|
701 // Nested global imports |
|
702 if (creationContext && start != -1) |
|
703 ctxt->importedScripts = creationContext->importedScripts; |
|
704 |
|
705 cc->importCache->addref(); |
|
706 ctxt->setParent(context); |
|
707 |
|
708 QObject *rv = begin(ctxt, ep, cc, start, count, &state, bindings); |
|
709 |
|
710 if (rv && !context->isInternal && ep->isDebugging) |
|
711 context->asQDeclarativeContextPrivate()->instances.append(rv); |
|
712 |
|
713 return rv; |
|
714 } |
|
715 |
|
716 QObject * QDeclarativeComponentPrivate::begin(QDeclarativeContextData *ctxt, QDeclarativeEnginePrivate *enginePriv, |
|
717 QDeclarativeCompiledData *component, int start, int count, |
|
718 ConstructionState *state, const QBitField &bindings) |
|
719 { |
|
720 bool isRoot = !enginePriv->inBeginCreate; |
|
721 enginePriv->inBeginCreate = true; |
|
722 |
|
723 QDeclarativeVME vme; |
|
724 QObject *rv = vme.run(ctxt, component, start, count, bindings); |
|
725 |
|
726 if (vme.isError()) |
|
727 state->errors = vme.errors(); |
|
728 |
|
729 if (isRoot) { |
|
730 enginePriv->inBeginCreate = false; |
|
731 |
|
732 state->bindValues = enginePriv->bindValues; |
|
733 state->parserStatus = enginePriv->parserStatus; |
|
734 state->finalizedParserStatus = enginePriv->finalizedParserStatus; |
|
735 state->componentAttached = enginePriv->componentAttached; |
|
736 if (state->componentAttached) |
|
737 state->componentAttached->prev = &state->componentAttached; |
|
738 |
|
739 enginePriv->componentAttached = 0; |
|
740 enginePriv->bindValues.clear(); |
|
741 enginePriv->parserStatus.clear(); |
|
742 enginePriv->finalizedParserStatus.clear(); |
|
743 state->completePending = true; |
|
744 enginePriv->inProgressCreations++; |
|
745 } |
|
746 |
|
747 return rv; |
|
748 } |
|
749 |
|
750 void QDeclarativeComponentPrivate::beginDeferred(QDeclarativeEnginePrivate *enginePriv, |
|
751 QObject *object, ConstructionState *state) |
|
752 { |
|
753 bool isRoot = !enginePriv->inBeginCreate; |
|
754 enginePriv->inBeginCreate = true; |
|
755 |
|
756 QDeclarativeVME vme; |
|
757 vme.runDeferred(object); |
|
758 |
|
759 if (vme.isError()) |
|
760 state->errors = vme.errors(); |
|
761 |
|
762 if (isRoot) { |
|
763 enginePriv->inBeginCreate = false; |
|
764 |
|
765 state->bindValues = enginePriv->bindValues; |
|
766 state->parserStatus = enginePriv->parserStatus; |
|
767 state->finalizedParserStatus = enginePriv->finalizedParserStatus; |
|
768 state->componentAttached = enginePriv->componentAttached; |
|
769 if (state->componentAttached) |
|
770 state->componentAttached->prev = &state->componentAttached; |
|
771 |
|
772 enginePriv->componentAttached = 0; |
|
773 enginePriv->bindValues.clear(); |
|
774 enginePriv->parserStatus.clear(); |
|
775 enginePriv->finalizedParserStatus.clear(); |
|
776 state->completePending = true; |
|
777 enginePriv->inProgressCreations++; |
|
778 } |
|
779 } |
|
780 |
|
781 void QDeclarativeComponentPrivate::complete(QDeclarativeEnginePrivate *enginePriv, ConstructionState *state) |
|
782 { |
|
783 if (state->completePending) { |
|
784 |
|
785 for (int ii = 0; ii < state->bindValues.count(); ++ii) { |
|
786 QDeclarativeEnginePrivate::SimpleList<QDeclarativeAbstractBinding> bv = |
|
787 state->bindValues.at(ii); |
|
788 for (int jj = 0; jj < bv.count; ++jj) { |
|
789 if(bv.at(jj)) |
|
790 bv.at(jj)->setEnabled(true, QDeclarativePropertyPrivate::BypassInterceptor | |
|
791 QDeclarativePropertyPrivate::DontRemoveBinding); |
|
792 } |
|
793 QDeclarativeEnginePrivate::clear(bv); |
|
794 } |
|
795 |
|
796 for (int ii = 0; ii < state->parserStatus.count(); ++ii) { |
|
797 QDeclarativeEnginePrivate::SimpleList<QDeclarativeParserStatus> ps = |
|
798 state->parserStatus.at(ii); |
|
799 |
|
800 for (int jj = ps.count - 1; jj >= 0; --jj) { |
|
801 QDeclarativeParserStatus *status = ps.at(jj); |
|
802 if (status && status->d) { |
|
803 status->d = 0; |
|
804 status->componentComplete(); |
|
805 } |
|
806 } |
|
807 QDeclarativeEnginePrivate::clear(ps); |
|
808 } |
|
809 |
|
810 for (int ii = 0; ii < state->finalizedParserStatus.count(); ++ii) { |
|
811 QPair<QDeclarativeGuard<QObject>, int> status = state->finalizedParserStatus.at(ii); |
|
812 QObject *obj = status.first; |
|
813 if (obj) { |
|
814 void *args[] = { 0 }; |
|
815 QMetaObject::metacall(obj, QMetaObject::InvokeMetaMethod, |
|
816 status.second, args); |
|
817 } |
|
818 } |
|
819 |
|
820 while (state->componentAttached) { |
|
821 QDeclarativeComponentAttached *a = state->componentAttached; |
|
822 a->rem(); |
|
823 QDeclarativeData *d = QDeclarativeData::get(a->parent()); |
|
824 Q_ASSERT(d); |
|
825 Q_ASSERT(d->context); |
|
826 a->add(&d->context->componentAttached); |
|
827 emit a->completed(); |
|
828 } |
|
829 |
|
830 state->bindValues.clear(); |
|
831 state->parserStatus.clear(); |
|
832 state->finalizedParserStatus.clear(); |
|
833 state->completePending = false; |
|
834 |
|
835 enginePriv->inProgressCreations--; |
|
836 if (0 == enginePriv->inProgressCreations) { |
|
837 while (enginePriv->erroredBindings) { |
|
838 enginePriv->warning(enginePriv->erroredBindings->error); |
|
839 enginePriv->erroredBindings->removeError(); |
|
840 } |
|
841 } |
|
842 } |
|
843 } |
|
844 |
|
845 /*! |
|
846 This method provides more advanced control over component instance creation. |
|
847 In general, programmers should use QDeclarativeComponent::create() to create a |
|
848 component. |
|
849 |
|
850 Complete a component creation begin with QDeclarativeComponent::beginCreate(). |
|
851 */ |
|
852 void QDeclarativeComponent::completeCreate() |
|
853 { |
|
854 Q_D(QDeclarativeComponent); |
|
855 d->completeCreate(); |
|
856 } |
|
857 |
|
858 void QDeclarativeComponentPrivate::completeCreate() |
|
859 { |
|
860 if (state.completePending) { |
|
861 QDeclarativeEnginePrivate *ep = QDeclarativeEnginePrivate::get(engine); |
|
862 complete(ep, &state); |
|
863 } |
|
864 } |
|
865 |
|
866 QDeclarativeComponentAttached::QDeclarativeComponentAttached(QObject *parent) |
|
867 : QObject(parent), prev(0), next(0) |
|
868 { |
|
869 } |
|
870 |
|
871 QDeclarativeComponentAttached::~QDeclarativeComponentAttached() |
|
872 { |
|
873 if (prev) *prev = next; |
|
874 if (next) next->prev = prev; |
|
875 prev = 0; |
|
876 next = 0; |
|
877 } |
|
878 |
|
879 /*! |
|
880 \internal |
|
881 */ |
|
882 QDeclarativeComponentAttached *QDeclarativeComponent::qmlAttachedProperties(QObject *obj) |
|
883 { |
|
884 QDeclarativeComponentAttached *a = new QDeclarativeComponentAttached(obj); |
|
885 |
|
886 QDeclarativeEngine *engine = qmlEngine(obj); |
|
887 if (!engine) |
|
888 return a; |
|
889 |
|
890 if (QDeclarativeEnginePrivate::get(engine)->inBeginCreate) { |
|
891 QDeclarativeEnginePrivate *p = QDeclarativeEnginePrivate::get(engine); |
|
892 a->add(&p->componentAttached); |
|
893 } else { |
|
894 QDeclarativeData *d = QDeclarativeData::get(obj); |
|
895 Q_ASSERT(d); |
|
896 Q_ASSERT(d->context); |
|
897 a->add(&d->context->componentAttached); |
|
898 } |
|
899 |
|
900 return a; |
|
901 } |
|
902 |
|
903 QT_END_NAMESPACE |