diff options
author | Alan Alpert <alan.alpert@nokia.com> | 2009-11-11 07:46:20 (GMT) |
---|---|---|
committer | Alan Alpert <alan.alpert@nokia.com> | 2009-11-11 07:46:20 (GMT) |
commit | 8e5166aba8c8d79e47d4c4fccd5acf52f9cffec6 (patch) | |
tree | f9b91b73e52ec389107cc8791ac76e87f962944b /src/declarative/util | |
parent | 6ac7ce79b6041a7c35d08cb28f775dead9701c4b (diff) | |
parent | 0faef2ba318b7df095d5c20fdddd0933ae4912c3 (diff) | |
download | Qt-8e5166aba8c8d79e47d4c4fccd5acf52f9cffec6.zip Qt-8e5166aba8c8d79e47d4c4fccd5acf52f9cffec6.tar.gz Qt-8e5166aba8c8d79e47d4c4fccd5acf52f9cffec6.tar.bz2 |
Merge branch 'kinetic-declarativeui' of git@scm.dev.nokia.troll.no:qt/kinetic into kinetic-declarativeui
Conflicts:
src/declarative/util/util.pri
Diffstat (limited to 'src/declarative/util')
-rw-r--r-- | src/declarative/util/qmlbehavior.cpp | 157 | ||||
-rw-r--r-- | src/declarative/util/qmlbehavior_p.h | 84 | ||||
-rw-r--r-- | src/declarative/util/qmldatetimeformatter.cpp | 370 | ||||
-rw-r--r-- | src/declarative/util/qmldatetimeformatter_p.h | 116 | ||||
-rw-r--r-- | src/declarative/util/qmlfontloader.cpp | 219 | ||||
-rw-r--r-- | src/declarative/util/qmlfontloader_p.h | 94 | ||||
-rw-r--r-- | src/declarative/util/qmllistmodel.cpp | 12 | ||||
-rw-r--r-- | src/declarative/util/qmlnumberformatter.cpp | 215 | ||||
-rw-r--r-- | src/declarative/util/qmlnumberformatter_p.h | 92 | ||||
-rw-r--r-- | src/declarative/util/qmlopenmetaobject_p.h | 2 | ||||
-rw-r--r-- | src/declarative/util/qmlxmllistmodel.cpp | 727 | ||||
-rw-r--r-- | src/declarative/util/qmlxmllistmodel_p.h | 134 | ||||
-rw-r--r-- | src/declarative/util/qnumberformat.cpp | 225 | ||||
-rw-r--r-- | src/declarative/util/qnumberformat_p.h | 173 | ||||
-rw-r--r-- | src/declarative/util/util.pri | 20 |
15 files changed, 2631 insertions, 9 deletions
diff --git a/src/declarative/util/qmlbehavior.cpp b/src/declarative/util/qmlbehavior.cpp new file mode 100644 index 0000000..8364d8c --- /dev/null +++ b/src/declarative/util/qmlbehavior.cpp @@ -0,0 +1,157 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the QtDeclarative module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <private/qobject_p.h> +#include <private/qmlanimation_p.h> +#include <private/qmltransition_p.h> +#include "qmlbehavior_p.h" +#include <QtDeclarative/qmlcontext.h> +#include <QtDeclarative/qmlinfo.h> +#include <QtCore/qparallelanimationgroup.h> + +QT_BEGIN_NAMESPACE + +QML_DEFINE_TYPE(Qt,4,6,Behavior,QmlBehavior) + +class QmlBehaviorPrivate : public QObjectPrivate +{ + Q_DECLARE_PUBLIC(QmlBehavior) +public: + QmlBehaviorPrivate() : animation(0) {} + + QmlMetaProperty property; + QVariant currentValue; + QmlAbstractAnimation *animation; +}; + +/*! + \qmlclass Behavior QmlBehavior + \brief The Behavior element allows you to specify a default animation for a property change. + + In example below, the rect will use a bounce easing curve over 200 millisecond for any changes to its y property: + \code + Rectangle { + width: 20; height: 20 + color: "#00ff00" + y: 200 //initial value + y: Behavior { + NumberAnimation { + easing: "easeOutBounce(amplitude:100)" + duration: 200 + } + } + } + \endcode +*/ + + +QmlBehavior::QmlBehavior(QObject *parent) + : QObject(*(new QmlBehaviorPrivate), parent) +{ +} + +QmlBehavior::~QmlBehavior() +{ +} + +/*! + \qmlproperty Animation Behavior::animation + \default + + The animation to use when the behavior is triggered. +*/ + +QmlAbstractAnimation *QmlBehavior::animation() +{ + Q_D(QmlBehavior); + return d->animation; +} + +void QmlBehavior::setAnimation(QmlAbstractAnimation *animation) +{ + Q_D(QmlBehavior); + if (d->animation) { + qmlInfo(this) << tr("Can't change the animation assigned to a Behavior."); + return; + } + + d->animation = animation; + if (d->animation) + d->animation->setTarget(d->property); +} + +void QmlBehavior::write(const QVariant &value) +{ + Q_D(QmlBehavior); + if (!d->animation) { + d->property.write(value, QmlMetaProperty::BypassInterceptor | QmlMetaProperty::DontRemoveBinding); + return; + } + + d->currentValue = d->property.read(); + + d->animation->qtAnimation()->stop(); + + QmlStateOperation::ActionList actions; + Action action; + action.property = d->property; + action.fromValue = d->currentValue; + action.toValue = value; + actions << action; + + QList<QmlMetaProperty> after; + if (d->animation) + d->animation->transition(actions, after, QmlAbstractAnimation::Forward); + d->animation->qtAnimation()->start(); + if (!after.contains(d->property)) + d->property.write(value, QmlMetaProperty::BypassInterceptor | QmlMetaProperty::DontRemoveBinding); +} + +void QmlBehavior::setTarget(const QmlMetaProperty &property) +{ + Q_D(QmlBehavior); + d->property = property; + d->currentValue = property.read(); + if (d->animation) + d->animation->setTarget(property); +} + +QT_END_NAMESPACE diff --git a/src/declarative/util/qmlbehavior_p.h b/src/declarative/util/qmlbehavior_p.h new file mode 100644 index 0000000..9f85f84 --- /dev/null +++ b/src/declarative/util/qmlbehavior_p.h @@ -0,0 +1,84 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the QtDeclarative module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QMLBEHAVIOR_H +#define QMLBEHAVIOR_H + +#include <QtDeclarative/qmlpropertyvaluesource.h> +#include <QtDeclarative/qmlpropertyvalueinterceptor.h> +#include <QtDeclarative/qml.h> +#include <private/qmlstate_p.h> + +QT_BEGIN_HEADER + +QT_BEGIN_NAMESPACE + +QT_MODULE(Declarative) + +class QmlAbstractAnimation; +class QmlBehaviorPrivate; +class Q_DECLARATIVE_EXPORT QmlBehavior : public QObject, public QmlPropertyValueInterceptor +{ + Q_OBJECT + Q_DECLARE_PRIVATE(QmlBehavior) + + Q_INTERFACES(QmlPropertyValueInterceptor) + Q_CLASSINFO("DefaultProperty", "animation") + Q_PROPERTY(QmlAbstractAnimation *animation READ animation WRITE setAnimation) + +public: + QmlBehavior(QObject *parent=0); + ~QmlBehavior(); + + virtual void setTarget(const QmlMetaProperty &); + virtual void write(const QVariant &value); + + QmlAbstractAnimation *animation(); + void setAnimation(QmlAbstractAnimation *); +}; + +QT_END_NAMESPACE + +QML_DECLARE_TYPE(QmlBehavior) + +QT_END_HEADER + +#endif // QMLBEHAVIOR_H diff --git a/src/declarative/util/qmldatetimeformatter.cpp b/src/declarative/util/qmldatetimeformatter.cpp new file mode 100644 index 0000000..983287e --- /dev/null +++ b/src/declarative/util/qmldatetimeformatter.cpp @@ -0,0 +1,370 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the QtDeclarative module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "qmldatetimeformatter_p.h" +#include "private/qobject_p.h" +#include <QtCore/qlocale.h> + +QT_BEGIN_NAMESPACE + +//TODO: may need optimisation as the QDateTime member may not be needed? +// be able to set a locale? + +class QmlDateTimeFormatterPrivate : public QObjectPrivate +{ + Q_DECLARE_PUBLIC(QmlDateTimeFormatter) +public: + QmlDateTimeFormatterPrivate() : locale(QLocale::system()), longStyle(false), componentComplete(true) {} + + void updateText(); + + QDateTime dateTime; + QDate date; + QTime time; + QLocale locale; + QString dateTimeText; + QString dateText; + QString timeText; + QString dateTimeFormat; //set for convienience? + QString dateFormat; + QString timeFormat; + bool longStyle; + bool componentComplete; +}; + +/*! + \qmlclass DateTimeFormatter QmlDateTimeFormatter + \brief The DateTimeFormatter allows you to control the format of a date string. + + \code + DateTimeFormatter { id: formatter; date: System.date } + Text { text: formatter.dateText } + \endcode + + By default, the text properties (dateText, timeText, and dateTimeText) will return the + date and time using the current system locale's format. +*/ + +/*! + \internal + \class QmlDateTimeFormatter + \ingroup group_utility + \brief The QmlDateTimeFormatter class allows you to format a date string. +*/ + +QmlDateTimeFormatter::QmlDateTimeFormatter(QObject *parent) +: QObject(*(new QmlDateTimeFormatterPrivate), parent) +{ +} + +QmlDateTimeFormatter::~QmlDateTimeFormatter() +{ +} + +/*! + \qmlproperty string DateTimeFormatter::dateText + \qmlproperty string DateTimeFormatter::timeText + \qmlproperty string DateTimeFormatter::dateTimeText + + Formatted text representations of the \c date, \c time, + and \c {date and time}, respectively. + + If there is no explictly specified format the DateTimeFormatter + will use the system locale's default 'short' setting. + + \code + // specify source date (assuming today is February 19, 2009) + DateTimeFormatter { id: formatter; dateTime: Today.date } + + // display the full date and time + Text { text: formatter.dateText } + \endcode + + Would be equivalent to the following for a US English locale: + + \code + // display the date + Text { text: "2/19/09" } + \endcode +*/ +QString QmlDateTimeFormatter::dateTimeText() const +{ + Q_D(const QmlDateTimeFormatter); + return d->dateTimeText; +} + +QString QmlDateTimeFormatter::dateText() const +{ + Q_D(const QmlDateTimeFormatter); + return d->dateText; +} + +QString QmlDateTimeFormatter::timeText() const +{ + Q_D(const QmlDateTimeFormatter); + return d->timeText; +} + +/*! + \qmlproperty date DateTimeFormatter::date + \qmlproperty time DateTimeFormatter::time + \qmlproperty datetime DateTimeFormatter::dateTime + + The source date and time to be used by the formatter. + + \code + // setting the date and time + DateTimeFormatter { date: System.date; time: System.time } + \endcode + + For convienience it is possible to set the datetime property to set both the date and the time. + \code + // setting the datetime + DateTimeFormatter { dateTime: System.dateTime } + \endcode + + There can only be one instance of date and time per formatter; if date, time, and dateTime are all + set the actual date and time used is not guaranteed. + + \note If no date is set, dateTimeText will be just the date; + If no time is set, the dateTimeText will be just the time. + +*/ +QDate QmlDateTimeFormatter::date() const +{ + Q_D(const QmlDateTimeFormatter); + return d->date; +} + +QTime QmlDateTimeFormatter::time() const +{ + Q_D(const QmlDateTimeFormatter); + return d->time; +} + +QDateTime QmlDateTimeFormatter::dateTime() const +{ + Q_D(const QmlDateTimeFormatter); + return d->dateTime; +} + +/*! + \qmlproperty string DateTimeFormatter::dateFormat + \qmlproperty string DateTimeFormatter::timeFormat + \qmlproperty string DateTimeFormatter::dateTimeFormat + + Specifies a custom format which the DateTime Formatter can use. + + If there is no explictly specified format the DateTimeFormatter + will use the system locale's default 'short' setting. + + The text's format may be modified by setting: + \list + \i \c dateFormat + \i \c timeFormat + \i \c dateTimeFormat + \endlist + + If only the format for date is defined, the time and dateTime formats will be defined + as the system locale default and likewise for the others. + + Syntax for the format is based on the QDateTime::toString() formatting options. + + \code + // Format the date such that the dateText is: '1997-12-12' + DateTimeFormatter { id: formatter; dateTime: Today.dateTime; formatDate: "yyyy-MM-d" } + \endcode + + Assigning an empty string to a particular format will reset it. +*/ +QString QmlDateTimeFormatter::dateTimeFormat() const +{ + Q_D(const QmlDateTimeFormatter); + return d->dateTimeFormat; +} + +QString QmlDateTimeFormatter::dateFormat() const +{ + Q_D(const QmlDateTimeFormatter); + return d->dateFormat; +} + +QString QmlDateTimeFormatter::timeFormat() const +{ + Q_D(const QmlDateTimeFormatter); + return d->timeFormat; +} + +/*! + \qmlproperty bool DateTimeFormatter::longStyle + + This property causes the formatter to use the system locale's long format rather than short format + by default. + + This setting is off by default. +*/ +bool QmlDateTimeFormatter::longStyle() const +{ + Q_D(const QmlDateTimeFormatter); + return d->longStyle; +} + +void QmlDateTimeFormatter::setDateTime(const QDateTime &dateTime) +{ + Q_D(QmlDateTimeFormatter); + if (d->dateTime == dateTime) + return; + d->dateTime = dateTime; + d->date = d->dateTime.date(); + d->time = d->dateTime.time(); + d->updateText(); +} + +void QmlDateTimeFormatter::setTime(const QTime &time) +{ + Q_D(QmlDateTimeFormatter); + if (d->dateTime.time() == time) + return; + d->time = time; + d->dateTime.setTime(time); + d->updateText(); +} + +void QmlDateTimeFormatter::setDate(const QDate &date) +{ + Q_D(QmlDateTimeFormatter); + if (d->dateTime.date() == date) + return; + d->date = date; + bool clearTime = d->dateTime.time().isValid() ? false : true; //because setting date generates default time + d->dateTime.setDate(date); + if (clearTime) + d->dateTime.setTime(QTime()); + d->updateText(); +} + +//DateTime formatting may be a combination of date and time? +void QmlDateTimeFormatter::setDateTimeFormat(const QString &format) +{ + Q_D(QmlDateTimeFormatter); + //no format checking + d->dateTimeFormat = format; + d->updateText(); +} + +void QmlDateTimeFormatter::setDateFormat(const QString &format) +{ + Q_D(QmlDateTimeFormatter); + //no format checking + d->dateFormat = format; + d->updateText(); +} + +void QmlDateTimeFormatter::setTimeFormat(const QString &format) +{ + Q_D(QmlDateTimeFormatter); + //no format checking + d->timeFormat = format; + d->updateText(); +} + +void QmlDateTimeFormatter::setLongStyle(bool longStyle) +{ + Q_D(QmlDateTimeFormatter); + d->longStyle = longStyle; + d->updateText(); +} + +void QmlDateTimeFormatterPrivate::updateText() +{ + Q_Q(QmlDateTimeFormatter); + if (!componentComplete) + return; + + QString str; + QString str1; + QString str2; + + Qt::DateFormat defaultFormat = longStyle ? Qt::SystemLocaleLongDate : Qt::SystemLocaleShortDate; + + if (dateFormat.isEmpty()) + str1 = date.toString(defaultFormat); + else + str1 = date.toString(dateFormat); + + if (timeFormat.isEmpty()) + str2 = time.toString(defaultFormat); + else + str2 = time.toString(timeFormat); + + if (dateTimeFormat.isEmpty()) + str = dateTime.toString(defaultFormat); + //else if (!formatTime.isEmpty() && !formatDate.isEmpty()) + // str = str1 + QLatin1Char(' ') + str2; + else + str = dateTime.toString(dateTimeFormat); + + if (dateTimeText == str && dateText == str1 && timeText == str2) + return; + + dateTimeText = str; + dateText = str1; + timeText = str2; + + emit q->textChanged(); +} + +void QmlDateTimeFormatter::classBegin() +{ + Q_D(QmlDateTimeFormatter); + d->componentComplete = false; +} + +void QmlDateTimeFormatter::componentComplete() +{ + Q_D(QmlDateTimeFormatter); + d->componentComplete = true; + d->updateText(); +} + +QML_DEFINE_TYPE(Qt,4,6,DateTimeFormatter,QmlDateTimeFormatter) + +QT_END_NAMESPACE diff --git a/src/declarative/util/qmldatetimeformatter_p.h b/src/declarative/util/qmldatetimeformatter_p.h new file mode 100644 index 0000000..e350f96 --- /dev/null +++ b/src/declarative/util/qmldatetimeformatter_p.h @@ -0,0 +1,116 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the QtDeclarative module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QMLDATETIMEFORMATTER_H +#define QMLDATETIMEFORMATTER_H + +#include <QtCore/qdatetime.h> +#include <QtDeclarative/qml.h> + +QT_BEGIN_HEADER + +QT_BEGIN_NAMESPACE + +QT_MODULE(Declarative) + +class QmlDateTimeFormatterPrivate; +class Q_DECLARATIVE_EXPORT QmlDateTimeFormatter : public QObject, public QmlParserStatus +{ + Q_OBJECT + Q_INTERFACES(QmlParserStatus) + + Q_PROPERTY(QString dateText READ dateText NOTIFY textChanged) + Q_PROPERTY(QString timeText READ timeText NOTIFY textChanged) + Q_PROPERTY(QString dateTimeText READ dateTimeText NOTIFY textChanged) + Q_PROPERTY(QDate date READ date WRITE setDate) + Q_PROPERTY(QTime time READ time WRITE setTime) + Q_PROPERTY(QDateTime dateTime READ dateTime WRITE setDateTime) + Q_PROPERTY(QString dateFormat READ dateFormat WRITE setDateFormat) + Q_PROPERTY(QString timeFormat READ timeFormat WRITE setTimeFormat) + Q_PROPERTY(QString dateTimeFormat READ dateTimeFormat WRITE setDateTimeFormat) + Q_PROPERTY(bool longStyle READ longStyle WRITE setLongStyle) +public: + QmlDateTimeFormatter(QObject *parent=0); + ~QmlDateTimeFormatter(); + + QString dateTimeText() const; + QString dateText() const; + QString timeText() const; + + QDate date() const; + void setDate(const QDate &); + + QTime time() const; + void setTime(const QTime &); + + QDateTime dateTime() const; + void setDateTime(const QDateTime &); + + QString dateTimeFormat() const; + void setDateTimeFormat(const QString &); + + QString dateFormat() const; + void setDateFormat(const QString &); + + QString timeFormat() const; + void setTimeFormat(const QString &); + + bool longStyle() const; + void setLongStyle(bool); + + virtual void classBegin(); + virtual void componentComplete(); + +Q_SIGNALS: + void textChanged(); + +private: + Q_DISABLE_COPY(QmlDateTimeFormatter) + Q_DECLARE_PRIVATE(QmlDateTimeFormatter) +}; + +QT_END_NAMESPACE + +QML_DECLARE_TYPE(QmlDateTimeFormatter) + +QT_END_HEADER + +#endif diff --git a/src/declarative/util/qmlfontloader.cpp b/src/declarative/util/qmlfontloader.cpp new file mode 100644 index 0000000..36ee1bb --- /dev/null +++ b/src/declarative/util/qmlfontloader.cpp @@ -0,0 +1,219 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the QtDeclarative module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "private/qobject_p.h" +#include "qmlfontloader_p.h" +#include <QUrl> +#include <QDebug> +#include <QNetworkRequest> +#include <QNetworkReply> +#include <QFile> +#include <QmlContext> +#include <QtDeclarative/qmlengine.h> +#include <QFontDatabase> + +QT_BEGIN_NAMESPACE + +class QmlFontLoaderPrivate : public QObjectPrivate +{ + Q_DECLARE_PUBLIC(QmlFontLoader) + +public: + QmlFontLoaderPrivate() : reply(0), status(QmlFontLoader::Null) {} + + void addFontToDatabase(const QByteArray &); + + QUrl url; + QString name; + QNetworkReply *reply; + QmlFontLoader::Status status; +}; + +QML_DEFINE_TYPE(Qt,4,6,FontLoader,QmlFontLoader) + +/*! + \qmlclass FontLoader QmlFontLoader + \ingroup group_utility + \brief This item allows using fonts by name or url. + + Example: + \qml + FontLoader { id: fixedFont; name: "Courier" } + FontLoader { id: webFont; source: "http://www.mysite.com/myfont.ttf" } + + Text { text: "Fixed-size font"; font.family: fixedFont.name } + Text { text: "Fancy font"; font.family: webFont.name } + \endqml +*/ +QmlFontLoader::QmlFontLoader(QObject *parent) + : QObject(*(new QmlFontLoaderPrivate), parent) +{ +} + +QmlFontLoader::~QmlFontLoader() +{ +} + +static QString toLocalFileOrQrc(const QUrl& url) +{ + QString r = url.toLocalFile(); + if (r.isEmpty() && url.scheme() == QLatin1String("qrc")) + r = QLatin1Char(':') + url.path(); + return r; +} + + +/*! + \qmlproperty url FontLoader::source + The url of the font to load. +*/ +QUrl QmlFontLoader::source() const +{ + Q_D(const QmlFontLoader); + return d->url; +} + +void QmlFontLoader::setSource(const QUrl &url) +{ + Q_D(QmlFontLoader); + if (url == d->url) + return; + d->url = qmlContext(this)->resolvedUrl(url); + + d->status = Loading; + emit statusChanged(); +#ifndef QT_NO_LOCALFILE_OPTIMIZED_QML + QString lf = toLocalFileOrQrc(d->url); + if (!lf.isEmpty()) { + QFile file(lf); + file.open(QIODevice::ReadOnly); + QByteArray ba = file.readAll(); + d->addFontToDatabase(ba); + } else +#endif + { + QNetworkRequest req(d->url); + d->reply = qmlEngine(this)->networkAccessManager()->get(req); + QObject::connect(d->reply, SIGNAL(finished()), this, SLOT(replyFinished())); + } +} + +/*! + \qmlproperty string FontLoader::name + + This property holds the name of the font family. + It is set automatically when a font is loaded using the \c url property. + + Use this to set the \c font.family property of a \c Text item. + + Example: + \qml + FontLoader { id: webFont; source: "http://www.mysite.com/myfont.ttf" } + Text { text: "Fancy font"; font.family: webFont.name } + \endqml +*/ +QString QmlFontLoader::name() const +{ + Q_D(const QmlFontLoader); + return d->name; +} + +void QmlFontLoader::setName(const QString &name) +{ + Q_D(QmlFontLoader); + if (d->name == name ) + return; + d->name = name; + emit nameChanged(); + d->status = Ready; + emit statusChanged(); +} + +/*! + \qmlproperty enum FontLoader::status + + This property holds the status of font loading. It can be one of: + \list + \o Null - no font has been set + \o Ready - the font has been loaded + \o Loading - the font is currently being loaded + \o Error - an error occurred while loading the font + \endlist +*/ +QmlFontLoader::Status QmlFontLoader::status() const +{ + Q_D(const QmlFontLoader); + return d->status; +} + +void QmlFontLoader::replyFinished() +{ + Q_D(QmlFontLoader); + if (d->reply) { + if (!d->reply->error()) { + QByteArray ba = d->reply->readAll(); + d->addFontToDatabase(ba); + } else { + d->status = Error; + emit statusChanged(); + } + d->reply->deleteLater(); + d->reply = 0; + } +} + +void QmlFontLoaderPrivate::addFontToDatabase(const QByteArray &ba) +{ + Q_Q(QmlFontLoader); + + int id = QFontDatabase::addApplicationFontFromData(ba); + if (id != -1) { + name = QFontDatabase::applicationFontFamilies(id).at(0); + emit q->nameChanged(); + status = QmlFontLoader::Ready; + } else { + status = QmlFontLoader::Error; + qWarning() << "Cannot load font:" << url; + } + emit q->statusChanged(); +} + +QT_END_NAMESPACE diff --git a/src/declarative/util/qmlfontloader_p.h b/src/declarative/util/qmlfontloader_p.h new file mode 100644 index 0000000..d02a181 --- /dev/null +++ b/src/declarative/util/qmlfontloader_p.h @@ -0,0 +1,94 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the QtDeclarative module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QMLFONTLOADER_H +#define QMLFONTLOADER_H + +#include <QtCore/qobject.h> +#include <QtDeclarative/qml.h> + +QT_BEGIN_HEADER + +QT_BEGIN_NAMESPACE + +QT_MODULE(Declarative) + +class QmlFontLoaderPrivate; +class Q_DECLARATIVE_EXPORT QmlFontLoader : public QObject +{ + Q_OBJECT + Q_DECLARE_PRIVATE(QmlFontLoader) + Q_ENUMS(Status) + + Q_PROPERTY(QUrl source READ source WRITE setSource NOTIFY sourceChanged) + Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged) + Q_PROPERTY(Status status READ status NOTIFY statusChanged) + +public: + enum Status { Null = 0, Ready, Loading, Error }; + + QmlFontLoader(QObject *parent = 0); + ~QmlFontLoader(); + + QUrl source() const; + void setSource(const QUrl &url); + + QString name() const; + void setName(const QString &name); + + Status status() const; + +private Q_SLOTS: + void replyFinished(); + +Q_SIGNALS: + void nameChanged(); + void statusChanged(); +}; + +QT_END_NAMESPACE + +QML_DECLARE_TYPE(QmlFontLoader) + +QT_END_HEADER + +#endif // QMLFONTLOADER_H + diff --git a/src/declarative/util/qmllistmodel.cpp b/src/declarative/util/qmllistmodel.cpp index 19282eb..9e91147 100644 --- a/src/declarative/util/qmllistmodel.cpp +++ b/src/declarative/util/qmllistmodel.cpp @@ -467,7 +467,7 @@ void QmlListModel::remove(int index) } /*! - \qmlmethod ListModel::insert(index,dict) + \qmlmethod ListModel::insert(int index, jsobject dict) Adds a new item to the list model at position \a index, with the values in \a dict. @@ -500,7 +500,7 @@ void QmlListModel::insert(int index, const QScriptValue& valuemap) } /*! - \qmlmethod ListModel::move(from,to,n) + \qmlmethod ListModel::move(int from, int to, int n) Moves \a n items \a from one position \a to another. @@ -551,7 +551,7 @@ void QmlListModel::move(int from, int to, int n) } /*! - \qmlmethod ListModel::append(dict) + \qmlmethod ListModel::append(jsobject dict) Adds a new item to the end of the list model, with the values in \a dict. @@ -578,7 +578,7 @@ void QmlListModel::append(const QScriptValue& valuemap) } /*! - \qmlmethod object ListModel::get(index) + \qmlmethod object ListModel::get(int index) Returns the item at \a index in the list model. @@ -620,7 +620,7 @@ QScriptValue QmlListModel::get(int index) const } /*! - \qmlmethod ListModel::set(index,dict) + \qmlmethod ListModel::set(int index, jsobject dict) Changes the item at \a index in the list model with the values in \a dict. Properties not appearing in \a valuemap @@ -663,7 +663,7 @@ void QmlListModel::set(int index, const QScriptValue& valuemap) } /*! - \qmlmethod ListModel::set(index,property,value) + \qmlmethod ListModel::set(int index, string property, variant value) Changes the \a property of the item at \a index in the list model to \a value. diff --git a/src/declarative/util/qmlnumberformatter.cpp b/src/declarative/util/qmlnumberformatter.cpp new file mode 100644 index 0000000..b09be5b --- /dev/null +++ b/src/declarative/util/qmlnumberformatter.cpp @@ -0,0 +1,215 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the QtDeclarative module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "qmlnumberformatter_p.h" +#include "private/qobject_p.h" + +QT_BEGIN_NAMESPACE + +//TODO: set locale +// docs +// this is a wrapper around qnumberformat (test integration) +// if number or format haven't been explictly set, text should be an empty string + +class QmlNumberFormatterPrivate : public QObjectPrivate +{ + Q_DECLARE_PUBLIC(QmlNumberFormatter) +public: + QmlNumberFormatterPrivate() : locale(QLocale::system()), number(0), componentComplete(true) {} + + void updateText(); + + QLocale locale; + QString format; + QNumberFormat numberFormat; + QString text; + qreal number; + bool componentComplete; +}; +/*! + \qmlclass NumberFormatter + \brief The NumberFormatter allows you to control the format of a number string. + + The format property documentation has more details on how the format can be manipulated. + + In the following example, the text element will display the text "1,234.57". + \code + NumberFormatter { id: formatter; number: 1234.5678; format: "##,##0.##" } + Text { text: formatter.text } + \endcode + + */ +/*! + \internal + \class QmlNumberFormatter + \ingroup group_utility + \brief The QmlNumberFormatter class allows you to format a number to a particular string format/locale specific number format. +*/ + +QmlNumberFormatter::QmlNumberFormatter(QObject *parent) +: QObject(*(new QmlNumberFormatterPrivate), parent) +{ +} + +QmlNumberFormatter::~QmlNumberFormatter() +{ +} + +/*! + \qmlproperty string NumberFormatter::text + + The number in the specified format. + <br> + If no format is specified the text will be empty. +*/ + +QString QmlNumberFormatter::text() const +{ + Q_D(const QmlNumberFormatter); + return d->text; +} + +/*! + \qmlproperty real NumberFormatter::number + + A single point precision number. (Doubles are not yet supported) + +*/ +qreal QmlNumberFormatter::number() const +{ + Q_D(const QmlNumberFormatter); + return d->number; +} + +/*! + \qmlproperty string NumberFormatter::format + + The particular format the number will adhere to during the conversion to text. + <br> + The format syntax follows a style similar to the Unicode Standard (UTS35). + + The table below shows the characters, patterns that can be used in the format. + + <table border="0" align="center"> + <tr style="background-color: #D6E2E8"><th> Character </th><th> Meaning </th></tr> + <tr><td> # </td><td> Any digit(s), zero shows as absent (for leading/trailing zeroes) </td></tr> + <tr><td> 0 </td><td> Implicit digit. Zero will show in the case that the input number is too small.</td></tr> + <tr><td> . </td><td> Decimal separator. Output decimal seperator will be dependant on system locale.</td></tr> + <tr><td> , </td><td> Grouping separator. The number of digits (either #, or 0) between the grouping separator and the decimal (or the rightmost digit) will determine the groupingSize)</td></tr> + <tr><td> other </td><td> Any other character will be taken as a string literal and placed directly into the output string </td></tr> + </table> + + Invalid formats will not guarantee a meaningful text output.<br> + + \note <i>Input numbers that are too long for the given format will be rounded dependent on precison based on the position of the decimal point </i> + + The following table illustrates the output text created by applying some examples of numeric formats to the formatter. + + <table border="0" align="center"> + <tr style="background-color: #D6E2E8"><th> Format </th><th> Number </th><th> Output </th></tr> + <tr><td> ### </td><td> 123456 </td><td> 123456 </td></tr> + <tr><td> 000 </td><td> 123456 </td><td> 123456 </td></tr> + <tr><td> ###### </td><td> 1234 </td><td> 1234 </td></tr> + <tr><td> 000000 </td><td> 1234 </td><td> 001234 </td></tr> + <tr><td> ##,##0.## </td><td> 1234.456 </td><td> 1,234.46 (for US locale)<br> 1 234,46 (for FR locale)</td></tr> + <tr><td> 000000,000.# </td><td> 123456 </td><td> 000,123,456 (for US locale)<br> 000 123 456 (for FR locale)</td></tr> + <tr><td> 0.0### </td><td> 0.999997 </td><td> 1.0 </td></tr> + <tr><td> (000) 000 - 000 </td><td> 12345678 </td><td> (012) 345 - 678 </td></tr> + <tr><td> #A</td><td>12</td><td>12A</td></tr> + </table> + +*/ +QString QmlNumberFormatter::format() const +{ + Q_D(const QmlNumberFormatter); + return d->format; +} + +void QmlNumberFormatter::setNumber(const qreal &number) +{ + Q_D(QmlNumberFormatter); + if (d->number == number) + return; + d->number = number; + d->updateText(); +} + +void QmlNumberFormatter::setFormat(const QString &format) +{ + Q_D(QmlNumberFormatter); + //no format checking + if (format.isEmpty()) + d->format = QString::null; + else + d->format = format; + d->updateText(); +} + +void QmlNumberFormatterPrivate::updateText() +{ + Q_Q(QmlNumberFormatter); + if (!componentComplete) + return; + + QNumberFormat tempFormat; + tempFormat.setFormat(format); + tempFormat.setNumber(number); + + text = tempFormat.text(); + + emit q->textChanged(); +} + +void QmlNumberFormatter::classBegin() +{ + Q_D(QmlNumberFormatter); + d->componentComplete = false; +} + +void QmlNumberFormatter::componentComplete() +{ + Q_D(QmlNumberFormatter); + d->componentComplete = true; + d->updateText(); +} +QML_DEFINE_TYPE(Qt,4,6,NumberFormatter,QmlNumberFormatter); + +QT_END_NAMESPACE diff --git a/src/declarative/util/qmlnumberformatter_p.h b/src/declarative/util/qmlnumberformatter_p.h new file mode 100644 index 0000000..0036cb9 --- /dev/null +++ b/src/declarative/util/qmlnumberformatter_p.h @@ -0,0 +1,92 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the QtDeclarative module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QMLNUMBERFORMATTER_H +#define QMLNUMBERFORMATTER_H + +#include <QtDeclarative/qml.h> +#include <private/qnumberformat_p.h> + +QT_BEGIN_HEADER + +QT_BEGIN_NAMESPACE + +QT_MODULE(Declarative) + +class QmlNumberFormatterPrivate; +class Q_DECLARATIVE_EXPORT QmlNumberFormatter : public QObject, public QmlParserStatus +{ + Q_OBJECT + Q_INTERFACES(QmlParserStatus) + + Q_PROPERTY(QString text READ text NOTIFY textChanged) + Q_PROPERTY(QString format READ format WRITE setFormat) + Q_PROPERTY(qreal number READ number WRITE setNumber) +public: + QmlNumberFormatter(QObject *parent=0); + ~QmlNumberFormatter(); + + QString text() const; + + qreal number() const; + void setNumber(const qreal &); + + QString format() const; + void setFormat(const QString &); + + virtual void classBegin(); + virtual void componentComplete(); + +Q_SIGNALS: + void textChanged(); + +private: + Q_DISABLE_COPY(QmlNumberFormatter) + Q_DECLARE_PRIVATE(QmlNumberFormatter) +}; + +QT_END_NAMESPACE + +QML_DECLARE_TYPE(QmlNumberFormatter) + +QT_END_HEADER + +#endif diff --git a/src/declarative/util/qmlopenmetaobject_p.h b/src/declarative/util/qmlopenmetaobject_p.h index 1cf12f8..7a5b4ce 100644 --- a/src/declarative/util/qmlopenmetaobject_p.h +++ b/src/declarative/util/qmlopenmetaobject_p.h @@ -43,7 +43,7 @@ #define QMLOPENMETAOBJECT_H #include <QtCore/QMetaObject> -#include <QtCore/private/qobject_p.h> +#include <private/qobject_p.h> #include <QtCore/QObject> QT_BEGIN_HEADER diff --git a/src/declarative/util/qmlxmllistmodel.cpp b/src/declarative/util/qmlxmllistmodel.cpp new file mode 100644 index 0000000..3d90b44 --- /dev/null +++ b/src/declarative/util/qmlxmllistmodel.cpp @@ -0,0 +1,727 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the QtDeclarative module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "qmlxmllistmodel_p.h" +#include "private/qobject_p.h" + +#include <QtDeclarative/qmlcontext.h> +#include <QtDeclarative/qmlengine.h> +#include <QDebug> +#include <QApplication> +#include <QThread> +#include <QMutex> +#include <QWaitCondition> +#include <QXmlQuery> +#include <QXmlResultItems> +#include <QXmlNodeModelIndex> +#include <QBuffer> +#include <QNetworkRequest> +#include <QNetworkReply> + +QT_BEGIN_NAMESPACE + +QML_DEFINE_TYPE(Qt,4,6,XmlRole,QmlXmlListModelRole) +QML_DEFINE_TYPE(Qt,4,6,XmlListModel,QmlXmlListModel) + +/*! + \qmlclass XmlRole QmlXmlListModelRole + \brief The XmlRole element allows you to specify a role for an XmlListModel. +*/ + +/*! + \qmlproperty string XmlRole::name + The name for the role. This name is used to access the model data for this role from Qml. + + \qml + XmlRole { name: "title"; query: "title/string()" } + + ... + + Component { + id: myDelegate + Text { text: title } + } + \endqml +*/ + +/*! + \qmlproperty string XmlRole::query + The relative XPath query for this role. The query should not start with a '/' (i.e. it must be + relative). + + \qml + XmlRole { name: "title"; query: "title/string()" } + \endqml +*/ + +class Q_DECLARATIVE_EXPORT QmlXmlListModelRole : public QObject +{ + Q_OBJECT + Q_PROPERTY(QString name READ name WRITE setName) + Q_PROPERTY(QString query READ query WRITE setQuery) + +public: + QmlXmlListModelRole() {} + ~QmlXmlListModelRole() {} + + QString name() const { return m_name; } + void setName(const QString &name) { m_name = name; } + + QString query() const { return m_query; } + void setQuery(const QString &query) + { + if (query.startsWith(QLatin1Char('/'))) { + qmlInfo(this) << tr("An XmlRole query must not start with '/'"); + return; + } + m_query = query; + } + + bool isValid() { + return !m_name.isEmpty() && !m_query.isEmpty(); + } + +private: + QString m_name; + QString m_query; +}; + +QML_DECLARE_TYPE(QmlXmlListModelRole) + +class QmlXmlListModelPrivate; +struct QmlXmlRoleList : public QmlConcreteList<QmlXmlListModelRole *> +{ + QmlXmlRoleList(QmlXmlListModelPrivate *p) + : model(p) {} + virtual void append(QmlXmlListModelRole *role); + //XXX clear, removeAt, and insert need to invalidate any cached data (in data table) as well + // (and the model should emit the appropriate signals) + virtual void clear(); + virtual void removeAt(int i); + virtual void insert(int i, QmlXmlListModelRole *role); + + QmlXmlListModelPrivate *model; +}; + +class QmlXmlQuery : public QThread +{ + Q_OBJECT +public: + QmlXmlQuery(QObject *parent=0) + : QThread(parent), m_quit(false), m_restart(false), m_abort(false), m_queryId(0) { + } + ~QmlXmlQuery() { + m_mutex.lock(); + m_quit = true; + m_condition.wakeOne(); + m_mutex.unlock(); + + wait(); + } + + void abort() { + QMutexLocker locker(&m_mutex); + m_abort = true; + } + + int doQuery(QString query, QString namespaces, QByteArray data, QmlXmlRoleList *roleObjects) { + QMutexLocker locker(&m_mutex); + m_modelData.clear(); + m_size = 0; + m_data = data; + m_query = QLatin1String("doc($src)") + query; + m_namespaces = namespaces; + m_roleObjects = roleObjects; + if (!isRunning()) { + m_abort = false; + start(); + } else { + m_restart = true; + m_condition.wakeOne(); + } + m_queryId++; + return m_queryId; + } + + QList<QList<QVariant> > modelData() { + QMutexLocker locker(&m_mutex); + return m_modelData; + } + +Q_SIGNALS: + void queryCompleted(int queryId, int size); + +protected: + void run() { + while (!m_quit) { + m_mutex.lock(); + int queryId = m_queryId; + doQueryJob(); + if (m_size > 0) + doSubQueryJob(); + m_data.clear(); // no longer needed + m_mutex.unlock(); + + m_mutex.lock(); + if (!m_abort && m_size > 0) + emit queryCompleted(queryId, m_size); + if (!m_restart) + m_condition.wait(&m_mutex); + m_abort = false; + m_restart = false; + m_mutex.unlock(); + } + } + +private: + void doQueryJob(); + void doSubQueryJob(); + +private: + QMutex m_mutex; + QWaitCondition m_condition; + bool m_quit; + bool m_restart; + bool m_abort; + QByteArray m_data; + QString m_query; + QString m_namespaces; + QString m_prefix; + int m_size; + int m_queryId; + const QmlXmlRoleList *m_roleObjects; + QList<QList<QVariant> > m_modelData; +}; + +void QmlXmlQuery::doQueryJob() +{ + QString r; + QXmlQuery query; + QBuffer buffer(&m_data); + buffer.open(QIODevice::ReadOnly); + query.bindVariable(QLatin1String("src"), &buffer); + query.setQuery(m_namespaces + m_query); + query.evaluateTo(&r); + + //qDebug() << r; + + //always need a single root element + QByteArray xml = "<dummy:items xmlns:dummy=\"http://qtsotware.com/dummy\">\n" + r.toUtf8() + "</dummy:items>"; + QBuffer b(&xml); + b.open(QIODevice::ReadOnly); + //qDebug() << xml; + + QString namespaces = QLatin1String("declare namespace dummy=\"http://qtsotware.com/dummy\";\n") + m_namespaces; + QString prefix = QLatin1String("doc($inputDocument)/dummy:items") + + m_query.mid(m_query.lastIndexOf(QLatin1Char('/'))); + + //figure out how many items we are dealing with + int count = -1; + { + QXmlResultItems result; + QXmlQuery countquery; + countquery.bindVariable(QLatin1String("inputDocument"), &b); + countquery.setQuery(namespaces + QLatin1String("count(") + prefix + QLatin1String(")")); + countquery.evaluateTo(&result); + QXmlItem item(result.next()); + if (item.isAtomicValue()) + count = item.toAtomicValue().toInt(); + } + //qDebug() << count; + + m_prefix = namespaces + prefix + QLatin1String("/"); + m_data = xml; + if (count > 0) + m_size = count; +} + +void QmlXmlQuery::doSubQueryJob() +{ + m_modelData.clear(); + + QBuffer b(&m_data); + b.open(QIODevice::ReadOnly); + + QXmlQuery subquery; + subquery.bindVariable(QLatin1String("inputDocument"), &b); + + //### we might be able to condense even further (query for everything in one go) + for (int i = 0; i < m_roleObjects->size(); ++i) { + QmlXmlListModelRole *role = m_roleObjects->at(i); + if (!role->isValid()) { + QList<QVariant> resultList; + for (int j = 0; j < m_size; ++j) + resultList << QVariant(); + m_modelData << resultList; + continue; + } + subquery.setQuery(m_prefix + QLatin1String("(let $v := ") + role->query() + QLatin1String(" return if ($v) then ") + role->query() + QLatin1String(" else \"\")")); + QXmlResultItems output3; + subquery.evaluateTo(&output3); + QXmlItem item(output3.next()); + QList<QVariant> resultList; + while (!item.isNull()) { + resultList << item.toAtomicValue(); //### we used to trim strings + item = output3.next(); + } + //### should warn here if things have gone wrong. + while (resultList.count() < m_size) + resultList << QVariant(); + m_modelData << resultList; + b.seek(0); + } + + //XXX this method is much slower, but would work better for incremental loading + /*for (int j = 0; j < m_size; ++j) { + QList<QVariant> resultList; + for (int i = 0; i < m_roleObjects->size(); ++i) { + QmlXmlListModelRole *role = m_roleObjects->at(i); + subquery.setQuery(m_prefix.arg(j+1) + role->query()); + if (role->isStringList()) { + QStringList data; + subquery.evaluateTo(&data); + resultList << QVariant(data); + //qDebug() << data; + } else { + QString s; + subquery.evaluateTo(&s); + if (role->isCData()) { + //un-escape + s.replace(QLatin1String("<"), QLatin1String("<")); + s.replace(QLatin1String(">"), QLatin1String(">")); + s.replace(QLatin1String("&"), QLatin1String("&")); + } + resultList << s.trimmed(); + //qDebug() << s; + } + b.seek(0); + } + m_modelData << resultList; + }*/ +} + + +//TODO: error handling (currently quite fragile) +// profile doQuery and doSubquery +// support complex/nested objects? +// how do we handle data updates (like rss feed -- usually items inserted at beginning) + + +class QmlXmlListModelPrivate : public QObjectPrivate +{ + Q_DECLARE_PUBLIC(QmlXmlListModel) +public: + QmlXmlListModelPrivate() + : isComponentComplete(true), size(-1), highestRole(Qt::UserRole) + , reply(0), status(QmlXmlListModel::Null), progress(0.0) + , queryId(-1), roleObjects(this) {} + + bool isComponentComplete; + QUrl src; + QString xml; + QString query; + QString namespaces; + int size; + QList<int> roles; + QStringList roleNames; + int highestRole; + QNetworkReply *reply; + QmlXmlListModel::Status status; + qreal progress; + QmlXmlQuery qmlXmlQuery; + int queryId; + QmlXmlRoleList roleObjects; + QList<QList<QVariant> > data; +}; + + +void QmlXmlRoleList::append(QmlXmlListModelRole *role) { + QmlConcreteList<QmlXmlListModelRole *>::append(role); + model->roles << model->highestRole; + model->roleNames << role->name(); + ++model->highestRole; +} + +//XXX clear, removeAt, and insert need to invalidate any cached data (in data table) as well +// (and the model should emit the appropriate signals) +void QmlXmlRoleList::clear() +{ + model->roles.clear(); + model->roleNames.clear(); + QmlConcreteList<QmlXmlListModelRole *>::clear(); +} + +void QmlXmlRoleList::removeAt(int i) +{ + model->roles.removeAt(i); + model->roleNames.removeAt(i); + QmlConcreteList<QmlXmlListModelRole *>::removeAt(i); +} + +//### we should enforce unique role names +void QmlXmlRoleList::insert(int i, QmlXmlListModelRole *role) +{ + QmlConcreteList<QmlXmlListModelRole *>::insert(i, role); + model->roles.insert(i, model->highestRole); + model->roleNames.insert(i, role->name()); + ++model->highestRole; +} + +/*! + \class QmlXmlListModel + \internal +*/ + +/*! + \qmlclass XmlListModel QmlXmlListModel + \brief The XmlListModel element allows you to specify a model using XPath expressions. + + XmlListModel allows you to construct a model from XML data that can then be used as a data source + for the view classes (ListView, PathView, GridView) and any other classes that interact with model + data (like Repeater). + + The following is an example of a model containing news from a Yahoo RSS feed: + \qml + XmlListModel { + id: feedModel + source: "http://rss.news.yahoo.com/rss/oceania" + query: "/rss/channel/item" + XmlRole { name: "title"; query: "title/string()" } + XmlRole { name: "link"; query: "link/string()" } + XmlRole { name: "description"; query: "description/string()" } + } + \endqml + \note The model is currently static, so the above is really just a snapshot of an RSS feed. To force a + reload of the entire model, you can call the reload function. +*/ + +QmlXmlListModel::QmlXmlListModel(QObject *parent) + : QListModelInterface(*(new QmlXmlListModelPrivate), parent) +{ + Q_D(QmlXmlListModel); + connect(&d->qmlXmlQuery, SIGNAL(queryCompleted(int,int)), + this, SLOT(queryCompleted(int,int))); +} + +QmlXmlListModel::~QmlXmlListModel() +{ +} + +/*! + \qmlproperty list<XmlRole> XmlListModel::roles + + The roles to make available for this model. +*/ +QmlList<QmlXmlListModelRole *> *QmlXmlListModel::roleObjects() +{ + Q_D(QmlXmlListModel); + return &d->roleObjects; +} + +QHash<int,QVariant> QmlXmlListModel::data(int index, const QList<int> &roles) const +{ + Q_D(const QmlXmlListModel); + QHash<int, QVariant> rv; + for (int i = 0; i < roles.size(); ++i) { + int role = roles.at(i); + int roleIndex = d->roles.indexOf(role); + rv.insert(role, roleIndex == -1 ? QVariant() : d->data.at(roleIndex).at(index)); + } + return rv; +} + +/*! + \qmlproperty int XmlListModel::count + The number of data entries in the model. +*/ +int QmlXmlListModel::count() const +{ + Q_D(const QmlXmlListModel); + return d->size; +} + +QList<int> QmlXmlListModel::roles() const +{ + Q_D(const QmlXmlListModel); + return d->roles; +} + +QString QmlXmlListModel::toString(int role) const +{ + Q_D(const QmlXmlListModel); + int index = d->roles.indexOf(role); + if (index == -1) + return QString(); + return d->roleNames.at(index); +} + +/*! + \qmlproperty url XmlListModel::source + The location of the XML data source. + + If both source and xml are set, xml will be used. +*/ +QUrl QmlXmlListModel::source() const +{ + Q_D(const QmlXmlListModel); + return d->src; +} + +void QmlXmlListModel::setSource(const QUrl &src) +{ + Q_D(QmlXmlListModel); + if (d->src != src) { + d->src = src; + reload(); + } +} + +/*! + \qmlproperty string XmlListModel::xml + This property holds XML text set directly. + + The text is assumed to be UTF-8 encoded. + + If both source and xml are set, xml will be used. +*/ +QString QmlXmlListModel::xml() const +{ + Q_D(const QmlXmlListModel); + return d->xml; +} + +void QmlXmlListModel::setXml(const QString &xml) +{ + Q_D(QmlXmlListModel); + d->xml = xml; + reload(); +} + +/*! + \qmlproperty url XmlListModel::query + An absolute XPath query representing the base query for the model items. The query should start with + a '/' or '//'. +*/ +QString QmlXmlListModel::query() const +{ + Q_D(const QmlXmlListModel); + return d->query; +} + +void QmlXmlListModel::setQuery(const QString &query) +{ + Q_D(QmlXmlListModel); + if (!query.startsWith(QLatin1Char('/'))) { + qmlInfo(this) << tr("An XmlListModel query must start with '/' or \"//\""); + return; + } + + if (d->query != query) { + d->query = query; + reload(); + } +} + +/*! + \qmlproperty string XmlListModel::namespaceDeclarations + A set of declarations for the namespaces used in the query. +*/ +QString QmlXmlListModel::namespaceDeclarations() const +{ + Q_D(const QmlXmlListModel); + return d->namespaces; +} + +void QmlXmlListModel::setNamespaceDeclarations(const QString &declarations) +{ + Q_D(QmlXmlListModel); + if (d->namespaces != declarations) { + d->namespaces = declarations; + reload(); + } +} + +/*! + \qmlproperty enum XmlListModel::status + + This property holds the status of data source loading. It can be one of: + \list + \o Null - no data source has been set + \o Ready - nthe data source has been loaded + \o Loading - the data source is currently being loaded + \o Error - an error occurred while loading the data source + \endlist + + \sa progress + +*/ +QmlXmlListModel::Status QmlXmlListModel::status() const +{ + Q_D(const QmlXmlListModel); + return d->status; +} + +/*! + \qmlproperty real XmlListModel::progress + + This property holds the progress of data source loading, from 0.0 (nothing loaded) + to 1.0 (finished). + + \sa status +*/ +qreal QmlXmlListModel::progress() const +{ + Q_D(const QmlXmlListModel); + return d->progress; +} + +void QmlXmlListModel::classBegin() +{ + Q_D(QmlXmlListModel); + d->isComponentComplete = false; +} + +void QmlXmlListModel::componentComplete() +{ + Q_D(QmlXmlListModel); + d->isComponentComplete = true; + reload(); +} + +/*! + \qmlmethod XmlListModel::reload() + + Reloads the model. All the existing model data will be removed, and the model + will be rebuilt from scratch. +*/ +void QmlXmlListModel::reload() +{ + Q_D(QmlXmlListModel); + + if (!d->isComponentComplete) + return; + + d->qmlXmlQuery.abort(); + d->queryId = -1; + + //clear existing data + int count = d->size; + d->size = 0; + d->data.clear(); + if (count > 0) + emit itemsRemoved(0, count); + + if (d->src.isEmpty() && d->xml.isEmpty()) + return; + + if (d->reply) { + d->reply->abort(); + d->reply->deleteLater(); + d->reply = 0; + } + + if (!d->xml.isEmpty()) { + d->queryId = d->qmlXmlQuery.doQuery(d->query, d->namespaces, d->xml.toUtf8(), &d->roleObjects); + d->progress = 1.0; + d->status = Ready; + emit progressChanged(d->progress); + emit statusChanged(d->status); + return; + } + + d->progress = 0.0; + d->status = Loading; + emit progressChanged(d->progress); + emit statusChanged(d->status); + + QNetworkRequest req(d->src); + d->reply = qmlContext(this)->engine()->networkAccessManager()->get(req); + QObject::connect(d->reply, SIGNAL(finished()), this, SLOT(requestFinished())); + QObject::connect(d->reply, SIGNAL(downloadProgress(qint64,qint64)), + this, SLOT(requestProgress(qint64,qint64))); +} + +void QmlXmlListModel::requestFinished() +{ + Q_D(QmlXmlListModel); + if (d->reply->error() != QNetworkReply::NoError) { + disconnect(d->reply, 0, this, 0); + d->reply->deleteLater(); + d->reply = 0; + d->status = Error; + } else { + d->status = Ready; + QByteArray data = d->reply->readAll(); + d->queryId = d->qmlXmlQuery.doQuery(d->query, d->namespaces, data, &d->roleObjects); + disconnect(d->reply, 0, this, 0); + d->reply->deleteLater(); + d->reply = 0; + } + d->progress = 1.0; + emit progressChanged(d->progress); + emit statusChanged(d->status); +} + +void QmlXmlListModel::requestProgress(qint64 received, qint64 total) +{ + Q_D(QmlXmlListModel); + if (d->status == Loading && total > 0) { + d->progress = qreal(received)/total; + emit progressChanged(d->progress); + } +} + +void QmlXmlListModel::queryCompleted(int id, int size) +{ + Q_D(QmlXmlListModel); + if (id != d->queryId) + return; + d->size = size; + if (size > 0) { + d->data = d->qmlXmlQuery.modelData(); + emit itemsInserted(0, d->size); + emit countChanged(); + } +} + +QT_END_NAMESPACE + +#include "qmlxmllistmodel.moc" diff --git a/src/declarative/util/qmlxmllistmodel_p.h b/src/declarative/util/qmlxmllistmodel_p.h new file mode 100644 index 0000000..18bf9d2 --- /dev/null +++ b/src/declarative/util/qmlxmllistmodel_p.h @@ -0,0 +1,134 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the QtDeclarative module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QMLXMLLISTMODEL_H +#define QMLXMLLISTMODEL_H + +#include <QtDeclarative/qml.h> +#include <private/qlistmodelinterface_p.h> +#include <QtDeclarative/qmlinfo.h> + +QT_BEGIN_HEADER + +QT_BEGIN_NAMESPACE + +QT_MODULE(Declarative) + +class QmlContext; + +class QmlXmlListModelRole; + +class QmlXmlListModelPrivate; +class Q_DECLARATIVE_EXPORT QmlXmlListModel : public QListModelInterface, public QmlParserStatus +{ + Q_OBJECT + Q_INTERFACES(QmlParserStatus) + Q_ENUMS(Status) + + Q_PROPERTY(Status status READ status NOTIFY statusChanged) + Q_PROPERTY(qreal progress READ progress NOTIFY progressChanged) + Q_PROPERTY(QUrl source READ source WRITE setSource) + Q_PROPERTY(QString xml READ xml WRITE setXml) + Q_PROPERTY(QString query READ query WRITE setQuery) + Q_PROPERTY(QString namespaceDeclarations READ namespaceDeclarations WRITE setNamespaceDeclarations) + Q_PROPERTY(QmlList<QmlXmlListModelRole *> *roles READ roleObjects) + Q_PROPERTY(int count READ count NOTIFY countChanged) + Q_CLASSINFO("DefaultProperty", "roles") + +public: + QmlXmlListModel(QObject *parent = 0); + ~QmlXmlListModel(); + + virtual QHash<int,QVariant> data(int index, const QList<int> &roles = (QList<int>())) const; + virtual int count() const; + virtual QList<int> roles() const; + virtual QString toString(int role) const; + + QmlList<QmlXmlListModelRole *> *roleObjects(); + + QUrl source() const; + void setSource(const QUrl&); + + QString xml() const; + void setXml(const QString&); + + QString query() const; + void setQuery(const QString&); + + QString namespaceDeclarations() const; + void setNamespaceDeclarations(const QString&); + + enum Status { Null, Ready, Loading, Error }; + Status status() const; + qreal progress() const; + + virtual void classBegin(); + virtual void componentComplete(); + +Q_SIGNALS: + void statusChanged(Status); + void progressChanged(qreal progress); + void countChanged(); + +public Q_SLOTS: + // ### need to use/expose Expiry to guess when to call this? + // ### property to auto-call this on reasonable Expiry? + // ### LastModified/Age also useful to guess. + // ### Probably also applies to other network-requesting types. + void reload(); + +private Q_SLOTS: + void requestFinished(); + void requestProgress(qint64,qint64); + void queryCompleted(int,int); + +private: + Q_DECLARE_PRIVATE(QmlXmlListModel) + Q_DISABLE_COPY(QmlXmlListModel) +}; + +QT_END_NAMESPACE + +QML_DECLARE_TYPE(QmlXmlListModel) + +QT_END_HEADER + +#endif // QMLXMLLISTMODEL_H diff --git a/src/declarative/util/qnumberformat.cpp b/src/declarative/util/qnumberformat.cpp new file mode 100644 index 0000000..c6a03e9 --- /dev/null +++ b/src/declarative/util/qnumberformat.cpp @@ -0,0 +1,225 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the QtDeclarative module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "qnumberformat_p.h" + +QT_BEGIN_NAMESPACE + +QML_DEFINE_NOCREATE_TYPE(QNumberFormat) + +QNumberFormat::QNumberFormat(QObject *parent) : QObject(parent), _number(0), _type(Decimal), + _groupingSize(0) +{ + _locale = QLocale::system(); + _groupingSeparator = _locale.groupSeparator(); + _decimalSeparator = _locale.decimalPoint(); + _currencySymbol = QLatin1Char('$'); +} + +QNumberFormat::~QNumberFormat() +{ + +} + +void QNumberFormat::updateText() +{ + QTime t; + t.start(); + static int totalTime; + + handleFormat(); + + totalTime += t.elapsed(); + emit textChanged(); +} + +void QNumberFormat::handleFormat() +{ + // ### is extremely messy + if (_format.isEmpty()) { + _text = QString(QLatin1String("%1")).arg(_number, -1, 'f', -1); + return; + } + + QString inputString; + + // ### possible to use the following parsed data in the future + + int remainingLength = _format.size(); + int currentIndex = _format.size()-1; + + int maxDigits = 0; + int minDigits = 0; + int decimalLength = 0; + + while (remainingLength > 0) { + switch(_format.at(currentIndex).unicode()) { + case ',': + if (decimalLength && !_groupingSize) + setGroupingSize(maxDigits - decimalLength); + else if (!_groupingSize) + setGroupingSize(maxDigits); + break; + case '.': + if (!decimalLength) + decimalLength = maxDigits; + break; + case '0': + minDigits++; + case '#': + maxDigits++; + break; + default: + break; + } + currentIndex--; + remainingLength--; + } + + // round given the decimal length/precision + inputString = QString(QLatin1String("%1")).arg(_number, -1, 'f', decimalLength); + + QStringList parts = inputString.split(QLatin1Char('.')); + QStringList formatParts = _format.split(QLatin1Char('.')); + + if (formatParts.size() > 2 || parts.size() > 2 ) + return; + + QString formatInt = formatParts.at(0); + + QString formatDec; + if (formatParts.size() == 2) + formatDec = formatParts.at(1); + + QString integer = parts.at(0); + + QString decimal; + if (parts.size() == 2) + decimal = parts.at(1); + + QString outputDecimal = formatDecimal(formatDec, decimal); + QString outputInteger = formatInteger(formatInt, integer); + + // insert separators + if (_groupingSize) { + unsigned int count = 0; + for (int i = outputInteger.size()-1; i > 0; i--) { + if (outputInteger.at(i).digitValue() >= 0) { + if (count == _groupingSize - 1) { + count = 0; + outputInteger.insert(i, _groupingSeparator); + } + else + count++; + } + } + } + if (!outputDecimal.isEmpty()) + _text = outputInteger + _decimalSeparator + outputDecimal; + else + _text = outputInteger; +} + +QString QNumberFormat::formatInteger(const QString &formatInt, const QString &integer) +{ + if (formatInt.isEmpty() || integer.isEmpty()) + return QString(); + + QString outputInteger; + int formatIndex = formatInt.size()-1; + + //easier for carry? + for (int index= integer.size()-1; index >= 0; index--) { + if (formatIndex < 0) { + outputInteger.push_front(integer.at(index)); + } + else { + switch(formatInt.at(formatIndex).unicode()) { + case '0': + if (index > integer.size()-1) { + outputInteger.push_front(QLatin1Char('0')); + break; + } + case '#': + outputInteger.push_front(integer.at(index)); + break; + case ',': + index++; + break; + default: + outputInteger.push_front(formatInt.at(formatIndex)); + index++; + break; + } + formatIndex--; + } + } + while (formatIndex >= 0) { + if (formatInt.at(formatIndex).unicode() != '#' && formatInt.at(formatIndex).unicode() != ',') + outputInteger.push_front(formatInt.at(formatIndex)); + formatIndex--; + } + return outputInteger; +} + +QString QNumberFormat::formatDecimal(const QString &formatDec, const QString &decimal) +{ + QString outputDecimal; + + // up to max 6 decimal places + for (int index=formatDec.size()-1; index >= 0; index--) { + switch(formatDec.at(index).unicode()) { + case '0': + outputDecimal.push_front(decimal.at(index)); + break; + case '#': + if (decimal.at(index) != QLatin1Char('0') || outputDecimal.size() > 0) + outputDecimal.push_front(decimal.at(index)); + break; + default: + outputDecimal.push_front(formatDec.at(index)); + break; + } + } + return outputDecimal; +} + +QT_END_NAMESPACE diff --git a/src/declarative/util/qnumberformat_p.h b/src/declarative/util/qnumberformat_p.h new file mode 100644 index 0000000..1a7b8e4 --- /dev/null +++ b/src/declarative/util/qnumberformat_p.h @@ -0,0 +1,173 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the QtDeclarative module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef NUMBERFORMAT_H +#define NUMBERFORMAT_H + +#include <QtDeclarative/qml.h> +#include <QtCore/QLocale> +#include <QtCore/QTime> + +QT_BEGIN_HEADER + +QT_BEGIN_NAMESPACE + +QT_MODULE(Declarative) + +// TODO +// be able to set Locale, instead of default system for dynamic formatting +// add currency support +// add additional syntax, extend to format scientific, percentiles, significant digits etc + + +class QNumberFormat : public QObject +{ + Q_OBJECT + Q_ENUMS(NumberType) +public: + QNumberFormat(QObject *parent=0); + ~QNumberFormat(); + + enum NumberType { + Percent, + Scientific, + Currency, + Decimal + }; + + //external property, only visible + Q_PROPERTY(QString text READ text NOTIFY textChanged) + + //mutatable properties to modify the output (text) + Q_PROPERTY(qreal number READ number WRITE setNumber) + Q_PROPERTY(QString format READ format WRITE setFormat) + Q_PROPERTY(QLocale locale READ locale WRITE setLocale) + + //Format specific settings + Q_PROPERTY(unsigned short groupingSeparator READ groupingSeparator WRITE setGroupingSeparator) + Q_PROPERTY(unsigned short decimalSeperator READ decimalSeparator WRITE setDecimalSeparator) + Q_PROPERTY(unsigned int groupingSize READ groupingSize WRITE setGroupingSize) + Q_PROPERTY(unsigned short currencySymbol READ currencySymbol WRITE setCurrencySymbol) + + + QString text() const { return _text; } + + qreal number() const { return _number; } + void setNumber(qreal n) { + if (_number == n) + return; + _number = n; + updateText(); + } + + QString format() const { return _format; } + void setFormat(const QString &format) { + if (format.isEmpty()) + _format = QString::null; + else if (_format == format) + return; + + _format = format; + updateText(); + } + + QLocale locale() const { return _locale; } + void setLocale(const QLocale &locale) { _locale = locale; updateText(); } + + //Do we deal with unicode standard? or create our own + // ### since this is the backend for the number conversions, we will use the unicode + // the front-end will handle the QChar/QString -> short int + + unsigned short groupingSeparator() { return _groupingSeparator.unicode(); } + void setGroupingSeparator(unsigned short unicodeSymbol) + { + _groupingSeparator = QChar(unicodeSymbol); + } + + unsigned short decimalSeparator() { return _decimalSeparator.unicode(); } + void setDecimalSeparator(unsigned short unicodeSymbol) + { + _decimalSeparator = QChar(unicodeSymbol); + } + + unsigned short currencySymbol() { return _currencySymbol.unicode(); } + void setCurrencySymbol(unsigned short unicodeSymbol) + { + _currencySymbol = QChar(unicodeSymbol); + } + + unsigned int groupingSize() { return _groupingSize; } + void setGroupingSize(unsigned int size) + { + _groupingSize = size; + } + +Q_SIGNALS: + void textChanged(); + +private: + void updateText(); + void handleFormat(); + QString formatInteger(const QString &formatInt, const QString &integer); + QString formatDecimal(const QString &formatDec, const QString &decimal); + + qreal _number; + NumberType _type; + QChar _groupingSeparator; + QChar _decimalSeparator; + QChar _currencySymbol; + unsigned int _groupingSize; + + QLocale _locale; + QString _format; + + // only hooked member at the moment + QString _text; + +}; + +QT_END_NAMESPACE + +QML_DECLARE_TYPE(QNumberFormat) + +QT_END_HEADER + +#endif diff --git a/src/declarative/util/util.pri b/src/declarative/util/util.pri index e043cca..d2faec9 100644 --- a/src/declarative/util/util.pri +++ b/src/declarative/util/util.pri @@ -21,7 +21,12 @@ SOURCES += \ util/qmltimer.cpp \ util/qmlbind.cpp \ util/qmlpropertymap.cpp \ - util/qmlpixmapcache.cpp + util/qmlpixmapcache.cpp \ + util/qnumberformat.cpp \ + util/qmlnumberformatter.cpp \ + util/qmldatetimeformatter.cpp \ + util/qmlbehavior.cpp \ + util/qmlfontloader.cpp HEADERS += \ util/qmlview.h \ @@ -49,4 +54,15 @@ HEADERS += \ util/qmltimer_p.h \ util/qmlbind_p.h \ util/qmlpropertymap.h \ - util/qmlpixmapcache_p.h + util/qmlpixmapcache_p.h \ + util/qnumberformat_p.h \ + util/qmlnumberformatter_p.h \ + util/qmldatetimeformatter_p.h \ + util/qmlbehavior_p.h \ + util/qmlfontloader_p.h + +contains(QT_CONFIG, xmlpatterns) { + QT+=xmlpatterns + SOURCES += util/qmlxmllistmodel.cpp + HEADERS += util/qmlxmllistmodel_p.h +} |