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