summaryrefslogtreecommitdiffstats
path: root/puic/embed.cpp
blob: 148aa61896949787d9bddb130a009261a5e5d043 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/**********************************************************************
** Copyright (C) 2000 Trolltech AS.  All rights reserved.
** Copyright (c) 2001 Phil Thompson <[email protected]>
** Copyright (c) 2002 Riverbank Computing Limited <[email protected]>
** Copyright (c) 2002 Germain Garand <[email protected]>
**
** This file is part of TQt Designer.
**
** This file may be distributed and/or modified under the terms of the
** GNU General Public License version 2 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
** See http://www.trolltech.com/gpl/ for GPL licensing information.
**
** Contact [email protected] if any conditions of this licensing are
** not clear to you.
**
**********************************************************************/
/*
** 06/2002 : Initial release of puic, the PerlTQt User Interface Compiler,
**           a work derivated from uic (the TQt User Interface Compiler)
**           and pyuic (the PyTQt User Interface Compiler).
**
**           G.Garand
**
**********************************************************************/

#include "uic.h"
#include <qfile.h>
#include <qimage.h>
#include <qstringlist.h>
#include <qdatetime.h>
#include <qfileinfo.h>
#define NO_STATIC_COLORS
#include <globaldefs.h>
#include <qregexp.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

struct EmbedImage
{
    int width, height, depth;
    int numColors;
    TQRgb* colorTable;
    TQString name;
    TQString cname;
    bool alpha;
};

static TQString convertToCIdentifier( const char *s )
{
    TQString r = s;
    int len = r.length();
    if ( len > 0 && !isalpha( (char)r[0].latin1() ) )
	r[0] = '_';
    for ( int i=1; i<len; i++ ) {
	if ( !isalnum( (char)r[i].latin1() ) )
	    r[i] = '_';
    }
    return r;
}


static void embedData( TQTextStream& out, const uchar* input, int nbytes )
{
    static const char hexdigits[] = "0123456789abcdef";
    TQString s;
    for ( int i=0; i<nbytes; i++ )
    {
	if ( (i%14) == 0 )
        {
            s += "\n    ";
	    out << (const char*)s;
	    s.truncate( 0 );
	}
	uint v = input[i];
        	s += "0x";
	s += hexdigits[(v >> 4) & 15];
	s += hexdigits[v & 15];
	if ( i < nbytes-1 )
	    s += ", ";
	else
	    s += "\n";
    }
    if ( s.length() )
	out << (const char*)s;
}

static void embedData( TQTextStream& out, const TQRgb* input, int n )
{
    out << hex;
    const TQRgb *v = input;
    for ( int i=0; i<n; i++ ) {
	if ( (i%6) == 0  )
	    out << endl << "    ";
	out << "0x";
	out << hex << *v++;
	if ( i < n-1 )
	    out << ", ";
	else
	    out << ";" << endl;
    }
    out << dec; // back to decimal mode
}

void Uic::embed( TQTextStream& out, const char* project, const TQStringList& images )
{

    TQString cProject = convertToCIdentifier( project );

    TQStringList::ConstIterator it;
    out << "# Image collection for project '" << project << "'." << endl;
    out << "#" << endl;
    out << "# Generated from reading image files: " << endl;
    for ( it = images.begin(); it != images.end(); ++it )
	out << "#      " << *it << endl;
    out << "#" << endl;
    out << "# Created: " << TQDateTime::currentDateTime().toString() << endl;
    out << "#      by: The PerlTQt User Interface Compiler (puic)" << endl;
    out << "#" << endl;
    out << "# WARNING! All changes made in this file will be lost!" << endl;
    out << endl;
    out << "use strict;" << endl;
    out << "use utf8;"   << endl;
    out << endl;

    out << indent << "package DesignerMimeSourceFactory_" << cProject << ";" << endl;
    out << indent << "use TQt;" << endl;
    out << indent << "use TQt::isa qw(TQt::MimeSourceFactory);" << endl;
    out << endl;

    TQPtrList<EmbedImage> list_image;
    int image_count = 0;
    for ( it = images.begin(); it != images.end(); ++it ) {
	TQImage img;
	if ( !img.load( *it  ) ) {
	    fprintf( stderr, "puic: cannot load image file %s\n", (*it).latin1() );
	    continue;
	}
	EmbedImage *e = new EmbedImage;
	e->width = img.width();
	e->height = img.height();
	e->depth = img.depth();
	e->numColors = img.numColors();
	e->colorTable = new TQRgb[e->numColors];
	e->alpha = img.hasAlphaBuffer();
	memcpy(e->colorTable, img.colorTable(), e->numColors*sizeof(TQRgb));
	TQFileInfo fi( *it );
	e->name = fi.fileName();
	e->cname = TQString("$image_%1").arg( image_count++);
	list_image.append( e );
	out << "# " << *it << endl;
	TQString s;
        TQString imgname = (const char *)e->cname;


//my $i0 = TQt::Image($image_0_data, 22, 22, 32, undef, &TQt::Image::BigEndian);
//$i0->setAlphaBuffer(1);
//my $image0 = TQt::Pixmap($i0);

	if ( e->depth == 32 ) {
	    out << indent << "my " << imgname << "_data = pack 'L*'," << endl;
	    embedData( out, (TQRgb*)img.bits(), e->width*e->height );
	} else {
	    if ( e->depth == 1 )
		img = img.convertBitOrder(TQImage::BigEndian);
	    out << indent << "my " << imgname << "_data = pack 'C*'," << endl;
	    embedData( out, img.bits(), img.numBytes() );
	}
        out << endl;
	if ( e->numColors ) {
	    out << indent << "my " << imgname << "_ctable = " << endl;
	    out << indent << "[" << endl;
	    embedData( out, e->colorTable, e->numColors );
            out << endl;
            out << indent << "];" << endl;
	}
    }

    if ( !list_image.isEmpty() ) {
         out << indent << "my %embed_images = (\n";
	++indent;
	EmbedImage *e = list_image.first();
	while ( e )
        {
	    out << indent << "\"" << e->name << "\"" << " => [" << e->cname << "_data, "
                << e->width << ", " << e->height << ", " << e->depth << ", "
                << (e->numColors ? e->cname + "_ctable" : TQString::fromLatin1("undef") ) << ", "
                << (e->alpha ? "1" : "0") << "]," << endl;
	    e = list_image.next();
	}
	--indent;
	out << indent << ");" << endl;

	out << endl;
	out << indent << "my %images = ();" << endl;
	out << endl;
	out << endl;
	out << indent << "sub uic_findImage" << endl;
	out << indent << "{" << endl;
	++indent;
        out << indent << "my $name = shift;" << endl;
	out << indent << "return $images{$name} if exists $images{$name};" << endl;
        out << indent << "return TQt::Image() unless exists $embed_images{$name};" << endl;
        out << indent << endl;
	out << indent << "my $img = TQt::Image(@{$embed_images{$name}}[0..4], &TQt::Image::BigEndian);" << endl;
        out << indent << "${$embed_images{$name}}[5] && $img->setAlphaBuffer(1);" << endl;
	out << indent << "$images{$name} = $img;" << endl;
	out << indent << "return $img;" << endl;
	--indent;
	out << indent << "}" << endl;
	out << endl;
	out << indent << "sub data" << endl;
	out << indent << "{" << endl;
	++indent;
        out << indent << "my $abs_name = shift;" << endl;
	out << indent << "my $img = uic_findImage($abs_name);" << endl;
	out << indent << "if($img->isNull())" << endl;
	out << indent << "{" << endl;
	++indent;
	out << indent << "TQt::MimeSourceFactory::removeFactory(this);" << endl;
	out << indent << "my $s = TQt::MimeSourceFactory::defaultFactory()->data($abs_name);" << endl;
	out << indent << "TQt::MimeSourceFactory::addFactory(this);" << endl;
	out << indent << "return $s;" << endl;
	--indent;
	out << indent << "}" << endl;
	out << indent << "TQt::MimeSourceFactory::defaultFactory()->setImage($abs_name, $img);" << endl;
	out << indent << "return TQt::MimeSourceFactory::defaultFactory()->data($abs_name);" << endl;
	--indent;
	out << indent << "}" << endl;

	out << endl;
	out << endl;

	out << indent << "package staticImages;" << endl;
        out << indent << "use TQt;" << endl;
	out << indent << "use DesignerMimeSourceFactory_" << cProject << ";" << endl;
        out << indent << "our %factories;" << endl;
        out << indent << endl;
        out << indent << "my $factory = DesignerMimeSourceFactory_" << cProject << ";" << endl;
        out << indent << "TQt::MimeSourceFactory::defaultFactory()->addFactory($factory);" << endl;
        out << indent << "$factories{'DesignerMimeSourceFactory_" << cProject << "'} = $factory;" << endl;
	out << endl;
	out << indent << "END" << endl;
        out << indent << "{" << endl;
        ++indent;
        out << indent << "for( values %factories )" << endl;
        out << indent << "{" << endl;
        ++indent;
        out << indent << "TQt::MimeSourceFactory::defaultFactory()->removeFactory($_);" << endl;
        --indent;
        out << indent << "}" << endl;
        out << indent << "%factories = ();" << endl;
        --indent;
        out << indent << "}" << endl;
        out << indent << "1;" << endl;;
	out << endl;
    }
}