summaryrefslogtreecommitdiff
path: root/library/resource.cpp
Unidiff
Diffstat (limited to 'library/resource.cpp') (more/less context) (show whitespace changes)
-rw-r--r--library/resource.cpp119
1 files changed, 95 insertions, 24 deletions
diff --git a/library/resource.cpp b/library/resource.cpp
index 0915c45..0db2a75 100644
--- a/library/resource.cpp
+++ b/library/resource.cpp
@@ -1,5 +1,5 @@
1/********************************************************************** 1/**********************************************************************
2** Copyright (C) 2000 Trolltech AS. All rights reserved. 2** Copyright (C) 2000-2002 Trolltech AS. All rights reserved.
3** 3**
4** This file is part of Qtopia Environment. 4** This file is part of the Qtopia Environment.
5** 5**
@@ -20,4 +20,6 @@
20 20
21#define QTOPIA_INTERNAL_MIMEEXT
21#include "qpeapplication.h" 22#include "qpeapplication.h"
22#include "resource.h" 23#include "resource.h"
24#include "mimetype.h"
23#include <qdir.h> 25#include <qdir.h>
@@ -35,2 +37,10 @@
35 The resources may be provided from files or other sources. 37 The resources may be provided from files or other sources.
38
39 The allSounds() function returns a list of all the sounds available.
40 A particular sound can be searched for using findSound().
41
42 Images can be loaded with loadImage(), loadPixmap(), loadBitmap()
43 and loadIconSet().
44
45 \ingroup qtopiaemb
36*/ 46*/
@@ -43,4 +53,4 @@
43/*! 53/*!
44 Returns the QPixmap named \a pix. You should avoid including 54 Returns the QPixmap called \a pix. You should avoid including
45 any filename type extension (eg. .png, .xpm). 55 any filename type extension (e.g. .png, .xpm).
46*/ 56*/
@@ -58,4 +68,4 @@ QPixmap Resource::loadPixmap( const QString &pix )
58/*! 68/*!
59 Returns the QBitmap named \a pix. You should avoid including 69 Returns the QBitmap called \a pix. You should avoid including
60 any filename type extension (eg. .png, .xpm). 70 any filename type extension (e.g. .png, .xpm).
61*/ 71*/
@@ -69,4 +79,4 @@ QBitmap Resource::loadBitmap( const QString &pix )
69/*! 79/*!
70 Returns the filename of a pixmap named \a pix. You should avoid including 80 Returns the filename of a pixmap called \a pix. You should avoid including
71 any filename type extension (eg. .png, .xpm .jpg .jpeg). 81 any filename type extension (e.g. .png, .xpm).
72 82
@@ -78,11 +88,27 @@ QString Resource::findPixmap( const QString &pix )
78 88
79 if ( QFile( picsPath + pix + ".png").exists() ) 89 QString f;
80 return picsPath + pix + ".png"; 90
81 else if ( QFile( picsPath + pix + ".jpeg").exists() ) 91 // Common case optimizations...
82 return picsPath + pix + ".jpeg"; 92 f = picsPath + pix + ".png";
83 else if ( QFile( picsPath + pix + ".jpg").exists() ) 93 if ( QFile( f ).exists() )
84 return picsPath + pix + ".jpg"; 94 return f;
85 else if ( QFile( picsPath + pix + ".xpm").exists() ) 95 f = picsPath + pix + ".xpm";
86 return picsPath + pix + ".xpm"; 96 if ( QFile( f ).exists() )
87 else if ( QFile( picsPath + pix ).exists() ) 97 return f;
98
99 // All formats...
100 QStrList fileFormats = QImageIO::inputFormats();
101 QString ff = fileFormats.first();
102 while ( fileFormats.current() ) {
103 QStringList exts = MimeType("image/"+ff.lower()).extensions();
104 for ( QStringList::ConstIterator it = exts.begin(); it!=exts.end(); ++it ) {
105 QString f = picsPath + pix + "." + *it;
106 if ( QFile(f).exists() )
107 return f;
108 }
109 ff = fileFormats.next();
110 }
111
112 // Finally, no (or existing) extension...
113 if ( QFile( picsPath + pix ).exists() )
88 return picsPath + pix; 114 return picsPath + pix;
@@ -94,4 +120,9 @@ QString Resource::findPixmap( const QString &pix )
94/*! 120/*!
95 Returns a sound file for a sound named \a name. 121 Returns a sound file for a sound called \a name.
96 You should avoid including any filename type extension (eg. .wav, .au, .mp3). 122
123 You should avoid including any filename type extension (e.g. .wav),
124 as the system will search for only those fileformats which are supported
125 by the library.
126
127 Currently, only WAV files are supported.
97*/ 128*/
@@ -121,5 +152,17 @@ QStringList Resource::allSounds()
121 152
153static QImage load_image(const QString &name)
154{
155 QImage img = qembed_findImage(name.latin1());
156 if ( img.isNull() ) {
157 // No inlined image, try file
158 QString f = Resource::findPixmap(name);
159 if ( !f.isEmpty() )
160 img.load(f);
161 }
162 return img;
163}
164
122/*! 165/*!
123 Returns the QImage named \a name. You should avoid including 166 Returns the QImage called \a name. You should avoid including
124 any filename type extension (eg. .png, .xpm .jpg). 167 any filename type extension (e.g. .png, .xpm).
125*/ 168*/
@@ -127,5 +170,33 @@ QImage Resource::loadImage( const QString &name)
127{ 170{
128 QImage img = qembed_findImage(name.latin1()); 171 #ifndef QT_NO_DEPTH_32// have alpha-blended pixmaps
129 if ( img.isNull() ) 172 static QImage last_enabled;
130 return QImage(findPixmap(name)); 173 static QString last_enabled_name;
174 if ( name == last_enabled_name )
175 return last_enabled;
176#endif
177 QImage img = load_image(name);
178 #ifndef QT_NO_DEPTH_32// have alpha-blended pixmaps
179 if ( img.isNull() ) {
180 // No file, try generating
181 if ( name[name.length()-1]=='d' && name.right(9)=="_disabled" ) {
182 last_enabled_name = name.left(name.length()-9);
183 last_enabled = load_image(last_enabled_name);
184 if ( last_enabled.isNull() ) {
185 last_enabled_name = QString::null;
186 } else {
187 img.detach();
188 img.create( last_enabled.width(), last_enabled.height(), 32 );
189 for ( int y = 0; y < img.height(); y++ ) {
190 for ( int x = 0; x < img.width(); x++ ) {
191 QRgb p = last_enabled.pixel( x, y );
192 int a = qAlpha(p)/3;
193 int g = qGray(qRed(p),qGreen(p),qBlue(p));
194 img.setPixel( x, y, qRgba(g,g,g,a) );
195 }
196 }
197 img.setAlphaBuffer( TRUE );
198 }
199 }
200 }
201#endif
131 return img; 202 return img;