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 #include <private/qqmlglobal_p.h>
41
42 #include <QtQml/qqmlengine.h>
43 #include <QtCore/qvariant.h>
44 #include <QtCore/qstringlist.h>
45 #include <QtCore/qdebug.h>
46 #include <QtCore/QCoreApplication>
47
48 QT_BEGIN_NAMESPACE
49
QQmlValueTypeProvider()50 QQmlValueTypeProvider::QQmlValueTypeProvider()
51 : next(nullptr)
52 {
53 }
54
~QQmlValueTypeProvider()55 QQmlValueTypeProvider::~QQmlValueTypeProvider()
56 {
57 QQml_removeValueTypeProvider(this);
58 }
59
metaObjectForMetaType(int type)60 const QMetaObject *QQmlValueTypeProvider::metaObjectForMetaType(int type)
61 {
62 QQmlValueTypeProvider *p = this;
63 do {
64 if (const QMetaObject *mo = p->getMetaObjectForMetaType(type))
65 return mo;
66 } while ((p = p->next));
67
68 return nullptr;
69 }
70
initValueType(int type,QVariant & dst)71 bool QQmlValueTypeProvider::initValueType(int type, QVariant& dst)
72 {
73 QQmlValueTypeProvider *p = this;
74 do {
75 if (p->init(type, dst))
76 return true;
77 } while ((p = p->next));
78
79 return false;
80 }
81
createValueType(int type,int argc,const void * argv[])82 QVariant QQmlValueTypeProvider::createValueType(int type, int argc, const void *argv[])
83 {
84 QVariant v;
85
86 QQmlValueTypeProvider *p = this;
87 do {
88 if (p->create(type, argc, argv, &v))
89 return v;
90 } while ((p = p->next));
91
92 return QVariant();
93 }
94
createValueFromString(int type,const QString & s,void * data,size_t n)95 bool QQmlValueTypeProvider::createValueFromString(int type, const QString &s, void *data, size_t n)
96 {
97 Q_ASSERT(data);
98
99 QQmlValueTypeProvider *p = this;
100 do {
101 if (p->createFromString(type, s, data, n))
102 return true;
103 } while ((p = p->next));
104
105 return false;
106 }
107
createStringFromValue(int type,const void * data,QString * s)108 bool QQmlValueTypeProvider::createStringFromValue(int type, const void *data, QString *s)
109 {
110 Q_ASSERT(data);
111 Q_ASSERT(s);
112
113 QQmlValueTypeProvider *p = this;
114 do {
115 if (p->createStringFrom(type, data, s))
116 return true;
117 } while ((p = p->next));
118
119 return false;
120 }
121
createVariantFromString(const QString & s)122 QVariant QQmlValueTypeProvider::createVariantFromString(const QString &s)
123 {
124 QVariant v;
125
126 QQmlValueTypeProvider *p = this;
127 do {
128 if (p->variantFromString(s, &v))
129 return v;
130 } while ((p = p->next));
131
132 // Return a variant containing the string itself
133 return QVariant(s);
134 }
135
createVariantFromString(int type,const QString & s,bool * ok)136 QVariant QQmlValueTypeProvider::createVariantFromString(int type, const QString &s, bool *ok)
137 {
138 QVariant v;
139
140 QQmlValueTypeProvider *p = this;
141 do {
142 if (p->variantFromString(type, s, &v)) {
143 if (ok) *ok = true;
144 return v;
145 }
146 } while ((p = p->next));
147
148 if (ok) *ok = false;
149 return QVariant();
150 }
151
createVariantFromJsObject(int type,const QV4::Value & obj,QV4::ExecutionEngine * e,bool * ok)152 QVariant QQmlValueTypeProvider::createVariantFromJsObject(int type, const QV4::Value &obj,
153 QV4::ExecutionEngine *e, bool *ok)
154 {
155 QVariant v;
156
157 QQmlValueTypeProvider *p = this;
158 do {
159 if (p->variantFromJsObject(type, obj, e, &v)) {
160 if (ok) *ok = true;
161 return v;
162 }
163 } while ((p = p->next));
164
165 if (ok) *ok = false;
166 return QVariant();
167 }
168
equalValueType(int type,const void * lhs,const QVariant & rhs)169 bool QQmlValueTypeProvider::equalValueType(int type, const void *lhs, const QVariant& rhs)
170 {
171 Q_ASSERT(lhs);
172
173 QQmlValueTypeProvider *p = this;
174 do {
175 if (p->equal(type, lhs, rhs))
176 return true;
177 } while ((p = p->next));
178
179 return false;
180 }
181
storeValueType(int type,const void * src,void * dst,size_t dstSize)182 bool QQmlValueTypeProvider::storeValueType(int type, const void *src, void *dst, size_t dstSize)
183 {
184 Q_ASSERT(src);
185 Q_ASSERT(dst);
186
187 QQmlValueTypeProvider *p = this;
188 do {
189 if (p->store(type, src, dst, dstSize))
190 return true;
191 } while ((p = p->next));
192
193 return false;
194 }
195
readValueType(const QVariant & src,void * dst,int dstType)196 bool QQmlValueTypeProvider::readValueType(const QVariant& src, void *dst, int dstType)
197 {
198 Q_ASSERT(dst);
199
200 QQmlValueTypeProvider *p = this;
201 do {
202 if (p->read(src, dst, dstType))
203 return true;
204 } while ((p = p->next));
205
206 return false;
207 }
208
writeValueType(int type,const void * src,QVariant & dst)209 bool QQmlValueTypeProvider::writeValueType(int type, const void *src, QVariant& dst)
210 {
211 Q_ASSERT(src);
212
213 QQmlValueTypeProvider *p = this;
214 do {
215 if (p->write(type, src, dst))
216 return true;
217 } while ((p = p->next));
218
219 return false;
220 }
221
getMetaObjectForMetaType(int)222 const QMetaObject *QQmlValueTypeProvider::getMetaObjectForMetaType(int) { return nullptr; }
init(int,QVariant &)223 bool QQmlValueTypeProvider::init(int, QVariant&) { return false; }
create(int,int,const void * [],QVariant *)224 bool QQmlValueTypeProvider::create(int, int, const void *[], QVariant *) { return false; }
createFromString(int,const QString &,void *,size_t)225 bool QQmlValueTypeProvider::createFromString(int, const QString &, void *, size_t) { return false; }
createStringFrom(int,const void *,QString *)226 bool QQmlValueTypeProvider::createStringFrom(int, const void *, QString *) { return false; }
variantFromString(const QString &,QVariant *)227 bool QQmlValueTypeProvider::variantFromString(const QString &, QVariant *) { return false; }
variantFromString(int,const QString &,QVariant *)228 bool QQmlValueTypeProvider::variantFromString(int, const QString &, QVariant *) { return false; }
variantFromJsObject(int,const QV4::Value &,QV4::ExecutionEngine *,QVariant *)229 bool QQmlValueTypeProvider::variantFromJsObject(int, const QV4::Value &, QV4::ExecutionEngine *, QVariant *) { return false; }
equal(int,const void *,const QVariant &)230 bool QQmlValueTypeProvider::equal(int, const void *, const QVariant&) { return false; }
store(int,const void *,void *,size_t)231 bool QQmlValueTypeProvider::store(int, const void *, void *, size_t) { return false; }
read(const QVariant &,void *,int)232 bool QQmlValueTypeProvider::read(const QVariant&, void *, int) { return false; }
write(int,const void *,QVariant &)233 bool QQmlValueTypeProvider::write(int, const void *, QVariant&) { return false; }
234
235 struct ValueTypeProviderList {
236 QQmlValueTypeProvider nullProvider;
237 QQmlValueTypeProvider *head = &nullProvider;
238 };
239
Q_GLOBAL_STATIC(ValueTypeProviderList,valueTypeProviders)240 Q_GLOBAL_STATIC(ValueTypeProviderList, valueTypeProviders)
241
242 Q_QML_PRIVATE_EXPORT void QQml_addValueTypeProvider(QQmlValueTypeProvider *newProvider)
243 {
244 if (ValueTypeProviderList *providers = valueTypeProviders()) {
245 newProvider->next = providers->head;
246 providers->head = newProvider;
247 }
248 }
249
QQml_removeValueTypeProvider(QQmlValueTypeProvider * oldProvider)250 Q_QML_PRIVATE_EXPORT void QQml_removeValueTypeProvider(QQmlValueTypeProvider *oldProvider)
251 {
252 if (ValueTypeProviderList *providers = valueTypeProviders()) {
253 QQmlValueTypeProvider *prev = providers->head;
254 if (prev == oldProvider) {
255 providers->head = oldProvider->next;
256 return;
257 }
258
259 // singly-linked list removal
260 for (; prev; prev = prev->next) {
261 if (prev->next != oldProvider)
262 continue; // this is not the provider you're looking for
263 prev->next = oldProvider->next;
264 return;
265 }
266
267 qWarning("QQml_removeValueTypeProvider: was asked to remove provider %p but it was not found", oldProvider);
268 }
269 }
270
QQml_valueTypeProvider()271 Q_AUTOTEST_EXPORT QQmlValueTypeProvider *QQml_valueTypeProvider()
272 {
273 if (ValueTypeProviderList *providers = valueTypeProviders())
274 return providers->head;
275 return nullptr;
276 }
277
~QQmlColorProvider()278 QQmlColorProvider::~QQmlColorProvider() {}
colorFromString(const QString &,bool * ok)279 QVariant QQmlColorProvider::colorFromString(const QString &, bool *ok) { if (ok) *ok = false; return QVariant(); }
rgbaFromString(const QString &,bool * ok)280 unsigned QQmlColorProvider::rgbaFromString(const QString &, bool *ok) { if (ok) *ok = false; return 0; }
fromRgbF(double,double,double,double)281 QVariant QQmlColorProvider::fromRgbF(double, double, double, double) { return QVariant(); }
fromHslF(double,double,double,double)282 QVariant QQmlColorProvider::fromHslF(double, double, double, double) { return QVariant(); }
fromHsvF(double,double,double,double)283 QVariant QQmlColorProvider::fromHsvF(double, double, double, double) { return QVariant(); }
lighter(const QVariant &,qreal)284 QVariant QQmlColorProvider::lighter(const QVariant &, qreal) { return QVariant(); }
darker(const QVariant &,qreal)285 QVariant QQmlColorProvider::darker(const QVariant &, qreal) { return QVariant(); }
tint(const QVariant &,const QVariant &)286 QVariant QQmlColorProvider::tint(const QVariant &, const QVariant &) { return QVariant(); }
287
288 static QQmlColorProvider *colorProvider = nullptr;
289
QQml_setColorProvider(QQmlColorProvider * newProvider)290 Q_QML_PRIVATE_EXPORT QQmlColorProvider *QQml_setColorProvider(QQmlColorProvider *newProvider)
291 {
292 QQmlColorProvider *old = colorProvider;
293 colorProvider = newProvider;
294 return old;
295 }
296
getColorProvider(void)297 static QQmlColorProvider **getColorProvider(void)
298 {
299 if (colorProvider == nullptr) {
300 qWarning() << "Warning: QQml_colorProvider: no color provider has been set!";
301 static QQmlColorProvider nullColorProvider;
302 colorProvider = &nullColorProvider;
303 }
304
305 return &colorProvider;
306 }
307
QQml_colorProvider(void)308 Q_AUTOTEST_EXPORT QQmlColorProvider *QQml_colorProvider(void)
309 {
310 static QQmlColorProvider **providerPtr = getColorProvider();
311 return *providerPtr;
312 }
313
314
~QQmlGuiProvider()315 QQmlGuiProvider::~QQmlGuiProvider() {}
application(QObject *)316 QObject *QQmlGuiProvider::application(QObject *) { return new QQmlApplication(); }
fontFamilies()317 QStringList QQmlGuiProvider::fontFamilies() { return QStringList(); }
openUrlExternally(QUrl &)318 bool QQmlGuiProvider::openUrlExternally(QUrl &) { return false; }
319
inputMethod()320 QObject *QQmlGuiProvider::inputMethod()
321 {
322 // We don't have any input method code by default
323 QObject *o = new QObject();
324 o->setObjectName(QStringLiteral("No inputMethod available"));
325 QQmlEngine::setObjectOwnership(o, QQmlEngine::JavaScriptOwnership);
326 return o;
327 }
328
styleHints()329 QObject *QQmlGuiProvider::styleHints()
330 {
331 QObject *o = new QObject();
332 o->setObjectName(QStringLiteral("No styleHints available"));
333 QQmlEngine::setObjectOwnership(o, QQmlEngine::JavaScriptOwnership);
334 return o;
335 }
336
pluginName() const337 QString QQmlGuiProvider::pluginName() const { return QString(); }
338
339 static QQmlGuiProvider *guiProvider = nullptr;
340
QQml_setGuiProvider(QQmlGuiProvider * newProvider)341 Q_QML_PRIVATE_EXPORT QQmlGuiProvider *QQml_setGuiProvider(QQmlGuiProvider *newProvider)
342 {
343 QQmlGuiProvider *old = guiProvider;
344 guiProvider = newProvider;
345 return old;
346 }
347
getGuiProvider(void)348 static QQmlGuiProvider **getGuiProvider(void)
349 {
350 if (guiProvider == nullptr) {
351 static QQmlGuiProvider nullGuiProvider; //Still provides an application with no GUI support
352 guiProvider = &nullGuiProvider;
353 }
354
355 return &guiProvider;
356 }
357
QQml_guiProvider(void)358 Q_AUTOTEST_EXPORT QQmlGuiProvider *QQml_guiProvider(void)
359 {
360 static QQmlGuiProvider **providerPtr = getGuiProvider();
361 return *providerPtr;
362 }
363
364 //Docs in qqmlengine.cpp
QQmlApplication(QObject * parent)365 QQmlApplication::QQmlApplication(QObject *parent)
366 : QObject(*(new QQmlApplicationPrivate),parent)
367 {
368 connect(QCoreApplication::instance(), SIGNAL(aboutToQuit()),
369 this, SIGNAL(aboutToQuit()));
370 connect(QCoreApplication::instance(), SIGNAL(applicationNameChanged()),
371 this, SIGNAL(nameChanged()));
372 connect(QCoreApplication::instance(), SIGNAL(applicationVersionChanged()),
373 this, SIGNAL(versionChanged()));
374 connect(QCoreApplication::instance(), SIGNAL(organizationNameChanged()),
375 this, SIGNAL(organizationChanged()));
376 connect(QCoreApplication::instance(), SIGNAL(organizationDomainChanged()),
377 this, SIGNAL(domainChanged()));
378 }
379
QQmlApplication(QQmlApplicationPrivate & dd,QObject * parent)380 QQmlApplication::QQmlApplication(QQmlApplicationPrivate &dd, QObject *parent)
381 : QObject(dd, parent)
382 {
383 connect(QCoreApplication::instance(), SIGNAL(aboutToQuit()),
384 this, SIGNAL(aboutToQuit()));
385 connect(QCoreApplication::instance(), SIGNAL(applicationNameChanged()),
386 this, SIGNAL(nameChanged()));
387 connect(QCoreApplication::instance(), SIGNAL(applicationVersionChanged()),
388 this, SIGNAL(versionChanged()));
389 connect(QCoreApplication::instance(), SIGNAL(organizationNameChanged()),
390 this, SIGNAL(organizationChanged()));
391 connect(QCoreApplication::instance(), SIGNAL(organizationDomainChanged()),
392 this, SIGNAL(domainChanged()));
393 }
394
args()395 QStringList QQmlApplication::args()
396 {
397 Q_D(QQmlApplication);
398 if (!d->argsInit) {
399 d->argsInit = true;
400 d->args = QCoreApplication::arguments();
401 }
402 return d->args;
403 }
404
name() const405 QString QQmlApplication::name() const
406 {
407 return QCoreApplication::instance()->applicationName();
408 }
409
version() const410 QString QQmlApplication::version() const
411 {
412 return QCoreApplication::instance()->applicationVersion();
413 }
414
organization() const415 QString QQmlApplication::organization() const
416 {
417 return QCoreApplication::instance()->organizationName();
418 }
419
domain() const420 QString QQmlApplication::domain() const
421 {
422 return QCoreApplication::instance()->organizationDomain();
423 }
424
setName(const QString & arg)425 void QQmlApplication::setName(const QString &arg)
426 {
427 QCoreApplication::instance()->setApplicationName(arg);
428 }
429
setVersion(const QString & arg)430 void QQmlApplication::setVersion(const QString &arg)
431 {
432 QCoreApplication::instance()->setApplicationVersion(arg);
433 }
434
setOrganization(const QString & arg)435 void QQmlApplication::setOrganization(const QString &arg)
436 {
437 QCoreApplication::instance()->setOrganizationName(arg);
438 }
439
setDomain(const QString & arg)440 void QQmlApplication::setDomain(const QString &arg)
441 {
442 QCoreApplication::instance()->setOrganizationDomain(arg);
443 }
444
445 QT_END_NAMESPACE
446
447 #include "moc_qqmlglobal_p.cpp"
448