QJson project page QJson home page

qobjecthelper.cpp
1 /* This file is part of qjson
2  *
3  * Copyright (C) 2009 Till Adam <adam@kde.org>
4  * Copyright (C) 2009 Flavio Castelli <flavio@castelli.name>
5  *
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.
10  *
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.
15  *
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.
20  */
21 
22 
23 #include "qobjecthelper.h"
24 
25 #include <QtCore/QMetaObject>
26 #include <QtCore/QMetaProperty>
27 #include <QtCore/QObject>
28 
29 using namespace QJson;
30 
31 class QObjectHelper::QObjectHelperPrivate {
32 };
33 
34 QObjectHelper::QObjectHelper()
35  : d (new QObjectHelperPrivate)
36 {
37 }
38 
39 QObjectHelper::~QObjectHelper()
40 {
41  delete d;
42 }
43 
44 QVariantMap QObjectHelper::qobject2qvariant( const QObject* object,
45  const QStringList& ignoredProperties)
46 {
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();
53 
54  if (ignoredProperties.contains(QLatin1String(name)) || (!metaproperty.isReadable()))
55  continue;
56 
57  QVariant value = object->property(name);
58  result[QLatin1String(name)] = value;
59  }
60  return result;
61 }
62 
63 void QObjectHelper::qvariant2qobject(const QVariantMap& variant, QObject* object)
64 {
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());
72  }
73  }
74 
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());
79  }
80  }
81 }

SourceForge Logo hosts this site. Send comments to:
QJson Developers