diff options
author | Michael Brasser <michael.brasser@nokia.com> | 2010-03-04 04:57:40 (GMT) |
---|---|---|
committer | Michael Brasser <michael.brasser@nokia.com> | 2010-03-04 05:21:56 (GMT) |
commit | 78254634c87f8e1b9c79841d87c530d3af8c9734 (patch) | |
tree | 4d8cd9691f55072906b8b57f3350647466c3498e /src/declarative/qml | |
parent | 0559b8d2af9f1f9277ff3bedd9a33cca7e255abb (diff) | |
download | Qt-78254634c87f8e1b9c79841d87c530d3af8c9734.zip Qt-78254634c87f8e1b9c79841d87c530d3af8c9734.tar.gz Qt-78254634c87f8e1b9c79841d87c530d3af8c9734.tar.bz2 |
Add formatting functions to QML's global Qt object.
The plan is for these to replace DateTimeFormatter.
Diffstat (limited to 'src/declarative/qml')
-rw-r--r-- | src/declarative/qml/qdeclarativeengine.cpp | 65 | ||||
-rw-r--r-- | src/declarative/qml/qdeclarativeengine_p.h | 4 |
2 files changed, 69 insertions, 0 deletions
diff --git a/src/declarative/qml/qdeclarativeengine.cpp b/src/declarative/qml/qdeclarativeengine.cpp index bd67b0b..20bbf86 100644 --- a/src/declarative/qml/qdeclarativeengine.cpp +++ b/src/declarative/qml/qdeclarativeengine.cpp @@ -236,6 +236,11 @@ QDeclarativeScriptEngine::QDeclarativeScriptEngine(QDeclarativeEnginePrivate *pr qtObject.setProperty(QLatin1String("tint"), newFunction(QDeclarativeEnginePrivate::tint, 2)); } + //date/time formatting + qtObject.setProperty(QLatin1String("formatDate"),newFunction(QDeclarativeEnginePrivate::formatDate, 2)); + qtObject.setProperty(QLatin1String("formatTime"),newFunction(QDeclarativeEnginePrivate::formatTime, 2)); + qtObject.setProperty(QLatin1String("formatDateTime"),newFunction(QDeclarativeEnginePrivate::formatDateTime, 2)); + //misc methods qtObject.setProperty(QLatin1String("closestAngle"), newFunction(QDeclarativeEnginePrivate::closestAngle, 2)); qtObject.setProperty(QLatin1String("playSound"), newFunction(QDeclarativeEnginePrivate::playSound, 1)); @@ -936,6 +941,66 @@ QScriptValue QDeclarativeEnginePrivate::vector(QScriptContext *ctxt, QScriptEngi return engine->newVariant(qVariantFromValue(QVector3D(x, y, z))); } +QScriptValue QDeclarativeEnginePrivate::formatDate(QScriptContext*ctxt, QScriptEngine*engine) +{ + int argCount = ctxt->argumentCount(); + if(argCount == 0 || argCount > 2) + return engine->nullValue(); + + QDate date = ctxt->argument(0).toDateTime().date(); + Qt::DateFormat enumFormat = Qt::DefaultLocaleShortDate; + if (argCount == 2) { + if (ctxt->argument(1).isString()) { + QString format = ctxt->argument(1).toString(); + return engine->newVariant(qVariantFromValue(date.toString(format))); + } else if (ctxt->argument(1).isNumber()) { + enumFormat = Qt::DateFormat(ctxt->argument(1).toInteger()); + } else + return engine->nullValue(); + } + return engine->newVariant(qVariantFromValue(date.toString(enumFormat))); +} + +QScriptValue QDeclarativeEnginePrivate::formatTime(QScriptContext*ctxt, QScriptEngine*engine) +{ + int argCount = ctxt->argumentCount(); + if(argCount == 0 || argCount > 2) + return engine->nullValue(); + + QTime date = ctxt->argument(0).toDateTime().time(); + Qt::DateFormat enumFormat = Qt::DefaultLocaleShortDate; + if (argCount == 2) { + if (ctxt->argument(1).isString()) { + QString format = ctxt->argument(1).toString(); + return engine->newVariant(qVariantFromValue(date.toString(format))); + } else if (ctxt->argument(1).isNumber()) { + enumFormat = Qt::DateFormat(ctxt->argument(1).toInteger()); + } else + return engine->nullValue(); + } + return engine->newVariant(qVariantFromValue(date.toString(enumFormat))); +} + +QScriptValue QDeclarativeEnginePrivate::formatDateTime(QScriptContext*ctxt, QScriptEngine*engine) +{ + int argCount = ctxt->argumentCount(); + if(argCount == 0 || argCount > 2) + return engine->nullValue(); + + QDateTime date = ctxt->argument(0).toDateTime(); + Qt::DateFormat enumFormat = Qt::DefaultLocaleShortDate; + if (argCount == 2) { + if (ctxt->argument(1).isString()) { + QString format = ctxt->argument(1).toString(); + return engine->newVariant(qVariantFromValue(date.toString(format))); + } else if (ctxt->argument(1).isNumber()) { + enumFormat = Qt::DateFormat(ctxt->argument(1).toInteger()); + } else + return engine->nullValue(); + } + return engine->newVariant(qVariantFromValue(date.toString(enumFormat))); +} + QScriptValue QDeclarativeEnginePrivate::rgba(QScriptContext *ctxt, QScriptEngine *engine) { int argCount = ctxt->argumentCount(); diff --git a/src/declarative/qml/qdeclarativeengine_p.h b/src/declarative/qml/qdeclarativeengine_p.h index d3eb583..a9ba73a 100644 --- a/src/declarative/qml/qdeclarativeengine_p.h +++ b/src/declarative/qml/qdeclarativeengine_p.h @@ -327,6 +327,10 @@ public: static QScriptValue consoleLog(QScriptContext*, QScriptEngine*); static QScriptValue quit(QScriptContext*, QScriptEngine*); + static QScriptValue formatDate(QScriptContext*, QScriptEngine*); + static QScriptValue formatTime(QScriptContext*, QScriptEngine*); + static QScriptValue formatDateTime(QScriptContext*, QScriptEngine*); + static QScriptEngine *getScriptEngine(QDeclarativeEngine *e) { return &e->d_func()->scriptEngine; } static QDeclarativeEngine *getEngine(QScriptEngine *e) { return static_cast<QDeclarativeScriptEngine*>(e)->p->q_func(); } static QDeclarativeEnginePrivate *get(QDeclarativeEngine *e) { return e->d_func(); } |