diff options
author | Aaron Kennedy <aaron.kennedy@nokia.com> | 2010-01-28 04:26:48 (GMT) |
---|---|---|
committer | Aaron Kennedy <aaron.kennedy@nokia.com> | 2010-01-28 04:26:48 (GMT) |
commit | cfc536842a205a951457e2630b04de2262ffaa46 (patch) | |
tree | be69c9314fa5ec7bc992d8d9fece9aee2559c374 /src | |
parent | 56130de61b2e9e65d09ca4feef2749deea0bb4bb (diff) | |
download | Qt-cfc536842a205a951457e2630b04de2262ffaa46.zip Qt-cfc536842a205a951457e2630b04de2262ffaa46.tar.gz Qt-cfc536842a205a951457e2630b04de2262ffaa46.tar.bz2 |
Support builtin string converters in every context
QTBUG-6919
Diffstat (limited to 'src')
-rw-r--r-- | src/declarative/qml/qmlcompiler.cpp | 28 | ||||
-rw-r--r-- | src/declarative/qml/qmlmetaproperty.cpp | 9 | ||||
-rw-r--r-- | src/declarative/qml/qmlstringconverters.cpp | 72 | ||||
-rw-r--r-- | src/declarative/qml/qmlstringconverters_p.h | 6 |
4 files changed, 83 insertions, 32 deletions
diff --git a/src/declarative/qml/qmlcompiler.cpp b/src/declarative/qml/qmlcompiler.cpp index 2e18b85..bb7abf3 100644 --- a/src/declarative/qml/qmlcompiler.cpp +++ b/src/declarative/qml/qmlcompiler.cpp @@ -257,26 +257,30 @@ bool QmlCompiler::testLiteralAssignment(const QMetaProperty &prop, break; case QVariant::Color: { - QColor c = QmlStringConverters::colorFromString(string); - if (!c.isValid()) COMPILE_EXCEPTION(v, QCoreApplication::translate("QmlCompiler","Invalid property assignment: color expected")); + bool ok; + QmlStringConverters::colorFromString(string, &ok); + if (!ok) COMPILE_EXCEPTION(v, QCoreApplication::translate("QmlCompiler","Invalid property assignment: color expected")); } break; case QVariant::Date: { - QDate d = QDate::fromString(string, Qt::ISODate); - if (!d.isValid()) COMPILE_EXCEPTION(v, QCoreApplication::translate("QmlCompiler","Invalid property assignment: date expected")); + bool ok; + QmlStringConverters::dateFromString(string, &ok); + if (!ok) COMPILE_EXCEPTION(v, QCoreApplication::translate("QmlCompiler","Invalid property assignment: date expected")); } break; case QVariant::Time: { - QTime time = QTime::fromString(string, Qt::ISODate); - if (!time.isValid()) COMPILE_EXCEPTION(v, QCoreApplication::translate("QmlCompiler","Invalid property assignment: time expected")); + bool ok; + QmlStringConverters::timeFromString(string, &ok); + if (!ok) COMPILE_EXCEPTION(v, QCoreApplication::translate("QmlCompiler","Invalid property assignment: time expected")); } break; case QVariant::DateTime: { - QDateTime dateTime = QDateTime::fromString(string, Qt::ISODate); - if (!dateTime.isValid()) COMPILE_EXCEPTION(v, QCoreApplication::translate("QmlCompiler","Invalid property assignment: datetime expected")); + bool ok; + QmlStringConverters::dateTimeFromString(string, &ok); + if (!ok) COMPILE_EXCEPTION(v, QCoreApplication::translate("QmlCompiler","Invalid property assignment: datetime expected")); } break; case QVariant::Point: @@ -311,7 +315,7 @@ bool QmlCompiler::testLiteralAssignment(const QMetaProperty &prop, case QVariant::Vector3D: { bool ok; - QVector3D point = QmlStringConverters::vector3DFromString(string, &ok); + QmlStringConverters::vector3DFromString(string, &ok); if (!ok) COMPILE_EXCEPTION(v, QCoreApplication::translate("QmlCompiler","Invalid property assignment: 3D vector expected")); } break; @@ -417,7 +421,7 @@ void QmlCompiler::genLiteralAssignment(const QMetaProperty &prop, break; case QVariant::Date: { - QDate d = QDate::fromString(string, Qt::ISODate); + QDate d = QmlStringConverters::dateFromString(string); instr.type = QmlInstruction::StoreDate; instr.storeDate.propertyIndex = prop.propertyIndex(); instr.storeDate.value = d.toJulianDay(); @@ -425,7 +429,7 @@ void QmlCompiler::genLiteralAssignment(const QMetaProperty &prop, break; case QVariant::Time: { - QTime time = QTime::fromString(string, Qt::ISODate); + QTime time = QmlStringConverters::timeFromString(string); int data[] = { time.hour(), time.minute(), time.second(), time.msec() }; int index = output->indexForInt(data, 4); @@ -436,7 +440,7 @@ void QmlCompiler::genLiteralAssignment(const QMetaProperty &prop, break; case QVariant::DateTime: { - QDateTime dateTime = QDateTime::fromString(string, Qt::ISODate); + QDateTime dateTime = QmlStringConverters::dateTimeFromString(string); int data[] = { dateTime.date().toJulianDay(), dateTime.time().hour(), dateTime.time().minute(), diff --git a/src/declarative/qml/qmlmetaproperty.cpp b/src/declarative/qml/qmlmetaproperty.cpp index dbf6548..0603a9c 100644 --- a/src/declarative/qml/qmlmetaproperty.cpp +++ b/src/declarative/qml/qmlmetaproperty.cpp @@ -51,6 +51,7 @@ #include "qmlengine.h" #include "qmlengine_p.h" #include "qmldeclarativedata_p.h" +#include "qmlstringconverters_p.h" #include <qfxperf_p_p.h> @@ -962,6 +963,14 @@ bool QmlMetaPropertyPrivate::write(QObject *object, const QmlPropertyCache::Data void *a[] = { (void *)v.constData(), 0, &status, &flags}; QMetaObject::metacall(object, QMetaObject::WriteProperty, coreIdx, a); } + } else if (vt == QVariant::String) { + bool ok = false; + QVariant v = QmlStringConverters::variantFromString(value.toString(), t, &ok); + if (!ok) + return false; + + void *a[] = { (void *)v.constData(), 0, &status, &flags}; + QMetaObject::metacall(object, QMetaObject::WriteProperty, coreIdx, a); } else { return false; } diff --git a/src/declarative/qml/qmlstringconverters.cpp b/src/declarative/qml/qmlstringconverters.cpp index c68654f..2963ab5 100644 --- a/src/declarative/qml/qmlstringconverters.cpp +++ b/src/declarative/qml/qmlstringconverters.cpp @@ -47,6 +47,7 @@ #include <QtCore/qrect.h> #include <QtCore/qsize.h> #include <QtCore/qvariant.h> +#include <QtCore/qdatetime.h> QT_BEGIN_NAMESPACE @@ -94,14 +95,43 @@ QVariant QmlStringConverters::variantFromString(const QString &s) if (ok) return QVariant(p); QSizeF sz = sizeFFromString(s, &ok); if (ok) return QVariant(sz); - bool b = boolFromString(s, &ok); - if (ok) return QVariant(b); QVector3D v = vector3DFromString(s, &ok); if (ok) return qVariantFromValue(v); return QVariant(s); } +QVariant QmlStringConverters::variantFromString(const QString &s, int preferredType, bool *ok) +{ + switch (preferredType) { + case QMetaType::QColor: + return QVariant::fromValue(colorFromString(s, ok)); + case QMetaType::QDate: + return QVariant::fromValue(dateFromString(s, ok)); + case QMetaType::QTime: + return QVariant::fromValue(timeFromString(s, ok)); + case QMetaType::QDateTime: + return QVariant::fromValue(dateTimeFromString(s, ok)); + case QMetaType::QPointF: + return QVariant::fromValue(pointFFromString(s, ok)); + case QMetaType::QPoint: + return QVariant::fromValue(pointFFromString(s, ok).toPoint()); + case QMetaType::QSizeF: + return QVariant::fromValue(sizeFFromString(s, ok)); + case QMetaType::QSize: + return QVariant::fromValue(sizeFFromString(s, ok).toSize()); + case QMetaType::QRectF: + return QVariant::fromValue(rectFFromString(s, ok)); + case QMetaType::QRect: + return QVariant::fromValue(rectFFromString(s, ok).toRect()); + case QMetaType::QVector3D: + return QVariant::fromValue(vector3DFromString(s, ok)); + default: + if (ok) *ok = false; + return QVariant(); + } +} + QColor QmlStringConverters::colorFromString(const QString &s, bool *ok) { if (s.startsWith(QLatin1Char('#')) && s.length() == 9) { @@ -120,6 +150,27 @@ QColor QmlStringConverters::colorFromString(const QString &s, bool *ok) } } +QDate QmlStringConverters::dateFromString(const QString &s, bool *ok) +{ + QDate d = QDate::fromString(s, Qt::ISODate); + if (ok) *ok = d.isValid(); + return d; +} + +QTime QmlStringConverters::timeFromString(const QString &s, bool *ok) +{ + QTime t = QTime::fromString(s, Qt::ISODate); + if (ok) *ok = t.isValid(); + return t; +} + +QDateTime QmlStringConverters::dateTimeFromString(const QString &s, bool *ok) +{ + QDateTime d = QDateTime::fromString(s, Qt::ISODate); + if (ok) *ok = d.isValid(); + return d; +} + //expects input of "x,y" QPointF QmlStringConverters::pointFFromString(const QString &s, bool *ok) { @@ -196,23 +247,6 @@ QRectF QmlStringConverters::rectFFromString(const QString &s, bool *ok) return QRectF(x, y, width, height); } -bool QmlStringConverters::boolFromString(const QString &str, bool *ok) -{ - if (str.isEmpty() || str == QLatin1String("false") || str == QLatin1String("0")) { - if (ok) - *ok = true; - return false; - } else if (str == QLatin1String("true") || str == QLatin1String("1")) { - if (ok) - *ok = true; - return true; - } - - if (ok) - *ok = false; - return true; -} - //expects input of "x,y,z" QVector3D QmlStringConverters::vector3DFromString(const QString &s, bool *ok) { diff --git a/src/declarative/qml/qmlstringconverters_p.h b/src/declarative/qml/qmlstringconverters_p.h index 380a904..dfc59ce 100644 --- a/src/declarative/qml/qmlstringconverters_p.h +++ b/src/declarative/qml/qmlstringconverters_p.h @@ -70,11 +70,15 @@ QT_BEGIN_NAMESPACE namespace QmlStringConverters { QVariant Q_DECLARATIVE_EXPORT variantFromString(const QString &); + QVariant Q_DECLARATIVE_EXPORT variantFromString(const QString &, int preferredType, bool *ok = 0); + QColor Q_DECLARATIVE_EXPORT colorFromString(const QString &, bool *ok = 0); + QDate Q_DECLARATIVE_EXPORT dateFromString(const QString &, bool *ok = 0); + QTime Q_DECLARATIVE_EXPORT timeFromString(const QString &, bool *ok = 0); + QDateTime Q_DECLARATIVE_EXPORT dateTimeFromString(const QString &, bool *ok = 0); QPointF Q_DECLARATIVE_EXPORT pointFFromString(const QString &, bool *ok = 0); QSizeF Q_DECLARATIVE_EXPORT sizeFFromString(const QString &, bool *ok = 0); QRectF Q_DECLARATIVE_EXPORT rectFFromString(const QString &, bool *ok = 0); - bool Q_DECLARATIVE_EXPORT boolFromString(const QString &, bool *ok = 0); QVector3D Q_DECLARATIVE_EXPORT vector3DFromString(const QString &, bool *ok = 0); }; |