| Absolute File Name: | /home/opencoverage/opencoverage/guest-scripts/qtdeclarative/src/qtdeclarative/src/qml/types/qqmlinstantiator.cpp |
| Source code | Switch to Preprocessed file |
| Line | Source | Count | ||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 1 | /**************************************************************************** | - | ||||||||||||||||||||||||
| 2 | ** | - | ||||||||||||||||||||||||
| 3 | ** Copyright (C) 2016 Research In Motion. | - | ||||||||||||||||||||||||
| 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 "qqmlinstantiator_p.h" | - | ||||||||||||||||||||||||
| 41 | #include "qqmlinstantiator_p_p.h" | - | ||||||||||||||||||||||||
| 42 | #include <QtQml/QQmlContext> | - | ||||||||||||||||||||||||
| 43 | #include <QtQml/QQmlComponent> | - | ||||||||||||||||||||||||
| 44 | #include <QtQml/QQmlInfo> | - | ||||||||||||||||||||||||
| 45 | #include <QtQml/QQmlError> | - | ||||||||||||||||||||||||
| 46 | #include <QtQml/private/qqmlobjectmodel_p.h> | - | ||||||||||||||||||||||||
| 47 | #if QT_CONFIG(qml_delegate_model) | - | ||||||||||||||||||||||||
| 48 | #include <QtQml/private/qqmldelegatemodel_p.h> | - | ||||||||||||||||||||||||
| 49 | #endif | - | ||||||||||||||||||||||||
| 50 | - | |||||||||||||||||||||||||
| 51 | QT_BEGIN_NAMESPACE | - | ||||||||||||||||||||||||
| 52 | - | |||||||||||||||||||||||||
| 53 | QQmlInstantiatorPrivate::QQmlInstantiatorPrivate() | - | ||||||||||||||||||||||||
| 54 | : componentComplete(true) | - | ||||||||||||||||||||||||
| 55 | , effectiveReset(false) | - | ||||||||||||||||||||||||
| 56 | , active(true) | - | ||||||||||||||||||||||||
| 57 | , async(false) | - | ||||||||||||||||||||||||
| 58 | #if QT_CONFIG(qml_delegate_model) | - | ||||||||||||||||||||||||
| 59 | , ownModel(false) | - | ||||||||||||||||||||||||
| 60 | #endif | - | ||||||||||||||||||||||||
| 61 | , requestedIndex(-1) | - | ||||||||||||||||||||||||
| 62 | , model(QVariant(1)) | - | ||||||||||||||||||||||||
| 63 | , instanceModel(nullptr) | - | ||||||||||||||||||||||||
| 64 | , delegate(nullptr) | - | ||||||||||||||||||||||||
| 65 | { | - | ||||||||||||||||||||||||
| 66 | } executed 18 times by 1 test: end of blockExecuted by:
| 18 | ||||||||||||||||||||||||
| 67 | - | |||||||||||||||||||||||||
| 68 | QQmlInstantiatorPrivate::~QQmlInstantiatorPrivate() | - | ||||||||||||||||||||||||
| 69 | { | - | ||||||||||||||||||||||||
| 70 | qDeleteAll(objects); | - | ||||||||||||||||||||||||
| 71 | } never executed: end of block | 0 | ||||||||||||||||||||||||
| 72 | - | |||||||||||||||||||||||||
| 73 | void QQmlInstantiatorPrivate::clear() | - | ||||||||||||||||||||||||
| 74 | { | - | ||||||||||||||||||||||||
| 75 | Q_Q(QQmlInstantiator); | - | ||||||||||||||||||||||||
| 76 | if (!instanceModel)
| 0-20 | ||||||||||||||||||||||||
| 77 | return; never executed: return; | 0 | ||||||||||||||||||||||||
| 78 | if (!objects.count())
| 2-18 | ||||||||||||||||||||||||
| 79 | return; executed 18 times by 1 test: return;Executed by:
| 18 | ||||||||||||||||||||||||
| 80 | - | |||||||||||||||||||||||||
| 81 | for (int i=0; i < objects.count(); i++) {
| 2-20 | ||||||||||||||||||||||||
| 82 | q->objectRemoved(i, objects[i]); | - | ||||||||||||||||||||||||
| 83 | instanceModel->release(objects[i]); | - | ||||||||||||||||||||||||
| 84 | } executed 20 times by 1 test: end of blockExecuted by:
| 20 | ||||||||||||||||||||||||
| 85 | objects.clear(); | - | ||||||||||||||||||||||||
| 86 | q->objectChanged(); | - | ||||||||||||||||||||||||
| 87 | } executed 2 times by 1 test: end of blockExecuted by:
| 2 | ||||||||||||||||||||||||
| 88 | - | |||||||||||||||||||||||||
| 89 | QObject *QQmlInstantiatorPrivate::modelObject(int index, bool async) | - | ||||||||||||||||||||||||
| 90 | { | - | ||||||||||||||||||||||||
| 91 | requestedIndex = index; | - | ||||||||||||||||||||||||
| 92 | QObject *o = instanceModel->object(index, async ? QQmlIncubator::Asynchronous : QQmlIncubator::AsynchronousIfNested); | - | ||||||||||||||||||||||||
| 93 | requestedIndex = -1; | - | ||||||||||||||||||||||||
| 94 | return o; executed 104 times by 1 test: return o;Executed by:
| 104 | ||||||||||||||||||||||||
| 95 | } | - | ||||||||||||||||||||||||
| 96 | - | |||||||||||||||||||||||||
| 97 | - | |||||||||||||||||||||||||
| 98 | void QQmlInstantiatorPrivate::regenerate() | - | ||||||||||||||||||||||||
| 99 | { | - | ||||||||||||||||||||||||
| 100 | Q_Q(QQmlInstantiator); | - | ||||||||||||||||||||||||
| 101 | if (!componentComplete)
| 2-20 | ||||||||||||||||||||||||
| 102 | return; executed 2 times by 1 test: return;Executed by:
| 2 | ||||||||||||||||||||||||
| 103 | - | |||||||||||||||||||||||||
| 104 | int prevCount = q->count(); | - | ||||||||||||||||||||||||
| 105 | - | |||||||||||||||||||||||||
| 106 | clear(); | - | ||||||||||||||||||||||||
| 107 | - | |||||||||||||||||||||||||
| 108 | if (!active || !instanceModel || !instanceModel->count() || !instanceModel->isValid()) {
| 0-18 | ||||||||||||||||||||||||
| 109 | if (prevCount)
| 0-4 | ||||||||||||||||||||||||
| 110 | q->countChanged(); never executed: q->countChanged(); | 0 | ||||||||||||||||||||||||
| 111 | return; executed 4 times by 1 test: return;Executed by:
| 4 | ||||||||||||||||||||||||
| 112 | } | - | ||||||||||||||||||||||||
| 113 | - | |||||||||||||||||||||||||
| 114 | for (int i = 0; i < instanceModel->count(); i++) {
| 16-96 | ||||||||||||||||||||||||
| 115 | QObject *object = modelObject(i, async); | - | ||||||||||||||||||||||||
| 116 | // If the item was already created we won't get a createdItem | - | ||||||||||||||||||||||||
| 117 | if (object)
| 40-56 | ||||||||||||||||||||||||
| 118 | _q_createdItem(i, object); executed 56 times by 1 test: _q_createdItem(i, object);Executed by:
| 56 | ||||||||||||||||||||||||
| 119 | } executed 96 times by 1 test: end of blockExecuted by:
| 96 | ||||||||||||||||||||||||
| 120 | if (q->count() != prevCount)
| 4-12 | ||||||||||||||||||||||||
| 121 | q->countChanged(); executed 12 times by 1 test: q->countChanged();Executed by:
| 12 | ||||||||||||||||||||||||
| 122 | } executed 16 times by 1 test: end of blockExecuted by:
| 16 | ||||||||||||||||||||||||
| 123 | - | |||||||||||||||||||||||||
| 124 | void QQmlInstantiatorPrivate::_q_createdItem(int idx, QObject* item) | - | ||||||||||||||||||||||||
| 125 | { | - | ||||||||||||||||||||||||
| 126 | Q_Q(QQmlInstantiator); | - | ||||||||||||||||||||||||
| 127 | if (objects.contains(item)) //Case when it was created synchronously in regenerate
| 64-104 | ||||||||||||||||||||||||
| 128 | return; executed 64 times by 1 test: return;Executed by:
| 64 | ||||||||||||||||||||||||
| 129 | if (requestedIndex != idx) // Asynchronous creation, reference the object
| 40-64 | ||||||||||||||||||||||||
| 130 | (void)instanceModel->object(idx); executed 40 times by 1 test: (void)instanceModel->object(idx);Executed by:
| 40 | ||||||||||||||||||||||||
| 131 | item->setParent(q); | - | ||||||||||||||||||||||||
| 132 | if (objects.size() < idx + 1) {
| 44-60 | ||||||||||||||||||||||||
| 133 | int modelCount = instanceModel->count(); | - | ||||||||||||||||||||||||
| 134 | if (objects.capacity() < modelCount)
| 14-46 | ||||||||||||||||||||||||
| 135 | objects.reserve(modelCount); executed 14 times by 1 test: objects.reserve(modelCount);Executed by:
| 14 | ||||||||||||||||||||||||
| 136 | objects.resize(idx + 1); | - | ||||||||||||||||||||||||
| 137 | } executed 60 times by 1 test: end of blockExecuted by:
| 60 | ||||||||||||||||||||||||
| 138 | if (QObject *o = objects.at(idx))
| 0-104 | ||||||||||||||||||||||||
| 139 | instanceModel->release(o); never executed: instanceModel->release(o); | 0 | ||||||||||||||||||||||||
| 140 | objects.replace(idx, item); | - | ||||||||||||||||||||||||
| 141 | if (objects.count() == 1)
| 14-90 | ||||||||||||||||||||||||
| 142 | q->objectChanged(); executed 14 times by 1 test: q->objectChanged();Executed by:
| 14 | ||||||||||||||||||||||||
| 143 | q->objectAdded(idx, item); | - | ||||||||||||||||||||||||
| 144 | } executed 104 times by 1 test: end of blockExecuted by:
| 104 | ||||||||||||||||||||||||
| 145 | - | |||||||||||||||||||||||||
| 146 | void QQmlInstantiatorPrivate::_q_modelUpdated(const QQmlChangeSet &changeSet, bool reset) | - | ||||||||||||||||||||||||
| 147 | { | - | ||||||||||||||||||||||||
| 148 | Q_Q(QQmlInstantiator); | - | ||||||||||||||||||||||||
| 149 | - | |||||||||||||||||||||||||
| 150 | if (!componentComplete || effectiveReset)
| 0-14 | ||||||||||||||||||||||||
| 151 | return; executed 4 times by 1 test: return;Executed by:
| 4 | ||||||||||||||||||||||||
| 152 | - | |||||||||||||||||||||||||
| 153 | if (reset) {
| 0-10 | ||||||||||||||||||||||||
| 154 | regenerate(); | - | ||||||||||||||||||||||||
| 155 | if (changeSet.difference() != 0)
| 0 | ||||||||||||||||||||||||
| 156 | q->countChanged(); never executed: q->countChanged(); | 0 | ||||||||||||||||||||||||
| 157 | return; never executed: return; | 0 | ||||||||||||||||||||||||
| 158 | } | - | ||||||||||||||||||||||||
| 159 | - | |||||||||||||||||||||||||
| 160 | int difference = 0; | - | ||||||||||||||||||||||||
| 161 | QHash<int, QVector<QPointer<QObject> > > moved; | - | ||||||||||||||||||||||||
| 162 | const QVector<QQmlChangeSet::Change> &removes = changeSet.removes(); | - | ||||||||||||||||||||||||
| 163 | for (const QQmlChangeSet::Change &remove : removes) { | - | ||||||||||||||||||||||||
| 164 | int index = qMin(remove.index, objects.count()); | - | ||||||||||||||||||||||||
| 165 | int count = qMin(remove.index + remove.count, objects.count()) - index; | - | ||||||||||||||||||||||||
| 166 | if (remove.isMove()) {
| 0-2 | ||||||||||||||||||||||||
| 167 | moved.insert(remove.moveId, objects.mid(index, count)); | - | ||||||||||||||||||||||||
| 168 | objects.erase( | - | ||||||||||||||||||||||||
| 169 | objects.begin() + index, | - | ||||||||||||||||||||||||
| 170 | objects.begin() + index + count); | - | ||||||||||||||||||||||||
| 171 | } else while (count--) { never executed: end of block
| 0-2 | ||||||||||||||||||||||||
| 172 | QObject *obj = objects.at(index); | - | ||||||||||||||||||||||||
| 173 | objects.remove(index); | - | ||||||||||||||||||||||||
| 174 | q->objectRemoved(index, obj); | - | ||||||||||||||||||||||||
| 175 | if (obj)
| 0-2 | ||||||||||||||||||||||||
| 176 | instanceModel->release(obj); executed 2 times by 1 test: instanceModel->release(obj);Executed by:
| 2 | ||||||||||||||||||||||||
| 177 | } executed 2 times by 1 test: end of blockExecuted by:
| 2 | ||||||||||||||||||||||||
| 178 | - | |||||||||||||||||||||||||
| 179 | difference -= remove.count; | - | ||||||||||||||||||||||||
| 180 | } executed 2 times by 1 test: end of blockExecuted by:
| 2 | ||||||||||||||||||||||||
| 181 | - | |||||||||||||||||||||||||
| 182 | const QVector<QQmlChangeSet::Change> &inserts = changeSet.inserts(); | - | ||||||||||||||||||||||||
| 183 | for (const QQmlChangeSet::Change &insert : inserts) { | - | ||||||||||||||||||||||||
| 184 | int index = qMin(insert.index, objects.count()); | - | ||||||||||||||||||||||||
| 185 | if (insert.isMove()) {
| 0-8 | ||||||||||||||||||||||||
| 186 | QVector<QPointer<QObject> > movedObjects = moved.value(insert.moveId); | - | ||||||||||||||||||||||||
| 187 | objects = objects.mid(0, index) + movedObjects + objects.mid(index); | - | ||||||||||||||||||||||||
| 188 | } else { never executed: end of block | 0 | ||||||||||||||||||||||||
| 189 | if (insert.index <= objects.size())
| 0-8 | ||||||||||||||||||||||||
| 190 | objects.insert(insert.index, insert.count, nullptr); executed 8 times by 1 test: objects.insert(insert.index, insert.count, nullptr);Executed by:
| 8 | ||||||||||||||||||||||||
| 191 | for (int i = 0; i < insert.count; ++i) {
| 8 | ||||||||||||||||||||||||
| 192 | int modelIndex = index + i; | - | ||||||||||||||||||||||||
| 193 | QObject* obj = modelObject(modelIndex, async); | - | ||||||||||||||||||||||||
| 194 | if (obj)
| 0-8 | ||||||||||||||||||||||||
| 195 | _q_createdItem(modelIndex, obj); executed 8 times by 1 test: _q_createdItem(modelIndex, obj);Executed by:
| 8 | ||||||||||||||||||||||||
| 196 | } executed 8 times by 1 test: end of blockExecuted by:
| 8 | ||||||||||||||||||||||||
| 197 | } executed 8 times by 1 test: end of blockExecuted by:
| 8 | ||||||||||||||||||||||||
| 198 | difference += insert.count; | - | ||||||||||||||||||||||||
| 199 | } executed 8 times by 1 test: end of blockExecuted by:
| 8 | ||||||||||||||||||||||||
| 200 | - | |||||||||||||||||||||||||
| 201 | if (difference != 0)
| 0-10 | ||||||||||||||||||||||||
| 202 | q->countChanged(); executed 10 times by 1 test: q->countChanged();Executed by:
| 10 | ||||||||||||||||||||||||
| 203 | } executed 10 times by 1 test: end of blockExecuted by:
| 10 | ||||||||||||||||||||||||
| 204 | - | |||||||||||||||||||||||||
| 205 | #if QT_CONFIG(qml_delegate_model) | - | ||||||||||||||||||||||||
| 206 | void QQmlInstantiatorPrivate::makeModel() | - | ||||||||||||||||||||||||
| 207 | { | - | ||||||||||||||||||||||||
| 208 | Q_Q(QQmlInstantiator); | - | ||||||||||||||||||||||||
| 209 | QQmlDelegateModel* delegateModel = new QQmlDelegateModel(qmlContext(q), q); | - | ||||||||||||||||||||||||
| 210 | instanceModel = delegateModel; | - | ||||||||||||||||||||||||
| 211 | ownModel = true; | - | ||||||||||||||||||||||||
| 212 | delegateModel->setDelegate(delegate); | - | ||||||||||||||||||||||||
| 213 | delegateModel->classBegin(); //Pretend it was made in QML | - | ||||||||||||||||||||||||
| 214 | if (componentComplete)
| 0-16 | ||||||||||||||||||||||||
| 215 | delegateModel->componentComplete(); executed 16 times by 1 test: delegateModel->componentComplete();Executed by:
| 16 | ||||||||||||||||||||||||
| 216 | } executed 16 times by 1 test: end of blockExecuted by:
| 16 | ||||||||||||||||||||||||
| 217 | #endif | - | ||||||||||||||||||||||||
| 218 | - | |||||||||||||||||||||||||
| 219 | - | |||||||||||||||||||||||||
| 220 | /*! | - | ||||||||||||||||||||||||
| 221 | \qmltype Instantiator | - | ||||||||||||||||||||||||
| 222 | \instantiates QQmlInstantiator | - | ||||||||||||||||||||||||
| 223 | \inqmlmodule QtQml | - | ||||||||||||||||||||||||
| 224 | \brief Dynamically creates objects. | - | ||||||||||||||||||||||||
| 225 | - | |||||||||||||||||||||||||
| 226 | A Instantiator can be used to control the dynamic creation of objects, or to dynamically | - | ||||||||||||||||||||||||
| 227 | create multiple objects from a template. | - | ||||||||||||||||||||||||
| 228 | - | |||||||||||||||||||||||||
| 229 | The Instantiator element will manage the objects it creates. Those objects are parented to the | - | ||||||||||||||||||||||||
| 230 | Instantiator and can also be deleted by the Instantiator if the Instantiator's properties change. Objects | - | ||||||||||||||||||||||||
| 231 | can also be destroyed dynamically through other means, and the Instantiator will not recreate | - | ||||||||||||||||||||||||
| 232 | them unless the properties of the Instantiator change. | - | ||||||||||||||||||||||||
| 233 | - | |||||||||||||||||||||||||
| 234 | */ | - | ||||||||||||||||||||||||
| 235 | QQmlInstantiator::QQmlInstantiator(QObject *parent) | - | ||||||||||||||||||||||||
| 236 | : QObject(*(new QQmlInstantiatorPrivate), parent) | - | ||||||||||||||||||||||||
| 237 | { | - | ||||||||||||||||||||||||
| 238 | } executed 18 times by 1 test: end of blockExecuted by:
| 18 | ||||||||||||||||||||||||
| 239 | - | |||||||||||||||||||||||||
| 240 | QQmlInstantiator::~QQmlInstantiator() | - | ||||||||||||||||||||||||
| 241 | { | - | ||||||||||||||||||||||||
| 242 | } | - | ||||||||||||||||||||||||
| 243 | - | |||||||||||||||||||||||||
| 244 | /*! | - | ||||||||||||||||||||||||
| 245 | \qmlsignal QtQml::Instantiator::objectAdded(int index, QtObject object) | - | ||||||||||||||||||||||||
| 246 | - | |||||||||||||||||||||||||
| 247 | This signal is emitted when an object is added to the Instantiator. The \a index | - | ||||||||||||||||||||||||
| 248 | parameter holds the index which the object has been given, and the \a object | - | ||||||||||||||||||||||||
| 249 | parameter holds the \l QtObject that has been added. | - | ||||||||||||||||||||||||
| 250 | - | |||||||||||||||||||||||||
| 251 | The corresponding handler is \c onObjectAdded. | - | ||||||||||||||||||||||||
| 252 | */ | - | ||||||||||||||||||||||||
| 253 | - | |||||||||||||||||||||||||
| 254 | /*! | - | ||||||||||||||||||||||||
| 255 | \qmlsignal QtQml::Instantiator::objectRemoved(int index, QtObject object) | - | ||||||||||||||||||||||||
| 256 | - | |||||||||||||||||||||||||
| 257 | This signal is emitted when an object is removed from the Instantiator. The \a index | - | ||||||||||||||||||||||||
| 258 | parameter holds the index which the object had been given, and the \a object | - | ||||||||||||||||||||||||
| 259 | parameter holds the \l QtObject that has been removed. | - | ||||||||||||||||||||||||
| 260 | - | |||||||||||||||||||||||||
| 261 | Do not keep a reference to \a object if it was created by this Instantiator, as | - | ||||||||||||||||||||||||
| 262 | in these cases it will be deleted shortly after the signal is handled. | - | ||||||||||||||||||||||||
| 263 | - | |||||||||||||||||||||||||
| 264 | The corresponding handler is \c onObjectRemoved. | - | ||||||||||||||||||||||||
| 265 | */ | - | ||||||||||||||||||||||||
| 266 | /*! | - | ||||||||||||||||||||||||
| 267 | \qmlproperty bool QtQml::Instantiator::active | - | ||||||||||||||||||||||||
| 268 | - | |||||||||||||||||||||||||
| 269 | When active is true, and the delegate component is ready, the Instantiator will | - | ||||||||||||||||||||||||
| 270 | create objects according to the model. When active is false, no objects | - | ||||||||||||||||||||||||
| 271 | will be created and any previously created objects will be destroyed. | - | ||||||||||||||||||||||||
| 272 | - | |||||||||||||||||||||||||
| 273 | Default is true. | - | ||||||||||||||||||||||||
| 274 | */ | - | ||||||||||||||||||||||||
| 275 | bool QQmlInstantiator::isActive() const | - | ||||||||||||||||||||||||
| 276 | { | - | ||||||||||||||||||||||||
| 277 | Q_D(const QQmlInstantiator); | - | ||||||||||||||||||||||||
| 278 | return d->active; executed 16 times by 1 test: return d->active;Executed by:
| 16 | ||||||||||||||||||||||||
| 279 | } | - | ||||||||||||||||||||||||
| 280 | - | |||||||||||||||||||||||||
| 281 | void QQmlInstantiator::setActive(bool newVal) | - | ||||||||||||||||||||||||
| 282 | { | - | ||||||||||||||||||||||||
| 283 | Q_D(QQmlInstantiator); | - | ||||||||||||||||||||||||
| 284 | if (newVal == d->active)
| 0-4 | ||||||||||||||||||||||||
| 285 | return; never executed: return; | 0 | ||||||||||||||||||||||||
| 286 | d->active = newVal; | - | ||||||||||||||||||||||||
| 287 | emit activeChanged(); | - | ||||||||||||||||||||||||
| 288 | d->regenerate(); | - | ||||||||||||||||||||||||
| 289 | } executed 4 times by 1 test: end of blockExecuted by:
| 4 | ||||||||||||||||||||||||
| 290 | - | |||||||||||||||||||||||||
| 291 | /*! | - | ||||||||||||||||||||||||
| 292 | \qmlproperty bool QtQml::Instantiator::asynchronous | - | ||||||||||||||||||||||||
| 293 | - | |||||||||||||||||||||||||
| 294 | When asynchronous is true the Instantiator will attempt to create objects | - | ||||||||||||||||||||||||
| 295 | asynchronously. This means that objects may not be available immediately, | - | ||||||||||||||||||||||||
| 296 | even if active is set to true. | - | ||||||||||||||||||||||||
| 297 | - | |||||||||||||||||||||||||
| 298 | You can use the objectAdded signal to respond to items being created. | - | ||||||||||||||||||||||||
| 299 | - | |||||||||||||||||||||||||
| 300 | Default is false. | - | ||||||||||||||||||||||||
| 301 | */ | - | ||||||||||||||||||||||||
| 302 | bool QQmlInstantiator::isAsync() const | - | ||||||||||||||||||||||||
| 303 | { | - | ||||||||||||||||||||||||
| 304 | Q_D(const QQmlInstantiator); | - | ||||||||||||||||||||||||
| 305 | return d->async; never executed: return d->async; | 0 | ||||||||||||||||||||||||
| 306 | } | - | ||||||||||||||||||||||||
| 307 | - | |||||||||||||||||||||||||
| 308 | void QQmlInstantiator::setAsync(bool newVal) | - | ||||||||||||||||||||||||
| 309 | { | - | ||||||||||||||||||||||||
| 310 | Q_D(QQmlInstantiator); | - | ||||||||||||||||||||||||
| 311 | if (newVal == d->async)
| 0-2 | ||||||||||||||||||||||||
| 312 | return; never executed: return; | 0 | ||||||||||||||||||||||||
| 313 | d->async = newVal; | - | ||||||||||||||||||||||||
| 314 | emit asynchronousChanged(); | - | ||||||||||||||||||||||||
| 315 | } executed 2 times by 1 test: end of blockExecuted by:
| 2 | ||||||||||||||||||||||||
| 316 | - | |||||||||||||||||||||||||
| 317 | - | |||||||||||||||||||||||||
| 318 | /*! | - | ||||||||||||||||||||||||
| 319 | \qmlproperty int QtQml::Instantiator::count | - | ||||||||||||||||||||||||
| 320 | - | |||||||||||||||||||||||||
| 321 | The number of objects the Instantiator is currently managing. | - | ||||||||||||||||||||||||
| 322 | */ | - | ||||||||||||||||||||||||
| 323 | - | |||||||||||||||||||||||||
| 324 | int QQmlInstantiator::count() const | - | ||||||||||||||||||||||||
| 325 | { | - | ||||||||||||||||||||||||
| 326 | Q_D(const QQmlInstantiator); | - | ||||||||||||||||||||||||
| 327 | return d->objects.count(); executed 56 times by 1 test: return d->objects.count();Executed by:
| 56 | ||||||||||||||||||||||||
| 328 | } | - | ||||||||||||||||||||||||
| 329 | - | |||||||||||||||||||||||||
| 330 | /*! | - | ||||||||||||||||||||||||
| 331 | \qmlproperty QtQml::Component QtQml::Instantiator::delegate | - | ||||||||||||||||||||||||
| 332 | \default | - | ||||||||||||||||||||||||
| 333 | - | |||||||||||||||||||||||||
| 334 | The component used to create all objects. | - | ||||||||||||||||||||||||
| 335 | - | |||||||||||||||||||||||||
| 336 | Note that an extra variable, index, will be available inside instances of the | - | ||||||||||||||||||||||||
| 337 | delegate. This variable refers to the index of the instance inside the Instantiator, | - | ||||||||||||||||||||||||
| 338 | and can be used to obtain the object through the objectAt method of the Instantiator. | - | ||||||||||||||||||||||||
| 339 | - | |||||||||||||||||||||||||
| 340 | If this property is changed, all instances using the old delegate will be destroyed | - | ||||||||||||||||||||||||
| 341 | and new instances will be created using the new delegate. | - | ||||||||||||||||||||||||
| 342 | */ | - | ||||||||||||||||||||||||
| 343 | QQmlComponent* QQmlInstantiator::delegate() | - | ||||||||||||||||||||||||
| 344 | { | - | ||||||||||||||||||||||||
| 345 | Q_D(QQmlInstantiator); | - | ||||||||||||||||||||||||
| 346 | return d->delegate; executed 6 times by 1 test: return d->delegate;Executed by:
| 6 | ||||||||||||||||||||||||
| 347 | } | - | ||||||||||||||||||||||||
| 348 | - | |||||||||||||||||||||||||
| 349 | void QQmlInstantiator::setDelegate(QQmlComponent* c) | - | ||||||||||||||||||||||||
| 350 | { | - | ||||||||||||||||||||||||
| 351 | Q_D(QQmlInstantiator); | - | ||||||||||||||||||||||||
| 352 | if (c == d->delegate)
| 0-18 | ||||||||||||||||||||||||
| 353 | return; never executed: return; | 0 | ||||||||||||||||||||||||
| 354 | - | |||||||||||||||||||||||||
| 355 | d->delegate = c; | - | ||||||||||||||||||||||||
| 356 | emit delegateChanged(); | - | ||||||||||||||||||||||||
| 357 | - | |||||||||||||||||||||||||
| 358 | #if QT_CONFIG(qml_delegate_model) | - | ||||||||||||||||||||||||
| 359 | if (!d->ownModel)
| 0-18 | ||||||||||||||||||||||||
| 360 | return; executed 18 times by 1 test: return;Executed by:
| 18 | ||||||||||||||||||||||||
| 361 | - | |||||||||||||||||||||||||
| 362 | if (QQmlDelegateModel *dModel = qobject_cast<QQmlDelegateModel*>(d->instanceModel))
| 0 | ||||||||||||||||||||||||
| 363 | dModel->setDelegate(c); never executed: dModel->setDelegate(c); | 0 | ||||||||||||||||||||||||
| 364 | if (d->componentComplete)
| 0 | ||||||||||||||||||||||||
| 365 | d->regenerate(); never executed: d->regenerate(); | 0 | ||||||||||||||||||||||||
| 366 | #endif | - | ||||||||||||||||||||||||
| 367 | } never executed: end of block | 0 | ||||||||||||||||||||||||
| 368 | - | |||||||||||||||||||||||||
| 369 | /*! | - | ||||||||||||||||||||||||
| 370 | \qmlproperty variant QtQml::Instantiator::model | - | ||||||||||||||||||||||||
| 371 | - | |||||||||||||||||||||||||
| 372 | This property can be set to any of the supported \l {qml-data-models}{data models}: | - | ||||||||||||||||||||||||
| 373 | - | |||||||||||||||||||||||||
| 374 | \list | - | ||||||||||||||||||||||||
| 375 | \li A number that indicates the number of delegates to be created by the repeater | - | ||||||||||||||||||||||||
| 376 | \li A model (e.g. a ListModel item, or a QAbstractItemModel subclass) | - | ||||||||||||||||||||||||
| 377 | \li A string list | - | ||||||||||||||||||||||||
| 378 | \li An object list | - | ||||||||||||||||||||||||
| 379 | \endlist | - | ||||||||||||||||||||||||
| 380 | - | |||||||||||||||||||||||||
| 381 | The type of model affects the properties that are exposed to the \l delegate. | - | ||||||||||||||||||||||||
| 382 | - | |||||||||||||||||||||||||
| 383 | Default value is 1, which creates a single delegate instance. | - | ||||||||||||||||||||||||
| 384 | - | |||||||||||||||||||||||||
| 385 | \sa {qml-data-models}{Data Models} | - | ||||||||||||||||||||||||
| 386 | */ | - | ||||||||||||||||||||||||
| 387 | - | |||||||||||||||||||||||||
| 388 | QVariant QQmlInstantiator::model() const | - | ||||||||||||||||||||||||
| 389 | { | - | ||||||||||||||||||||||||
| 390 | Q_D(const QQmlInstantiator); | - | ||||||||||||||||||||||||
| 391 | return d->model; never executed: return d->model; | 0 | ||||||||||||||||||||||||
| 392 | } | - | ||||||||||||||||||||||||
| 393 | - | |||||||||||||||||||||||||
| 394 | void QQmlInstantiator::setModel(const QVariant &v) | - | ||||||||||||||||||||||||
| 395 | { | - | ||||||||||||||||||||||||
| 396 | Q_D(QQmlInstantiator); | - | ||||||||||||||||||||||||
| 397 | if (d->model == v)
| 2-32 | ||||||||||||||||||||||||
| 398 | return; executed 2 times by 1 test: return;Executed by:
| 2 | ||||||||||||||||||||||||
| 399 | - | |||||||||||||||||||||||||
| 400 | d->model = v; | - | ||||||||||||||||||||||||
| 401 | //Don't actually set model until componentComplete in case it wants to create its delegates immediately | - | ||||||||||||||||||||||||
| 402 | if (!d->componentComplete)
| 14-18 | ||||||||||||||||||||||||
| 403 | return; executed 14 times by 1 test: return;Executed by:
| 14 | ||||||||||||||||||||||||
| 404 | - | |||||||||||||||||||||||||
| 405 | QQmlInstanceModel *prevModel = d->instanceModel; | - | ||||||||||||||||||||||||
| 406 | QObject *object = qvariant_cast<QObject*>(v); | - | ||||||||||||||||||||||||
| 407 | QQmlInstanceModel *vim = nullptr; | - | ||||||||||||||||||||||||
| 408 | if (object && (vim = qobject_cast<QQmlInstanceModel *>(object))) {
| 0-16 | ||||||||||||||||||||||||
| 409 | #if QT_CONFIG(qml_delegate_model) | - | ||||||||||||||||||||||||
| 410 | if (d->ownModel) {
| 0 | ||||||||||||||||||||||||
| 411 | delete d->instanceModel; | - | ||||||||||||||||||||||||
| 412 | prevModel = nullptr; | - | ||||||||||||||||||||||||
| 413 | d->ownModel = false; | - | ||||||||||||||||||||||||
| 414 | } never executed: end of block | 0 | ||||||||||||||||||||||||
| 415 | #endif | - | ||||||||||||||||||||||||
| 416 | d->instanceModel = vim; | - | ||||||||||||||||||||||||
| 417 | #if QT_CONFIG(qml_delegate_model) | - | ||||||||||||||||||||||||
| 418 | } else if (v != QVariant(0)){ never executed: end of block
| 0-18 | ||||||||||||||||||||||||
| 419 | if (!d->ownModel)
| 2-16 | ||||||||||||||||||||||||
| 420 | d->makeModel(); executed 16 times by 1 test: d->makeModel();Executed by:
| 16 | ||||||||||||||||||||||||
| 421 | - | |||||||||||||||||||||||||
| 422 | if (QQmlDelegateModel *dataModel = qobject_cast<QQmlDelegateModel *>(d->instanceModel)) {
| 0-18 | ||||||||||||||||||||||||
| 423 | d->effectiveReset = true; | - | ||||||||||||||||||||||||
| 424 | dataModel->setModel(v); | - | ||||||||||||||||||||||||
| 425 | d->effectiveReset = false; | - | ||||||||||||||||||||||||
| 426 | } executed 18 times by 1 test: end of blockExecuted by:
| 18 | ||||||||||||||||||||||||
| 427 | #endif | - | ||||||||||||||||||||||||
| 428 | } executed 18 times by 1 test: end of blockExecuted by:
| 18 | ||||||||||||||||||||||||
| 429 | - | |||||||||||||||||||||||||
| 430 | if (d->instanceModel != prevModel) {
| 2-16 | ||||||||||||||||||||||||
| 431 | if (prevModel) {
| 0-16 | ||||||||||||||||||||||||
| 432 | disconnect(prevModel, SIGNAL(modelUpdated(QQmlChangeSet,bool)), | - | ||||||||||||||||||||||||
| 433 | this, SLOT(_q_modelUpdated(QQmlChangeSet,bool))); | - | ||||||||||||||||||||||||
| 434 | disconnect(prevModel, SIGNAL(createdItem(int,QObject*)), this, SLOT(_q_createdItem(int,QObject*))); | - | ||||||||||||||||||||||||
| 435 | //disconnect(prevModel, SIGNAL(initItem(int,QObject*)), this, SLOT(initItem(int,QObject*))); | - | ||||||||||||||||||||||||
| 436 | } never executed: end of block | 0 | ||||||||||||||||||||||||
| 437 | - | |||||||||||||||||||||||||
| 438 | if (d->instanceModel) {
| 0-16 | ||||||||||||||||||||||||
| 439 | connect(d->instanceModel, SIGNAL(modelUpdated(QQmlChangeSet,bool)), | - | ||||||||||||||||||||||||
| 440 | this, SLOT(_q_modelUpdated(QQmlChangeSet,bool))); | - | ||||||||||||||||||||||||
| 441 | connect(d->instanceModel, SIGNAL(createdItem(int,QObject*)), this, SLOT(_q_createdItem(int,QObject*))); | - | ||||||||||||||||||||||||
| 442 | //connect(d->instanceModel, SIGNAL(initItem(int,QObject*)), this, SLOT(initItem(int,QObject*))); | - | ||||||||||||||||||||||||
| 443 | } executed 16 times by 1 test: end of blockExecuted by:
| 16 | ||||||||||||||||||||||||
| 444 | } executed 16 times by 1 test: end of blockExecuted by:
| 16 | ||||||||||||||||||||||||
| 445 | - | |||||||||||||||||||||||||
| 446 | d->regenerate(); | - | ||||||||||||||||||||||||
| 447 | emit modelChanged(); | - | ||||||||||||||||||||||||
| 448 | } executed 18 times by 1 test: end of blockExecuted by:
| 18 | ||||||||||||||||||||||||
| 449 | - | |||||||||||||||||||||||||
| 450 | /*! | - | ||||||||||||||||||||||||
| 451 | \qmlproperty QtObject QtQml::Instantiator::object | - | ||||||||||||||||||||||||
| 452 | - | |||||||||||||||||||||||||
| 453 | This is a reference to the first created object, intended as a convenience | - | ||||||||||||||||||||||||
| 454 | for the case where only one object has been created. | - | ||||||||||||||||||||||||
| 455 | */ | - | ||||||||||||||||||||||||
| 456 | QObject *QQmlInstantiator::object() const | - | ||||||||||||||||||||||||
| 457 | { | - | ||||||||||||||||||||||||
| 458 | Q_D(const QQmlInstantiator); | - | ||||||||||||||||||||||||
| 459 | if (d->objects.count())
| 0-4 | ||||||||||||||||||||||||
| 460 | return d->objects[0]; executed 4 times by 1 test: return d->objects[0];Executed by:
| 4 | ||||||||||||||||||||||||
| 461 | return nullptr; never executed: return nullptr; | 0 | ||||||||||||||||||||||||
| 462 | } | - | ||||||||||||||||||||||||
| 463 | - | |||||||||||||||||||||||||
| 464 | /*! | - | ||||||||||||||||||||||||
| 465 | \qmlmethod QtObject QtQml::Instantiator::objectAt(int index) | - | ||||||||||||||||||||||||
| 466 | - | |||||||||||||||||||||||||
| 467 | Returns a reference to the object with the given \a index. | - | ||||||||||||||||||||||||
| 468 | */ | - | ||||||||||||||||||||||||
| 469 | QObject *QQmlInstantiator::objectAt(int index) const | - | ||||||||||||||||||||||||
| 470 | { | - | ||||||||||||||||||||||||
| 471 | Q_D(const QQmlInstantiator); | - | ||||||||||||||||||||||||
| 472 | if (index >= 0 && index < d->objects.count())
| 0-78 | ||||||||||||||||||||||||
| 473 | return d->objects[index]; executed 78 times by 1 test: return d->objects[index];Executed by:
| 78 | ||||||||||||||||||||||||
| 474 | return nullptr; never executed: return nullptr; | 0 | ||||||||||||||||||||||||
| 475 | } | - | ||||||||||||||||||||||||
| 476 | - | |||||||||||||||||||||||||
| 477 | /*! | - | ||||||||||||||||||||||||
| 478 | \internal | - | ||||||||||||||||||||||||
| 479 | */ | - | ||||||||||||||||||||||||
| 480 | void QQmlInstantiator::classBegin() | - | ||||||||||||||||||||||||
| 481 | { | - | ||||||||||||||||||||||||
| 482 | Q_D(QQmlInstantiator); | - | ||||||||||||||||||||||||
| 483 | d->componentComplete = false; | - | ||||||||||||||||||||||||
| 484 | } executed 18 times by 1 test: end of blockExecuted by:
| 18 | ||||||||||||||||||||||||
| 485 | - | |||||||||||||||||||||||||
| 486 | /*! | - | ||||||||||||||||||||||||
| 487 | \internal | - | ||||||||||||||||||||||||
| 488 | */ | - | ||||||||||||||||||||||||
| 489 | void QQmlInstantiator::componentComplete() | - | ||||||||||||||||||||||||
| 490 | { | - | ||||||||||||||||||||||||
| 491 | Q_D(QQmlInstantiator); | - | ||||||||||||||||||||||||
| 492 | d->componentComplete = true; | - | ||||||||||||||||||||||||
| 493 | #if QT_CONFIG(qml_delegate_model) | - | ||||||||||||||||||||||||
| 494 | if (d->ownModel) {
| 0-18 | ||||||||||||||||||||||||
| 495 | static_cast<QQmlDelegateModel*>(d->instanceModel)->componentComplete(); | - | ||||||||||||||||||||||||
| 496 | d->regenerate(); | - | ||||||||||||||||||||||||
| 497 | } else never executed: end of block | 0 | ||||||||||||||||||||||||
| 498 | #endif | - | ||||||||||||||||||||||||
| 499 | { | - | ||||||||||||||||||||||||
| 500 | QVariant realModel = d->model; | - | ||||||||||||||||||||||||
| 501 | d->model = QVariant(0); | - | ||||||||||||||||||||||||
| 502 | setModel(realModel); //If realModel == d->model this won't do anything, but that's fine since the model's 0 | - | ||||||||||||||||||||||||
| 503 | //setModel calls regenerate | - | ||||||||||||||||||||||||
| 504 | } executed 18 times by 1 test: end of blockExecuted by:
| 18 | ||||||||||||||||||||||||
| 505 | } | - | ||||||||||||||||||||||||
| 506 | - | |||||||||||||||||||||||||
| 507 | QT_END_NAMESPACE | - | ||||||||||||||||||||||||
| 508 | - | |||||||||||||||||||||||||
| 509 | #include "moc_qqmlinstantiator_p.cpp" | - | ||||||||||||||||||||||||
| Source code | Switch to Preprocessed file |