OpenCoverage

qqmlpropertymap.cpp

Absolute File Name:/home/opencoverage/opencoverage/guest-scripts/qtdeclarative/src/qtdeclarative/src/qml/util/qqmlpropertymap.cpp
Source codeSwitch to Preprocessed file
LineSourceCount
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-
47QT_BEGIN_NAMESPACE-
48-
49//QQmlPropertyMapMetaObject lets us listen for changes coming from QML-
50//so we can emit the changed signal.-
51class QQmlPropertyMapMetaObject : public QQmlOpenMetaObject-
52{-
53public:-
54 QQmlPropertyMapMetaObject(QQmlPropertyMap *obj, QQmlPropertyMapPrivate *objPriv, const QMetaObject *staticMetaObject);-
55-
56protected:-
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-
64private:-
65 QQmlPropertyMap *map;-
66 QQmlPropertyMapPrivate *priv;-
67};-
68-
69class QQmlPropertyMapPrivate : public QObjectPrivate-
70{-
71 Q_DECLARE_PUBLIC(QQmlPropertyMap)-
72public:-
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-
83bool 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:
  • tst_qqmlpropertymap
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:
  • tst_qqmlpropertymap
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:
  • tst_qqmlpropertymap
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:
  • tst_qqmlpropertymap
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:
  • tst_qqmlpropertymap
100
91}-
92-
93QVariant 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:
  • tst_qqmlpropertymap
10
97}-
98-
99void 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 block
Executed by:
  • tst_qqmlpropertymap
10
104-
105const 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:
  • tst_qqmlpropertymap
20
109}-
110-
111QQmlPropertyMapMetaObject::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 block
Executed by:
  • tst_qqmlpropertymap
  • tst_qquickanimationcontroller
  • tst_qquicklayouts
  • tst_quicktestmainwithsetup
  • tst_signalspy
  • tst_testfiltering
68
117-
118QVariant 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:
  • tst_qqmlpropertymap
10
121}-
122-
123void QQmlPropertyMapMetaObject::propertyWritten(int index)-
124{-
125 priv->emitChanged(priv->propertyName(index), value(index));-
126}
executed 10 times by 1 test: end of block
Executed by:
  • tst_qqmlpropertymap
10
127-
128void QQmlPropertyMapMetaObject::propertyCreated(int, QMetaPropertyBuilder &b)-
129{-
130 priv->keys.append(QString::fromUtf8(b.name()));-
131}
executed 46 times by 1 test: end of block
Executed by:
  • tst_qqmlpropertymap
46
132-
133int QQmlPropertyMapMetaObject::createProperty(const char *name, const char *value)-
134{-
135 if (!priv->validKeyName(QString::fromUtf8(name)))
!priv->validKe...romUtf8(name))Description
TRUEnever evaluated
FALSEevaluated 46 times by 1 test
Evaluated by:
  • tst_qqmlpropertymap
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:
  • tst_qqmlpropertymap
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*/-
188QQmlPropertyMap::QQmlPropertyMap(QObject *parent)-
189: QQmlPropertyMap(&staticMetaObject, parent)-
190{-
191}
executed 54 times by 5 tests: end of block
Executed by:
  • tst_qqmlpropertymap
  • tst_qquickanimationcontroller
  • tst_qquicklayouts
  • tst_quicktestmainwithsetup
  • tst_testfiltering
54
192-
193/*!-
194 Destroys the bindable map.-
195*/-
196QQmlPropertyMap::~QQmlPropertyMap()-
197{-
198}-
199-
200/*!-
201 Clears the value (if any) associated with \a key.-
202*/-
203void 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 block
Executed by:
  • tst_qqmlpropertymap
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*/-
215QVariant 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:
  • tst_qqmlpropertymap
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*/-
226void QQmlPropertyMap::insert(const QString &key, const QVariant &value)-
227{-
228 Q_D(QQmlPropertyMap);-
229-
230 if (d->validKeyName(key)) {
d->validKeyName(key)Description
TRUEevaluated 48 times by 1 test
Evaluated by:
  • tst_qqmlpropertymap
FALSEevaluated 6 times by 1 test
Evaluated by:
  • tst_qqmlpropertymap
6-48
231 d->mo->setValue(key.toUtf8(), value);-
232 } else {
executed 48 times by 1 test: end of block
Executed by:
  • tst_qqmlpropertymap
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 block
Executed by:
  • tst_qqmlpropertymap
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*/-
245QStringList QQmlPropertyMap::keys() const-
246{-
247 Q_D(const QQmlPropertyMap);-
248 return d->keys;
executed 20 times by 1 test: return d->keys;
Executed by:
  • tst_qqmlpropertymap
20
249}-
250-
251/*!-
252 \overload-
253-
254 Same as size().-
255*/-
256int 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:
  • tst_qqmlpropertymap
10
260}-
261-
262/*!-
263 Returns the number of keys in the map.-
264-
265 \sa isEmpty(), count()-
266*/-
267int 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:
  • tst_qqmlpropertymap
2
271}-
272-
273/*!-
274 Returns true if the map contains no keys; otherwise returns-
275 false.-
276-
277 \sa size()-
278*/-
279bool 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:
  • tst_qqmlpropertymap
6
283}-
284-
285/*!-
286 Returns true if the map contains \a key.-
287-
288 \sa size()-
289*/-
290bool 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:
  • tst_qqmlpropertymap
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*/-
306QVariant &QQmlPropertyMap::operator[](const QString &key)-
307{-
308 //### optimize-
309 Q_D(QQmlPropertyMap);-
310 QByteArray utf8key = key.toUtf8();-
311 if (!d->keys.contains(key))
!d->keys.contains(key)Description
TRUEevaluated 4 times by 1 test
Evaluated by:
  • tst_qqmlpropertymap
FALSEevaluated 2 times by 1 test
Evaluated by:
  • tst_qqmlpropertymap
2-4
312 insert(key, QVariant());//force creation -- needed below
executed 4 times by 1 test: insert(key, QVariant());
Executed by:
  • tst_qqmlpropertymap
4
313-
314 return d->mo->valueRef(utf8key);
executed 6 times by 1 test: return d->mo->valueRef(utf8key);
Executed by:
  • tst_qqmlpropertymap
6
315}-
316-
317/*!-
318 \overload-
319-
320 Same as value().-
321*/-
322QVariant QQmlPropertyMap::operator[](const QString &key) const-
323{-
324 return value(key);
executed 4 times by 1 test: return value(key);
Executed by:
  • tst_qqmlpropertymap
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*/-
334QVariant 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:
  • tst_qqmlpropertymap
6
338}-
339-
340/*! \internal */-
341QQmlPropertyMap::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 block
Executed by:
  • tst_qqmlpropertymap
  • tst_qquickanimationcontroller
  • tst_qquicklayouts
  • tst_quicktestmainwithsetup
  • tst_signalspy
  • tst_testfiltering
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-
368QT_END_NAMESPACE-
Source codeSwitch to Preprocessed file

Generated by Squish Coco 4.2.0