-rw-r--r-- | library/resource.cpp | 33 |
1 files changed, 29 insertions, 4 deletions
diff --git a/library/resource.cpp b/library/resource.cpp index cfa0d26..43fdc60 100644 --- a/library/resource.cpp +++ b/library/resource.cpp | |||
@@ -1,230 +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 | namespace { | 32 | namespace { |
33 | #include "inlinepics_p.h" | 33 | #include "inlinepics_p.h" |
34 | } | 34 | } |
35 | 35 | ||
36 | static bool g_notUseSet = ::getenv("OVERWRITE_ICON_SET"); | 36 | static bool g_notUseSet = ::getenv("OVERWRITE_ICON_SET"); |
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 | QPixmap Resource::loadPixmap( const QString &pix ) | 62 | QPixmap Resource::loadPixmap( const QString &pix ) |
63 | { | 63 | { |
64 | QPixmap pm; | 64 | QPixmap pm; |
65 | QString key="QPE_"+pix; | 65 | |
66 | if ( !QPixmapCache::find(key,pm) ) { | 66 | // Give the pixmaps some kind of namespace in the pixmapcache |
67 | pm.convertFromImage(loadImage(pix)); | 67 | int index = pix.find('/'); |
68 | QPixmapCache::insert(key,pm); | 68 | QString appName1 = qApp->argv()[0]; |
69 | appName1 = appName1.replace(QRegExp(".*/"),""); | ||
70 | QString appName2 = pix.left(index); | ||
71 | |||
72 | if ( appName2 == "" || appName2 == pix || appName2 == "icons" ) | ||
73 | appName2 = "Global"; | ||
74 | |||
75 | QString appKey1 = "_QPE_" + appName1 + "_" + pix; | ||
76 | QString appKey2 = "_QPE_" + appName2 + "_" + pix.mid(index+1); | ||
77 | |||
78 | if ( !QPixmapCache::find(appKey1, pm) ) { | ||
79 | if ( !QPixmapCache::find(appKey2, pm) ) { | ||
80 | QImage img; | ||
81 | QString f = findPixmap( pix ); | ||
82 | if ( !f.isEmpty() ) { | ||
83 | img.load(f); | ||
84 | if ( !img.isNull() ) { | ||
85 | pm.convertFromImage(img); | ||
86 | if ( f.contains(appName1) ) { | ||
87 | QPixmapCache::insert( appKey1, pm); | ||
88 | } else { | ||
89 | QPixmapCache::insert( appKey2, pm); | ||
90 | } | ||
91 | } | ||
92 | } | ||
93 | } | ||
69 | } | 94 | } |
70 | return pm; | 95 | return pm; |
71 | } | 96 | } |
72 | 97 | ||
73 | /*! | 98 | /*! |
74 | Returns the QBitmap called \a pix. You should avoid including | 99 | Returns the QBitmap called \a pix. You should avoid including |
75 | any filename type extension (e.g. .png, .xpm). | 100 | any filename type extension (e.g. .png, .xpm). |
76 | */ | 101 | */ |
77 | QBitmap Resource::loadBitmap( const QString &pix ) | 102 | QBitmap Resource::loadBitmap( const QString &pix ) |
78 | { | 103 | { |
79 | QBitmap bm; | 104 | QBitmap bm; |
80 | bm = loadPixmap(pix); | 105 | bm = loadPixmap(pix); |
81 | return bm; | 106 | return bm; |
82 | } | 107 | } |
83 | 108 | ||
84 | /*! | 109 | /*! |
85 | Returns the filename of a pixmap called \a pix. You should avoid including | 110 | Returns the filename of a pixmap called \a pix. You should avoid including |
86 | any filename type extension (e.g. .png, .xpm). | 111 | any filename type extension (e.g. .png, .xpm). |
87 | 112 | ||
88 | Normally you will use loadPixmap() rather than this function. | 113 | Normally you will use loadPixmap() rather than this function. |
89 | */ | 114 | */ |
90 | QString Resource::findPixmap( const QString &pix ) | 115 | QString Resource::findPixmap( const QString &pix ) |
91 | { | 116 | { |
92 | QString picsPath = QPEApplication::qpeDir() + "pics/"; | 117 | QString picsPath = QPEApplication::qpeDir() + "pics/"; |
93 | 118 | ||
94 | QString f; | 119 | QString f; |
95 | 120 | ||
96 | // Common case optimizations... | 121 | // Common case optimizations... |
97 | f = picsPath + pix + ".png"; | 122 | f = picsPath + pix + ".png"; |
98 | if ( QFile( f ).exists() ) | 123 | if ( QFile( f ).exists() ) |
99 | return f; | 124 | return f; |
100 | f = picsPath + pix + ".xpm"; | 125 | f = picsPath + pix + ".xpm"; |
101 | if ( QFile( f ).exists() ) | 126 | if ( QFile( f ).exists() ) |
102 | return f; | 127 | return f; |
103 | 128 | ||
104 | 129 | ||
105 | // All formats... | 130 | // All formats... |
106 | QStrList fileFormats = QImageIO::inputFormats(); | 131 | QStrList fileFormats = QImageIO::inputFormats(); |
107 | QString ff = fileFormats.first(); | 132 | QString ff = fileFormats.first(); |
108 | while ( fileFormats.current() ) { | 133 | while ( fileFormats.current() ) { |
109 | QStringList exts = MimeType("image/"+ff.lower()).extensions(); | 134 | QStringList exts = MimeType("image/"+ff.lower()).extensions(); |
110 | for ( QStringList::ConstIterator it = exts.begin(); it!=exts.end(); ++it ) { | 135 | for ( QStringList::ConstIterator it = exts.begin(); it!=exts.end(); ++it ) { |
111 | QString f = picsPath + pix + "." + *it; | 136 | QString f = picsPath + pix + "." + *it; |
112 | if ( QFile(f).exists() ) | 137 | if ( QFile(f).exists() ) |
113 | return f; | 138 | return f; |
114 | } | 139 | } |
115 | ff = fileFormats.next(); | 140 | ff = fileFormats.next(); |
116 | } | 141 | } |
117 | 142 | ||
118 | // Finally, no (or existing) extension... | 143 | // Finally, no (or existing) extension... |
119 | if ( QFile( picsPath + pix ).exists() ) | 144 | if ( QFile( picsPath + pix ).exists() ) |
120 | return picsPath + pix; | 145 | return picsPath + pix; |
121 | 146 | ||
122 | //qDebug("Cannot find pixmap: %s", pix.latin1()); | 147 | //qDebug("Cannot find pixmap: %s", pix.latin1()); |
123 | return QString(); | 148 | return QString(); |
124 | } | 149 | } |
125 | 150 | ||
126 | /*! | 151 | /*! |
127 | Returns a sound file for a sound called \a name. | 152 | Returns a sound file for a sound called \a name. |
128 | 153 | ||
129 | You should avoid including any filename type extension (e.g. .wav), | 154 | You should avoid including any filename type extension (e.g. .wav), |
130 | as the system will search for only those fileformats which are supported | 155 | as the system will search for only those fileformats which are supported |
131 | by the library. | 156 | by the library. |
132 | 157 | ||
133 | Currently, only WAV files are supported. | 158 | Currently, only WAV files are supported. |
134 | */ | 159 | */ |
135 | QString Resource::findSound( const QString &name ) | 160 | QString Resource::findSound( const QString &name ) |
136 | { | 161 | { |
137 | QString picsPath = QPEApplication::qpeDir() + "sounds/"; | 162 | QString picsPath = QPEApplication::qpeDir() + "sounds/"; |
138 | 163 | ||
139 | QString result; | 164 | QString result; |
140 | if ( QFile( (result = picsPath + name + ".wav") ).exists() ) | 165 | if ( QFile( (result = picsPath + name + ".wav") ).exists() ) |
141 | return result; | 166 | return result; |
142 | 167 | ||
143 | return QString(); | 168 | return QString(); |
144 | } | 169 | } |
145 | 170 | ||
146 | /*! | 171 | /*! |
147 | Returns a list of all sound names. | 172 | Returns a list of all sound names. |
148 | */ | 173 | */ |
149 | QStringList Resource::allSounds() | 174 | QStringList Resource::allSounds() |
150 | { | 175 | { |
151 | QDir resourcedir( QPEApplication::qpeDir() + "sounds/", "*.wav" ); | 176 | QDir resourcedir( QPEApplication::qpeDir() + "sounds/", "*.wav" ); |
152 | QStringList entries = resourcedir.entryList(); | 177 | QStringList entries = resourcedir.entryList(); |
153 | QStringList result; | 178 | QStringList result; |
154 | for (QStringList::Iterator i=entries.begin(); i != entries.end(); ++i) | 179 | for (QStringList::Iterator i=entries.begin(); i != entries.end(); ++i) |
155 | result.append((*i).replace(QRegExp("\\.wav"),"")); | 180 | result.append((*i).replace(QRegExp("\\.wav"),"")); |
156 | return result; | 181 | return result; |
157 | } | 182 | } |
158 | 183 | ||
159 | static QImage load_image(const QString &name) | 184 | static QImage load_image(const QString &name) |
160 | { | 185 | { |
161 | if (g_notUseSet ) { | 186 | if (g_notUseSet ) { |
162 | // try file | 187 | // try file |
163 | QImage img; | 188 | QImage img; |
164 | QString f = Resource::findPixmap(name); | 189 | QString f = Resource::findPixmap(name); |
165 | if ( !f.isEmpty() ) | 190 | if ( !f.isEmpty() ) |
166 | img.load(f); | 191 | img.load(f); |
167 | if (img.isNull() ) | 192 | if (img.isNull() ) |
168 | img = qembed_findImage(name.latin1() ); | 193 | img = qembed_findImage(name.latin1() ); |
169 | return img; | 194 | return img; |
170 | } | 195 | } |
171 | else{ | 196 | else{ |
172 | QImage img = qembed_findImage(name.latin1()); | 197 | QImage img = qembed_findImage(name.latin1()); |
173 | 198 | ||
174 | if ( img.isNull() ) { | 199 | if ( img.isNull() ) { |
175 | // No inlined image, try file | 200 | // No inlined image, try file |
176 | QString f = Resource::findPixmap(name); | 201 | QString f = Resource::findPixmap(name); |
177 | if ( !f.isEmpty() ) | 202 | if ( !f.isEmpty() ) |
178 | img.load(f); | 203 | img.load(f); |
179 | } | 204 | } |
180 | return img; | 205 | return img; |
181 | } | 206 | } |
182 | } | 207 | } |
183 | 208 | ||
184 | /*! | 209 | /*! |
185 | Returns the QImage called \a name. You should avoid including | 210 | Returns the QImage called \a name. You should avoid including |
186 | any filename type extension (e.g. .png, .xpm). | 211 | any filename type extension (e.g. .png, .xpm). |
187 | */ | 212 | */ |
188 | QImage Resource::loadImage( const QString &name) | 213 | QImage Resource::loadImage( const QString &name) |
189 | { | 214 | { |
190 | #ifndef QT_NO_DEPTH_32// have alpha-blended pixmaps | 215 | #ifndef QT_NO_DEPTH_32// have alpha-blended pixmaps |
191 | static QImage last_enabled; | 216 | static QImage last_enabled; |
192 | static QString last_enabled_name; | 217 | static QString last_enabled_name; |
193 | if ( name == last_enabled_name ) | 218 | if ( name == last_enabled_name ) |
194 | return last_enabled; | 219 | return last_enabled; |
195 | #endif | 220 | #endif |
196 | QImage img = load_image(name); | 221 | QImage img = load_image(name); |
197 | #ifndef QT_NO_DEPTH_32// have alpha-blended pixmaps | 222 | #ifndef QT_NO_DEPTH_32// have alpha-blended pixmaps |
198 | if ( img.isNull() ) { | 223 | if ( img.isNull() ) { |
199 | // No file, try generating | 224 | // No file, try generating |
200 | if ( name[name.length()-1]=='d' && name.right(9)=="_disabled" ) { | 225 | if ( name[name.length()-1]=='d' && name.right(9)=="_disabled" ) { |
201 | last_enabled_name = name.left(name.length()-9); | 226 | last_enabled_name = name.left(name.length()-9); |
202 | last_enabled = load_image(last_enabled_name); | 227 | last_enabled = load_image(last_enabled_name); |
203 | if ( last_enabled.isNull() ) { | 228 | if ( last_enabled.isNull() ) { |
204 | last_enabled_name = QString::null; | 229 | last_enabled_name = QString::null; |
205 | } else { | 230 | } else { |
206 | img.detach(); | 231 | img.detach(); |
207 | img.create( last_enabled.width(), last_enabled.height(), 32 ); | 232 | img.create( last_enabled.width(), last_enabled.height(), 32 ); |
208 | for ( int y = 0; y < img.height(); y++ ) { | 233 | for ( int y = 0; y < img.height(); y++ ) { |
209 | for ( int x = 0; x < img.width(); x++ ) { | 234 | for ( int x = 0; x < img.width(); x++ ) { |
210 | QRgb p = last_enabled.pixel( x, y ); | 235 | QRgb p = last_enabled.pixel( x, y ); |
211 | int a = qAlpha(p)/3; | 236 | int a = qAlpha(p)/3; |
212 | int g = qGray(qRed(p),qGreen(p),qBlue(p)); | 237 | int g = qGray(qRed(p),qGreen(p),qBlue(p)); |
213 | img.setPixel( x, y, qRgba(g,g,g,a) ); | 238 | img.setPixel( x, y, qRgba(g,g,g,a) ); |
214 | } | 239 | } |
215 | } | 240 | } |
216 | img.setAlphaBuffer( TRUE ); | 241 | img.setAlphaBuffer( TRUE ); |
217 | } | 242 | } |
218 | } | 243 | } |
219 | } | 244 | } |
220 | #endif | 245 | #endif |
221 | return img; | 246 | return img; |
222 | } | 247 | } |
223 | 248 | ||
224 | /*! | 249 | /*! |
225 | \fn QIconSet Resource::loadIconSet( const QString &name ) | 250 | \fn QIconSet Resource::loadIconSet( const QString &name ) |
226 | 251 | ||
227 | 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 |
228 | generated that conforms to the Qtopia look & feel. You should avoid | 253 | generated that conforms to the Qtopia look & feel. You should avoid |
229 | including any filename type extension (eg. .png, .xpm). | 254 | including any filename type extension (eg. .png, .xpm). |
230 | */ | 255 | */ |