00001 /****************************************************************************** 00002 * This file is part of the Gluon Development Platform 00003 * Copyright (c) 2010 Arjen Hiemstra <ahiemstra@heimr.nl> 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 00020 #include "textureasset.h" 00021 00022 #include <core/debughelper.h> 00023 #include <graphics/texture.h> 00024 #include <graphics/engine.h> 00025 00026 #include <QtCore/QUrl> 00027 #include <QtCore/QMimeData> 00028 #include <QtGui/QImage> 00029 #include <QtGui/QImageReader> 00030 00031 REGISTER_OBJECTTYPE( GluonEngine, TextureAsset ) 00032 00033 using namespace GluonEngine; 00034 00035 class TextureAsset::TextureAssetPrivate 00036 { 00037 public: 00038 TextureAssetPrivate() {} 00039 ~TextureAssetPrivate() {} 00040 00041 QPixmap icon; 00042 GluonGraphics::Texture* texture; 00043 }; 00044 00045 TextureAsset::TextureAsset( QObject* parent ) 00046 : Asset( parent ) 00047 , d( new TextureAssetPrivate ) 00048 { 00049 d->texture = GluonGraphics::Engine::instance()->createTexture( name() ); 00050 } 00051 00052 TextureAsset::~TextureAsset() 00053 { 00054 d->texture = 0; 00055 } 00056 00057 QIcon TextureAsset::icon() const 00058 { 00059 if( d->icon.isNull() ) 00060 return GluonEngine::Asset::icon(); 00061 00062 return QIcon( d->icon ); 00063 } 00064 00065 const QStringList TextureAsset::supportedMimeTypes() const 00066 { 00067 QList<QByteArray> supported = QImageReader::supportedImageFormats(); 00068 00069 QStringList supportedTypes; 00070 foreach( const QByteArray & type, supported ) 00071 { 00072 supportedTypes << QString( "image/%1" ).arg( QString( type ) ); 00073 } 00074 00075 return supportedTypes; 00076 } 00077 00078 void TextureAsset::load() 00079 { 00080 if( !file().isEmpty() ) 00081 { 00082 if( d->texture->load( file() ) ) 00083 { 00084 mimeData()->setText( name() ); 00085 //d->icon = QPixmap::fromImage(d->texture->scaled(QSize(128, 128), Qt::KeepAspectRatio)); 00086 setLoaded( true ); 00087 return; 00088 } 00089 } 00090 00091 debug( "Error loading texture: %1", name() ); 00092 } 00093 00094 void TextureAsset::setName( const QString& newName ) 00095 { 00096 GluonGraphics::Engine::instance()->removeTexture( name() ); 00097 GluonGraphics::Engine::instance()->addTexture( newName, d->texture ); 00098 GluonEngine::Asset::setName( newName ); 00099 } 00100 00101 Q_EXPORT_PLUGIN2( gluon_asset_texture, GluonEngine::TextureAsset ) 00102 00103 #include "textureasset.moc"