aboutsummaryrefslogtreecommitdiffstats
path: root/src/quick/util/qquickglobal.cpp
blob: b99cb7a1d4d1b099e83e9d4fa2186c3d8219b864 (plain)
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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
/****************************************************************************
**
** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/
**
** This file is part of the QtQml module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** GNU Lesser General Public License Usage
** 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.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU General
** Public License version 3.0 as published by the Free Software Foundation
** and appearing in the file LICENSE.GPL included in the packaging of this
** file. Please review the following information to ensure the GNU General
** Public License version 3.0 requirements will be met:
** http://www.gnu.org/copyleft/gpl.html.
**
** Other Usage
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include <private/qquickvaluetypes_p.h>
#include <private/qquickapplication_p.h>
#include <private/qqmlglobal_p.h>

#include <QtGui/QGuiApplication>
#include <QtGui/qdesktopservices.h>
#include <QtGui/qfontdatabase.h>


QT_BEGIN_NAMESPACE

class QQuickColorProvider : public QQmlColorProvider
{
public:
    static inline QColor QColorFromString(const QString &s)
    {
        // Should we also handle #rrggbb here?
        if (s.length() == 9 && s.startsWith(QLatin1Char('#'))) {
            uchar a = fromHex(s, 1);
            uchar r = fromHex(s, 3);
            uchar g = fromHex(s, 5);
            uchar b = fromHex(s, 7);
            return QColor(r, g, b, a);
        }

        return QColor(s);
    }

    QVariant colorFromString(const QString &s, bool *ok)
    {
        QColor c(QColorFromString(s));
        if (c.isValid()) {
            if (ok) *ok = true;
            return QVariant(c);
        }

        if (ok) *ok = false;
        return QVariant();
    }

    unsigned rgbaFromString(const QString &s, bool *ok)
    {
        QColor c(QColorFromString(s));
        if (c.isValid()) {
            if (ok) *ok = true;
            return c.rgba();
        }

        if (ok) *ok = false;
        return 0;
    }

    QString stringFromRgba(unsigned rgba)
    {
        QColor c(QColor::fromRgba(rgba));
        if (c.isValid()) {
            return QVariant(c).toString();
        }

        return QString();
    }

    QVariant fromRgbF(double r, double g, double b, double a)
    {
        return QVariant(QColor::fromRgbF(r, g, b, a));
    }

    QVariant fromHslF(double h, double s, double l, double a)
    {
        return QVariant(QColor::fromHslF(h, s, l, a));
    }

    QVariant lighter(const QVariant &var, qreal factor)
    {
        QColor color = var.value<QColor>();
        color = color.lighter(int(qRound(factor*100.)));
        return QVariant::fromValue(color);
    }

    QVariant darker(const QVariant &var, qreal factor)
    {
        QColor color = var.value<QColor>();
        color = color.darker(int(qRound(factor*100.)));
        return QVariant::fromValue(color);
    }

    QVariant tint(const QVariant &baseVar, const QVariant &tintVar)
    {
        QColor tintColor = tintVar.value<QColor>();

        int tintAlpha = tintColor.alpha();
        if (tintAlpha == 0xFF) {
            return tintVar;
        } else if (tintAlpha == 0x00) {
            return baseVar;
        }

        // tint the base color and return the final color
        QColor baseColor = baseVar.value<QColor>();
        qreal a = tintColor.alphaF();
        qreal inv_a = 1.0 - a;

        qreal r = tintColor.redF() * a + baseColor.redF() * inv_a;
        qreal g = tintColor.greenF() * a + baseColor.greenF() * inv_a;
        qreal b = tintColor.blueF() * a + baseColor.blueF() * inv_a;

        return QVariant::fromValue(QColor::fromRgbF(r, g, b, a + inv_a * baseColor.alphaF()));
    }

private:
    static uchar fromHex(const uchar c, const uchar c2)
    {
        uchar rv = 0;
        if (c >= '0' && c <= '9')
            rv += (c - '0') * 16;
        else if (c >= 'A' && c <= 'F')
            rv += (c - 'A' + 10) * 16;
        else if (c >= 'a' && c <= 'f')
            rv += (c - 'a' + 10) * 16;

        if (c2 >= '0' && c2 <= '9')
            rv += (c2 - '0');
        else if (c2 >= 'A' && c2 <= 'F')
            rv += (c2 - 'A' + 10);
        else if (c2 >= 'a' && c2 <= 'f')
            rv += (c2 - 'a' + 10);

        return rv;
    }

    static inline uchar fromHex(const QString &s, int idx)
    {
        uchar c = s.at(idx).toAscii();
        uchar c2 = s.at(idx + 1).toAscii();
        return fromHex(c, c2);
    }
};


class QQuickValueTypeProvider : public QQmlValueTypeProvider
{
public:
    static QVector3D vector3DFromString(const QString &s, bool *ok)
    {
        if (s.count(QLatin1Char(',')) == 2) {
            int index = s.indexOf(QLatin1Char(','));
            int index2 = s.indexOf(QLatin1Char(','), index+1);

            bool xGood, yGood, zGood;
            qreal xCoord = s.left(index).toDouble(&xGood);
            qreal yCoord = s.mid(index+1, index2-index-1).toDouble(&yGood);
            qreal zCoord = s.mid(index2+1).toDouble(&zGood);

            if (xGood && yGood && zGood) {
                if (ok) *ok = true;
                return QVector3D(xCoord, yCoord, zCoord);
            }
        }

        if (ok) *ok = false;
        return QVector3D();
    }

    static QVector4D vector4DFromString(const QString &s, bool *ok)
    {
        if (s.count(QLatin1Char(',')) == 3) {
            int index = s.indexOf(QLatin1Char(','));
            int index2 = s.indexOf(QLatin1Char(','), index+1);
            int index3 = s.indexOf(QLatin1Char(','), index2+1);

            bool xGood, yGood, zGood, wGood;
            qreal xCoord = s.left(index).toDouble(&xGood);
            qreal yCoord = s.mid(index+1, index2-index-1).toDouble(&yGood);
            qreal zCoord = s.mid(index2+1, index3-index2-1).toDouble(&zGood);
            qreal wCoord = s.mid(index3+1).toDouble(&wGood);

            if (xGood && yGood && zGood && wGood) {
                if (ok) *ok = true;
                return QVector4D(xCoord, yCoord, zCoord, wCoord);
            }
        }

        if (ok) *ok = false;
        return QVector4D();
    }

    bool create(int type, QQmlValueType *&v)
    {
        switch (type) {
        case QMetaType::QColor:
            v = new QQuickColorValueType;
            return true;
        case QMetaType::QVector2D:
            v = new QQuickVector2DValueType;
            return true;
        case QMetaType::QVector3D:
            v = new QQuickVector3DValueType;
            return true;
        case QMetaType::QVector4D:
            v = new QQuickVector4DValueType;
            return true;
        case QMetaType::QQuaternion:
            v = new QQuickQuaternionValueType;
            return true;
        case QMetaType::QMatrix4x4:
            v = new QQuickMatrix4x4ValueType;
            return true;
        case QMetaType::QFont:
            v = new QQuickFontValueType;
            return true;
        default:
            return false;
        }
    }

    bool init(int type, void *data, size_t n)
    {
        if (type == QMetaType::QColor) {
            Q_ASSERT(n >= sizeof(QColor));
            QColor *color = reinterpret_cast<QColor *>(data);
            new (color) QColor();
            return true;
        }

        return false;
    }

    bool destroy(int type, void *data, size_t n)
    {
        if (type == QMetaType::QColor) {
            Q_ASSERT(n >= sizeof(QColor));
            QColor *color = reinterpret_cast<QColor *>(data);
            color->~QColor();
            return true;
        }

        return false;
    }

    bool copy(int type, const void *src, void *dst, size_t n)
    {
        if (type == QMetaType::QColor) {
            Q_ASSERT(n >= sizeof(QColor));
            const QColor *srcColor = reinterpret_cast<const QColor *>(src);
            QColor *dstColor = reinterpret_cast<QColor *>(dst);
            new (dstColor) QColor(*srcColor);
            return true;
        }

        return false;
    }

    bool create(int type, int argc, const void *argv[], QVariant *v)
    {
        switch (type) {
        case QMetaType::QVector3D:
            if (argc == 1) {
                const float *xyz = reinterpret_cast<const float*>(argv[0]);
                QVector3D v3(xyz[0], xyz[1], xyz[2]);
                *v = QVariant(v3);
                return true;
            }
            break;
        case QMetaType::QVector4D:
            if (argc == 1) {
                const float *xyzw = reinterpret_cast<const float*>(argv[0]);
                QVector4D v4(xyzw[0], xyzw[1], xyzw[2], xyzw[3]);
                *v = QVariant(v4);
                return true;
            }
            break;
        }

        return false;
    }

    bool createFromString(int type, const QString &s, void *data, size_t n)
    {
        bool ok = false;

        switch (type) {
        case QMetaType::QColor:
            {
            Q_ASSERT(n >= sizeof(QColor));
            QColor *color = reinterpret_cast<QColor *>(data);
            new (color) QColor(QQuickColorProvider::QColorFromString(s));
            return true;
            }
        case QMetaType::QVector3D:
            {
            Q_ASSERT(n >= sizeof(QVector3D));
            QVector3D *v3 = reinterpret_cast<QVector3D *>(data);
            new (v3) QVector3D(vector3DFromString(s, &ok));
            return true;
            }
        case QMetaType::QVector4D:
            {
            Q_ASSERT(n >= sizeof(QVector4D));
            QVector4D *v4 = reinterpret_cast<QVector4D *>(data);
            new (v4) QVector4D(vector4DFromString(s, &ok));
            return true;
            }
        }

        return false;
    }

    bool createStringFrom(int type, const void *data, QString *s)
    {
        if (type == QMetaType::QColor) {
            const QColor *color = reinterpret_cast<const QColor *>(data);
            new (s) QString(QVariant(*color).toString());
            return true;
        }

        return false;
    }

    bool variantFromString(const QString &s, QVariant *v)
    {
        QColor c(QQuickColorProvider::QColorFromString(s));
        if (c.isValid()) {
            *v = QVariant::fromValue(c);
            return true;
        }

        bool ok = false;

        QVector3D v3 = vector3DFromString(s, &ok);
        if (ok) {
            *v = QVariant::fromValue(v3);
            return true;
        }

        QVector4D v4 = vector4DFromString(s, &ok);
        if (ok) {
            *v = QVariant::fromValue(v4);
            return true;
        }

        return false;
    }

    bool variantFromString(int type, const QString &s, QVariant *v)
    {
        bool ok = false;

        switch (type) {
        case QMetaType::QColor:
            {
            QColor c(QQuickColorProvider::QColorFromString(s));
            *v = QVariant::fromValue(c);
            return true;
            }
        case QMetaType::QVector3D:
            *v = QVariant::fromValue(vector3DFromString(s, &ok));
            return true;
        case QMetaType::QVector4D:
            *v = QVariant::fromValue(vector4DFromString(s, &ok));
            return true;
        }

        return false;
    }

    bool store(int type, const void *src, void *dst, size_t n)
    {
        switch (type) {
        case QMetaType::QColor:
            {
            Q_ASSERT(n >= sizeof(QColor));
            const QRgb *rgb = reinterpret_cast<const QRgb *>(src);
            QColor *color = reinterpret_cast<QColor *>(dst);
            new (color) QColor(QColor::fromRgba(*rgb));
            return true;
            }
        case QMetaType::QVector3D:
            {
            Q_ASSERT(n >= sizeof(QVector3D));
            const QVector3D *srcVector = reinterpret_cast<const QVector3D *>(src);
            QVector3D *dstVector = reinterpret_cast<QVector3D *>(dst);
            new (dstVector) QVector3D(*srcVector);
            return true;
            }
        case QMetaType::QVector4D:
            {
            Q_ASSERT(n >= sizeof(QVector4D));
            const QVector4D *srcVector = reinterpret_cast<const QVector4D *>(src);
            QVector4D *dstVector = reinterpret_cast<QVector4D *>(dst);
            new (dstVector) QVector4D(*srcVector);
            return true;
            }
        }

        return false;
    }

    bool read(int srcType, const void *src, int dstType, void *dst)
    {
        if (dstType == QMetaType::QColor) {
            QColor *dstColor = reinterpret_cast<QColor *>(dst);
            if (srcType == QMetaType::QColor) {
                const QColor *srcColor = reinterpret_cast<const QColor *>(src);
                *dstColor = *srcColor;
            } else {
                *dstColor = QColor();
            }
            return true;
        }

        return false;
    }

    bool write(int type, const void *src, void *dst, size_t n)
    {
        if (type == QMetaType::QColor) {
            Q_ASSERT(n >= sizeof(QColor));
            const QColor *srcColor = reinterpret_cast<const QColor *>(src);
            QColor *dstColor = reinterpret_cast<QColor *>(dst);
            if (*dstColor != *srcColor) {
                *dstColor = *srcColor;
                return true;
            }
        }

        return false;
    }
};


class QQuickGuiProvider : public QQmlGuiProvider
{
public:
    QQuickApplication *application(QObject *parent)
    {
        return new QQuickApplication(parent);
    }

    QInputMethod *inputMethod()
    {
        return qGuiApp->inputMethod();
    }

    QStringList fontFamilies()
    {
        QFontDatabase database;
        return database.families();
    }

    bool openUrlExternally(QUrl &url)
    {
#ifndef QT_NO_DESKTOPSERVICES
        return QDesktopServices::openUrl(url);
#else
        return false;
#endif
    }
};


static QQuickValueTypeProvider *getValueTypeProvider()
{
    static QQuickValueTypeProvider valueTypeProvider;
    return &valueTypeProvider;
}

static QQuickColorProvider *getColorProvider()
{
    static QQuickColorProvider colorProvider;
    return &colorProvider;
}

static QQuickGuiProvider *getGuiProvider()
{
    static QQuickGuiProvider guiProvider;
    return &guiProvider;
}

static bool initializeProviders()
{
    QQml_addValueTypeProvider(getValueTypeProvider());
    QQml_setColorProvider(getColorProvider());
    QQml_setGuiProvider(getGuiProvider());
    return true;
}

static bool initialized = initializeProviders();

QT_END_NAMESPACE