summaryrefslogtreecommitdiff
path: root/library
Unidiff
Diffstat (limited to 'library') (more/less context) (ignore whitespace changes)
-rw-r--r--library/resource.cpp49
1 files changed, 39 insertions, 10 deletions
diff --git a/library/resource.cpp b/library/resource.cpp
index a093e2f..18139b9 100644
--- a/library/resource.cpp
+++ b/library/resource.cpp
@@ -1,226 +1,255 @@
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 32#ifndef LIBQPE_NO_INLINE_IMAGES
33namespace { 33namespace {
34#include "inlinepics_p.h" 34#include "inlinepics_p.h"
35} 35}
36#endif 36#endif
37 37
38/*! 38/*!
39 \class Resource resource.h 39 \class Resource resource.h
40 \brief The Resource class provides access to named resources. 40 \brief The Resource class provides access to named resources.
41 41
42 The resources may be provided from files or other sources. 42 The resources may be provided from files or other sources.
43 43
44 The allSounds() function returns a list of all the sounds available. 44 The allSounds() function returns a list of all the sounds available.
45 A particular sound can be searched for using findSound(). 45 A particular sound can be searched for using findSound().
46 46
47 Images can be loaded with loadImage(), loadPixmap(), loadBitmap() 47 Images can be loaded with loadImage(), loadPixmap(), loadBitmap()
48 and loadIconSet(). 48 and loadIconSet().
49 49
50 \ingroup qtopiaemb 50 \ingroup qtopiaemb
51*/ 51*/
52 52
53/*! 53/*!
54 \fn Resource::Resource() 54 \fn Resource::Resource()
55 \internal 55 \internal
56*/ 56*/
57 57
58/*! 58/*!
59 Returns the QPixmap called \a pix. You should avoid including 59 Returns the QPixmap called \a pix. You should avoid including
60 any filename type extension (e.g. .png, .xpm). 60 any filename type extension (e.g. .png, .xpm).
61*/ 61*/
62#include <stdio.h> 62#include <stdio.h>
63QPixmap Resource::loadPixmap( const QString &pix ) 63QPixmap Resource::loadPixmap( const QString &pix )
64{ 64{
65 QPixmap pm; // null pixmap 65 QPixmap pm; // null pixmap
66 QString key="QPE_"+pix; 66 QString key="QPE_"+pix;
67 if ( !QPixmapCache::find(key,pm) ) { 67 if ( !QPixmapCache::find(key,pm) ) {
68 QImage I = loadImage(pix); 68 QImage I = loadImage(pix);
69 if( I.isNull() ) { 69 if( I.isNull() ) {
70 qWarning( "Could not load %s", pix.latin1() ); 70 qWarning( "Could not load %s", pix.latin1() );
71 } else { 71 } else {
72 pm.convertFromImage(I); 72 pm.convertFromImage(I);
73 QPixmapCache::insert(key,pm); 73 QPixmapCache::insert(key,pm);
74 } 74 }
75 } 75 }
76 return pm; 76 return pm;
77} 77}
78 78
79/*! 79/*!
80 Returns the QBitmap called \a pix. You should avoid including 80 Returns the QBitmap called \a pix. You should avoid including
81 any filename type extension (e.g. .png, .xpm). 81 any filename type extension (e.g. .png, .xpm).
82*/ 82*/
83QBitmap Resource::loadBitmap( const QString &pix ) 83QBitmap Resource::loadBitmap( const QString &pix )
84{ 84{
85 QBitmap bm; 85 QBitmap bm;
86 bm = loadPixmap(pix); 86 bm = loadPixmap(pix);
87 return bm; 87 return bm;
88} 88}
89 89
90/*
91 * @internal
92 * Parse the extensions only once. If the MimeType mapping
93 * changes we will still use the old extensions, applications
94 * will need to be restarted to be aware of new extensions...
95 * For now it seems ok to have that limitation, if that is a wrong
96 * assumption we will need to invalidate this list
97 */
98QStringList *opie_image_extension_List = 0;
99static void clean_opie_image_extension_List() {
100 delete opie_image_extension_List;
101 opie_image_extension_List = 0;
102}
103
104QStringList opie_imageExtensions() {
105 /*
106 * File extensions (e.g jpeg JPG jpg) are not
107 * parsed yet
108 */
109 if ( !opie_image_extension_List ) {
110 opie_image_extension_List = new QStringList();
111 qAddPostRoutine( clean_opie_image_extension_List );
112
113 QStrList fileFormats = QImageIO::inputFormats();
114 QString ff = fileFormats.first();
115 while ( fileFormats.current() ) {
116 *opie_image_extension_List += MimeType("image/"+ff.lower()).extensions();
117 ff = fileFormats.next();
118 }
119 }
120
121 return *opie_image_extension_List; // QShared so it should be efficient
122}
123
90/*! 124/*!
91 Returns the filename of a pixmap called \a pix. You should avoid including 125 Returns the filename of a pixmap called \a pix. You should avoid including
92 any filename type extension (e.g. .png, .xpm). 126 any filename type extension (e.g. .png, .xpm).
93 127
94 Normally you will use loadPixmap() rather than this function. 128 Normally you will use loadPixmap() rather than this function.
95*/ 129*/
96QString Resource::findPixmap( const QString &pix ) 130QString Resource::findPixmap( const QString &pix )
97{ 131{
98 QString picsPath = QPEApplication::qpeDir() + "pics/"; 132 QString picsPath = QPEApplication::qpeDir() + "pics/";
99 QString f; 133 QString f;
100 134
101 // Common case optimizations... 135 // Common case optimizations...
102 f = picsPath + pix + ".png"; 136 f = picsPath + pix + ".png";
103 if ( QFile( f ).exists() ) 137 if ( QFile( f ).exists() )
104 return f; 138 return f;
105 f = picsPath + pix + ".xpm"; 139 f = picsPath + pix + ".xpm";
106 if ( QFile( f ).exists() ) 140 if ( QFile( f ).exists() )
107 return f; 141 return f;
108 142
109 // All formats... 143 // All formats...
110 QStrList fileFormats = QImageIO::inputFormats(); 144 QStringList exts = opie_imageExtensions();
111 QString ff = fileFormats.first(); 145 for ( QStringList::ConstIterator it = exts.begin(); it!=exts.end(); ++it ) {
112 while ( fileFormats.current() ) { 146 QString f = picsPath + pix + "." + *it;
113 QStringList exts = MimeType("image/"+ff.lower()).extensions(); 147 if ( QFile(f).exists() )
114 for ( QStringList::ConstIterator it = exts.begin(); it!=exts.end(); ++it ) { 148 return f;
115 QString f = picsPath + pix + "." + *it;
116 if ( QFile(f).exists() )
117 return f;
118 }
119 ff = fileFormats.next();
120 } 149 }
121 150
122 // Finally, no (or existing) extension... 151 // Finally, no (or existing) extension...
123 if ( QFile( picsPath + pix ).exists() ) 152 if ( QFile( picsPath + pix ).exists() )
124 return picsPath + pix; 153 return picsPath + pix;
125 154
126 //qDebug("Cannot find pixmap: %s", pix.latin1()); 155 //qDebug("Cannot find pixmap: %s", pix.latin1());
127 return QString(); 156 return QString();
128} 157}
129 158
130/*! 159/*!
131 Returns a sound file for a sound called \a name. 160 Returns a sound file for a sound called \a name.
132 161
133 You should avoid including any filename type extension (e.g. .wav), 162 You should avoid including any filename type extension (e.g. .wav),
134 as the system will search for only those fileformats which are supported 163 as the system will search for only those fileformats which are supported
135 by the library. 164 by the library.
136 165
137 Currently, only WAV files are supported. 166 Currently, only WAV files are supported.
138*/ 167*/
139QString Resource::findSound( const QString &name ) 168QString Resource::findSound( const QString &name )
140{ 169{
141 QString picsPath = QPEApplication::qpeDir() + "sounds/"; 170 QString picsPath = QPEApplication::qpeDir() + "sounds/";
142 171
143 QString result; 172 QString result;
144 if ( QFile( (result = picsPath + name + ".wav") ).exists() ) 173 if ( QFile( (result = picsPath + name + ".wav") ).exists() )
145 return result; 174 return result;
146 175
147 return QString(); 176 return QString();
148} 177}
149 178
150/*! 179/*!
151 Returns a list of all sound names. 180 Returns a list of all sound names.
152*/ 181*/
153QStringList Resource::allSounds() 182QStringList Resource::allSounds()
154{ 183{
155 QDir resourcedir( QPEApplication::qpeDir() + "sounds/", "*.wav" ); 184 QDir resourcedir( QPEApplication::qpeDir() + "sounds/", "*.wav" );
156 QStringList entries = resourcedir.entryList(); 185 QStringList entries = resourcedir.entryList();
157 QStringList result; 186 QStringList result;
158 for (QStringList::Iterator i=entries.begin(); i != entries.end(); ++i) 187 for (QStringList::Iterator i=entries.begin(); i != entries.end(); ++i)
159 result.append((*i).replace(QRegExp("\\.wav"),"")); 188 result.append((*i).replace(QRegExp("\\.wav"),""));
160 return result; 189 return result;
161} 190}
162 191
163static QImage load_image(const QString &name) 192static QImage load_image(const QString &name)
164{ 193{
165 QImage img; 194 QImage img;
166 195
167#ifndef LIBQPE_NO_INLINE_IMAGES 196#ifndef LIBQPE_NO_INLINE_IMAGES
168 img = qembed_findImage(name.latin1()); 197 img = qembed_findImage(name.latin1());
169#endif 198#endif
170 if ( img.isNull() ) 199 if ( img.isNull() )
171 { 200 {
172 // No inlined image, try file 201 // No inlined image, try file
173 QString f = Resource::findPixmap(name); 202 QString f = Resource::findPixmap(name);
174 if ( !f.isEmpty() ) 203 if ( !f.isEmpty() )
175 img.load(f); 204 img.load(f);
176 } 205 }
177 return img; 206 return img;
178} 207}
179 208
180/*! 209/*!
181 Returns the QImage called \a name. You should avoid including 210 Returns the QImage called \a name. You should avoid including
182 any filename type extension (e.g. .png, .xpm). 211 any filename type extension (e.g. .png, .xpm).
183*/ 212*/
184QImage Resource::loadImage( const QString &name) 213QImage Resource::loadImage( const QString &name)
185{ 214{
186 #ifndef QT_NO_DEPTH_32// have alpha-blended pixmaps 215 #ifndef QT_NO_DEPTH_32// have alpha-blended pixmaps
187 static QImage last_enabled; 216 static QImage last_enabled;
188 static QString last_enabled_name; 217 static QString last_enabled_name;
189 if ( name == last_enabled_name ) 218 if ( name == last_enabled_name )
190 return last_enabled; 219 return last_enabled;
191#endif 220#endif
192 QImage img = load_image(name); 221 QImage img = load_image(name);
193 #ifndef QT_NO_DEPTH_32// have alpha-blended pixmaps 222 #ifndef QT_NO_DEPTH_32// have alpha-blended pixmaps
194 if ( img.isNull() ) { 223 if ( img.isNull() ) {
195 // No file, try generating 224 // No file, try generating
196 if ( name[name.length()-1]=='d' && name.right(9)=="_disabled" ) { 225 if ( name[name.length()-1]=='d' && name.right(9)=="_disabled" ) {
197 last_enabled_name = name.left(name.length()-9); 226 last_enabled_name = name.left(name.length()-9);
198 last_enabled = load_image(last_enabled_name); 227 last_enabled = load_image(last_enabled_name);
199 if ( last_enabled.isNull() ) { 228 if ( last_enabled.isNull() ) {
200 last_enabled_name = QString::null; 229 last_enabled_name = QString::null;
201 } else { 230 } else {
202 img.detach(); 231 img.detach();
203 img.create( last_enabled.width(), last_enabled.height(), 32 ); 232 img.create( last_enabled.width(), last_enabled.height(), 32 );
204 for ( int y = 0; y < img.height(); y++ ) { 233 for ( int y = 0; y < img.height(); y++ ) {
205 for ( int x = 0; x < img.width(); x++ ) { 234 for ( int x = 0; x < img.width(); x++ ) {
206 QRgb p = last_enabled.pixel( x, y ); 235 QRgb p = last_enabled.pixel( x, y );
207 int a = qAlpha(p)/3; 236 int a = qAlpha(p)/3;
208 int g = qGray(qRed(p),qGreen(p),qBlue(p)); 237 int g = qGray(qRed(p),qGreen(p),qBlue(p));
209 img.setPixel( x, y, qRgba(g,g,g,a) ); 238 img.setPixel( x, y, qRgba(g,g,g,a) );
210 } 239 }
211 } 240 }
212 img.setAlphaBuffer( TRUE ); 241 img.setAlphaBuffer( TRUE );
213 } 242 }
214 } 243 }
215 } 244 }
216#endif 245#endif
217 return img; 246 return img;
218} 247}
219 248
220/*! 249/*!
221 \fn QIconSet Resource::loadIconSet( const QString &name ) 250 \fn QIconSet Resource::loadIconSet( const QString &name )
222 251
223 Returns a QIconSet for the pixmap named \a name. A disabled icon is 252 Returns a QIconSet for the pixmap named \a name. A disabled icon is
224 generated that conforms to the Qtopia look & feel. You should avoid 253 generated that conforms to the Qtopia look & feel. You should avoid
225 including any filename type extension (eg. .png, .xpm). 254 including any filename type extension (eg. .png, .xpm).
226*/ 255*/