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