Skip to content

Commit

Permalink
Add QSyncable::assign(QVariantMap&, QObject*) - Assign properties fro…
Browse files Browse the repository at this point in the history
…m source object to the dest object
  • Loading branch information
benlau committed Sep 24, 2016
1 parent 9b21ddb commit cb1bf7f
Show file tree
Hide file tree
Showing 9 changed files with 95 additions and 3 deletions.
1 change: 1 addition & 0 deletions qsdiffrunner.h
Expand Up @@ -9,6 +9,7 @@

#include "qspatch.h"
#include "qspatchable.h"
#include "qsyncablefunctions.h"

class QSDiffRunner
{
Expand Down
1 change: 1 addition & 0 deletions qslistmodel.h
Expand Up @@ -11,6 +11,7 @@
#include <QPointer>
#include <QSharedPointer>
#include "qspatchable.h"
#include "qsyncablefunctions.h"

class QSListModel : public QAbstractListModel, public QSPatchable
{
Expand Down
6 changes: 4 additions & 2 deletions qsyncable.pri
Expand Up @@ -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 \
Expand All @@ -22,4 +23,5 @@ SOURCES += \
$$PWD/qstree.cpp \
$$PWD/qstreenode.cpp \
$$PWD/qsjsonlistmodel.cpp \
$$PWD/qsyncableqmltypes.cpp
$$PWD/qsyncableqmltypes.cpp \
$$PWD/qsyncablefunctions.cpp
23 changes: 23 additions & 0 deletions qsyncablefunctions.cpp
@@ -0,0 +1,23 @@
#include <QMetaProperty>
#include <QtQml>
#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<QObject*>()) {
QVariantMap map;
assign(map, value.value<QObject*>());
value = map;
}

dest[property.name()] = value;
}

}
14 changes: 14 additions & 0 deletions qsyncablefunctions.h
@@ -0,0 +1,14 @@
#ifndef QSYNCABLEFUNCTIONS_H
#define QSYNCABLEFUNCTIONS_H

#include <QObject>

namespace QSyncable {

/// Assign properties from source object to the destination objec
void assign(QVariantMap& dest,
const QObject* source);

}

#endif // QSYNCABLEFUNCTIONS_H
29 changes: 29 additions & 0 deletions tests/qsyncableunittests/integrationtests.cpp
Expand Up @@ -2,6 +2,8 @@
#include <QSListModel>
#include <QSortFilterProxyModel>
#include <QSDiffRunner>
#include "QQmlApplicationEngine"
#include "automator.h"
#include "integrationtests.h"

IntegrationTests::IntegrationTests(QObject *parent) : QObject(parent)
Expand Down Expand Up @@ -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);


}


3 changes: 3 additions & 0 deletions tests/qsyncableunittests/integrationtests.h
Expand Up @@ -3,6 +3,7 @@

#include <QObject>

/// Test integration with other component
class IntegrationTests : public QObject
{
Q_OBJECT
Expand All @@ -14,7 +15,9 @@ class IntegrationTests : public QObject
public slots:

private slots:

void sortFilterProxyModel();
void test_assign();
};

#endif // INTEGRATIONTESTS_H
3 changes: 2 additions & 1 deletion tests/qsyncableunittests/qsyncableunittests.pro
Expand Up @@ -34,4 +34,5 @@ include(../../qsyncable.pri)
DISTFILES += \
tst_jsonModel.qml \
tst_uuid.qml \
../../README.md
../../README.md \
test_Assign.qml
18 changes: 18 additions & 0 deletions 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
}

}

0 comments on commit cb1bf7f

Please sign in to comment.