00001 /****************************************************************************** 00002 * This file is part of the Gluon Development Platform 00003 * Copyright (c) 2010 Dan Leinir Turthra Jensen <admin@leinir.dk> 00004 * 00005 * This library is free software; you can redistribute it and/or 00006 * modify it under the terms of the GNU Lesser General Public 00007 * License as published by the Free Software Foundation; either 00008 * version 2.1 of the License, or (at your option) any later version. 00009 * 00010 * This library is distributed in the hope that it will be useful, 00011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00013 * Lesser General Public License for more details. 00014 * 00015 * You should have received a copy of the GNU Lesser General Public 00016 * License along with this library; if not, write to the Free Software 00017 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00018 */ 00019 #include "propertywidgetitemfactory.h" 00020 #include "nullpropertywidgetitem.h" 00021 #include "enumpropertywidgetitem.h" 00022 #include "gluonobjectpropertywidgetitem.h" 00023 00024 #include "core/debughelper.h" 00025 #include "core/gluonobjectfactory.h" 00026 #include <QMetaClassInfo> 00027 00028 using namespace GluonCreator; 00029 00030 template<> GLUONCREATOR_EXPORT PropertyWidgetItemFactory* GluonCore::Singleton<PropertyWidgetItemFactory>::m_instance = 0; 00031 00032 PropertyWidgetItem* 00033 PropertyWidgetItemFactory::create( const QObject* object, const QString& type, QWidget* parent ) 00034 { 00035 DEBUG_BLOCK 00036 00037 // First check if there's any PIW which supports the type 00038 QList<QString> pwiKeys = piwTypes.keys(); 00039 foreach( const QString & thisType, pwiKeys ) 00040 { 00041 if( thisType == type ) 00042 { 00043 PropertyWidgetItem* item = piwTypes[thisType]->instantiate(); 00044 item->setParent( parent ); 00045 return item; 00046 } 00047 } 00048 00049 // Check whether we've got an item which uses an enum to grab its value 00050 const QMetaObject* mo = object->metaObject(); 00051 if( mo ) 00052 { 00053 // if (mo->enumeratorCount() > 0) 00054 // { 00055 // for (int i = 0; i < mo->enumeratorCount(); ++i) 00056 // { 00057 // DEBUG_TEXT(QString("Enumerator found: %1").arg(mo->enumerator(i).name())); 00058 // } 00059 // } 00060 if( mo->indexOfEnumerator( type.toUtf8() ) > -1 ) 00061 { 00062 return new EnumPropertyWidgetItem( type, parent ); 00063 } 00064 } 00065 00066 // Then see if it's a reference type inheriting GluonObject... 00067 QString typeTruncated = type.left( type.length() - 1 ); 00068 foreach( const QString & thisType, GluonCore::GluonObjectFactory::instance()->objectTypeNames() ) 00069 { 00070 if( thisType == typeTruncated ) 00071 { 00072 return new GluonObjectPropertyWidgetItem( typeTruncated, parent ); 00073 } 00074 } 00075 00076 // Finally, throw back a Null item if we've got nothing... 00077 DEBUG_TEXT( QString( "Attempting to instantiate unknown property widget item of type %1" ).arg( type ) ); 00078 00079 return new NullPropertyWidgetItem( parent ); 00080 } 00081 00082 void 00083 PropertyWidgetItemFactory::registerNewPIW( PropertyWidgetItem* newPIW ) 00084 { 00085 if( newPIW ) 00086 { 00087 foreach( const QString & type, newPIW->supportedDataTypes() ) 00088 { 00089 piwTypes[type] = newPIW; 00090 } 00091 } 00092 } 00093