diff options
author | Michael Brasser <michael.brasser@nokia.com> | 2009-10-21 04:57:07 (GMT) |
---|---|---|
committer | Michael Brasser <michael.brasser@nokia.com> | 2009-10-21 04:57:50 (GMT) |
commit | 1208d2800d4810a472262d8e04f0cf3a59a3efdb (patch) | |
tree | 8f599585e1d77872fae50e4c3d1bc1da0a395070 /src/declarative/util | |
parent | 2b7c33da538e1fd596c679a089c1949cc8651103 (diff) | |
download | Qt-1208d2800d4810a472262d8e04f0cf3a59a3efdb.zip Qt-1208d2800d4810a472262d8e04f0cf3a59a3efdb.tar.gz Qt-1208d2800d4810a472262d8e04f0cf3a59a3efdb.tar.bz2 |
Give QmlPropertyMap a more complete API.
Diffstat (limited to 'src/declarative/util')
-rw-r--r-- | src/declarative/util/qmlopenmetaobject.cpp | 8 | ||||
-rw-r--r-- | src/declarative/util/qmlopenmetaobject.h | 1 | ||||
-rw-r--r-- | src/declarative/util/qmlpropertymap.cpp | 105 | ||||
-rw-r--r-- | src/declarative/util/qmlpropertymap.h | 14 |
4 files changed, 110 insertions, 18 deletions
diff --git a/src/declarative/util/qmlopenmetaobject.cpp b/src/declarative/util/qmlopenmetaobject.cpp index 7305362..11648f6 100644 --- a/src/declarative/util/qmlopenmetaobject.cpp +++ b/src/declarative/util/qmlopenmetaobject.cpp @@ -136,6 +136,14 @@ QVariant QmlOpenMetaObject::value(const QByteArray &name) const return d->data.at(*iter); } +QVariant &QmlOpenMetaObject::operator[](const QByteArray &name) +{ + QHash<QByteArray, int>::ConstIterator iter = d->names.find(name); + Q_ASSERT(iter != d->names.end()); + + return d->data[*iter]; +} + void QmlOpenMetaObject::setValue(const QByteArray &name, const QVariant &val) { QHash<QByteArray, int>::ConstIterator iter = d->names.find(name); diff --git a/src/declarative/util/qmlopenmetaobject.h b/src/declarative/util/qmlopenmetaobject.h index f65660d..be0490d 100644 --- a/src/declarative/util/qmlopenmetaobject.h +++ b/src/declarative/util/qmlopenmetaobject.h @@ -64,6 +64,7 @@ public: void setValue(const QByteArray &, const QVariant &); QVariant value(int) const; void setValue(int, const QVariant &); + QVariant &operator[](const QByteArray &); int count() const; QByteArray name(int) const; diff --git a/src/declarative/util/qmlpropertymap.cpp b/src/declarative/util/qmlpropertymap.cpp index 0a92a8b..a587af3 100644 --- a/src/declarative/util/qmlpropertymap.cpp +++ b/src/declarative/util/qmlpropertymap.cpp @@ -72,7 +72,7 @@ public: void QmlPropertyMapPrivate::emitChanged(const QString &key) { Q_Q(QmlPropertyMap); - emit q->changed(key); + emit q->valueChanged(key); } QmlPropertyMapMetaObject::QmlPropertyMapMetaObject(QmlPropertyMap *obj, QmlPropertyMapPrivate *objPriv) : QmlOpenMetaObject(obj) @@ -98,8 +98,8 @@ void QmlPropertyMapMetaObject::propertyWrite(int index) \code //create our data QmlPropertyMap ownerData; - ownerData.setValue("name", QVariant(QString("John Smith"))); - ownerData.setValue("phone", QVariant(QString("555-5555"))); + ownerData.insert("name", QVariant(QString("John Smith"))); + ownerData.insert("phone", QVariant(QString("555-5555"))); //expose it to the UI layer QmlContext *ctxt = view->bindContext(); @@ -115,16 +115,13 @@ void QmlPropertyMapMetaObject::propertyWrite(int index) The binding is dynamic - whenever a key's value is updated, anything bound to that key will be updated as well. - To detect value changes made in the UI layer you can connect to the changed() signal. - However, note that changed() is \b NOT emitted when changes are made by calling setValue() - or clearValue() - it is only emitted when a value is updated from QML. -*/ + To detect value changes made in the UI layer you can connect to the valueChanged() signal. + However, note that valueChanged() is \b NOT emitted when changes are made by calling insert() + or clear() - it is only emitted when a value is updated from QML. -// is there a more efficient way to store/return keys? -// (or should we just provide an iterator or something else instead?) -// can we provide a way to clear keys? -// do we want to make any claims regarding key ordering? -// should we have signals for insertion and and deletion -- becoming more model like + \note It is not possible to remove keys from the map; once a key has been added, you can only + modify or clear its associated value. +*/ /*! Constructs a bindable map with parent object \a parent. @@ -146,7 +143,7 @@ QmlPropertyMap::~QmlPropertyMap() /*! Clears the value (if any) associated with \a key. */ -void QmlPropertyMap::clearValue(const QString &key) +void QmlPropertyMap::clear(const QString &key) { Q_D(QmlPropertyMap); d->mo->setValue(key.toUtf8(), QVariant()); @@ -169,7 +166,7 @@ QVariant QmlPropertyMap::value(const QString &key) const If the key doesn't exist, it is automatically created. */ -void QmlPropertyMap::setValue(const QString &key, const QVariant &value) +void QmlPropertyMap::insert(const QString &key, const QVariant &value) { Q_D(QmlPropertyMap); if (!d->keys.contains(key)) @@ -190,7 +187,85 @@ QStringList QmlPropertyMap::keys() const } /*! - \fn void QmlPropertyMap::changed(const QString &key) + \overload + + Same as size(). +*/ +int QmlPropertyMap::count() const +{ + Q_D(const QmlPropertyMap); + return d->keys.count(); +} + +/*! + Returns the number of keys in the map. + + \sa isEmpty(), count() +*/ +int QmlPropertyMap::size() const +{ + Q_D(const QmlPropertyMap); + return d->keys.size(); +} + +/*! + Returns true if the map contains no keys; otherwise returns + false. + + \sa size() +*/ +bool QmlPropertyMap::isEmpty() const +{ + Q_D(const QmlPropertyMap); + return d->keys.isEmpty(); +} + +/*! + Returns true if the map contains \a key. + + \sa size() +*/ +bool QmlPropertyMap::contains(const QString &key) const +{ + Q_D(const QmlPropertyMap); + return d->keys.contains(key); +} + +/*! + Returns the value associated with the key \a key as a modifiable + reference. + + If the map contains no item with key \a key, the function inserts + an invalid QVariant into the map with key \a key, and + returns a reference to it. + + \sa insert(), value() +*/ +QVariant &QmlPropertyMap::operator[](const QString &key) +{ + //### optimize + Q_D(QmlPropertyMap); + QByteArray utf8key = key.toUtf8(); + if (!d->keys.contains(key)) { + d->keys.append(key); + d->mo->setValue(utf8key, QVariant()); //force creation -- needed below + } + + return (*(d->mo))[utf8key]; +} + +/*! + \overload + + Same as value(). +*/ +const QVariant QmlPropertyMap::operator[](const QString &key) const +{ + return value(key); +} + +/*! + \fn void QmlPropertyMap::valueChanged(const QString &key) This signal is emitted whenever one of the values in the map is changed. \a key is the key corresponding to the value that was changed. */ diff --git a/src/declarative/util/qmlpropertymap.h b/src/declarative/util/qmlpropertymap.h index 295f4b7..24b4395 100644 --- a/src/declarative/util/qmlpropertymap.h +++ b/src/declarative/util/qmlpropertymap.h @@ -63,13 +63,21 @@ public: virtual ~QmlPropertyMap(); QVariant value(const QString &key) const; - void setValue(const QString &key, const QVariant &value); - void clearValue(const QString &key); + void insert(const QString &key, const QVariant &value); + void clear(const QString &key); Q_INVOKABLE QStringList keys() const; + int count() const; + int size() const; + bool isEmpty() const; + bool contains(const QString &key) const; + + QVariant &operator[](const QString &key); + const QVariant operator[](const QString &key) const; + Q_SIGNALS: - void changed(const QString &key); + void valueChanged(const QString &key); private: Q_DECLARE_PRIVATE(QmlPropertyMap) |