aboutsummaryrefslogtreecommitdiffstats
path: root/src/quick/util/qquickglobal.cpp
diff options
context:
space:
mode:
authorUlf Hermann <[email protected]>2020-09-21 10:40:27 +0200
committerUlf Hermann <[email protected]>2020-10-02 13:21:09 +0200
commit45594322fe91eadcd9b2d7b1d76c1a6662bc1472 (patch)
treed14e743f40351ca7a660984616b2500aa83032f5 /src/quick/util/qquickglobal.cpp
parentd621027babff9a30d56ab6af871a465108c9eaba (diff)
Use factory functions and ctors for creating value types
As you can extend value types with QML_EXTENDED we may as well allow a factory function in the extended type. Furthermore, if the original type allows construction from QJSValue, we may just use that. In turn, we can get rid of the value type providers now. Change-Id: I9124ea47537eab6c33d7451080ab2fff942eaa7b Reviewed-by: Fabian Kosmale <[email protected]>
Diffstat (limited to 'src/quick/util/qquickglobal.cpp')
-rw-r--r--src/quick/util/qquickglobal.cpp397
1 files changed, 0 insertions, 397 deletions
diff --git a/src/quick/util/qquickglobal.cpp b/src/quick/util/qquickglobal.cpp
index c12137efe3..ab6cb24bfb 100644
--- a/src/quick/util/qquickglobal.cpp
+++ b/src/quick/util/qquickglobal.cpp
@@ -285,395 +285,6 @@ public:
}
};
-
-// Note: The functions in this class provide handling only for the types
-// that the QML engine will currently actually call them for, so many
-// appear incompletely implemented. For some functions, the implementation
-// would be obvious, but for others (particularly create)
-// the exact semantics are unknown. For this reason unused functionality
-// has been omitted.
-
-class QQuickValueTypeProvider : public QQmlValueTypeProvider
-{
-public:
-
-#if defined(QT_NO_DEBUG) && !defined(QT_FORCE_ASSERTS)
- #define ASSERT_VALID_SIZE(size, min) Q_UNUSED(size);
-#else
- #define ASSERT_VALID_SIZE(size, min) Q_ASSERT(size >= min)
-#endif
-
- static QVector2D vector2DFromString(const QString &s, bool *ok)
- {
- if (s.count(QLatin1Char(',')) == 1) {
- int index = s.indexOf(QLatin1Char(','));
-
- bool xGood, yGood;
- float xCoord = QStringView{s}.left(index).toFloat(&xGood);
- float yCoord = QStringView{s}.mid(index + 1).toFloat(&yGood);
-
- if (xGood && yGood) {
- if (ok) *ok = true;
- return QVector2D(xCoord, yCoord);
- }
- }
-
- if (ok) *ok = false;
- return QVector2D();
- }
-
- 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;
- float xCoord = QStringView{s}.left(index).toFloat(&xGood);
- float yCoord = QStringView{s}.mid(index + 1, index2 - index - 1).toFloat(&yGood);
- float zCoord = QStringView{s}.mid(index2 + 1).toFloat(&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;
- float xCoord = QStringView{s}.left(index).toFloat(&xGood);
- float yCoord = QStringView{s}.mid(index + 1, index2 - index - 1).toFloat(&yGood);
- float zCoord = QStringView{s}.mid(index2 + 1, index3 - index2 - 1).toFloat(&zGood);
- float wCoord = QStringView{s}.mid(index3 + 1).toFloat(&wGood);
-
- if (xGood && yGood && zGood && wGood) {
- if (ok) *ok = true;
- return QVector4D(xCoord, yCoord, zCoord, wCoord);
- }
- }
-
- if (ok) *ok = false;
- return QVector4D();
- }
-
- static QQuaternion quaternionFromString(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 sGood, xGood, yGood, zGood;
- qreal sCoord = QStringView{s}.left(index).toDouble(&sGood);
- qreal xCoord = QStringView{s}.mid(index+1, index2-index-1).toDouble(&xGood);
- qreal yCoord = QStringView{s}.mid(index2+1, index3-index2-1).toDouble(&yGood);
- qreal zCoord = QStringView{s}.mid(index3+1).toDouble(&zGood);
-
- if (sGood && xGood && yGood && zGood) {
- if (ok) *ok = true;
- return QQuaternion(sCoord, xCoord, yCoord, zCoord);
- }
- }
-
- if (ok) *ok = false;
- return QQuaternion();
- }
-
- static QMatrix4x4 matrix4x4FromString(const QString &s, bool *ok)
- {
- if (s.count(QLatin1Char(',')) == 15) {
- float matValues[16];
- bool vOK = true;
- QStringView mutableStr(s);
- for (int i = 0; vOK && i < 16; ++i) {
- int cidx = mutableStr.indexOf(QLatin1Char(','));
- matValues[i] = mutableStr.left(cidx).toDouble(&vOK);
- mutableStr = mutableStr.mid(cidx + 1);
- }
-
- if (vOK) {
- if (ok) *ok = true;
- return QMatrix4x4(matValues);
- }
- }
-
- if (ok) *ok = false;
- return QMatrix4x4();
- }
-
- static QColorSpace colorSpaceFromObject(const QJSValue &object, bool *ok)
- {
- if (ok)
- *ok = false;
-
- QColorSpace retn;
- if (!object.isObject())
- return retn;
-
- const QJSValue vName = object.property(QStringLiteral("namedColorSpace"));
- if (vName.isNumber()) {
- if (ok)
- *ok = true;
- return QColorSpace((QColorSpace::NamedColorSpace)vName.toInt());
- }
-
- const QJSValue vPri = object.property(QStringLiteral("primaries"));
- const QJSValue vTra = object.property(QStringLiteral("transferFunction"));
- if (!vPri.isNumber() || !vTra.isNumber())
- return retn;
-
- QColorSpace::Primaries pri = static_cast<QColorSpace::Primaries>(vPri.toInt());
- QColorSpace::TransferFunction tra = static_cast<QColorSpace::TransferFunction>(vTra.toInt());
- float gamma = 0.0f;
- if (tra == QColorSpace::TransferFunction::Gamma) {
- const QJSValue vGam = object.property(QStringLiteral("gamma"));
- if (!vGam.isNumber())
- return retn;
- gamma = vGam.toNumber();
- }
-
- if (ok)
- *ok = true;
- return QColorSpace(pri, tra, gamma);
- }
-
- static QFont fontFromObject(const QJSValue &object, bool *ok)
- {
- if (ok)
- *ok = false;
- QFont retn;
-
- if (!object.isObject()) {
- if (ok)
- *ok = false;
- return retn;
- }
-
- const QJSValue vbold = object.property(QStringLiteral("bold"));
- const QJSValue vcap = object.property(QStringLiteral("capitalization"));
- const QJSValue vfam = object.property(QStringLiteral("family"));
- const QJSValue vstyle = object.property(QStringLiteral("styleName"));
- const QJSValue vital = object.property(QStringLiteral("italic"));
- const QJSValue vlspac = object.property(QStringLiteral("letterSpacing"));
- const QJSValue vpixsz = object.property(QStringLiteral("pixelSize"));
- const QJSValue vpntsz = object.property(QStringLiteral("pointSize"));
- const QJSValue vstrk = object.property(QStringLiteral("strikeout"));
- const QJSValue vundl = object.property(QStringLiteral("underline"));
- const QJSValue vweight = object.property(QStringLiteral("weight"));
- const QJSValue vwspac = object.property(QStringLiteral("wordSpacing"));
- const QJSValue vhint = object.property(QStringLiteral("hintingPreference"));
- const QJSValue vkerning = object.property(QStringLiteral("kerning"));
- const QJSValue vshaping = object.property(QStringLiteral("preferShaping"));
-
- // pull out the values, set ok to true if at least one valid field is given.
- if (vbold.isBool()) {
- retn.setBold(vbold.toBool());
- if (ok) *ok = true;
- }
- if (vcap.isNumber()) {
- retn.setCapitalization(static_cast<QFont::Capitalization>(vcap.toInt()));
- if (ok) *ok = true;
- }
- if (vfam.isString()) {
- retn.setFamily(vfam.toString());
- if (ok) *ok = true;
- }
- if (vstyle.isString()) {
- retn.setStyleName(vstyle.toString());
- if (ok) *ok = true;
- }
- if (vital.isBool()) {
- retn.setItalic(vital.toBool());
- if (ok) *ok = true;
- }
- if (vlspac.isNumber()) {
- retn.setLetterSpacing(QFont::AbsoluteSpacing, vlspac.toNumber());
- if (ok) *ok = true;
- }
- if (vpixsz.isNumber()) {
- retn.setPixelSize(vpixsz.toInt());
- if (ok) *ok = true;
- }
- if (vpntsz.isNumber()) {
- retn.setPointSize(vpntsz.toNumber());
- if (ok) *ok = true;
- }
- if (vstrk.isBool()) {
- retn.setStrikeOut(vstrk.toBool());
- if (ok) *ok = true;
- }
- if (vundl.isBool()) {
- retn.setUnderline(vundl.toBool());
- if (ok) *ok = true;
- }
- if (vweight.isNumber()) {
- retn.setWeight(QFont::Weight(vweight.toInt()));
- if (ok) *ok = true;
- }
- if (vwspac.isNumber()) {
- retn.setWordSpacing(vwspac.toNumber());
- if (ok) *ok = true;
- }
- if (vhint.isNumber()) {
- retn.setHintingPreference(static_cast<QFont::HintingPreference>(vhint.toInt()));
- if (ok) *ok = true;
- }
- if (vkerning.isBool()) {
- retn.setKerning(vkerning.toBool());
- if (ok) *ok = true;
- }
- if (vshaping.isBool()) {
- bool enable = vshaping.toBool();
- if (enable)
- retn.setStyleStrategy(static_cast<QFont::StyleStrategy>(retn.styleStrategy() & ~QFont::PreferNoShaping));
- else
- retn.setStyleStrategy(static_cast<QFont::StyleStrategy>(retn.styleStrategy() | QFont::PreferNoShaping));
- }
-
- return retn;
- }
-
- bool create(int type, const QJSValue &params, QVariant *v) override
- {
- switch (type) {
- case QMetaType::QColor:
- if (params.isString()) {
- *v = QVariant(QColor(params.toString()));
- return true;
- }
- break;
- case QMetaType::QColorSpace: {
- bool ok = false;
- auto val = colorSpaceFromObject(params, &ok);
- if (ok) {
- *v = QVariant::fromValue(val);
- return true;
- }
- break;
- }
- case QMetaType::QFont: {
- bool ok = false;
- auto val = fontFromObject(params, &ok);
- if (ok) {
- *v = QVariant::fromValue(val);
- return true;
- }
- break;
- }
- case QMetaType::QVector2D:
- if (params.isArray()) {
- *v = QVariant(QVector2D(params.property(0).toNumber(),
- params.property(1).toNumber()));
- return true;
- } else if (params.isString()) {
- bool ok = false;
- auto vector = vector2DFromString(params.toString(), &ok);
- if (ok) {
- *v = QVariant(vector);
- return true;
- }
- }
- break;
- case QMetaType::QVector3D:
- if (params.isArray()) {
- *v = QVariant(QVector3D(params.property(0).toNumber(),
- params.property(1).toNumber(),
- params.property(2).toNumber()));
- return true;
- } else if (params.isString()) {
- bool ok = false;
- auto vector = vector3DFromString(params.toString(), &ok);
- if (ok) {
- *v = QVariant(vector);
- return true;
- }
- }
- break;
- case QMetaType::QVector4D:
- if (params.isArray()) {
- *v = QVariant(QVector4D(params.property(0).toNumber(),
- params.property(1).toNumber(),
- params.property(2).toNumber(),
- params.property(3).toNumber()));
- return true;
- } else if (params.isString()) {
- bool ok = false;
- auto vector = vector4DFromString(params.toString(), &ok);
- if (ok) {
- *v = QVariant(vector);
- return true;
- }
- }
- break;
- case QMetaType::QQuaternion:
- if (params.isArray()) {
- *v = QVariant(QQuaternion(params.property(0).toNumber(),
- params.property(1).toNumber(),
- params.property(2).toNumber(),
- params.property(3).toNumber()));
- return true;
- } else if (params.isString()) {
- bool ok = false;
- auto vector = quaternionFromString(params.toString(), &ok);
- if (ok) {
- *v = QVariant(vector);
- return true;
- }
- }
- break;
- case QMetaType::QMatrix4x4:
- if (params.isNull() || params.isUndefined()) {
- QMatrix4x4 m;
- *v = QVariant(m);
- return true;
- } else if (params.isArray()
- && params.property(QStringLiteral("length")).toInt() == 16) {
- *v = QVariant(QMatrix4x4(params.property(0).toNumber(),
- params.property(1).toNumber(),
- params.property(2).toNumber(),
- params.property(3).toNumber(),
- params.property(4).toNumber(),
- params.property(5).toNumber(),
- params.property(6).toNumber(),
- params.property(7).toNumber(),
- params.property(8).toNumber(),
- params.property(9).toNumber(),
- params.property(10).toNumber(),
- params.property(11).toNumber(),
- params.property(12).toNumber(),
- params.property(13).toNumber(),
- params.property(14).toNumber(),
- params.property(15).toNumber()));
- return true;
- } else if (params.isString()) {
- bool ok = false;
- auto vector = matrix4x4FromString(params.toString(), &ok);
- if (ok) {
- *v = QVariant(vector);
- return true;
- }
- }
- break;
- default: break;
- }
-
- return false;
- }
-
-#undef ASSERT_VALID_SIZE
-};
-
-
class QQuickGuiProvider : public QQmlGuiProvider
{
public:
@@ -720,13 +331,6 @@ public:
}
};
-
-static QQuickValueTypeProvider *getValueTypeProvider()
-{
- static QQuickValueTypeProvider valueTypeProvider;
- return &valueTypeProvider;
-}
-
static QQuickColorProvider *getColorProvider()
{
static QQuickColorProvider colorProvider;
@@ -744,7 +348,6 @@ void QQuick_initializeModule()
// This is used by QQuickPath, and on macOS it fails to automatically register.
qRegisterMetaType<QVector<QVector<QPointF>>>();
- QQml_addValueTypeProvider(getValueTypeProvider());
QQml_setColorProvider(getColorProvider());
QQml_setGuiProvider(getGuiProvider());