diff options
author | Lars Knoll <lars.knoll@qt.io> | 2017-11-08 10:28:30 +0100 |
---|---|---|
committer | Lars Knoll <lars.knoll@qt.io> | 2017-11-08 12:32:01 +0100 |
commit | 0a27a14ec1879096e5fffca75b3bd107b813db87 (patch) | |
tree | 2e4332c9545af969903cb9c8bd619f2adc53f5a8 /src/qml/types | |
parent | b853a1e1b003d4dc01884201c09c2fcbf75f2cf6 (diff) | |
parent | 7bedd55551fbe95355b0db11f9d576924e829f9d (diff) |
Merge remote-tracking branch 'origin/dev' into new-backend
Change-Id: I1a49b4a242ed0764101521d06ec612e96bff0e4c
Diffstat (limited to 'src/qml/types')
-rw-r--r-- | src/qml/types/qqmlitemmodels.qdoc | 10 | ||||
-rw-r--r-- | src/qml/types/qqmlitemselectionmodel.qdoc | 10 | ||||
-rw-r--r-- | src/qml/types/qqmllistmodel.cpp | 178 | ||||
-rw-r--r-- | src/qml/types/qqmllistmodel_p.h | 4 | ||||
-rw-r--r-- | src/qml/types/qqmllistmodel_p_p.h | 6 | ||||
-rw-r--r-- | src/qml/types/qqmlobjectmodel.cpp | 6 | ||||
-rw-r--r-- | src/qml/types/qquickworkerscript_p.h | 2 |
7 files changed, 152 insertions, 64 deletions
diff --git a/src/qml/types/qqmlitemmodels.qdoc b/src/qml/types/qqmlitemmodels.qdoc index 6733330209..f6e1b0b1b9 100644 --- a/src/qml/types/qqmlitemmodels.qdoc +++ b/src/qml/types/qqmlitemmodels.qdoc @@ -1,7 +1,7 @@ /**************************************************************************** ** -** Copyright (C) 2015 The Qt Company Ltd. -** Contact: http://www.qt.io/licensing/ +** Copyright (C) 2017 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ ** ** This file is part of the documentation of the Qt Toolkit. ** @@ -11,8 +11,8 @@ ** accordance with the commercial license agreement provided with the ** Software or, alternatively, in accordance with the terms contained in ** a written agreement between you and The Qt Company. For licensing terms -** and conditions see http://www.qt.io/terms-conditions. For further -** information use the contact form at http://www.qt.io/contact-us. +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. ** ** GNU Free Documentation License Usage ** Alternatively, this file may be used under the terms of the GNU Free @@ -20,7 +20,7 @@ ** Foundation and appearing in the file included in the packaging of ** this file. Please review the following information to ensure ** the GNU Free Documentation License version 1.3 requirements -** will be met: http://www.gnu.org/copyleft/fdl.html. +** will be met: https://www.gnu.org/licenses/fdl-1.3.html. ** $QT_END_LICENSE$ ** ****************************************************************************/ diff --git a/src/qml/types/qqmlitemselectionmodel.qdoc b/src/qml/types/qqmlitemselectionmodel.qdoc index c223ef614e..29ac1aa17a 100644 --- a/src/qml/types/qqmlitemselectionmodel.qdoc +++ b/src/qml/types/qqmlitemselectionmodel.qdoc @@ -1,7 +1,7 @@ /**************************************************************************** ** -** Copyright (C) 2015 The Qt Company Ltd. -** Contact: http://www.qt.io/licensing/ +** Copyright (C) 2017 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ ** ** This file is part of the documentation of the Qt Toolkit. ** @@ -11,8 +11,8 @@ ** accordance with the commercial license agreement provided with the ** Software or, alternatively, in accordance with the terms contained in ** a written agreement between you and The Qt Company. For licensing terms -** and conditions see http://www.qt.io/terms-conditions. For further -** information use the contact form at http://www.qt.io/contact-us. +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. ** ** GNU Free Documentation License Usage ** Alternatively, this file may be used under the terms of the GNU Free @@ -20,7 +20,7 @@ ** Foundation and appearing in the file included in the packaging of ** this file. Please review the following information to ensure ** the GNU Free Documentation License version 1.3 requirements -** will be met: http://www.gnu.org/copyleft/fdl.html. +** will be met: https://www.gnu.org/licenses/fdl-1.3.html. ** $QT_END_LICENSE$ ** ****************************************************************************/ diff --git a/src/qml/types/qqmllistmodel.cpp b/src/qml/types/qqmllistmodel.cpp index d088a5da42..e32e0c75f3 100644 --- a/src/qml/types/qqmllistmodel.cpp +++ b/src/qml/types/qqmllistmodel.cpp @@ -42,6 +42,7 @@ #include <private/qqmlopenmetaobject_p.h> #include <private/qqmljsast_p.h> #include <private/qqmljsengine_p.h> +#include <private/qjsvalue_p.h> #include <private/qqmlcustomparser_p.h> #include <private/qqmlengine_p.h> @@ -84,7 +85,7 @@ static QString roleTypeName(ListLayout::Role::DataType t) static const QString roleTypeNames[] = { QStringLiteral("String"), QStringLiteral("Number"), QStringLiteral("Bool"), QStringLiteral("List"), QStringLiteral("QObject"), QStringLiteral("VariantMap"), - QStringLiteral("DateTime") + QStringLiteral("DateTime"), QStringLiteral("Function") }; if (t > ListLayout::Role::Invalid && t < ListLayout::Role::MaxDataType) @@ -123,8 +124,8 @@ const ListLayout::Role &ListLayout::getRoleOrCreate(QV4::String *key, Role::Data const ListLayout::Role &ListLayout::createRole(const QString &key, ListLayout::Role::DataType type) { - const int dataSizes[] = { sizeof(QString), sizeof(double), sizeof(bool), sizeof(ListModel *), sizeof(QPointer<QObject>), sizeof(QVariantMap), sizeof(QDateTime) }; - const int dataAlignments[] = { sizeof(QString), sizeof(double), sizeof(bool), sizeof(ListModel *), sizeof(QObject *), sizeof(QVariantMap), sizeof(QDateTime) }; + const int dataSizes[] = { sizeof(QString), sizeof(double), sizeof(bool), sizeof(ListModel *), sizeof(QPointer<QObject>), sizeof(QVariantMap), sizeof(QDateTime), sizeof(QJSValue) }; + const int dataAlignments[] = { sizeof(QString), sizeof(double), sizeof(bool), sizeof(ListModel *), sizeof(QObject *), sizeof(QVariantMap), sizeof(QDateTime), sizeof(QJSValue) }; Role *r = new Role; r->name = key; @@ -217,11 +218,20 @@ const ListLayout::Role *ListLayout::getRoleOrCreate(const QString &key, const QV switch (data.type()) { case QVariant::Double: type = Role::Number; break; case QVariant::Int: type = Role::Number; break; - case QVariant::UserType: type = Role::List; break; case QVariant::Bool: type = Role::Bool; break; case QVariant::String: type = Role::String; break; case QVariant::Map: type = Role::VariantMap; break; case QVariant::DateTime: type = Role::DateTime; break; + case QVariant::UserType: { + if (data.userType() == qMetaTypeId<QJSValue>() && + data.value<QJSValue>().isCallable()) { + type = Role::Function; + break; + } else { + type = Role::List; + break; + } + } default: type = Role::Invalid; break; } @@ -341,7 +351,9 @@ ListModel::ListModel(ListLayout *layout, QQmlListModel *modelCache, int uid) : m void ListModel::destroy() { - clear(); + for (const auto &destroyer : remove(0, elements.count())) + destroyer(); + m_uid = -1; m_layout = 0; if (m_modelCache && m_modelCache->m_primary == false) @@ -463,6 +475,12 @@ void ListModel::set(int elementIndex, QV4::Object *object, QVector<int> *roles) const ListLayout::Role &r = m_layout->getRoleOrCreate(propertyName, ListLayout::Role::DateTime); QDateTime dt = dd->toQDateTime(); roleIndex = e->setDateTimeProperty(r, dt); + } else if (QV4::FunctionObject *f = propertyValue->as<QV4::FunctionObject>()) { + const ListLayout::Role &r = m_layout->getRoleOrCreate(propertyName, ListLayout::Role::Function); + QV4::ScopedFunctionObject func(scope, f); + QJSValue jsv; + QJSValuePrivate::setValue(&jsv, v4, func); + roleIndex = e->setFunctionProperty(r, jsv); } else if (QV4::Object *o = propertyValue->as<QV4::Object>()) { if (QV4::QObjectWrapper *wrapper = o->as<QV4::QObjectWrapper>()) { QObject *o = wrapper->object(); @@ -562,16 +580,6 @@ void ListModel::set(int elementIndex, QV4::Object *object) } } -void ListModel::clear() -{ - int elementCount = elements.count(); - for (int i=0 ; i < elementCount ; ++i) { - elements[i]->destroy(m_layout); - delete elements[i]; - } - elements.clear(); -} - QVector<std::function<void()>> ListModel::remove(int index, int count) { QVector<std::function<void()>> toDestroy; @@ -696,6 +704,17 @@ QDateTime *ListElement::getDateTimeProperty(const ListLayout::Role &role) return dt; } +QJSValue *ListElement::getFunctionProperty(const ListLayout::Role &role) +{ + QJSValue *f = 0; + + char *mem = getPropertyMemory(role); + if (isMemoryUsed<QJSValue>(mem)) + f = reinterpret_cast<QJSValue *>(mem); + + return f; +} + QPointer<QObject> *ListElement::getGuardProperty(const ListLayout::Role &role) { char *mem = getPropertyMemory(role); @@ -789,6 +808,14 @@ QVariant ListElement::getProperty(const ListLayout::Role &role, const QQmlListMo } } break; + case ListLayout::Role::Function: + { + if (isMemoryUsed<QJSValue>(mem)) { + QJSValue *func = reinterpret_cast<QJSValue *>(mem); + data = QVariant::fromValue(*func); + } + } + break; default: break; } @@ -951,6 +978,24 @@ int ListElement::setDateTimeProperty(const ListLayout::Role &role, const QDateTi return roleIndex; } +int ListElement::setFunctionProperty(const ListLayout::Role &role, const QJSValue &f) +{ + int roleIndex = -1; + + if (role.type == ListLayout::Role::Function) { + char *mem = getPropertyMemory(role); + if (isMemoryUsed<QJSValue>(mem)) { + QJSValue *f = reinterpret_cast<QJSValue *>(mem); + f->~QJSValue(); + } + new (mem) QJSValue(f); + roleIndex = role.index; + } + + return roleIndex; +} + + void ListElement::setStringPropertyFast(const ListLayout::Role &role, const QString &s) { char *mem = getPropertyMemory(role); @@ -997,6 +1042,12 @@ void ListElement::setDateTimePropertyFast(const ListLayout::Role &role, const QD new (mem) QDateTime(dt); } +void ListElement::setFunctionPropertyFast(const ListLayout::Role &role, const QJSValue &f) +{ + char *mem = getPropertyMemory(role); + new (mem) QJSValue(f); +} + void ListElement::clearProperty(const ListLayout::Role &role) { switch (role.type) { @@ -1021,6 +1072,9 @@ void ListElement::clearProperty(const ListLayout::Role &role) case ListLayout::Role::VariantMap: setVariantMapProperty(role, (QVariantMap *)0); break; + case ListLayout::Role::Function: + setFunctionProperty(role, QJSValue()); + break; default: break; } @@ -1078,6 +1132,7 @@ void ListElement::sync(ListElement *src, ListLayout *srcLayout, ListElement *tar case ListLayout::Role::Number: case ListLayout::Role::Bool: case ListLayout::Role::DateTime: + case ListLayout::Role::Function: { QVariant v = src->getProperty(srcRole, 0, 0); target->setVariantProperty(targetRole, v); @@ -1140,6 +1195,13 @@ void ListElement::destroy(ListLayout *layout) dt->~QDateTime(); } break; + case ListLayout::Role::Function: + { + QJSValue *f = getFunctionProperty(r); + if (f) + f->~QJSValue(); + } + break; default: // other types don't need explicit cleanup. break; @@ -1179,6 +1241,9 @@ int ListElement::setVariantProperty(const ListLayout::Role &role, const QVariant case ListLayout::Role::DateTime: roleIndex = setDateTimeProperty(role, d.toDateTime()); break; + case ListLayout::Role::Function: + roleIndex = setFunctionProperty(role, d.value<QJSValue>()); + break; default: break; } @@ -1221,6 +1286,11 @@ int ListElement::setJsProperty(const ListLayout::Role &role, const QV4::Value &d QV4::Scoped<QV4::DateObject> dd(scope, d); QDateTime dt = dd->toQDateTime(); roleIndex = setDateTimeProperty(role, dt); + } else if (d.as<QV4::FunctionObject>()) { + QV4::ScopedFunctionObject f(scope, d); + QJSValue jsv; + QJSValuePrivate::setValue(&jsv, eng, f); + roleIndex = setFunctionProperty(role, jsv); } else if (d.isObject()) { QV4::ScopedObject o(scope, d); QV4::QObjectWrapper *wrapper = o->as<QV4::QObjectWrapper>(); @@ -2031,18 +2101,7 @@ int QQmlListModel::count() const */ void QQmlListModel::clear() { - const int cleared = count(); - - emitItemsAboutToBeRemoved(0, cleared); - - if (m_dynamicRoles) { - qDeleteAll(m_modelObjects); - m_modelObjects.clear(); - } else { - m_listModel->clear(); - } - - emitItemsRemoved(0, cleared); + removeElements(0, count()); } /*! @@ -2066,27 +2125,32 @@ void QQmlListModel::remove(QQmlV4Function *args) return; } - emitItemsAboutToBeRemoved(index, removeCount); + removeElements(index, removeCount); + } else { + qmlWarning(this) << tr("remove: incorrect number of arguments"); + } +} + +void QQmlListModel::removeElements(int index, int removeCount) +{ + emitItemsAboutToBeRemoved(index, removeCount); - QVector<std::function<void()>> toDestroy; - if (m_dynamicRoles) { - for (int i=0 ; i < removeCount ; ++i) { - auto modelObject = m_modelObjects[index+i]; - toDestroy.append([modelObject](){ - delete modelObject; - }); - } - m_modelObjects.remove(index, removeCount); - } else { - toDestroy = m_listModel->remove(index, removeCount); + QVector<std::function<void()>> toDestroy; + if (m_dynamicRoles) { + for (int i=0 ; i < removeCount ; ++i) { + auto modelObject = m_modelObjects[index+i]; + toDestroy.append([modelObject](){ + delete modelObject; + }); } - - emitItemsRemoved(index, removeCount); - for (const auto &destroyer : toDestroy) - destroyer(); + m_modelObjects.remove(index, removeCount); } else { - qmlWarning(this) << tr("remove: incorrect number of arguments"); + toDestroy = m_listModel->remove(index, removeCount); } + + emitItemsRemoved(index, removeCount); + for (const auto &destroyer : toDestroy) + destroyer(); } /*! @@ -2457,7 +2521,7 @@ bool QQmlListModelParser::verifyProperty(const QV4::CompiledData::Unit *qmlUnit, } } else if (binding->type == QV4::CompiledData::Binding::Type_Script) { QString scriptStr = binding->valueAsScriptString(qmlUnit); - if (!definesEmptyList(scriptStr)) { + if (!binding->isFunctionExpression() && !definesEmptyList(scriptStr)) { QByteArray script = scriptStr.toUtf8(); bool ok; evaluateEnum(script, &ok); @@ -2471,7 +2535,7 @@ bool QQmlListModelParser::verifyProperty(const QV4::CompiledData::Unit *qmlUnit, return true; } -bool QQmlListModelParser::applyProperty(const QV4::CompiledData::Unit *qmlUnit, const QV4::CompiledData::Binding *binding, ListModel *model, int outterElementIndex) +bool QQmlListModelParser::applyProperty(QV4::CompiledData::CompilationUnit *compilationUnit, const QV4::CompiledData::Unit *qmlUnit, const QV4::CompiledData::Binding *binding, ListModel *model, int outterElementIndex) { const QString elementName = qmlUnit->stringAt(binding->propertyNameIndex); @@ -2499,7 +2563,7 @@ bool QQmlListModelParser::applyProperty(const QV4::CompiledData::Unit *qmlUnit, const QV4::CompiledData::Binding *subBinding = target->bindingTable(); for (quint32 i = 0; i < target->nBindings; ++i, ++subBinding) { - roleSet |= applyProperty(qmlUnit, subBinding, subModel, elementIndex); + roleSet |= applyProperty(compilationUnit, qmlUnit, subBinding, subModel, elementIndex); } } else { @@ -2517,6 +2581,21 @@ bool QQmlListModelParser::applyProperty(const QV4::CompiledData::Unit *qmlUnit, const ListLayout::Role &role = model->getOrCreateListRole(elementName); ListModel *emptyModel = new ListModel(role.subLayout, 0, -1); value = QVariant::fromValue(emptyModel); + } else if (binding->isFunctionExpression()) { + QQmlBinding::Identifier id = binding->value.compiledScriptIndex; + Q_ASSERT(id != QQmlBinding::Invalid); + + auto v4 = compilationUnit->engine; + QV4::Scope scope(v4); + // for now we do not provide a context object; data from the ListElement must be passed to the function + QV4::ScopedContext context(scope, QV4::QmlContext::create(v4->rootContext(), QQmlContextData::get(qmlContext(model->m_modelCache)), nullptr)); + QV4::ScopedFunctionObject function(scope, QV4::FunctionObject::createScriptFunction(context, compilationUnit->runtimeFunctions[id])); + + QV4::ReturnedValue result = function->call(v4->globalObject, nullptr, 0); + + QJSValue v; + QJSValuePrivate::setValue(&v, v4, result); + value.setValue<QJSValue>(v); } else { QByteArray script = scriptStr.toUtf8(); bool ok; @@ -2560,7 +2639,7 @@ void QQmlListModelParser::applyBindings(QObject *obj, QV4::CompiledData::Compila for (const QV4::CompiledData::Binding *binding : bindings) { if (binding->type != QV4::CompiledData::Binding::Type_Object) continue; - setRoles |= applyProperty(qmlUnit, binding, rv->m_listModel, /*outter element index*/-1); + setRoles |= applyProperty(compilationUnit, qmlUnit, binding, rv->m_listModel, /*outter element index*/-1); } if (setRoles == false) @@ -2600,6 +2679,9 @@ bool QQmlListModelParser::definesEmptyList(const QString &s) strings (quoted and optionally within a call to QT_TR_NOOP), boolean values (true, false), numbers, or enumeration values (such as AlignText.AlignHCenter). + Beginning with Qt 5.11 ListElement also allows assigning a function declaration to + a role. This allows the definition of ListElements with callable actions. + \section1 Referencing Roles The role names are used by delegates to obtain data from list elements. diff --git a/src/qml/types/qqmllistmodel_p.h b/src/qml/types/qqmllistmodel_p.h index 372b7c70c7..499a113504 100644 --- a/src/qml/types/qqmllistmodel_p.h +++ b/src/qml/types/qqmllistmodel_p.h @@ -165,6 +165,8 @@ private: void emitItemsInserted(int index, int count); void emitItemsAboutToBeMoved(int from, int to, int n); void emitItemsMoved(int from, int to, int n); + + void removeElements(int index, int removeCount); }; // ### FIXME @@ -193,7 +195,7 @@ public: private: bool verifyProperty(const QV4::CompiledData::Unit *qmlUnit, const QV4::CompiledData::Binding *binding); // returns true if a role was set - bool applyProperty(const QV4::CompiledData::Unit *qmlUnit, const QV4::CompiledData::Binding *binding, ListModel *model, int outterElementIndex); + bool applyProperty(QV4::CompiledData::CompilationUnit *compilationUnit, const QV4::CompiledData::Unit *qmlUnit, const QV4::CompiledData::Binding *binding, ListModel *model, int outterElementIndex); static bool definesEmptyList(const QString &); diff --git a/src/qml/types/qqmllistmodel_p_p.h b/src/qml/types/qqmllistmodel_p_p.h index 77d2002afa..dea1ef2eb3 100644 --- a/src/qml/types/qqmllistmodel_p_p.h +++ b/src/qml/types/qqmllistmodel_p_p.h @@ -216,6 +216,7 @@ public: QObject, VariantMap, DateTime, + Function, MaxDataType }; @@ -283,6 +284,7 @@ private: int setVariantMapProperty(const ListLayout::Role &role, QV4::Object *o); int setVariantMapProperty(const ListLayout::Role &role, QVariantMap *m); int setDateTimeProperty(const ListLayout::Role &role, const QDateTime &dt); + int setFunctionProperty(const ListLayout::Role &role, const QJSValue &f); void setStringPropertyFast(const ListLayout::Role &role, const QString &s); void setDoublePropertyFast(const ListLayout::Role &role, double n); @@ -291,6 +293,7 @@ private: void setListPropertyFast(const ListLayout::Role &role, ListModel *m); void setVariantMapFast(const ListLayout::Role &role, QV4::Object *o); void setDateTimePropertyFast(const ListLayout::Role &role, const QDateTime &dt); + void setFunctionPropertyFast(const ListLayout::Role &role, const QJSValue &f); void clearProperty(const ListLayout::Role &role); @@ -301,6 +304,7 @@ private: QPointer<QObject> *getGuardProperty(const ListLayout::Role &role); QVariantMap *getVariantMapProperty(const ListLayout::Role &role); QDateTime *getDateTimeProperty(const ListLayout::Role &role); + QJSValue *getFunctionProperty(const ListLayout::Role &role); inline char *getPropertyMemory(const ListLayout::Role &role); @@ -366,7 +370,6 @@ public: int append(QV4::Object *object); void insert(int elementIndex, QV4::Object *object); - void clear(); Q_REQUIRED_RESULT QVector<std::function<void()>> remove(int index, int count); int appendElement(); @@ -401,6 +404,7 @@ private: friend class ListElement; friend class QQmlListModelWorkerAgent; + friend class QQmlListModelParser; }; QT_END_NAMESPACE diff --git a/src/qml/types/qqmlobjectmodel.cpp b/src/qml/types/qqmlobjectmodel.cpp index 64d0169f6b..dcd0360199 100644 --- a/src/qml/types/qqmlobjectmodel.cpp +++ b/src/qml/types/qqmlobjectmodel.cpp @@ -72,7 +72,7 @@ public: int ref; }; - QQmlObjectModelPrivate() : QObjectPrivate() {} + QQmlObjectModelPrivate() : QObjectPrivate(), moveId(0) {} static void children_append(QQmlListProperty<QObject> *prop, QObject *item) { int index = static_cast<QQmlObjectModelPrivate *>(prop->data)->children.count(); @@ -129,7 +129,7 @@ public: } QQmlChangeSet changeSet; - changeSet.move(from, to, n, 0); + changeSet.move(from, to, n, ++moveId); emit q->modelUpdated(changeSet, false); emit q->childrenChanged(); } @@ -166,7 +166,7 @@ public: return -1; } - + uint moveId; QList<Item> children; }; diff --git a/src/qml/types/qquickworkerscript_p.h b/src/qml/types/qquickworkerscript_p.h index dce3acc3e1..8ea630c685 100644 --- a/src/qml/types/qquickworkerscript_p.h +++ b/src/qml/types/qquickworkerscript_p.h @@ -51,7 +51,7 @@ // We mean it. // -#include "qqml.h" +#include <qqml.h> #include <QtQml/qqmlparserstatus.h> #include <QtCore/qthread.h> |