| Absolute File Name: | /home/opencoverage/opencoverage/guest-scripts/qtdeclarative/src/qtdeclarative/src/qml/util/qqmlpropertymap.cpp |
| Source code | Switch to Preprocessed file |
| Line | Source | Count | ||||||
|---|---|---|---|---|---|---|---|---|
| 1 | /**************************************************************************** | - | ||||||
| 2 | ** | - | ||||||
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. | - | ||||||
| 4 | ** Contact: https://www.qt.io/licensing/ | - | ||||||
| 5 | ** | - | ||||||
| 6 | ** This file is part of the QtQml module of the Qt Toolkit. | - | ||||||
| 7 | ** | - | ||||||
| 8 | ** $QT_BEGIN_LICENSE:LGPL$ | - | ||||||
| 9 | ** Commercial License Usage | - | ||||||
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in | - | ||||||
| 11 | ** accordance with the commercial license agreement provided with the | - | ||||||
| 12 | ** Software or, alternatively, in accordance with the terms contained in | - | ||||||
| 13 | ** a written agreement between you and The Qt Company. For licensing terms | - | ||||||
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further | - | ||||||
| 15 | ** information use the contact form at https://www.qt.io/contact-us. | - | ||||||
| 16 | ** | - | ||||||
| 17 | ** GNU Lesser General Public License Usage | - | ||||||
| 18 | ** Alternatively, this file may be used under the terms of the GNU Lesser | - | ||||||
| 19 | ** General Public License version 3 as published by the Free Software | - | ||||||
| 20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the | - | ||||||
| 21 | ** packaging of this file. Please review the following information to | - | ||||||
| 22 | ** ensure the GNU Lesser General Public License version 3 requirements | - | ||||||
| 23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. | - | ||||||
| 24 | ** | - | ||||||
| 25 | ** GNU General Public License Usage | - | ||||||
| 26 | ** Alternatively, this file may be used under the terms of the GNU | - | ||||||
| 27 | ** General Public License version 2.0 or (at your option) the GNU General | - | ||||||
| 28 | ** Public license version 3 or any later version approved by the KDE Free | - | ||||||
| 29 | ** Qt Foundation. The licenses are as published by the Free Software | - | ||||||
| 30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 | - | ||||||
| 31 | ** included in the packaging of this file. Please review the following | - | ||||||
| 32 | ** information to ensure the GNU General Public License requirements will | - | ||||||
| 33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and | - | ||||||
| 34 | ** https://www.gnu.org/licenses/gpl-3.0.html. | - | ||||||
| 35 | ** | - | ||||||
| 36 | ** $QT_END_LICENSE$ | - | ||||||
| 37 | ** | - | ||||||
| 38 | ****************************************************************************/ | - | ||||||
| 39 | - | |||||||
| 40 | #include "qqmlpropertymap.h" | - | ||||||
| 41 | - | |||||||
| 42 | #include <private/qmetaobjectbuilder_p.h> | - | ||||||
| 43 | #include <private/qqmlopenmetaobject_p.h> | - | ||||||
| 44 | - | |||||||
| 45 | #include <QDebug> | - | ||||||
| 46 | - | |||||||
| 47 | QT_BEGIN_NAMESPACE | - | ||||||
| 48 | - | |||||||
| 49 | //QQmlPropertyMapMetaObject lets us listen for changes coming from QML | - | ||||||
| 50 | //so we can emit the changed signal. | - | ||||||
| 51 | class QQmlPropertyMapMetaObject : public QQmlOpenMetaObject | - | ||||||
| 52 | { | - | ||||||
| 53 | public: | - | ||||||
| 54 | QQmlPropertyMapMetaObject(QQmlPropertyMap *obj, QQmlPropertyMapPrivate *objPriv, const QMetaObject *staticMetaObject); | - | ||||||
| 55 | - | |||||||
| 56 | protected: | - | ||||||
| 57 | QVariant propertyWriteValue(int, const QVariant &) override; | - | ||||||
| 58 | void propertyWritten(int index) override; | - | ||||||
| 59 | void propertyCreated(int, QMetaPropertyBuilder &) override; | - | ||||||
| 60 | int createProperty(const char *, const char *) override; | - | ||||||
| 61 | - | |||||||
| 62 | const QString &propertyName(int index); | - | ||||||
| 63 | - | |||||||
| 64 | private: | - | ||||||
| 65 | QQmlPropertyMap *map; | - | ||||||
| 66 | QQmlPropertyMapPrivate *priv; | - | ||||||
| 67 | }; | - | ||||||
| 68 | - | |||||||
| 69 | class QQmlPropertyMapPrivate : public QObjectPrivate | - | ||||||
| 70 | { | - | ||||||
| 71 | Q_DECLARE_PUBLIC(QQmlPropertyMap) | - | ||||||
| 72 | public: | - | ||||||
| 73 | QQmlPropertyMapMetaObject *mo; | - | ||||||
| 74 | QStringList keys; | - | ||||||
| 75 | - | |||||||
| 76 | QVariant updateValue(const QString &key, const QVariant &input); | - | ||||||
| 77 | void emitChanged(const QString &key, const QVariant &value); | - | ||||||
| 78 | bool validKeyName(const QString& name); | - | ||||||
| 79 | - | |||||||
| 80 | const QString &propertyName(int index) const; | - | ||||||
| 81 | }; | - | ||||||
| 82 | - | |||||||
| 83 | bool QQmlPropertyMapPrivate::validKeyName(const QString& name) | - | ||||||
| 84 | { | - | ||||||
| 85 | //The following strings shouldn't be used as property names | - | ||||||
| 86 | return name != QLatin1String("keys") executed 100 times by 1 test: return name != QLatin1String("keys") && name != QLatin1String("valueChanged") && name != QLatin1String("QObject") && name != QLatin1String("destroyed") && name != QLatin1String("deleteLater");Executed by:
| 100 | ||||||
| 87 | && name != QLatin1String("valueChanged") executed 100 times by 1 test: return name != QLatin1String("keys") && name != QLatin1String("valueChanged") && name != QLatin1String("QObject") && name != QLatin1String("destroyed") && name != QLatin1String("deleteLater");Executed by:
| 100 | ||||||
| 88 | && name != QLatin1String("QObject") executed 100 times by 1 test: return name != QLatin1String("keys") && name != QLatin1String("valueChanged") && name != QLatin1String("QObject") && name != QLatin1String("destroyed") && name != QLatin1String("deleteLater");Executed by:
| 100 | ||||||
| 89 | && name != QLatin1String("destroyed") executed 100 times by 1 test: return name != QLatin1String("keys") && name != QLatin1String("valueChanged") && name != QLatin1String("QObject") && name != QLatin1String("destroyed") && name != QLatin1String("deleteLater");Executed by:
| 100 | ||||||
| 90 | && name != QLatin1String("deleteLater"); executed 100 times by 1 test: return name != QLatin1String("keys") && name != QLatin1String("valueChanged") && name != QLatin1String("QObject") && name != QLatin1String("destroyed") && name != QLatin1String("deleteLater");Executed by:
| 100 | ||||||
| 91 | } | - | ||||||
| 92 | - | |||||||
| 93 | QVariant QQmlPropertyMapPrivate::updateValue(const QString &key, const QVariant &input) | - | ||||||
| 94 | { | - | ||||||
| 95 | Q_Q(QQmlPropertyMap); | - | ||||||
| 96 | return q->updateValue(key, input); executed 10 times by 1 test: return q->updateValue(key, input);Executed by:
| 10 | ||||||
| 97 | } | - | ||||||
| 98 | - | |||||||
| 99 | void QQmlPropertyMapPrivate::emitChanged(const QString &key, const QVariant &value) | - | ||||||
| 100 | { | - | ||||||
| 101 | Q_Q(QQmlPropertyMap); | - | ||||||
| 102 | emit q->valueChanged(key, value); | - | ||||||
| 103 | } executed 10 times by 1 test: end of blockExecuted by:
| 10 | ||||||
| 104 | - | |||||||
| 105 | const QString &QQmlPropertyMapPrivate::propertyName(int index) const | - | ||||||
| 106 | { | - | ||||||
| 107 | Q_ASSERT(index < keys.size()); | - | ||||||
| 108 | return keys[index]; executed 20 times by 1 test: return keys[index];Executed by:
| 20 | ||||||
| 109 | } | - | ||||||
| 110 | - | |||||||
| 111 | QQmlPropertyMapMetaObject::QQmlPropertyMapMetaObject(QQmlPropertyMap *obj, QQmlPropertyMapPrivate *objPriv, const QMetaObject *staticMetaObject) | - | ||||||
| 112 | : QQmlOpenMetaObject(obj, staticMetaObject) | - | ||||||
| 113 | { | - | ||||||
| 114 | map = obj; | - | ||||||
| 115 | priv = objPriv; | - | ||||||
| 116 | } executed 68 times by 6 tests: end of blockExecuted by:
| 68 | ||||||
| 117 | - | |||||||
| 118 | QVariant QQmlPropertyMapMetaObject::propertyWriteValue(int index, const QVariant &input) | - | ||||||
| 119 | { | - | ||||||
| 120 | return priv->updateValue(priv->propertyName(index), input); executed 10 times by 1 test: return priv->updateValue(priv->propertyName(index), input);Executed by:
| 10 | ||||||
| 121 | } | - | ||||||
| 122 | - | |||||||
| 123 | void QQmlPropertyMapMetaObject::propertyWritten(int index) | - | ||||||
| 124 | { | - | ||||||
| 125 | priv->emitChanged(priv->propertyName(index), value(index)); | - | ||||||
| 126 | } executed 10 times by 1 test: end of blockExecuted by:
| 10 | ||||||
| 127 | - | |||||||
| 128 | void QQmlPropertyMapMetaObject::propertyCreated(int, QMetaPropertyBuilder &b) | - | ||||||
| 129 | { | - | ||||||
| 130 | priv->keys.append(QString::fromUtf8(b.name())); | - | ||||||
| 131 | } executed 46 times by 1 test: end of blockExecuted by:
| 46 | ||||||
| 132 | - | |||||||
| 133 | int QQmlPropertyMapMetaObject::createProperty(const char *name, const char *value) | - | ||||||
| 134 | { | - | ||||||
| 135 | if (!priv->validKeyName(QString::fromUtf8(name)))
| 0-46 | ||||||
| 136 | return -1; never executed: return -1; | 0 | ||||||
| 137 | return QQmlOpenMetaObject::createProperty(name, value); executed 46 times by 1 test: return QQmlOpenMetaObject::createProperty(name, value);Executed by:
| 46 | ||||||
| 138 | } | - | ||||||
| 139 | - | |||||||
| 140 | /*! | - | ||||||
| 141 | \class QQmlPropertyMap | - | ||||||
| 142 | \brief The QQmlPropertyMap class allows you to set key-value pairs that can be used in QML bindings. | - | ||||||
| 143 | \inmodule QtQml | - | ||||||
| 144 | - | |||||||
| 145 | QQmlPropertyMap provides a convenient way to expose domain data to the UI layer. | - | ||||||
| 146 | The following example shows how you might declare data in C++ and then | - | ||||||
| 147 | access it in QML. | - | ||||||
| 148 | - | |||||||
| 149 | In the C++ file: | - | ||||||
| 150 | \code | - | ||||||
| 151 | // create our data | - | ||||||
| 152 | QQmlPropertyMap ownerData; | - | ||||||
| 153 | ownerData.insert("name", QVariant(QString("John Smith"))); | - | ||||||
| 154 | ownerData.insert("phone", QVariant(QString("555-5555"))); | - | ||||||
| 155 | - | |||||||
| 156 | // expose it to the UI layer | - | ||||||
| 157 | QQuickView view; | - | ||||||
| 158 | QQmlContext *ctxt = view.rootContext(); | - | ||||||
| 159 | ctxt->setContextProperty("owner", &ownerData); | - | ||||||
| 160 | - | |||||||
| 161 | view.setSource(QUrl::fromLocalFile("main.qml")); | - | ||||||
| 162 | view.show(); | - | ||||||
| 163 | \endcode | - | ||||||
| 164 | - | |||||||
| 165 | Then, in \c main.qml: | - | ||||||
| 166 | \code | - | ||||||
| 167 | Text { text: owner.name + " " + owner.phone } | - | ||||||
| 168 | \endcode | - | ||||||
| 169 | - | |||||||
| 170 | The binding is dynamic - whenever a key's value is updated, anything bound to that | - | ||||||
| 171 | key will be updated as well. | - | ||||||
| 172 | - | |||||||
| 173 | To detect value changes made in the UI layer you can connect to the valueChanged() signal. | - | ||||||
| 174 | However, note that valueChanged() is \b NOT emitted when changes are made by calling insert() | - | ||||||
| 175 | or clear() - it is only emitted when a value is updated from QML. | - | ||||||
| 176 | - | |||||||
| 177 | \note It is not possible to remove keys from the map; once a key has been added, you can only | - | ||||||
| 178 | modify or clear its associated value. | - | ||||||
| 179 | - | |||||||
| 180 | \note When deriving a class from QQmlPropertyMap, use the | - | ||||||
| 181 | \l {QQmlPropertyMap::QQmlPropertyMap(DerivedType *derived, QObject *parent)} {protected two-argument constructor} | - | ||||||
| 182 | which ensures that the class is correctly registered with the Qt \l {Meta-Object System}. | - | ||||||
| 183 | */ | - | ||||||
| 184 | - | |||||||
| 185 | /*! | - | ||||||
| 186 | Constructs a bindable map with parent object \a parent. | - | ||||||
| 187 | */ | - | ||||||
| 188 | QQmlPropertyMap::QQmlPropertyMap(QObject *parent) | - | ||||||
| 189 | : QQmlPropertyMap(&staticMetaObject, parent) | - | ||||||
| 190 | { | - | ||||||
| 191 | } executed 54 times by 5 tests: end of blockExecuted by:
| 54 | ||||||
| 192 | - | |||||||
| 193 | /*! | - | ||||||
| 194 | Destroys the bindable map. | - | ||||||
| 195 | */ | - | ||||||
| 196 | QQmlPropertyMap::~QQmlPropertyMap() | - | ||||||
| 197 | { | - | ||||||
| 198 | } | - | ||||||
| 199 | - | |||||||
| 200 | /*! | - | ||||||
| 201 | Clears the value (if any) associated with \a key. | - | ||||||
| 202 | */ | - | ||||||
| 203 | void QQmlPropertyMap::clear(const QString &key) | - | ||||||
| 204 | { | - | ||||||
| 205 | Q_D(QQmlPropertyMap); | - | ||||||
| 206 | d->mo->setValue(key.toUtf8(), QVariant()); | - | ||||||
| 207 | } executed 6 times by 1 test: end of blockExecuted by:
| 6 | ||||||
| 208 | - | |||||||
| 209 | /*! | - | ||||||
| 210 | Returns the value associated with \a key. | - | ||||||
| 211 | - | |||||||
| 212 | If no value has been set for this key (or if the value has been cleared), | - | ||||||
| 213 | an invalid QVariant is returned. | - | ||||||
| 214 | */ | - | ||||||
| 215 | QVariant QQmlPropertyMap::value(const QString &key) const | - | ||||||
| 216 | { | - | ||||||
| 217 | Q_D(const QQmlPropertyMap); | - | ||||||
| 218 | return d->mo->value(key.toUtf8()); executed 44 times by 1 test: return d->mo->value(key.toUtf8());Executed by:
| 44 | ||||||
| 219 | } | - | ||||||
| 220 | - | |||||||
| 221 | /*! | - | ||||||
| 222 | Sets the value associated with \a key to \a value. | - | ||||||
| 223 | - | |||||||
| 224 | If the key doesn't exist, it is automatically created. | - | ||||||
| 225 | */ | - | ||||||
| 226 | void QQmlPropertyMap::insert(const QString &key, const QVariant &value) | - | ||||||
| 227 | { | - | ||||||
| 228 | Q_D(QQmlPropertyMap); | - | ||||||
| 229 | - | |||||||
| 230 | if (d->validKeyName(key)) {
| 6-48 | ||||||
| 231 | d->mo->setValue(key.toUtf8(), value); | - | ||||||
| 232 | } else { executed 48 times by 1 test: end of blockExecuted by:
| 48 | ||||||
| 233 | qWarning() << "Creating property with name" | - | ||||||
| 234 | << key | - | ||||||
| 235 | << "is not permitted, conflicts with internal symbols."; | - | ||||||
| 236 | } executed 6 times by 1 test: end of blockExecuted by:
| 6 | ||||||
| 237 | } | - | ||||||
| 238 | - | |||||||
| 239 | /*! | - | ||||||
| 240 | Returns the list of keys. | - | ||||||
| 241 | - | |||||||
| 242 | Keys that have been cleared will still appear in this list, even though their | - | ||||||
| 243 | associated values are invalid QVariants. | - | ||||||
| 244 | */ | - | ||||||
| 245 | QStringList QQmlPropertyMap::keys() const | - | ||||||
| 246 | { | - | ||||||
| 247 | Q_D(const QQmlPropertyMap); | - | ||||||
| 248 | return d->keys; executed 20 times by 1 test: return d->keys;Executed by:
| 20 | ||||||
| 249 | } | - | ||||||
| 250 | - | |||||||
| 251 | /*! | - | ||||||
| 252 | \overload | - | ||||||
| 253 | - | |||||||
| 254 | Same as size(). | - | ||||||
| 255 | */ | - | ||||||
| 256 | int QQmlPropertyMap::count() const | - | ||||||
| 257 | { | - | ||||||
| 258 | Q_D(const QQmlPropertyMap); | - | ||||||
| 259 | return d->keys.count(); executed 10 times by 1 test: return d->keys.count();Executed by:
| 10 | ||||||
| 260 | } | - | ||||||
| 261 | - | |||||||
| 262 | /*! | - | ||||||
| 263 | Returns the number of keys in the map. | - | ||||||
| 264 | - | |||||||
| 265 | \sa isEmpty(), count() | - | ||||||
| 266 | */ | - | ||||||
| 267 | int QQmlPropertyMap::size() const | - | ||||||
| 268 | { | - | ||||||
| 269 | Q_D(const QQmlPropertyMap); | - | ||||||
| 270 | return d->keys.size(); executed 2 times by 1 test: return d->keys.size();Executed by:
| 2 | ||||||
| 271 | } | - | ||||||
| 272 | - | |||||||
| 273 | /*! | - | ||||||
| 274 | Returns true if the map contains no keys; otherwise returns | - | ||||||
| 275 | false. | - | ||||||
| 276 | - | |||||||
| 277 | \sa size() | - | ||||||
| 278 | */ | - | ||||||
| 279 | bool QQmlPropertyMap::isEmpty() const | - | ||||||
| 280 | { | - | ||||||
| 281 | Q_D(const QQmlPropertyMap); | - | ||||||
| 282 | return d->keys.isEmpty(); executed 6 times by 1 test: return d->keys.isEmpty();Executed by:
| 6 | ||||||
| 283 | } | - | ||||||
| 284 | - | |||||||
| 285 | /*! | - | ||||||
| 286 | Returns true if the map contains \a key. | - | ||||||
| 287 | - | |||||||
| 288 | \sa size() | - | ||||||
| 289 | */ | - | ||||||
| 290 | bool QQmlPropertyMap::contains(const QString &key) const | - | ||||||
| 291 | { | - | ||||||
| 292 | Q_D(const QQmlPropertyMap); | - | ||||||
| 293 | return d->keys.contains(key); executed 16 times by 1 test: return d->keys.contains(key);Executed by:
| 16 | ||||||
| 294 | } | - | ||||||
| 295 | - | |||||||
| 296 | /*! | - | ||||||
| 297 | Returns the value associated with the key \a key as a modifiable | - | ||||||
| 298 | reference. | - | ||||||
| 299 | - | |||||||
| 300 | If the map contains no item with key \a key, the function inserts | - | ||||||
| 301 | an invalid QVariant into the map with key \a key, and | - | ||||||
| 302 | returns a reference to it. | - | ||||||
| 303 | - | |||||||
| 304 | \sa insert(), value() | - | ||||||
| 305 | */ | - | ||||||
| 306 | QVariant &QQmlPropertyMap::operator[](const QString &key) | - | ||||||
| 307 | { | - | ||||||
| 308 | //### optimize | - | ||||||
| 309 | Q_D(QQmlPropertyMap); | - | ||||||
| 310 | QByteArray utf8key = key.toUtf8(); | - | ||||||
| 311 | if (!d->keys.contains(key))
| 2-4 | ||||||
| 312 | insert(key, QVariant());//force creation -- needed below executed 4 times by 1 test: insert(key, QVariant());Executed by:
| 4 | ||||||
| 313 | - | |||||||
| 314 | return d->mo->valueRef(utf8key); executed 6 times by 1 test: return d->mo->valueRef(utf8key);Executed by:
| 6 | ||||||
| 315 | } | - | ||||||
| 316 | - | |||||||
| 317 | /*! | - | ||||||
| 318 | \overload | - | ||||||
| 319 | - | |||||||
| 320 | Same as value(). | - | ||||||
| 321 | */ | - | ||||||
| 322 | QVariant QQmlPropertyMap::operator[](const QString &key) const | - | ||||||
| 323 | { | - | ||||||
| 324 | return value(key); executed 4 times by 1 test: return value(key);Executed by:
| 4 | ||||||
| 325 | } | - | ||||||
| 326 | - | |||||||
| 327 | /*! | - | ||||||
| 328 | Returns the new value to be stored for the key \a key. This function is provided | - | ||||||
| 329 | to intercept updates to a property from QML, where the value provided from QML is \a input. | - | ||||||
| 330 | - | |||||||
| 331 | Override this function to manipulate the property value as it is updated. Note that | - | ||||||
| 332 | this function is only invoked when the value is updated from QML. | - | ||||||
| 333 | */ | - | ||||||
| 334 | QVariant QQmlPropertyMap::updateValue(const QString &key, const QVariant &input) | - | ||||||
| 335 | { | - | ||||||
| 336 | Q_UNUSED(key) | - | ||||||
| 337 | return input; executed 6 times by 1 test: return input;Executed by:
| 6 | ||||||
| 338 | } | - | ||||||
| 339 | - | |||||||
| 340 | /*! \internal */ | - | ||||||
| 341 | QQmlPropertyMap::QQmlPropertyMap(const QMetaObject *staticMetaObject, QObject *parent) | - | ||||||
| 342 | : QObject(*(new QQmlPropertyMapPrivate), parent) | - | ||||||
| 343 | { | - | ||||||
| 344 | Q_D(QQmlPropertyMap); | - | ||||||
| 345 | d->mo = new QQmlPropertyMapMetaObject(this, d, staticMetaObject); | - | ||||||
| 346 | } executed 68 times by 6 tests: end of blockExecuted by:
| 68 | ||||||
| 347 | - | |||||||
| 348 | /*! | - | ||||||
| 349 | \fn void QQmlPropertyMap::valueChanged(const QString &key, const QVariant &value) | - | ||||||
| 350 | This signal is emitted whenever one of the values in the map is changed. \a key | - | ||||||
| 351 | is the key corresponding to the \a value that was changed. | - | ||||||
| 352 | - | |||||||
| 353 | \note valueChanged() is \b NOT emitted when changes are made by calling insert() | - | ||||||
| 354 | or clear() - it is only emitted when a value is updated from QML. | - | ||||||
| 355 | */ | - | ||||||
| 356 | - | |||||||
| 357 | /*! | - | ||||||
| 358 | \fn template<class DerivedType> QQmlPropertyMap::QQmlPropertyMap(DerivedType *derived, QObject *parent) | - | ||||||
| 359 | - | |||||||
| 360 | Constructs a bindable map with parent object \a parent. Use this constructor | - | ||||||
| 361 | in classes derived from QQmlPropertyMap. | - | ||||||
| 362 | - | |||||||
| 363 | The type of \a derived is used to register the property map with the \l {Meta-Object System}, | - | ||||||
| 364 | which is necessary to ensure that properties of the derived class are accessible. | - | ||||||
| 365 | This type must be derived from QQmlPropertyMap. | - | ||||||
| 366 | */ | - | ||||||
| 367 | - | |||||||
| 368 | QT_END_NAMESPACE | - | ||||||
| Source code | Switch to Preprocessed file |