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  * Copyright (C) 2016 Anton Kudryavtsev <a.kudryavtsev@netris.ru>
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License version 2.1, as published by the Free Software Foundation.
10  *
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this library; see the file COPYING.LIB. If not, write to
19  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20  * Boston, MA 02110-1301, USA.
21  */
22 
23 
24 #include "qobjecthelper.h"
25 
26 #include <QtCore/QMetaObject>
27 #include <QtCore/QMetaProperty>
28 #include <QtCore/QObject>
29 
30 using namespace QJson;
31 
32 class QObjectHelper::QObjectHelperPrivate {
33 };
34 
35 QObjectHelper::QObjectHelper()
36  : d (new QObjectHelperPrivate)
37 {
38 }
39 
40 QObjectHelper::~QObjectHelper()
41 {
42  delete d;
43 }
44 
45 QVariantMap QObjectHelper::qobject2qvariant( const QObject* object,
46  const QStringList& ignoredProperties)
47 {
48  QVariantMap result;
49  const QMetaObject *metaobject = object->metaObject();
50  int count = metaobject->propertyCount();
51  for (int i=0; i<count; ++i) {
52  QMetaProperty metaproperty = metaobject->property(i);
53  const char *name = metaproperty.name();
54 
55  if (!metaproperty.isReadable() || ignoredProperties.contains(QLatin1String(name)))
56  continue;
57 
58  QVariant value = object->property(name);
59  result[QLatin1String(name)] = value;
60  }
61  return result;
62 }
63 
64 void QObjectHelper::qvariant2qobject(const QVariantMap& variant, QObject* object)
65 {
66  const QMetaObject *metaobject = object->metaObject();
67 
68  for (QVariantMap::const_iterator iter = variant.constBegin(),
69  end = variant.constEnd(); iter != end; ++iter) {
70  int pIdx = metaobject->indexOfProperty( iter.key().toLatin1() );
71 
72  if ( pIdx < 0 ) {
73  continue;
74  }
75 
76  QMetaProperty metaproperty = metaobject->property( pIdx );
77  QVariant::Type type = metaproperty.type();
78  QVariant v( iter.value() );
79  if ( v.canConvert( type ) ) {
80  v.convert( type );
81  metaproperty.write( object, v );
82  } else if (QLatin1String("QVariant") == QLatin1String(metaproperty.typeName())) {
83  metaproperty.write( object, v );
84  }
85  }
86 }
static void qvariant2qobject(const QVariantMap &variant, QObject *object)
static QVariantMap qobject2qvariant(const QObject *object, const QStringList &ignoredProperties=QStringList(QString(QLatin1String("objectName"))))

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