From cb1bf7f9bf873bc911e8a63595c361bd27eddb15 Mon Sep 17 00:00:00 2001 From: Ben Lau Date: Sat, 24 Sep 2016 11:56:23 +0800 Subject: [PATCH] Add QSyncable::assign(QVariantMap&, QObject*) - Assign properties from source object to the dest object --- qsdiffrunner.h | 1 + qslistmodel.h | 1 + qsyncable.pri | 6 ++-- qsyncablefunctions.cpp | 23 +++++++++++++++ qsyncablefunctions.h | 14 +++++++++ tests/qsyncableunittests/integrationtests.cpp | 29 +++++++++++++++++++ tests/qsyncableunittests/integrationtests.h | 3 ++ .../qsyncableunittests/qsyncableunittests.pro | 3 +- tests/qsyncableunittests/test_Assign.qml | 18 ++++++++++++ 9 files changed, 95 insertions(+), 3 deletions(-) create mode 100644 qsyncablefunctions.cpp create mode 100644 qsyncablefunctions.h create mode 100644 tests/qsyncableunittests/test_Assign.qml diff --git a/qsdiffrunner.h b/qsdiffrunner.h index bcfede3..bc26b16 100644 --- a/qsdiffrunner.h +++ b/qsdiffrunner.h @@ -9,6 +9,7 @@ #include "qspatch.h" #include "qspatchable.h" +#include "qsyncablefunctions.h" class QSDiffRunner { diff --git a/qslistmodel.h b/qslistmodel.h index 3ceb8fe..1ddc3e6 100644 --- a/qslistmodel.h +++ b/qslistmodel.h @@ -11,6 +11,7 @@ #include #include #include "qspatchable.h" +#include "qsyncablefunctions.h" class QSListModel : public QAbstractListModel, public QSPatchable { diff --git a/qsyncable.pri b/qsyncable.pri index 5283545..4667b24 100644 --- a/qsyncable.pri +++ b/qsyncable.pri @@ -11,7 +11,8 @@ HEADERS += \ $$PWD/priv/qstreenode.h \ $$PWD/qsjsonlistmodel.h \ $$PWD/QSDiffRunner \ - $$PWD/QSListModel + $$PWD/QSListModel \ + $$PWD/qsyncablefunctions.h SOURCES += \ $$PWD/qsdiffrunner.cpp \ @@ -22,4 +23,5 @@ SOURCES += \ $$PWD/qstree.cpp \ $$PWD/qstreenode.cpp \ $$PWD/qsjsonlistmodel.cpp \ - $$PWD/qsyncableqmltypes.cpp + $$PWD/qsyncableqmltypes.cpp \ + $$PWD/qsyncablefunctions.cpp diff --git a/qsyncablefunctions.cpp b/qsyncablefunctions.cpp new file mode 100644 index 0000000..a9c3d64 --- /dev/null +++ b/qsyncablefunctions.cpp @@ -0,0 +1,23 @@ +#include +#include +#include "qsyncablefunctions.h" + +void QSyncable::assign(QVariantMap &dest, const QObject *source) +{ + + const QMetaObject* meta = source->metaObject(); + + for (int i = 0 ; i < meta->propertyCount(); i++) { + const QMetaProperty property = meta->property(i); + QVariant value = source->property(property.name()); + + if (value.canConvert()) { + QVariantMap map; + assign(map, value.value()); + value = map; + } + + dest[property.name()] = value; + } + +} diff --git a/qsyncablefunctions.h b/qsyncablefunctions.h new file mode 100644 index 0000000..2af3c04 --- /dev/null +++ b/qsyncablefunctions.h @@ -0,0 +1,14 @@ +#ifndef QSYNCABLEFUNCTIONS_H +#define QSYNCABLEFUNCTIONS_H + +#include + +namespace QSyncable { + + /// Assign properties from source object to the destination objec + void assign(QVariantMap& dest, + const QObject* source); + +} + +#endif // QSYNCABLEFUNCTIONS_H diff --git a/tests/qsyncableunittests/integrationtests.cpp b/tests/qsyncableunittests/integrationtests.cpp index 87efeba..d40bcfe 100644 --- a/tests/qsyncableunittests/integrationtests.cpp +++ b/tests/qsyncableunittests/integrationtests.cpp @@ -2,6 +2,8 @@ #include #include #include +#include "QQmlApplicationEngine" +#include "automator.h" #include "integrationtests.h" IntegrationTests::IntegrationTests(QObject *parent) : QObject(parent) @@ -68,3 +70,30 @@ void IntegrationTests::sortFilterProxyModel() QCOMPARE(proxyModel.data(proxyModel.index(0,0),roles.key("id")).toInt(), 4); } + + +void IntegrationTests::test_assign() +{ + QQmlApplicationEngine engine; + + engine.load(QUrl(QString(SRCDIR) + "/test_Assign.qml")); + Automator automator(&engine); + + QObject* root = automator.findObject("Root"); + QVERIFY(root); + + QVariantMap data; + QSyncable::assign(data, root); + + QVERIFY(data["objectName"] == "Root"); + QVERIFY(data["value1"].toInt() == 1); + QVERIFY(data["value2"].toString() == "2"); + QVERIFY(data["value3"].toBool()); + + QVERIFY(data["value4"].type() == QVariant::Map); + QVERIFY(data["value4"].toMap()["value1"].toInt() == 5); + + +} + + diff --git a/tests/qsyncableunittests/integrationtests.h b/tests/qsyncableunittests/integrationtests.h index bbf8c25..b59827d 100644 --- a/tests/qsyncableunittests/integrationtests.h +++ b/tests/qsyncableunittests/integrationtests.h @@ -3,6 +3,7 @@ #include +/// Test integration with other component class IntegrationTests : public QObject { Q_OBJECT @@ -14,7 +15,9 @@ class IntegrationTests : public QObject public slots: private slots: + void sortFilterProxyModel(); + void test_assign(); }; #endif // INTEGRATIONTESTS_H diff --git a/tests/qsyncableunittests/qsyncableunittests.pro b/tests/qsyncableunittests/qsyncableunittests.pro index 8e2caf6..a28b26e 100644 --- a/tests/qsyncableunittests/qsyncableunittests.pro +++ b/tests/qsyncableunittests/qsyncableunittests.pro @@ -34,4 +34,5 @@ include(../../qsyncable.pri) DISTFILES += \ tst_jsonModel.qml \ tst_uuid.qml \ - ../../README.md + ../../README.md \ + test_Assign.qml diff --git a/tests/qsyncableunittests/test_Assign.qml b/tests/qsyncableunittests/test_Assign.qml new file mode 100644 index 0000000..b84b989 --- /dev/null +++ b/tests/qsyncableunittests/test_Assign.qml @@ -0,0 +1,18 @@ +import QtQuick 2.0 + +QtObject { + id: root + objectName: "Root" + + property int value1 : 1 + + property string value2: "2" + + property bool value3 : true + + // Nested object + property var value4: QtObject { + property var value1 : 4 + 1 + } + +}