source: trunk/src/tools/uic/cpp/cppwriteicondata.cpp@ 5

Last change on this file since 5 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 5.6 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
5**
6** This file is part of the tools applications of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file. Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
23** In addition, as a special exception, Nokia gives you certain
24** additional rights. These rights are described in the Nokia Qt LGPL
25** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26** package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you are unsure which license is appropriate for your use, please
37** contact the sales department at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include "cppwriteicondata.h"
43#include "driver.h"
44#include "ui4.h"
45#include "uic.h"
46
47#include <QtCore/QTextStream>
48
49QT_BEGIN_NAMESPACE
50
51namespace CPP {
52
53static QByteArray transformImageData(QString data)
54{
55 int baSize = data.length() / 2;
56 uchar *ba = new uchar[baSize];
57 for (int i = 0; i < baSize; ++i) {
58 char h = data[2 * (i)].toLatin1();
59 char l = data[2 * (i) + 1].toLatin1();
60 uchar r = 0;
61 if (h <= '9')
62 r += h - '0';
63 else
64 r += h - 'a' + 10;
65 r = r << 4;
66 if (l <= '9')
67 r += l - '0';
68 else
69 r += l - 'a' + 10;
70 ba[i] = r;
71 }
72 QByteArray ret(reinterpret_cast<const char *>(ba), baSize);
73 delete [] ba;
74 return ret;
75}
76
77static QByteArray unzipXPM(QString data, ulong& length)
78{
79#ifndef QT_NO_COMPRESS
80 const int lengthOffset = 4;
81 QByteArray ba(lengthOffset, ' ');
82
83 // qUncompress() expects the first 4 bytes to be the expected length of the
84 // uncompressed data
85 ba[0] = (length & 0xff000000) >> 24;
86 ba[1] = (length & 0x00ff0000) >> 16;
87 ba[2] = (length & 0x0000ff00) >> 8;
88 ba[3] = (length & 0x000000ff);
89 ba.append(transformImageData(data));
90 QByteArray baunzip = qUncompress(ba);
91 return baunzip;
92#else
93 Q_UNUSED(data);
94 Q_UNUSED(length);
95 return QByteArray();
96#endif
97}
98
99
100WriteIconData::WriteIconData(Uic *uic)
101 : driver(uic->driver()), output(uic->output()), option(uic->option())
102{
103}
104
105void WriteIconData::acceptUI(DomUI *node)
106{
107 TreeWalker::acceptUI(node);
108}
109
110void WriteIconData::acceptImages(DomImages *images)
111{
112 TreeWalker::acceptImages(images);
113}
114
115void WriteIconData::acceptImage(DomImage *image)
116{
117 writeImage(output, option.indent, image);
118}
119
120void WriteIconData::writeImage(QTextStream &output, const QString &indent, DomImage *image)
121{
122 QString img = image->attributeName() + QLatin1String("_data");
123 QString data = image->elementData()->text();
124 QString fmt = image->elementData()->attributeFormat();
125 int size = image->elementData()->attributeLength();
126
127 if (fmt == QLatin1String("XPM.GZ")) {
128 ulong length = size;
129 QByteArray baunzip = unzipXPM(data, length);
130 length = baunzip.size();
131 // shouldn't we test the initial 'length' against the
132 // resulting 'length' to catch corrupt UIC files?
133 int a = 0;
134 int column = 0;
135 bool inQuote = false;
136 output << indent << "static const char* const " << img << "[] = { \n";
137 while (baunzip[a] != '\"')
138 a++;
139 for (; a < (int) length; a++) {
140 output << baunzip[a];
141 if (baunzip[a] == '\n') {
142 column = 0;
143 } else if (baunzip[a] == '"') {
144 inQuote = !inQuote;
145 }
146
147 if (column++ >= 511 && inQuote) {
148 output << "\"\n\""; // be nice with MSVC & Co.
149 column = 1;
150 }
151 }
152
153 if (! baunzip.trimmed ().endsWith ("};"))
154 output << "};";
155
156 output << "\n\n";
157 } else {
158 output << indent << "static const unsigned char " << img << "[] = { \n";
159 output << indent;
160 int a ;
161 for (a = 0; a < (int) (data.length()/2)-1; a++) {
162 output << "0x" << QString(data[2*a]) << QString(data[2*a+1]) << ',';
163 if (a % 12 == 11)
164 output << "\n" << indent;
165 else
166 output << " ";
167 }
168 output << "0x" << QString(data[2*a]) << QString(data[2*a+1]) << '\n';
169 output << "};\n\n";
170 }
171}
172
173void WriteIconData::writeImage(QIODevice &output, DomImage *image)
174{
175 QByteArray array = transformImageData(image->elementData()->text());
176 output.write(array, array.size());
177}
178
179} // namespace CPP
180
181QT_END_NAMESPACE
Note: See TracBrowser for help on using the repository browser.