Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

resource.cpp

Go to the documentation of this file.
00001 /**********************************************************************
00002 ** Copyright (C) 2000-2002 Trolltech AS.  All rights reserved.
00003 **
00004 ** This file is part of the Qtopia Environment.
00005 **
00006 ** This file may be distributed and/or modified under the terms of the
00007 ** GNU General Public License version 2 as published by the Free Software
00008 ** Foundation and appearing in the file LICENSE.GPL included in the
00009 ** packaging of this file.
00010 **
00011 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00012 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00013 **
00014 ** See http://www.trolltech.com/gpl/ for GPL licensing information.
00015 **
00016 ** Contact info@trolltech.com if any conditions of this licensing are
00017 ** not clear to you.
00018 **
00019 **********************************************************************/
00020 
00021 #define QTOPIA_INTERNAL_MIMEEXT
00022 #include <qpe/qpeapplication.h>
00023 #include "resource.h"
00024 #include "mimetype.h"
00025 #include <qdir.h>
00026 #include <qpixmapcache.h>
00027 
00028 /*
00029  * enable or disable the search for the icon without .png or .xpm
00030  * suffix. We would use MimeType to lookup possible extensions...
00031  */
00032 bool qpe_fast_findPixmap = false; // visible in libqpe
00033 
00034 // this namespace is just a workaround for a gcc bug
00035 // gcc exports inline functions in the generated file
00036 // inlinepics_p.h
00037 
00038 #ifndef LIBQPE_NO_INLINE_IMAGES
00039 namespace {
00040 #include "inlinepics_p.h"
00041 }
00042 #endif
00043 
00068 #include <stdio.h>
00069 QPixmap Resource::loadPixmap( const QString &pix )
00070 {
00071     QPixmap pm; // null pixmap
00072     QString key="QPE_"+pix;
00073     if ( !QPixmapCache::find(key,pm) ) {
00074         QImage I = loadImage(pix);
00075         if( I.isNull() ) {
00076           qWarning( "Could not load %s", pix.latin1() );
00077         } else {
00078           pm.convertFromImage(I);
00079           QPixmapCache::insert(key,pm);
00080         }
00081     }else {
00082         qWarning("In Cache for %s pixmap %s", qApp->argv()[0], pix.local8Bit().data() );
00083     }
00084     return pm;
00085 }
00086 
00091 QBitmap Resource::loadBitmap( const QString &pix )
00092 {
00093     QBitmap bm;
00094     bm = loadPixmap(pix);
00095     return bm;
00096 }
00097 
00098 /*
00099  * @internal
00100  * Parse the extensions only once. If the MimeType mapping
00101  * changes we will still use the old extensions, applications
00102  * will need to be restarted to be aware of new extensions...
00103  * For now it seems ok to have that limitation, if that is a wrong
00104  * assumption we will need to invalidate this list
00105  */
00106 QStringList *opie_image_extension_List = 0;
00107 static void clean_opie_image_extension_List() {
00108     delete opie_image_extension_List;
00109     opie_image_extension_List = 0;
00110 }
00111 
00112 QStringList opie_imageExtensions() {
00113     /*
00114      * File extensions (e.g jpeg JPG jpg) are not
00115      * parsed yet
00116      */
00117     if ( !opie_image_extension_List ) {
00118         opie_image_extension_List = new QStringList();
00119         qAddPostRoutine( clean_opie_image_extension_List );
00120 
00121         QStrList fileFormats = QImageIO::inputFormats();
00122         QString ff = fileFormats.first();
00123         while ( fileFormats.current() ) {
00124             *opie_image_extension_List += MimeType("image/"+ff.lower()).extensions();
00125             ff = fileFormats.next();
00126         }
00127     }
00128 
00129     return *opie_image_extension_List; // QShared so it should be efficient
00130 }
00131 
00138 QString Resource::findPixmap( const QString &pix )
00139 {
00140     QString picsPath = QPEApplication::qpeDir() + "pics/";
00141     QString f;
00142 
00143     // Common case optimizations...
00144     f = picsPath + pix + ".png";
00145     if ( QFile( f ).exists() )
00146         return f;
00147     f = picsPath + pix + ".xpm";
00148     if ( QFile( f ).exists() )
00149         return f;
00150 
00151     if ( !qpe_fast_findPixmap ) {
00152         printf("Doing slow search for %s %s\n", qApp->argv()[0], pix.local8Bit().data() );
00153         // All formats...
00154         QStringList exts = opie_imageExtensions();
00155         for ( QStringList::ConstIterator it = exts.begin(); it!=exts.end(); ++it ) {
00156             QString f = picsPath + pix + "." + *it;
00157             if ( QFile(f).exists() )
00158                 return f;
00159         }
00160 
00161         // Finally, no (or existing) extension...
00162         if ( QFile( picsPath + pix ).exists() )
00163             return picsPath + pix;
00164     }
00165 
00166     //qDebug("Cannot find pixmap: %s", pix.latin1());
00167     return QString();
00168 }
00169 
00179 QString Resource::findSound( const QString &name )
00180 {
00181     QString picsPath = QPEApplication::qpeDir() + "sounds/";
00182 
00183     QString result;
00184     if ( QFile( (result = picsPath + name + ".wav") ).exists() )
00185         return result;
00186 
00187     return QString();
00188 }
00189 
00193 QStringList Resource::allSounds()
00194 {
00195     QDir resourcedir( QPEApplication::qpeDir() + "sounds/", "*.wav" );
00196     QStringList entries = resourcedir.entryList();
00197     QStringList result;
00198     for (QStringList::Iterator i=entries.begin(); i != entries.end(); ++i)
00199         result.append((*i).replace(QRegExp("\\.wav"),""));
00200     return result;
00201 }
00202 
00203 static QImage load_image(const QString &name)
00204 {
00205     QImage img;
00206 
00207 #ifndef LIBQPE_NO_INLINE_IMAGES
00208     img = qembed_findImage(name.latin1());
00209 #endif
00210     if ( img.isNull() )
00211     {
00212     // No inlined image, try file
00213         QString f = Resource::findPixmap(name);
00214         if ( !f.isEmpty() )
00215             img.load(f);
00216     }
00217     return img;
00218 }
00219 
00224 QImage Resource::loadImage( const QString &name)
00225 {
00226 #ifndef QT_NO_DEPTH_32  // have alpha-blended pixmaps
00227     static QImage last_enabled;
00228     static QString last_enabled_name;
00229     if ( name == last_enabled_name )
00230         return last_enabled;
00231 #endif
00232     QImage img = load_image(name);
00233 #ifndef QT_NO_DEPTH_32  // have alpha-blended pixmaps
00234     if ( img.isNull() ) {
00235         // No file, try generating
00236         if ( name[name.length()-1]=='d' && name.right(9)=="_disabled" ) {
00237             last_enabled_name = name.left(name.length()-9);
00238             last_enabled = load_image(last_enabled_name);
00239             if ( last_enabled.isNull() ) {
00240                 last_enabled_name = QString::null;
00241             } else {
00242                 img.detach();
00243                 img.create( last_enabled.width(), last_enabled.height(), 32 );
00244                 for ( int y = 0; y < img.height(); y++ ) {
00245                     for ( int x = 0; x < img.width(); x++ ) {
00246                         QRgb p = last_enabled.pixel( x, y );
00247                         int a = qAlpha(p)/3;
00248                         int g = qGray(qRed(p),qGreen(p),qBlue(p));
00249                         img.setPixel( x, y, qRgba(g,g,g,a) );
00250                     }
00251                 }
00252                 img.setAlphaBuffer( TRUE );
00253             }
00254         }
00255     }
00256 #endif
00257     return img;
00258 }
00259 

Generated on Sat Nov 5 16:16:41 2005 for OPIE by  doxygen 1.4.2