Qtopia library API Documentation

resource.cpp

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 <qfile.h>
00027 #include <qregexp.h>
00028 #include <qpixmapcache.h>
00029 #include <qpainter.h>
00030 
00031 // this namespace is just a workaround for a gcc bug
00032 // gcc exports inline functions in the generated file
00033 // inlinepics_p.h
00034 
00035 namespace {
00036 #include "inlinepics_p.h"
00037 }
00038 
00063 QPixmap Resource::loadPixmap( const QString &pix )
00064 {
00065     QPixmap pm;
00066     QString key="QPE_"+pix;
00067     if ( !QPixmapCache::find(key,pm) ) {
00068     pm.convertFromImage(loadImage(pix));
00069     QPixmapCache::insert(key,pm);
00070     }
00071     return pm;
00072 }
00073 
00078 QBitmap Resource::loadBitmap( const QString &pix )
00079 {
00080     QBitmap bm;
00081     bm = loadPixmap(pix);
00082     return bm;
00083 }
00084 
00091 QString Resource::findPixmap( const QString &pix )
00092 {
00093     QString picsPath = QPEApplication::qpeDir() + "pics/";
00094 
00095     QString f;
00096 
00097     // Common case optimizations...
00098     f = picsPath + pix + ".png";
00099     if ( QFile( f ).exists() )
00100     return f;
00101     f = picsPath + pix + ".xpm";
00102     if ( QFile( f ).exists() )
00103     return f;
00104 
00105 
00106     // All formats...
00107     QStrList fileFormats = QImageIO::inputFormats();
00108     QString ff = fileFormats.first();
00109     while ( fileFormats.current() ) {
00110     QStringList exts = MimeType("image/"+ff.lower()).extensions();
00111     for ( QStringList::ConstIterator it = exts.begin(); it!=exts.end(); ++it ) {
00112         QString f = picsPath + pix + "." + *it;
00113         if ( QFile(f).exists() )
00114         return f;
00115     }
00116     ff = fileFormats.next();
00117     }
00118 
00119     // Finally, no (or existing) extension...
00120     if ( QFile( picsPath + pix ).exists() )
00121     return picsPath + pix;
00122 
00123     //qDebug("Cannot find pixmap: %s", pix.latin1());
00124     return QString();
00125 }
00126 
00136 QString Resource::findSound( const QString &name )
00137 {
00138     QString picsPath = QPEApplication::qpeDir() + "sounds/";
00139 
00140     QString result;
00141     if ( QFile( (result = picsPath + name + ".wav") ).exists() )
00142     return result;
00143 
00144     return QString();
00145 }
00146 
00150 QStringList Resource::allSounds()
00151 {
00152     QDir resourcedir( QPEApplication::qpeDir() + "sounds/", "*.wav" );
00153     QStringList entries = resourcedir.entryList();
00154     QStringList result;
00155     for (QStringList::Iterator i=entries.begin(); i != entries.end(); ++i)
00156     result.append((*i).replace(QRegExp("\\.wav"),""));
00157     return result;
00158 }
00159 
00160 static QImage load_image(const QString &name)
00161 {
00162     QImage img = qembed_findImage(name.latin1());
00163     if ( img.isNull() ) {
00164     // No inlined image, try file
00165     QString f = Resource::findPixmap(name);
00166     if ( !f.isEmpty() )
00167         img.load(f);
00168     }
00169     return img;
00170 }
00171 
00176 QImage Resource::loadImage( const QString &name)
00177 {
00178 #ifndef QT_NO_DEPTH_32  // have alpha-blended pixmaps
00179     static QImage last_enabled;
00180     static QString last_enabled_name;
00181     if ( name == last_enabled_name )
00182     return last_enabled;
00183 #endif
00184     QImage img = load_image(name);
00185 #ifndef QT_NO_DEPTH_32  // have alpha-blended pixmaps
00186     if ( img.isNull() ) {
00187     // No file, try generating
00188     if ( name[name.length()-1]=='d' && name.right(9)=="_disabled" ) {
00189         last_enabled_name = name.left(name.length()-9);
00190         last_enabled = load_image(last_enabled_name);
00191         if ( last_enabled.isNull() ) {
00192         last_enabled_name = QString::null;
00193         } else {
00194         img.detach();
00195         img.create( last_enabled.width(), last_enabled.height(), 32 );
00196         for ( int y = 0; y < img.height(); y++ ) {
00197             for ( int x = 0; x < img.width(); x++ ) {
00198             QRgb p = last_enabled.pixel( x, y );
00199             int a = qAlpha(p)/3;
00200             int g = qGray(qRed(p),qGreen(p),qBlue(p));
00201             img.setPixel( x, y, qRgba(g,g,g,a) );
00202             }
00203         }
00204         img.setAlphaBuffer( TRUE );
00205         }
00206     }
00207     }
00208 #endif
00209     return img;
00210 }
00211 
KDE Logo
This file is part of the documentation for OPIE Version 1.5.5.
Documentation copyright © 1997-2003 the KDE developers. 2003 OPIE developers
Generated on Tue Feb 10 20:24:08 2004 by doxygen 1.3.5 written by Dimitri van Heesch, © 1997-2001