00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "gluonobjectpropertywidgetitem.h"
00021 #include "core/debughelper.h"
00022 #include "engine/game.h"
00023 #include "engine/scene.h"
00024 #include "engine/gameobject.h"
00025 #include "engine/gameproject.h"
00026 #include "engine/gluon_engine_metatypes.h"
00027
00028 #include <filemanager.h>
00029
00030 #include <KMessageBox>
00031 #include <KLocalizedString>
00032 #include <KInputDialog>
00033
00034 #include <QtGui/QHBoxLayout>
00035 #include <QtGui/QLabel>
00036 #include <QtGui/QToolButton>
00037 #include <QPushButton>
00038
00039 namespace GluonCreator
00040 {
00041 class GluonObjectPWIPrivate
00042 {
00043 public:
00044 GluonObjectPWIPrivate()
00045 {
00046 currentValue = 0;
00047
00048 };
00049
00050 QPushButton* currentValue;
00051 QToolButton* editButton;
00052 QString typeName;
00053
00054 static QList<const GluonCore::GluonObject*> getChildrenOfType( const QString& typeName, const GluonCore::GluonObject* lookHere )
00055 {
00056 QList<const GluonCore::GluonObject*> foundChildren;
00057
00058 if( lookHere )
00059 {
00060 foreach( const QObject * child, lookHere->children() )
00061 {
00062 if( child->inherits( typeName.toUtf8() ) )
00063 foundChildren.append( qobject_cast<const GluonCore::GluonObject*>( child ) );
00064
00065 foundChildren.append( getChildrenOfType( typeName, qobject_cast<const GluonCore::GluonObject*>( child ) ) );
00066 }
00067 }
00068
00069 return foundChildren;
00070 }
00071 };
00072 }
00073
00074 using namespace GluonCreator;
00075
00076 GluonObjectPropertyWidgetItem::GluonObjectPropertyWidgetItem( const QString& typeName, QWidget* parent, Qt::WindowFlags f )
00077 : PropertyWidgetItem( parent, f )
00078 , d( new GluonObjectPWIPrivate )
00079 {
00080 d->typeName = typeName;
00081
00082 QHBoxLayout* base = new QHBoxLayout;
00083 base->setSpacing( 0 );
00084 base->setContentsMargins( 0, 0, 0, 0 );
00085
00086 d->currentValue = new QPushButton( this );
00087 d->currentValue->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Minimum );
00088 base->addWidget( d->currentValue );
00089 connect( d->currentValue, SIGNAL( clicked( bool ) ), SLOT( browseForItems() ) );
00090
00091 d->editButton = new QToolButton( this );
00092 d->editButton->setIcon( KIcon( "document-edit" ) );
00093 d->editButton->setText( i18nc( "Edit/preview currently selected object", "Edit" ) );
00094 d->editButton->setToolTip( i18n( "Edit/preview currently selected object" ) );
00095 base->addWidget( d->editButton );
00096 connect( d->editButton, SIGNAL( clicked( bool ) ), SLOT( openInEditor() ) );
00097
00098 QWidget* local = new QWidget( parent );
00099 local->setLayout( base );
00100 layout()->addWidget( local );
00101 }
00102
00103 GluonObjectPropertyWidgetItem::~GluonObjectPropertyWidgetItem()
00104 {
00105 delete d;
00106 }
00107
00108 PropertyWidgetItem *
00109 GluonObjectPropertyWidgetItem::instantiate()
00110 {
00111 return new GluonObjectPropertyWidgetItem( QString() );
00112 }
00113
00114 QStringList
00115 GluonObjectPropertyWidgetItem::supportedDataTypes() const
00116 {
00117 QStringList supportedTypes;
00118 return supportedTypes;
00119 }
00120
00121 void
00122 GluonObjectPropertyWidgetItem::setTypeName( const QString& typeName )
00123 {
00124 d->typeName = typeName;
00125 }
00126
00127 QString
00128 GluonObjectPropertyWidgetItem::typeName() const
00129 {
00130 return d->typeName;
00131 }
00132
00133 void
00134 GluonObjectPropertyWidgetItem::browseForItems()
00135 {
00136 QList<const GluonCore::GluonObject*> items = GluonObjectPWIPrivate::getChildrenOfType( d->typeName, GluonEngine::Game::instance()->gameProject() );
00137 items.append( GluonObjectPWIPrivate::getChildrenOfType( d->typeName, GluonEngine::Game::instance()->currentScene()->sceneContents() ) );
00138
00139 if( items.count() == 0 )
00140 {
00141 KMessageBox::information( this, i18n( "There are no items of the type %1 anywhere in this project. Please add some and try again.", d->typeName ), i18n( "No Items Found" ) );
00142 }
00143 else
00144 {
00145 QString caption( "" );
00146 QString label( "" );
00147 QStringList nameList;
00148 bool ok = false;
00149
00150 nameList.append( i18n( "Clear object reference" ) );
00151 foreach( const GluonCore::GluonObject * item, items )
00152 nameList.append( item->fullyQualifiedName() );
00153
00154 QString chosen = KInputDialog::getItem( caption, label, nameList, 0, false, &ok, this );
00155 if( ok )
00156 {
00157 GluonCore::GluonObject* chosenItem = 0;
00158 foreach( const GluonCore::GluonObject * testItem, items )
00159 {
00160 if( testItem->fullyQualifiedName() == chosen )
00161 {
00162 chosenItem = const_cast<GluonCore::GluonObject*>( testItem );
00163 break;
00164 }
00165 }
00166 objectValueChanged( chosenItem );
00167 }
00168 }
00169 }
00170
00171 void GluonObjectPropertyWidgetItem::openInEditor()
00172 {
00173 GluonEngine::Asset* theObject = qobject_cast<GluonEngine::Asset*>( GluonCore::GluonObjectFactory::instance()->wrappedObject( editObject()->property( editProperty().toUtf8() ) ) );
00174
00175 if( theObject )
00176 {
00177 FileManager::instance()->openAsset( theObject );
00178 }
00179 }
00180
00181 void
00182 GluonObjectPropertyWidgetItem::setEditValue( const QVariant& value )
00183 {
00184 PropertyWidgetItem::setEditValue( value );
00185
00186 GluonCore::GluonObject* theObject = GluonCore::GluonObjectFactory::instance()->wrappedObject( value );
00187 if( theObject )
00188 {
00189 d->currentValue->setText( theObject->name() );
00190 d->currentValue->setToolTip( theObject->fullyQualifiedName() );
00191
00192 }
00193 else
00194 {
00195 d->currentValue->setText( i18n( "(no object selected)" ) );
00196
00197 }
00198 }
00199
00200 void
00201 GluonObjectPropertyWidgetItem::objectValueChanged( GluonCore::GluonObject* value )
00202 {
00203 if( value )
00204 {
00205 d->currentValue->setText( value->name() );
00206
00207 }
00208 else
00209 {
00210 d->currentValue->setText( i18n( "(no object selected)" ) );
00211
00212 }
00213
00214 QVariant oldValue = editObject()->property( editProperty().toUtf8() );
00215 QVariant newValue = GluonCore::GluonObjectFactory::instance()->wrapObject( oldValue, value );
00216 PropertyWidgetItem::valueChanged( newValue );
00217 }
00218
00219