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 "enumpropertywidgetitem.h" 00020 #include "core/debughelper.h" 00021 00022 #include <QtGui/QLayout> 00023 #include <QtCore/QMetaEnum> 00024 00025 #include <KDE/KComboBox> 00026 00027 namespace GluonCreator 00028 { 00029 class EnumPWIPrivate 00030 { 00031 public: 00032 EnumPWIPrivate() 00033 { 00034 comboBox = 0; 00035 editObject = 0; 00036 }; 00037 ~EnumPWIPrivate() {}; 00038 00039 KComboBox* comboBox; 00040 QObject* editObject; 00041 QString typeName; 00042 QMetaEnum metaEnum; 00043 00044 void setupComboBox() 00045 { 00046 DEBUG_BLOCK 00047 if( !editObject ) 00048 return; 00049 00050 const QMetaObject* mo = editObject->metaObject(); 00051 int enumIndex = mo->indexOfEnumerator( typeName.toUtf8() ); 00052 if( enumIndex > -1 ) 00053 metaEnum = mo->enumerator( enumIndex ); 00054 else 00055 { 00056 DEBUG_TEXT( QString( "The enumerator %1 was not found! Maybe you forgot to declare it in the class?" ).arg( typeName ) ); 00057 metaEnum = QMetaEnum(); 00058 } 00059 00060 //DEBUG_TEXT(QString("Adding %1 items from the enum %2 (requested: %3)").arg(metaEnum.keyCount()).arg(metaEnum.name()).arg(typeName)); 00061 for( int i = 0; i < metaEnum.keyCount(); ++i ) 00062 { 00063 comboBox->addItem( QString( metaEnum.key( i ) ), QVariant( i ) ); 00064 } 00065 } 00066 }; 00067 } 00068 00069 using namespace GluonCreator; 00070 00071 EnumPropertyWidgetItem::EnumPropertyWidgetItem( const QString& typeName, QWidget* parent, Qt::WindowFlags f ) 00072 : PropertyWidgetItem( parent, f ) 00073 { 00074 d = new EnumPWIPrivate(); 00075 d->typeName = typeName; 00076 00077 d->comboBox = new KComboBox( this ); 00078 layout()->addWidget( d->comboBox ); 00079 } 00080 00081 EnumPropertyWidgetItem::~EnumPropertyWidgetItem() 00082 { 00083 delete d; 00084 } 00085 00086 PropertyWidgetItem* 00087 EnumPropertyWidgetItem::instantiate() 00088 { 00089 return new EnumPropertyWidgetItem( QString() ); 00090 } 00091 00092 QStringList 00093 EnumPropertyWidgetItem::supportedDataTypes() const 00094 { 00095 QStringList supportedTypes; 00096 return supportedTypes; 00097 } 00098 00099 void 00100 EnumPropertyWidgetItem::setEditObject( QObject* editThis ) 00101 { 00102 GluonCreator::PropertyWidgetItem::setEditObject( editThis ); 00103 d->editObject = editThis; 00104 d->setupComboBox(); 00105 connect( d->comboBox, SIGNAL( currentIndexChanged( int ) ), SLOT( indexChanged( int ) ) ); 00106 } 00107 00108 void 00109 EnumPropertyWidgetItem::setEditValue( const QVariant& value ) 00110 { 00111 int key = value.toInt(); 00112 d->comboBox->setCurrentIndex( d->comboBox->findData( QVariant( key ) ) ); 00113 GluonCreator::PropertyWidgetItem::setEditValue( value ); 00114 } 00115 00116 void 00117 EnumPropertyWidgetItem::indexChanged( int newIndex ) 00118 { 00119 d->editObject->setProperty( editProperty().toUtf8(), d->comboBox->itemData( newIndex ) ); 00120 GluonCreator::PropertyWidgetItem::valueChanged( QVariant( newIndex ) ); 00121 } 00122 00123 // #include "enumpropertywidgetitem.moc"