1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtQml module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#ifndef QQMLCOMPONENT_H
41#define QQMLCOMPONENT_H
42
43#include <QtQml/qqml.h>
44#include <QtQml/qqmlerror.h>
45
46#include <QtCore/qobject.h>
47#include <QtCore/qstring.h>
48#include <QtQml/qjsvalue.h>
49
50QT_BEGIN_NAMESPACE
51
52
53class QByteArray;
54class QQmlEngine;
55class QQmlComponent;
56class QQmlIncubator;
57class QQmlV4Function;
58class QQmlComponentPrivate;
59class QQmlComponentAttached;
60
61namespace QV4 {
62class ExecutableCompilationUnit;
63}
64
65class Q_QML_EXPORT QQmlComponent : public QObject
66{
67 Q_OBJECT
68 Q_DECLARE_PRIVATE(QQmlComponent)
69
70 Q_PROPERTY(qreal progress READ progress NOTIFY progressChanged)
71 Q_PROPERTY(Status status READ status NOTIFY statusChanged)
72 Q_PROPERTY(QUrl url READ url CONSTANT)
73 QML_NAMED_ELEMENT(Component)
74 QML_ATTACHED(QQmlComponentAttached)
75 Q_CLASSINFO("QML.Builtin", "QML")
76
77public:
78 enum CompilationMode { PreferSynchronous, Asynchronous };
79 Q_ENUM(CompilationMode)
80
81 QQmlComponent(QObject *parent = nullptr);
82 QQmlComponent(QQmlEngine *, QObject *parent = nullptr);
83 QQmlComponent(QQmlEngine *, const QString &fileName, QObject *parent = nullptr);
84 QQmlComponent(QQmlEngine *, const QString &fileName, CompilationMode mode, QObject *parent = nullptr);
85 QQmlComponent(QQmlEngine *, const QUrl &url, QObject *parent = nullptr);
86 QQmlComponent(QQmlEngine *, const QUrl &url, CompilationMode mode, QObject *parent = nullptr);
87 ~QQmlComponent() override;
88
89 enum Status { Null, Ready, Loading, Error };
90 Q_ENUM(Status)
91 Status status() const;
92
93 bool isNull() const;
94 bool isReady() const;
95 bool isError() const;
96 bool isLoading() const;
97
98 QList<QQmlError> errors() const;
99 Q_INVOKABLE QString errorString() const;
100
101 qreal progress() const;
102
103 QUrl url() const;
104
105 virtual QObject *create(QQmlContext *context = nullptr);
106 QObject *createWithInitialProperties(const QVariantMap& initialProperties, QQmlContext *context = nullptr);
107 void setInitialProperties(QObject *component, const QVariantMap &properties);
108 virtual QObject *beginCreate(QQmlContext *);
109 virtual void completeCreate();
110
111 void create(QQmlIncubator &, QQmlContext *context = nullptr,
112 QQmlContext *forContext = nullptr);
113
114 QQmlContext *creationContext() const;
115 QQmlEngine *engine() const;
116
117 static QQmlComponentAttached *qmlAttachedProperties(QObject *);
118
119public Q_SLOTS:
120 void loadUrl(const QUrl &url);
121 void loadUrl(const QUrl &url, CompilationMode mode);
122 void setData(const QByteArray &, const QUrl &baseUrl);
123
124Q_SIGNALS:
125 void statusChanged(QQmlComponent::Status);
126 void progressChanged(qreal);
127
128protected:
129 QQmlComponent(QQmlComponentPrivate &dd, QObject* parent);
130 Q_INVOKABLE void createObject(QQmlV4Function *);
131 Q_INVOKABLE void incubateObject(QQmlV4Function *);
132
133private:
134 QQmlComponent(QQmlEngine *, QV4::ExecutableCompilationUnit *compilationUnit, int,
135 QObject *parent);
136
137 Q_DISABLE_COPY(QQmlComponent)
138 friend class QQmlTypeData;
139 friend class QQmlObjectCreator;
140};
141
142
143// Don't do this at home.
144namespace QQmlPrivate {
145
146// Generally you cannot use QQmlComponentAttached as attached properties object in derived classes.
147// It is private.
148template<class T>
149struct OverridableAttachedType<T, QQmlComponentAttached>
150{
151 using Type = void;
152};
153
154// QQmlComponent itself is allowed to use QQmlComponentAttached, though.
155template<>
156struct OverridableAttachedType<QQmlComponent, QQmlComponentAttached>
157{
158 using Type = QQmlComponentAttached;
159};
160
161} // namespace QQmlPrivate
162
163
164QT_END_NAMESPACE
165QML_DECLARE_TYPE(QQmlComponent)
166
167#endif // QQMLCOMPONENT_H
168

source code of qtdeclarative/src/qml/qml/qqmlcomponent.h