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