source: trunk/tools/designer/uic/embed.cpp@ 9

Last change on this file since 9 was 2, checked in by dmik, 20 years ago

Imported xplatform parts of the official release 3.3.1 from Trolltech

  • Property svn:keywords set to Id
File size: 9.7 KB
Line 
1/**********************************************************************
2** Copyright (C) 2000-2003 Trolltech AS. All rights reserved.
3**
4** This file is part of Qt Designer.
5**
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
8** Foundation and appearing in the file LICENSE.GPL included in the
9** packaging of this file.
10**
11** Licensees holding valid Qt Enterprise Edition or Qt Professional Edition
12** licenses may use this file in accordance with the Qt Commercial License
13** Agreement provided with the Software.
14**
15** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
17**
18** See http://www.trolltech.com/gpl/ for GPL licensing information.
19** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
20** information about Qt Commercial License Agreements.
21**
22** Contact info@trolltech.com if any conditions of this licensing are
23** not clear to you.
24**
25**********************************************************************/
26
27#include "uic.h"
28#include <qfile.h>
29#include <qimage.h>
30#include <qstringlist.h>
31#include <qdatetime.h>
32#include <qfileinfo.h>
33#include <stdio.h>
34#include <ctype.h>
35
36// on embedded, we do not compress image data. Rationale: by mapping
37// the ready-only data directly into memory we are both faster and
38// more memory efficient
39#if defined(Q_WS_QWS) && !defined(QT_NO_IMAGE_COLLECTION_COMPRESSION)
40#define QT_NO_IMAGE_COLLECTION_COMPRESSION
41#endif
42
43struct EmbedImage
44{
45 ~EmbedImage() { delete[] colorTable; }
46 int width, height, depth;
47 int numColors;
48 QRgb* colorTable;
49 QString name;
50 QString cname;
51 bool alpha;
52#ifndef QT_NO_IMAGE_COLLECTION_COMPRESSION
53 ulong compressed;
54#endif
55};
56
57static QString convertToCIdentifier( const char *s )
58{
59 QString r = s;
60 int len = r.length();
61 if ( len > 0 && !isalpha( (char)r[0].latin1() ) )
62 r[0] = '_';
63 for ( int i=1; i<len; i++ ) {
64 if ( !isalnum( (char)r[i].latin1() ) )
65 r[i] = '_';
66 }
67 return r;
68}
69
70
71static ulong embedData( QTextStream& out, const uchar* input, int nbytes )
72{
73#ifndef QT_NO_IMAGE_COLLECTION_COMPRESSION
74 QByteArray bazip( qCompress( input, nbytes ) );
75 ulong len = bazip.size();
76#else
77 ulong len = nbytes;
78#endif
79 static const char hexdigits[] = "0123456789abcdef";
80 QString s;
81 for ( int i=0; i<(int)len; i++ ) {
82 if ( (i%14) == 0 ) {
83 s += "\n ";
84 out << (const char*)s;
85 s.truncate( 0 );
86 }
87 uint v = (uchar)
88#ifndef QT_NO_IMAGE_COLLECTION_COMPRESSION
89 bazip
90#else
91 input
92#endif
93 [i];
94 s += "0x";
95 s += hexdigits[(v >> 4) & 15];
96 s += hexdigits[v & 15];
97 if ( i < (int)len-1 )
98 s += ',';
99 }
100 if ( s.length() )
101 out << (const char*)s;
102 return len;
103}
104
105static void embedData( QTextStream& out, const QRgb* input, int n )
106{
107 out << hex;
108 const QRgb *v = input;
109 for ( int i=0; i<n; i++ ) {
110 if ( (i%14) == 0 )
111 out << "\n ";
112 out << "0x";
113 out << hex << *v++;
114 if ( i < n-1 )
115 out << ',';
116 }
117 out << dec; // back to decimal mode
118}
119
120void Uic::embed( QTextStream& out, const char* project, const QStringList& images )
121{
122
123 QString cProject = convertToCIdentifier( project );
124
125 QStringList::ConstIterator it;
126 out << "/****************************************************************************\n";
127 out << "** Image collection for project '" << project << "'.\n";
128 out << "**\n";
129 out << "** Generated from reading image files: \n";
130 for ( it = images.begin(); it != images.end(); ++it )
131 out << "** " << *it << "\n";
132 out << "**\n";
133 out << "** Created: " << QDateTime::currentDateTime().toString() << "\n";
134 out << "** by: The User Interface Compiler ($Id: embed.cpp 2 2005-11-16 15:49:26Z dmik $)\n";
135 out << "**\n";
136 out << "** WARNING! All changes made in this file will be lost!\n";
137 out << "****************************************************************************/\n";
138 out << "\n";
139
140 out << "#include <qimage.h>\n";
141 out << "#include <qdict.h>\n";
142 out << "#include <qmime.h>\n";
143 out << "#include <qdragobject.h>\n";
144 out << "\n";
145
146 QPtrList<EmbedImage> list_image;
147 list_image.setAutoDelete( TRUE );
148 int image_count = 0;
149 for ( it = images.begin(); it != images.end(); ++it ) {
150 QImage img;
151 if ( !img.load( *it ) ) {
152 fprintf( stderr, "uic: cannot load image file %s\n", (*it).latin1() );
153 continue;
154 }
155 EmbedImage *e = new EmbedImage;
156 e->width = img.width();
157 e->height = img.height();
158 e->depth = img.depth();
159 e->numColors = img.numColors();
160 e->colorTable = new QRgb[e->numColors];
161 e->alpha = img.hasAlphaBuffer();
162 memcpy(e->colorTable, img.colorTable(), e->numColors*sizeof(QRgb));
163 QFileInfo fi( *it );
164 e->name = fi.fileName();
165 e->cname = QString("image_%1").arg( image_count++);
166 list_image.append( e );
167 out << "// " << *it << "\n";
168 QString s;
169 if ( e->depth == 1 )
170 img = img.convertBitOrder(QImage::BigEndian);
171 out << s.sprintf( "static const unsigned char %s_data[] = {",
172 (const char *)e->cname );
173#ifndef QT_NO_IMAGE_COLLECTION_COMPRESSION
174 e->compressed =
175#endif
176 embedData( out, img.bits(), img.numBytes() );
177 out << "\n};\n\n";
178 if ( e->numColors ) {
179 out << s.sprintf( "static const QRgb %s_ctable[] = {",
180 (const char *)e->cname );
181 embedData( out, e->colorTable, e->numColors );
182 out << "\n};\n\n";
183 }
184 }
185
186 if ( !list_image.isEmpty() ) {
187 out << "static struct EmbedImage {\n"
188 " int width, height, depth;\n"
189 " const unsigned char *data;\n"
190#ifndef QT_NO_IMAGE_COLLECTION_COMPRESSION
191 " ulong compressed;\n"
192#endif
193 " int numColors;\n"
194 " const QRgb *colorTable;\n"
195 " bool alpha;\n"
196 " const char *name;\n"
197 "} embed_image_vec[] = {\n";
198 EmbedImage *e = list_image.first();
199 while ( e ) {
200 out << " { "
201 << e->width << ", "
202 << e->height << ", "
203 << e->depth << ", "
204 << "(const unsigned char*)" << e->cname << "_data, "
205#ifndef QT_NO_IMAGE_COLLECTION_COMPRESSION
206 << e->compressed << ", "
207#endif
208 << e->numColors << ", ";
209 if ( e->numColors )
210 out << e->cname << "_ctable, ";
211 else
212 out << "0, ";
213 if ( e->alpha )
214 out << "TRUE, ";
215 else
216 out << "FALSE, ";
217 out << "\"" << e->name << "\" },\n";
218 e = list_image.next();
219 }
220#ifndef QT_NO_IMAGE_COLLECTION_COMPRESSION
221 out << " { 0, 0, 0, 0, 0, 0, 0, 0, 0 }\n};\n";
222#else
223 out << " { 0, 0, 0, 0, 0, 0, 0, 0 }\n};\n";
224#endif
225
226 out << "\n"
227 "static QImage uic_findImage( const QString& name )\n"
228 "{\n"
229 " for ( int i=0; embed_image_vec[i].data; i++ ) {\n"
230 " if ( QString::fromUtf8(embed_image_vec[i].name) == name ) {\n"
231#ifndef QT_NO_IMAGE_COLLECTION_COMPRESSION
232 " QByteArray baunzip;\n"
233 " baunzip = qUncompress( embed_image_vec[i].data, \n"
234 " embed_image_vec[i].compressed );\n"
235 " QImage img((uchar*)baunzip.data(),\n"
236 " embed_image_vec[i].width,\n"
237 " embed_image_vec[i].height,\n"
238 " embed_image_vec[i].depth,\n"
239 " (QRgb*)embed_image_vec[i].colorTable,\n"
240 " embed_image_vec[i].numColors,\n"
241 " QImage::BigEndian\n"
242 " );\n"
243 " img = img.copy();\n"
244#else
245 " QImage img((uchar*)embed_image_vec[i].data,\n"
246 " embed_image_vec[i].width,\n"
247 " embed_image_vec[i].height,\n"
248 " embed_image_vec[i].depth,\n"
249 " (QRgb*)embed_image_vec[i].colorTable,\n"
250 " embed_image_vec[i].numColors,\n"
251 " QImage::BigEndian\n"
252 " );\n"
253#endif
254 " if ( embed_image_vec[i].alpha )\n"
255 " img.setAlphaBuffer(TRUE);\n"
256 " return img;\n"
257 " }\n"
258 " }\n"
259 " return QImage();\n"
260 "}\n\n";
261
262 out << "class MimeSourceFactory_" << cProject << " : public QMimeSourceFactory\n";
263 out << "{\n";
264 out << "public:\n";
265 out << " MimeSourceFactory_" << cProject << "() {}\n";
266 out << " ~MimeSourceFactory_" << cProject << "() {}\n";
267 out << " const QMimeSource* data( const QString& abs_name ) const {\n";
268 out << "\tconst QMimeSource* d = QMimeSourceFactory::data( abs_name );\n";
269 out << "\tif ( d || abs_name.isNull() ) return d;\n";
270 out << "\tQImage img = uic_findImage( abs_name );\n";
271 out << "\tif ( !img.isNull() )\n";
272 out << "\t ((QMimeSourceFactory*)this)->setImage( abs_name, img );\n";
273 out << "\treturn QMimeSourceFactory::data( abs_name );\n";
274 out << " };\n";
275 out << "};\n\n";
276
277 out << "static QMimeSourceFactory* factory = 0;\n";
278 out << "\n";
279
280 out << "void qInitImages_" << cProject << "()\n";
281 out << "{\n";
282 out << " if ( !factory ) {\n";
283 out << "\tfactory = new MimeSourceFactory_" << cProject << ";\n";
284 out << "\tQMimeSourceFactory::defaultFactory()->addFactory( factory );\n";
285 out << " }\n";
286 out << "}\n\n";
287
288 out << "void qCleanupImages_" << cProject << "()\n";
289 out << "{\n";
290 out << " if ( factory ) {\n";
291 out << "\tQMimeSourceFactory::defaultFactory()->removeFactory( factory );\n";
292 out << "\tdelete factory;\n";
293 out << "\tfactory = 0;\n";
294 out << " }\n";
295 out << "}\n\n";
296
297 out << "class StaticInitImages_" << cProject << "\n";
298 out << "{\n";
299 out << "public:\n";
300 out << " StaticInitImages_" << cProject << "() { qInitImages_" << cProject << "(); }\n";
301 out << "#if defined(Q_OS_SCO) || defined(Q_OS_UNIXWARE)\n";
302 out << " ~StaticInitImages_" << cProject << "() { }\n";
303 out << "#else\n";
304 out << " ~StaticInitImages_" << cProject << "() { qCleanupImages_" << cProject << "(); }\n";
305 out << "#endif\n";
306 out << "};\n\n";
307
308 out << "static StaticInitImages_" << cProject << " staticImages;\n";
309 }
310}
Note: See TracBrowser for help on using the repository browser.