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 | |
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.
-rw-r--r-- | doc/src/declarative/globalobject.qdoc | 76 | ||||
-rw-r--r-- | src/declarative/qml/qdeclarativeengine.cpp | 65 | ||||
-rw-r--r-- | src/declarative/qml/qdeclarativeengine_p.h | 4 | ||||
-rw-r--r-- | tests/auto/declarative/qdeclarativeqt/data/formatting.qml | 19 | ||||
-rw-r--r-- | tests/auto/declarative/qdeclarativeqt/tst_qdeclarativeqt.cpp | 29 |
5 files changed, 193 insertions, 0 deletions
diff --git a/doc/src/declarative/globalobject.qdoc b/doc/src/declarative/globalobject.qdoc index c718a6d..a8a07d2 100644 --- a/doc/src/declarative/globalobject.qdoc +++ b/doc/src/declarative/globalobject.qdoc @@ -87,6 +87,82 @@ This function returns a Point with the specified \c x and \c y coordinates. This function returns as Size with the specified \c width and \c height. \section3 Qt.vector3d(real x, real y, real z) This function returns a Vector3D with the specified \c x, \c y and \c z. + +\section2 Formatters +The Qt object contains several functions for formatting dates and times. + +\section3 Qt.formatDate(datetime date, variant format) +This function returns the string representation of \c date, formatted according to \c format. +\section3 Qt.formatTime(datetime time, variant format) +This function returns the string representation of \c time, formatted according to \c format. +\section3 Qt.formatDateTime(datetime dateTime, variant format) +This function returns the string representation of \c dateTime, formatted according to \c format. + +\c format for the above formatting functions can be specified as follows. + + These expressions may be used for the date: + + \table + \header \i Expression \i Output + \row \i d \i the day as number without a leading zero (1 to 31) + \row \i dd \i the day as number with a leading zero (01 to 31) + \row \i ddd + \i the abbreviated localized day name (e.g. 'Mon' to 'Sun'). + Uses QDate::shortDayName(). + \row \i dddd + \i the long localized day name (e.g. 'Monday' to 'Qt::Sunday'). + Uses QDate::longDayName(). + \row \i M \i the month as number without a leading zero (1-12) + \row \i MM \i the month as number with a leading zero (01-12) + \row \i MMM + \i the abbreviated localized month name (e.g. 'Jan' to 'Dec'). + Uses QDate::shortMonthName(). + \row \i MMMM + \i the long localized month name (e.g. 'January' to 'December'). + Uses QDate::longMonthName(). + \row \i yy \i the year as two digit number (00-99) + \row \i yyyy \i the year as four digit number + \endtable + + These expressions may be used for the time: + + \table + \header \i Expression \i Output + \row \i h + \i the hour without a leading zero (0 to 23 or 1 to 12 if AM/PM display) + \row \i hh + \i the hour with a leading zero (00 to 23 or 01 to 12 if AM/PM display) + \row \i m \i the minute without a leading zero (0 to 59) + \row \i mm \i the minute with a leading zero (00 to 59) + \row \i s \i the second without a leading zero (0 to 59) + \row \i ss \i the second with a leading zero (00 to 59) + \row \i z \i the milliseconds without leading zeroes (0 to 999) + \row \i zzz \i the milliseconds with leading zeroes (000 to 999) + \row \i AP + \i use AM/PM display. \e AP will be replaced by either "AM" or "PM". + \row \i ap + \i use am/pm display. \e ap will be replaced by either "am" or "pm". + \endtable + + All other input characters will be ignored. Any sequence of characters that + are enclosed in singlequotes will be treated as text and not be used as an + expression. Two consecutive singlequotes ("''") are replaced by a singlequote + in the output. + + Example format strings (assumed that the date and time is 21 May 2001 + 14:13:09): + + \table + \header \i Format \i Result + \row \i dd.MM.yyyy \i 21.05.2001 + \row \i ddd MMMM d yy \i Tue May 21 01 + \row \i hh:mm:ss.zzz \i 14:13:09.042 + \row \i h:m:s ap \i 2:13:9 pm + \endtable + +If no format is specified the locale's short format is used. Alternatively, you can specify +\c Qt.DefaultLocaleLongDate to get the locale's long format. + \section2 Functions The Qt object also contains the following miscellaneous functions which expose Qt functionality for use in QML. 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(); } diff --git a/tests/auto/declarative/qdeclarativeqt/data/formatting.qml b/tests/auto/declarative/qdeclarativeqt/data/formatting.qml new file mode 100644 index 0000000..e62749a --- /dev/null +++ b/tests/auto/declarative/qdeclarativeqt/data/formatting.qml @@ -0,0 +1,19 @@ +import Qt 4.6 + +QtObject { + property date date1: "2008-12-24" + property string test1: Qt.formatDate(date1) + property string test2: Qt.formatDate(date1, Qt.DefaultLocaleLongDate) + property string test3: Qt.formatDate(date1, "ddd MMMM d yy") + + property var time1: new Date(0,0,0,14,15,38,200) + property string test4: Qt.formatTime(time1) + property string test5: Qt.formatTime(time1, Qt.DefaultLocaleLongDate) + property string test6: Qt.formatTime(time1, "H:m:s a") + property string test7: Qt.formatTime(time1, "hh:mm:ss.zzz") + + property var dateTime1: new Date(1978,2,4,9,13,54) + property string test8: Qt.formatDateTime(dateTime1) + property string test9: Qt.formatDateTime(dateTime1, Qt.DefaultLocaleLongDate) + property string test10: Qt.formatDateTime(dateTime1, "M/d/yy H:m:s a") +} diff --git a/tests/auto/declarative/qdeclarativeqt/tst_qdeclarativeqt.cpp b/tests/auto/declarative/qdeclarativeqt/tst_qdeclarativeqt.cpp index 9ec6872..484cbd4 100644 --- a/tests/auto/declarative/qdeclarativeqt/tst_qdeclarativeqt.cpp +++ b/tests/auto/declarative/qdeclarativeqt/tst_qdeclarativeqt.cpp @@ -73,6 +73,7 @@ private slots: void createComponent(); void createQmlObject(); void consoleLog(); + void formatting(); private: QDeclarativeEngine engine; @@ -364,6 +365,34 @@ void tst_qdeclarativeqt::consoleLog() delete object; } +void tst_qdeclarativeqt::formatting() +{ + QDeclarativeComponent component(&engine, TEST_FILE("formatting.qml")); + QObject *object = component.create(); + QVERIFY(object != 0); + + QDate date1(2008,12,24); + QCOMPARE(object->property("date1").toDate(), date1); + QCOMPARE(object->property("test1").toString(), date1.toString(Qt::DefaultLocaleShortDate)); + QCOMPARE(object->property("test2").toString(), date1.toString(Qt::DefaultLocaleLongDate)); + QCOMPARE(object->property("test3").toString(), date1.toString("ddd MMMM d yy")); + + QTime time1(14,15,38,200); + QCOMPARE(object->property("time1").toTime(), time1); + QCOMPARE(object->property("test4").toString(), time1.toString(Qt::DefaultLocaleShortDate)); + QCOMPARE(object->property("test5").toString(), time1.toString(Qt::DefaultLocaleLongDate)); + QCOMPARE(object->property("test6").toString(), time1.toString("H:m:s a")); + QCOMPARE(object->property("test7").toString(), time1.toString("hh:mm:ss.zzz")); + + QDateTime dateTime1(QDate(1978,03,04),QTime(9,13,54)); + QCOMPARE(object->property("dateTime1").toDateTime(),dateTime1); + QCOMPARE(object->property("test8").toString(), dateTime1.toString(Qt::DefaultLocaleShortDate)); + QCOMPARE(object->property("test9").toString(), dateTime1.toString(Qt::DefaultLocaleLongDate)); + QCOMPARE(object->property("test10").toString(), dateTime1.toString("M/d/yy H:m:s a")); + + delete object; +} + QTEST_MAIN(tst_qdeclarativeqt) #include "tst_qdeclarativeqt.moc" |