00001 /* This file is part of qjson 00002 * 00003 * Copyright (C) 2009 Till Adam <adam@kde.org> 00004 * Copyright (C) 2009 Flavio Castelli <flavio@castelli.name> 00005 * 00006 * This library is free software; you can redistribute it and/or 00007 * modify it under the terms of the GNU Lesser General Public 00008 * License version 2.1, as published by the Free Software Foundation. 00009 * 00010 * 00011 * This library is distributed in the hope that it will be useful, 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00014 * Lesser General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU Lesser General Public License 00017 * along with this library; see the file COPYING.LIB. If not, write to 00018 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00019 * Boston, MA 02110-1301, USA. 00020 */ 00021 00022 00023 #include "qobjecthelper.h" 00024 00025 #include <QtCore/QMetaObject> 00026 #include <QtCore/QMetaProperty> 00027 #include <QtCore/QObject> 00028 00029 using namespace QJson; 00030 00031 class QObjectHelper::QObjectHelperPrivate { 00032 }; 00033 00034 QObjectHelper::QObjectHelper() 00035 : d (new QObjectHelperPrivate) 00036 { 00037 } 00038 00039 QObjectHelper::~QObjectHelper() 00040 { 00041 delete d; 00042 } 00043 00044 QVariantMap QObjectHelper::qobject2qvariant( const QObject* object, 00045 const QStringList& ignoredProperties) 00046 { 00047 QVariantMap result; 00048 const QMetaObject *metaobject = object->metaObject(); 00049 int count = metaobject->propertyCount(); 00050 for (int i=0; i<count; ++i) { 00051 QMetaProperty metaproperty = metaobject->property(i); 00052 const char *name = metaproperty.name(); 00053 00054 if (ignoredProperties.contains(QLatin1String(name)) || (!metaproperty.isReadable())) 00055 continue; 00056 00057 QVariant value = object->property(name); 00058 result[QLatin1String(name)] = value; 00059 } 00060 return result; 00061 } 00062 00063 void QObjectHelper::qvariant2qobject(const QVariantMap& variant, QObject* object) 00064 { 00065 const QMetaObject *metaobject = object->metaObject(); 00066 00067 QVariantMap::const_iterator iter; 00068 for (iter = variant.constBegin(); iter != variant.constEnd(); ++iter) { 00069 int pIdx = metaobject->indexOfProperty( iter.key().toLatin1() ); 00070 00071 if ( pIdx < 0 ) { 00072 continue; 00073 } 00074 00075 QMetaProperty metaproperty = metaobject->property( pIdx ); 00076 QVariant::Type type = metaproperty.type(); 00077 QVariant v( iter.value() ); 00078 if ( v.canConvert( type ) ) { 00079 v.convert( type ); 00080 metaproperty.write( object, v ); 00081 } else if (QString(QLatin1String("QVariant")).compare(QLatin1String(metaproperty.typeName())) == 0) { 00082 metaproperty.write( object, v ); 00083 } 00084 } 00085 }
|
hosts this site. |
Send comments to: QJson Developers |