summaryrefslogtreecommitdiffstats
path: root/src/script
diff options
context:
space:
mode:
authorKent Hansen <khansen@trolltech.com>2009-05-20 09:57:08 (GMT)
committerKent Hansen <khansen@trolltech.com>2009-05-20 09:57:08 (GMT)
commit691e83425ac5883922837bc5fd1efb5385db9871 (patch)
tree908ff0897699323cc9f0fe9401449bf66c86064a /src/script
parente2d70563652a89384d13c9a1d7ac1c839a81e9be (diff)
downloadQt-691e83425ac5883922837bc5fd1efb5385db9871.zip
Qt-691e83425ac5883922837bc5fd1efb5385db9871.tar.gz
Qt-691e83425ac5883922837bc5fd1efb5385db9871.tar.bz2
Add string-->enum conversion for properties and slots
Diffstat (limited to 'src/script')
-rw-r--r--src/script/qscriptengine_p.cpp5
-rw-r--r--src/script/qscriptenginefwd_p.h1
-rw-r--r--src/script/qscriptextqobject.cpp33
3 files changed, 32 insertions, 7 deletions
diff --git a/src/script/qscriptengine_p.cpp b/src/script/qscriptengine_p.cpp
index a2e58de..84a420d 100644
--- a/src/script/qscriptengine_p.cpp
+++ b/src/script/qscriptengine_p.cpp
@@ -1662,6 +1662,11 @@ bool QScriptEnginePrivate::convert(const QScriptValueImpl &value,
return false;
}
+QScriptEngine::DemarshalFunction QScriptEnginePrivate::demarshalFunction(int type) const
+{
+ return m_customTypes.value(type).demarshal;
+}
+
QScriptValuePrivate *QScriptEnginePrivate::registerValue(const QScriptValueImpl &value)
{
if (value.isString()) {
diff --git a/src/script/qscriptenginefwd_p.h b/src/script/qscriptenginefwd_p.h
index 2ea66c5..855317c 100644
--- a/src/script/qscriptenginefwd_p.h
+++ b/src/script/qscriptenginefwd_p.h
@@ -350,6 +350,7 @@ public:
QScriptValueImpl create(int type, const void *ptr);
static bool convert(const QScriptValueImpl &value, int type, void *ptr,
QScriptEnginePrivate *eng);
+ QScriptEngine::DemarshalFunction demarshalFunction(int type) const;
QScriptValueImpl arrayFromStringList(const QStringList &lst);
static QStringList stringListFromArray(const QScriptValueImpl &arr);
diff --git a/src/script/qscriptextqobject.cpp b/src/script/qscriptextqobject.cpp
index d18c3da..4522807 100644
--- a/src/script/qscriptextqobject.cpp
+++ b/src/script/qscriptextqobject.cpp
@@ -425,7 +425,7 @@ static void callQtMethod(QScriptContextPrivate *context, QMetaMethod::MethodType
matchDistance += 10;
}
}
- } else if (actual.isNumber()) {
+ } else if (actual.isNumber() || actual.isString()) {
// see if it's an enum value
QMetaEnum m;
if (argType.isMetaEnum()) {
@@ -436,11 +436,21 @@ static void callQtMethod(QScriptContextPrivate *context, QMetaMethod::MethodType
m = meta->enumerator(mi);
}
if (m.isValid()) {
- int ival = actual.toInt32();
- if (m.valueToKey(ival) != 0) {
- qVariantSetValue(v, ival);
- converted = true;
- matchDistance += 10;
+ if (actual.isNumber()) {
+ int ival = actual.toInt32();
+ if (m.valueToKey(ival) != 0) {
+ qVariantSetValue(v, ival);
+ converted = true;
+ matchDistance += 10;
+ }
+ } else {
+ QString sval = actual.toString();
+ int ival = m.keyToValue(sval.toLatin1());
+ if (ival != -1) {
+ qVariantSetValue(v, ival);
+ converted = true;
+ matchDistance += 10;
+ }
}
}
}
@@ -1809,7 +1819,16 @@ void QScript::QtPropertyFunction::execute(QScriptContextPrivate *context)
}
} else {
// set
- QVariant v = variantFromValue(eng_p, prop.userType(), context->argument(0));
+ QScriptValueImpl arg = context->argument(0);
+ QVariant v;
+ if (prop.isEnumType() && arg.isString()
+ && !eng_p->demarshalFunction(prop.userType())) {
+ // give QMetaProperty::write() a chance to convert from
+ // string to enum value
+ v = arg.toString();
+ } else {
+ v = variantFromValue(eng_p, prop.userType(), arg);
+ }
QScriptable *scriptable = scriptableFromQObject(qobject);
QScriptEngine *oldEngine = 0;