QJson project page QJson home page

/srv/build/STABLE_8/pkgs/qjson/BUILD/qjson/src/qobjecthelper.cpp
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 Library General Public
00008   * License as published by the Free Software Foundation; either
00009   * version 2 of the License, or (at your option) any later version.
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   * Library General Public License for more details.
00015   *
00016   * You should have received a copy of the GNU Library 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   QStringList properies;
00066   const QMetaObject *metaobject = object->metaObject();
00067   int count = metaobject->propertyCount();
00068   for (int i=0; i<count; ++i) {
00069     QMetaProperty metaproperty = metaobject->property(i);
00070     if (metaproperty.isWritable()) {
00071       properies << QLatin1String( metaproperty.name());
00072     }
00073   }
00074 
00075   QVariantMap::const_iterator iter;
00076   for (iter = variant.constBegin(); iter != variant.end(); iter++) {
00077     if (properies.contains(iter.key())) {
00078       object->setProperty(iter.key().toAscii(), iter.value());
00079     }
00080   }
00081 }

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