Import QJson to the tree and use it on Windows
[sloppygui.git] / projects / lib / 3rdparty / qjson / src / qobjecthelper.cpp
blob9f3ca90807a0413982502aac9506bf5396396283
1 /* This file is part of qjson
3 * Copyright (C) 2009 Till Adam <adam@kde.org>
4 * Copyright (C) 2009 Flavio Castelli <flavio@castelli.name>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public License
17 * along with this library; see the file COPYING.LIB. If not, write to
18 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
23 #include <qjson/qobjecthelper.h>
25 #include <QtCore/QMetaObject>
26 #include <QtCore/QMetaProperty>
27 #include <QtCore/QObject>
29 using namespace QJson;
31 class QObjectHelper::QObjectHelperPrivate {
34 QObjectHelper::QObjectHelper()
35 : d (new QObjectHelperPrivate)
39 QObjectHelper::~QObjectHelper()
41 delete d;
44 QVariantMap QObjectHelper::qobject2qvariant( const QObject* object,
45 const QStringList& ignoredProperties)
47 QVariantMap result;
48 const QMetaObject *metaobject = object->metaObject();
49 int count = metaobject->propertyCount();
50 for (int i=0; i<count; ++i) {
51 QMetaProperty metaproperty = metaobject->property(i);
52 const char *name = metaproperty.name();
54 if (ignoredProperties.contains(QLatin1String(name)) || (!metaproperty.isReadable()))
55 continue;
57 QVariant value = object->property(name);
58 result[QLatin1String(name)] = value;
60 return result;
63 void QObjectHelper::qvariant2qobject(const QVariantMap& variant, QObject* object)
65 QStringList properies;
66 const QMetaObject *metaobject = object->metaObject();
67 int count = metaobject->propertyCount();
68 for (int i=0; i<count; ++i) {
69 QMetaProperty metaproperty = metaobject->property(i);
70 if (metaproperty.isWritable()) {
71 properies << QLatin1String( metaproperty.name());
75 QVariantMap::const_iterator iter;
76 for (iter = variant.constBegin(); iter != variant.end(); iter++) {
77 if (properies.contains(iter.key())) {
78 object->setProperty(iter.key().toAscii(), iter.value());