summaryrefslogtreecommitdiff
path: root/library/resource.cpp
Unidiff
Diffstat (limited to 'library/resource.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--library/resource.cpp34
1 files changed, 29 insertions, 5 deletions
diff --git a/library/resource.cpp b/library/resource.cpp
index b31876f..3b5e9ec 100644
--- a/library/resource.cpp
+++ b/library/resource.cpp
@@ -1,235 +1,259 @@
1/********************************************************************** 1/**********************************************************************
2** Copyright (C) 2000-2002 Trolltech AS. All rights reserved. 2** Copyright (C) 2000-2002 Trolltech AS. All rights reserved.
3** 3**
4** This file is part of the Qtopia Environment. 4** This file is part of the Qtopia Environment.
5** 5**
6** This file may be distributed and/or modified under the terms of the 6** This file may be distributed and/or modified under the terms of the
7** GNU General Public License version 2 as published by the Free Software 7** GNU General Public License version 2 as published by the Free Software
8** Foundation and appearing in the file LICENSE.GPL included in the 8** Foundation and appearing in the file LICENSE.GPL included in the
9** packaging of this file. 9** packaging of this file.
10** 10**
11** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE 11** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
12** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 12** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
13** 13**
14** See http://www.trolltech.com/gpl/ for GPL licensing information. 14** See http://www.trolltech.com/gpl/ for GPL licensing information.
15** 15**
16** Contact info@trolltech.com if any conditions of this licensing are 16** Contact info@trolltech.com if any conditions of this licensing are
17** not clear to you. 17** not clear to you.
18** 18**
19**********************************************************************/ 19**********************************************************************/
20 20
21#define QTOPIA_INTERNAL_MIMEEXT 21#define QTOPIA_INTERNAL_MIMEEXT
22#include <qpe/qpeapplication.h> 22#include <qpe/qpeapplication.h>
23#include "resource.h" 23#include "resource.h"
24#include "mimetype.h" 24#include "mimetype.h"
25#include <qdir.h> 25#include <qdir.h>
26#include <qpixmapcache.h> 26#include <qpixmapcache.h>
27 27
28// this namespace is just a workaround for a gcc bug 28// this namespace is just a workaround for a gcc bug
29// gcc exports inline functions in the generated file 29// gcc exports inline functions in the generated file
30// inlinepics_p.h 30// inlinepics_p.h
31 31
32#ifndef LIBQPE_NO_INLINE_IMAGES
32namespace { 33namespace {
33#include "inlinepics_p.h" 34#include "inlinepics_p.h"
34} 35}
36#endif
35 37
36static bool g_notUseSet = ::getenv("OVERWRITE_ICON_SET"); 38static bool g_notUseSet = ::getenv("OVERWRITE_ICON_SET");
37 39
38/*! 40/*!
39 \class Resource resource.h 41 \class Resource resource.h
40 \brief The Resource class provides access to named resources. 42 \brief The Resource class provides access to named resources.
41 43
42 The resources may be provided from files or other sources. 44 The resources may be provided from files or other sources.
43 45
44 The allSounds() function returns a list of all the sounds available. 46 The allSounds() function returns a list of all the sounds available.
45 A particular sound can be searched for using findSound(). 47 A particular sound can be searched for using findSound().
46 48
47 Images can be loaded with loadImage(), loadPixmap(), loadBitmap() 49 Images can be loaded with loadImage(), loadPixmap(), loadBitmap()
48 and loadIconSet(). 50 and loadIconSet().
49 51
50 \ingroup qtopiaemb 52 \ingroup qtopiaemb
51*/ 53*/
52 54
53/*! 55/*!
54 \fn Resource::Resource() 56 \fn Resource::Resource()
55 \internal 57 \internal
56*/ 58*/
57 59
58/*! 60/*!
59 Returns the QPixmap called \a pix. You should avoid including 61 Returns the QPixmap called \a pix. You should avoid including
60 any filename type extension (e.g. .png, .xpm). 62 any filename type extension (e.g. .png, .xpm).
61*/ 63*/
62#include <stdio.h> 64#include <stdio.h>
63QPixmap Resource::loadPixmap( const QString &pix ) 65QPixmap Resource::loadPixmap( const QString &pix )
64{ 66{
65 QPixmap pm; // null pixmap 67 QPixmap pm; // null pixmap
66 QString key="QPE_"+pix; 68 QString key="QPE_"+pix;
67 if ( !QPixmapCache::find(key,pm) ) { 69 if ( !QPixmapCache::find(key,pm) ) {
68 QImage I = loadImage(pix); 70 QImage I = loadImage(pix);
69 if( I.isNull() ) { 71 if( I.isNull() ) {
70 qWarning( "Could not load %s", pix.latin1() ); 72 qWarning( "Could not load %s", pix.latin1() );
71 } else { 73 } else {
72 pm.convertFromImage(I); 74 pm.convertFromImage(I);
73 QPixmapCache::insert(key,pm); 75 QPixmapCache::insert(key,pm);
74 } 76 }
75 } 77 }
76 return pm; 78 return pm;
77} 79}
78 80
79/*! 81/*!
80 Returns the QBitmap called \a pix. You should avoid including 82 Returns the QBitmap called \a pix. You should avoid including
81 any filename type extension (e.g. .png, .xpm). 83 any filename type extension (e.g. .png, .xpm).
82*/ 84*/
83QBitmap Resource::loadBitmap( const QString &pix ) 85QBitmap Resource::loadBitmap( const QString &pix )
84{ 86{
85 QBitmap bm; 87 QBitmap bm;
86 bm = loadPixmap(pix); 88 bm = loadPixmap(pix);
87 return bm; 89 return bm;
88} 90}
89 91
90/*! 92/*!
91 Returns the filename of a pixmap called \a pix. You should avoid including 93 Returns the filename of a pixmap called \a pix. You should avoid including
92 any filename type extension (e.g. .png, .xpm). 94 any filename type extension (e.g. .png, .xpm).
93 95
94 Normally you will use loadPixmap() rather than this function. 96 Normally you will use loadPixmap() rather than this function.
95*/ 97*/
96QString Resource::findPixmap( const QString &pix ) 98QString Resource::findPixmap( const QString &pix )
97{ 99{
98 QString picsPath = QPEApplication::qpeDir() + "pics/"; 100 QString picsPath = QPEApplication::qpeDir() + "pics/";
99
100 QString f; 101 QString f;
101 102
102 // Common case optimizations... 103 // Common case optimizations...
103 f = picsPath + pix + ".png"; 104 f = picsPath + pix + ".png";
104 if ( QFile( f ).exists() ) 105 if ( QFile( f ).exists() )
105 return f; 106 return f;
106 f = picsPath + pix + ".xpm"; 107 f = picsPath + pix + ".xpm";
107 if ( QFile( f ).exists() ) 108 if ( QFile( f ).exists() )
108 return f; 109 return f;
109 110
111#ifdef LIBQPE_NO_INLINE_IMAGES
112 QString picsPathInline = picsPath + "inline/";
113 // Common case optimizations...
114 f = picsPathInline + pix + ".png";
115 if ( QFile( f ).exists() )
116 return f;
117 f = picsPathInline + pix + ".xpm";
118 if ( QFile( f ).exists() )
119 return f;
120#endif
121
110 // All formats... 122 // All formats...
111 QStrList fileFormats = QImageIO::inputFormats(); 123 QStrList fileFormats = QImageIO::inputFormats();
112 QString ff = fileFormats.first(); 124 QString ff = fileFormats.first();
113 while ( fileFormats.current() ) { 125 while ( fileFormats.current() ) {
114 QStringList exts = MimeType("image/"+ff.lower()).extensions(); 126 QStringList exts = MimeType("image/"+ff.lower()).extensions();
115 for ( QStringList::ConstIterator it = exts.begin(); it!=exts.end(); ++it ) { 127 for ( QStringList::ConstIterator it = exts.begin(); it!=exts.end(); ++it ) {
116 QString f = picsPath + pix + "." + *it; 128 QString f = picsPath + pix + "." + *it;
117 if ( QFile(f).exists() ) 129 if ( QFile(f).exists() )
118 return f; 130 return f;
119 } 131 }
120 ff = fileFormats.next(); 132 ff = fileFormats.next();
121 } 133 }
122 134
123 // Finally, no (or existing) extension... 135 // Finally, no (or existing) extension...
124 if ( QFile( picsPath + pix ).exists() ) 136 if ( QFile( picsPath + pix ).exists() )
125 return picsPath + pix; 137 return picsPath + pix;
126 138
127 //qDebug("Cannot find pixmap: %s", pix.latin1()); 139 //qDebug("Cannot find pixmap: %s", pix.latin1());
128 return QString(); 140 return QString();
129} 141}
130 142
131/*! 143/*!
132 Returns a sound file for a sound called \a name. 144 Returns a sound file for a sound called \a name.
133 145
134 You should avoid including any filename type extension (e.g. .wav), 146 You should avoid including any filename type extension (e.g. .wav),
135 as the system will search for only those fileformats which are supported 147 as the system will search for only those fileformats which are supported
136 by the library. 148 by the library.
137 149
138 Currently, only WAV files are supported. 150 Currently, only WAV files are supported.
139*/ 151*/
140QString Resource::findSound( const QString &name ) 152QString Resource::findSound( const QString &name )
141{ 153{
142 QString picsPath = QPEApplication::qpeDir() + "sounds/"; 154 QString picsPath = QPEApplication::qpeDir() + "sounds/";
143 155
144 QString result; 156 QString result;
145 if ( QFile( (result = picsPath + name + ".wav") ).exists() ) 157 if ( QFile( (result = picsPath + name + ".wav") ).exists() )
146 return result; 158 return result;
147 159
148 return QString(); 160 return QString();
149} 161}
150 162
151/*! 163/*!
152 Returns a list of all sound names. 164 Returns a list of all sound names.
153*/ 165*/
154QStringList Resource::allSounds() 166QStringList Resource::allSounds()
155{ 167{
156 QDir resourcedir( QPEApplication::qpeDir() + "sounds/", "*.wav" ); 168 QDir resourcedir( QPEApplication::qpeDir() + "sounds/", "*.wav" );
157 QStringList entries = resourcedir.entryList(); 169 QStringList entries = resourcedir.entryList();
158 QStringList result; 170 QStringList result;
159 for (QStringList::Iterator i=entries.begin(); i != entries.end(); ++i) 171 for (QStringList::Iterator i=entries.begin(); i != entries.end(); ++i)
160 result.append((*i).replace(QRegExp("\\.wav"),"")); 172 result.append((*i).replace(QRegExp("\\.wav"),""));
161 return result; 173 return result;
162} 174}
163 175
164static QImage load_image(const QString &name) 176static QImage load_image(const QString &name)
165{ 177{
178 QImage img;
179
166 if (g_notUseSet ) { 180 if (g_notUseSet ) {
167 // try file 181 // try file
168 QImage img;
169 QString f = Resource::findPixmap(name); 182 QString f = Resource::findPixmap(name);
170 if ( !f.isEmpty() ) 183 if ( !f.isEmpty() )
171 img.load(f); 184 img.load(f);
185#ifndef LIBQPE_NO_INLINE_IMAGES
172 if (img.isNull() ) 186 if (img.isNull() )
173 img = qembed_findImage(name.latin1() ); 187 img = qembed_findImage(name.latin1() );
188#endif
174 return img; 189 return img;
175 } 190 }
176 else{ 191 else{
177 QImage img = qembed_findImage(name.latin1()); 192#ifndef LIBQPE_NO_INLINE_IMAGES
178 193 img = qembed_findImage(name.latin1());
179 if ( img.isNull() ) { 194#else
195 QString f = Resource::findPixmap( "/inline/" + name );
196 if ( !f.isEmpty() )
197 {
198 img.load(f);
199 return img;
200 }
201#endif
202 if ( img.isNull() )
203 {
180 // No inlined image, try file 204 // No inlined image, try file
181 QString f = Resource::findPixmap(name); 205 QString f = Resource::findPixmap(name);
182 if ( !f.isEmpty() ) 206 if ( !f.isEmpty() )
183 img.load(f); 207 img.load(f);
184 } 208 }
185 return img; 209 return img;
186 } 210 }
187} 211}
188 212
189/*! 213/*!
190 Returns the QImage called \a name. You should avoid including 214 Returns the QImage called \a name. You should avoid including
191 any filename type extension (e.g. .png, .xpm). 215 any filename type extension (e.g. .png, .xpm).
192*/ 216*/
193QImage Resource::loadImage( const QString &name) 217QImage Resource::loadImage( const QString &name)
194{ 218{
195 #ifndef QT_NO_DEPTH_32// have alpha-blended pixmaps 219 #ifndef QT_NO_DEPTH_32// have alpha-blended pixmaps
196 static QImage last_enabled; 220 static QImage last_enabled;
197 static QString last_enabled_name; 221 static QString last_enabled_name;
198 if ( name == last_enabled_name ) 222 if ( name == last_enabled_name )
199 return last_enabled; 223 return last_enabled;
200#endif 224#endif
201 QImage img = load_image(name); 225 QImage img = load_image(name);
202 #ifndef QT_NO_DEPTH_32// have alpha-blended pixmaps 226 #ifndef QT_NO_DEPTH_32// have alpha-blended pixmaps
203 if ( img.isNull() ) { 227 if ( img.isNull() ) {
204 // No file, try generating 228 // No file, try generating
205 if ( name[name.length()-1]=='d' && name.right(9)=="_disabled" ) { 229 if ( name[name.length()-1]=='d' && name.right(9)=="_disabled" ) {
206 last_enabled_name = name.left(name.length()-9); 230 last_enabled_name = name.left(name.length()-9);
207 last_enabled = load_image(last_enabled_name); 231 last_enabled = load_image(last_enabled_name);
208 if ( last_enabled.isNull() ) { 232 if ( last_enabled.isNull() ) {
209 last_enabled_name = QString::null; 233 last_enabled_name = QString::null;
210 } else { 234 } else {
211 img.detach(); 235 img.detach();
212 img.create( last_enabled.width(), last_enabled.height(), 32 ); 236 img.create( last_enabled.width(), last_enabled.height(), 32 );
213 for ( int y = 0; y < img.height(); y++ ) { 237 for ( int y = 0; y < img.height(); y++ ) {
214 for ( int x = 0; x < img.width(); x++ ) { 238 for ( int x = 0; x < img.width(); x++ ) {
215 QRgb p = last_enabled.pixel( x, y ); 239 QRgb p = last_enabled.pixel( x, y );
216 int a = qAlpha(p)/3; 240 int a = qAlpha(p)/3;
217 int g = qGray(qRed(p),qGreen(p),qBlue(p)); 241 int g = qGray(qRed(p),qGreen(p),qBlue(p));
218 img.setPixel( x, y, qRgba(g,g,g,a) ); 242 img.setPixel( x, y, qRgba(g,g,g,a) );
219 } 243 }
220 } 244 }
221 img.setAlphaBuffer( TRUE ); 245 img.setAlphaBuffer( TRUE );
222 } 246 }
223 } 247 }
224 } 248 }
225#endif 249#endif
226 return img; 250 return img;
227} 251}
228 252
229/*! 253/*!
230 \fn QIconSet Resource::loadIconSet( const QString &name ) 254 \fn QIconSet Resource::loadIconSet( const QString &name )
231 255
232 Returns a QIconSet for the pixmap named \a name. A disabled icon is 256 Returns a QIconSet for the pixmap named \a name. A disabled icon is
233 generated that conforms to the Qtopia look & feel. You should avoid 257 generated that conforms to the Qtopia look & feel. You should avoid
234 including any filename type extension (eg. .png, .xpm). 258 including any filename type extension (eg. .png, .xpm).
235*/ 259*/