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 QQMLENGINE_H
41 #define QQMLENGINE_H
42 
43 #include <QtCore/qurl.h>
44 #include <QtCore/qobject.h>
45 #include <QtCore/qmap.h>
46 #include <QtQml/qjsengine.h>
47 #include <QtQml/qqml.h>
48 #include <QtQml/qqmlerror.h>
49 
50 QT_BEGIN_NAMESPACE
51 
52 class QQmlAbstractUrlInterceptor;
53 
54 class Q_QML_EXPORT QQmlImageProviderBase
55 {
56 public:
57     enum ImageType {
58         Image,
59         Pixmap,
60         Texture,
61         Invalid,
62         ImageResponse
63         // ### Qt6: reorder these, and give Invalid a fixed large value
64     };
65 
66     enum Flag {
67         ForceAsynchronousImageLoading  = 0x01
68     };
69     Q_DECLARE_FLAGS(Flags, Flag)
70 
71     virtual ~QQmlImageProviderBase();
72 
73     virtual ImageType imageType() const = 0;
74     virtual Flags flags() const = 0;
75 
76 private:
77     friend class QQuickImageProvider;
78     QQmlImageProviderBase();
79 };
80 Q_DECLARE_OPERATORS_FOR_FLAGS(QQmlImageProviderBase::Flags)
81 
82 class QQmlComponent;
83 class QQmlEnginePrivate;
84 class QQmlImportsPrivate;
85 class QQmlExpression;
86 class QQmlContext;
87 class QQmlType;
88 class QUrl;
89 #if QT_CONFIG(qml_network)
90 class QNetworkAccessManager;
91 class QQmlNetworkAccessManagerFactory;
92 #endif
93 class QQmlIncubationController;
94 class Q_QML_EXPORT QQmlEngine : public QJSEngine
95 {
96     Q_PROPERTY(QString offlineStoragePath READ offlineStoragePath WRITE setOfflineStoragePath)
97     Q_OBJECT
98 public:
99     explicit QQmlEngine(QObject *p = nullptr);
100     ~QQmlEngine() override;
101 
102     QQmlContext *rootContext() const;
103 
104     void clearComponentCache();
105     void trimComponentCache();
106 
107     QStringList importPathList() const;
108     void setImportPathList(const QStringList &paths);
109     void addImportPath(const QString& dir);
110 
111     QStringList pluginPathList() const;
112     void setPluginPathList(const QStringList &paths);
113     void addPluginPath(const QString& dir);
114 
115     bool addNamedBundle(const QString &name, const QString &fileName);
116 
117 #if QT_CONFIG(library)
118     bool importPlugin(const QString &filePath, const QString &uri, QList<QQmlError> *errors);
119 #endif
120 
121 #if QT_CONFIG(qml_network)
122     void setNetworkAccessManagerFactory(QQmlNetworkAccessManagerFactory *);
123     QQmlNetworkAccessManagerFactory *networkAccessManagerFactory() const;
124 
125     QNetworkAccessManager *networkAccessManager() const;
126 #endif
127 
128     void setUrlInterceptor(QQmlAbstractUrlInterceptor* urlInterceptor);
129     QQmlAbstractUrlInterceptor* urlInterceptor() const;
130 
131     void addImageProvider(const QString &id, QQmlImageProviderBase *);
132     QQmlImageProviderBase *imageProvider(const QString &id) const;
133     void removeImageProvider(const QString &id);
134 
135     void setIncubationController(QQmlIncubationController *);
136     QQmlIncubationController *incubationController() const;
137 
138     void setOfflineStoragePath(const QString& dir);
139     QString offlineStoragePath() const;
140     QString offlineStorageDatabaseFilePath(const QString &databaseName) const;
141 
142     QUrl baseUrl() const;
143     void setBaseUrl(const QUrl &);
144 
145     bool outputWarningsToStandardError() const;
146     void setOutputWarningsToStandardError(bool);
147 
148     template<typename T>
149     T singletonInstance(int qmlTypeId);
150 
151 public Q_SLOTS:
152     void retranslate();
153 
154 public:
155     static QQmlContext *contextForObject(const QObject *);
156     static void setContextForObject(QObject *, QQmlContext *);
157 
158     enum ObjectOwnership { CppOwnership, JavaScriptOwnership };
159     static void setObjectOwnership(QObject *, ObjectOwnership);
160     static ObjectOwnership objectOwnership(QObject *);
161 protected:
162     QQmlEngine(QQmlEnginePrivate &dd, QObject *p);
163     bool event(QEvent *) override;
164 
165 Q_SIGNALS:
166     void quit();
167     void exit(int retCode);
168     void warnings(const QList<QQmlError> &warnings);
169 
170 private:
171     Q_DISABLE_COPY(QQmlEngine)
172     Q_DECLARE_PRIVATE(QQmlEngine)
173 };
174 
175 template<>
176 Q_QML_EXPORT QJSValue QQmlEngine::singletonInstance<QJSValue>(int qmlTypeId);
177 
178 template<typename T>
singletonInstance(int qmlTypeId)179 T QQmlEngine::singletonInstance(int qmlTypeId) {
180     return qobject_cast<T>(singletonInstance<QJSValue>(qmlTypeId).toQObject());
181 }
182 
183 QT_END_NAMESPACE
184 
185 #endif // QQMLENGINE_H
186