From 884bc89e9fe765a7be245b3009339f999936a761 Mon Sep 17 00:00:00 2001 From: Matthew Vogt Date: Tue, 17 Jul 2012 11:14:43 +1000 Subject: Allow QQmlPropertyMap property updates to be controlled Allow clients to control updates made from QML to types derived from QQmlPropertyMap, by overriding the updateValue() function. Task-number: QTBUG-23183 Change-Id: I0169093779ebfe50dc9349f5aaac08ed85c80a8f Reviewed-by: Michael Brasser Reviewed-by: abcd --- src/qml/util/qqmlpropertymap.cpp | 39 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) (limited to 'src/qml/util/qqmlpropertymap.cpp') diff --git a/src/qml/util/qqmlpropertymap.cpp b/src/qml/util/qqmlpropertymap.cpp index 1571d2dfc9..cdc82fe86e 100644 --- a/src/qml/util/qqmlpropertymap.cpp +++ b/src/qml/util/qqmlpropertymap.cpp @@ -56,9 +56,13 @@ public: QQmlPropertyMapMetaObject(QQmlPropertyMap *obj, QQmlPropertyMapPrivate *objPriv); protected: + virtual QVariant propertyWriteValue(int, const QVariant &); virtual void propertyWritten(int index); virtual void propertyCreated(int, QMetaPropertyBuilder &); virtual int createProperty(const char *, const char *); + + const QString &propertyName(int index); + private: QQmlPropertyMap *map; QQmlPropertyMapPrivate *priv; @@ -70,8 +74,12 @@ class QQmlPropertyMapPrivate : public QObjectPrivate public: QQmlPropertyMapMetaObject *mo; QStringList keys; + + QVariant updateValue(const QString &key, const QVariant &input); void emitChanged(const QString &key, const QVariant &value); bool validKeyName(const QString& name); + + const QString &propertyName(int index) const; }; bool QQmlPropertyMapPrivate::validKeyName(const QString& name) @@ -84,21 +92,38 @@ bool QQmlPropertyMapPrivate::validKeyName(const QString& name) && name != QLatin1String("deleteLater"); } +QVariant QQmlPropertyMapPrivate::updateValue(const QString &key, const QVariant &input) +{ + Q_Q(QQmlPropertyMap); + return q->updateValue(key, input); +} + void QQmlPropertyMapPrivate::emitChanged(const QString &key, const QVariant &value) { Q_Q(QQmlPropertyMap); emit q->valueChanged(key, value); } +const QString &QQmlPropertyMapPrivate::propertyName(int index) const +{ + Q_ASSERT(index < keys.size()); + return keys[index]; +} + QQmlPropertyMapMetaObject::QQmlPropertyMapMetaObject(QQmlPropertyMap *obj, QQmlPropertyMapPrivate *objPriv) : QQmlOpenMetaObject(obj) { map = obj; priv = objPriv; } +QVariant QQmlPropertyMapMetaObject::propertyWriteValue(int index, const QVariant &input) +{ + return priv->updateValue(priv->propertyName(index), input); +} + void QQmlPropertyMapMetaObject::propertyWritten(int index) { - priv->emitChanged(QString::fromUtf8(name(index)), operator[](index)); + priv->emitChanged(priv->propertyName(index), operator[](index)); } void QQmlPropertyMapMetaObject::propertyCreated(int, QMetaPropertyBuilder &b) @@ -297,6 +322,18 @@ QVariant QQmlPropertyMap::operator[](const QString &key) const return value(key); } +/*! + Returns the new value to be stored for the key \a key. This function is provided + to intercept updates to a property from QML, where the value provided from QML is \a input. + + Override this function to manipulate the property value as it is updated. Note that + this function is only invoked when the value is updated from QML. +*/ +QVariant QQmlPropertyMap::updateValue(const QString &key, const QVariant &input) +{ + return input; +} + /*! \fn void QQmlPropertyMap::valueChanged(const QString &key, const QVariant &value) This signal is emitted whenever one of the values in the map is changed. \a key -- cgit v1.2.3