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

Last change on this file was 846, checked in by Dmitry A. Kuminov, 14 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

File size: 5.8 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation (qt-info@nokia.com)
6**
7** This file is part of the tools applications of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and Nokia.
15**
16** GNU Lesser General Public License Usage
17** Alternatively, this file may be used under the terms of the GNU Lesser
18** General Public License version 2.1 as published by the Free Software
19** Foundation and appearing in the file LICENSE.LGPL included in the
20** packaging of this file. Please review the following information to
21** ensure the GNU Lesser General Public License version 2.1 requirements
22** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23**
24** In addition, as a special exception, Nokia gives you certain additional
25** rights. These rights are described in the Nokia Qt LGPL Exception
26** version 1.1, included in the file LGPL_EXCEPTION.txt in this 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 have questions regarding the use of this file, please contact
37** Nokia at qt-info@nokia.com.
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 // Limit line length when writing code.
118 writeImage(output, option.indent, true, image);
119}
120
121void WriteIconData::writeImage(QTextStream &output, const QString &indent,
122 bool limitXPM_LineLength, const DomImage *image)
123{
124 QString img = image->attributeName() + QLatin1String("_data");
125 QString data = image->elementData()->text();
126 QString fmt = image->elementData()->attributeFormat();
127 int size = image->elementData()->attributeLength();
128
129 if (fmt == QLatin1String("XPM.GZ")) {
130 ulong length = size;
131 QByteArray baunzip = unzipXPM(data, length);
132 length = baunzip.size();
133 // shouldn't we test the initial 'length' against the
134 // resulting 'length' to catch corrupt UIC files?
135 int a = 0;
136 int column = 0;
137 bool inQuote = false;
138 output << indent << "/* XPM */\n"
139 << indent << "static const char* const " << img << "[] = { \n";
140 while (baunzip[a] != '\"')
141 a++;
142 for (; a < (int) length; a++) {
143 output << baunzip[a];
144 if (baunzip[a] == '\n') {
145 column = 0;
146 } else if (baunzip[a] == '"') {
147 inQuote = !inQuote;
148 }
149
150 column++;
151 if (limitXPM_LineLength && column >= 512 && inQuote) {
152 output << "\"\n\""; // be nice with MSVC & Co.
153 column = 1;
154 }
155 }
156
157 if (! baunzip.trimmed ().endsWith ("};"))
158 output << "};";
159
160 output << "\n\n";
161 } else {
162 output << indent << "static const unsigned char " << img << "[] = { \n";
163 output << indent;
164 int a ;
165 for (a = 0; a < (int) (data.length()/2)-1; a++) {
166 output << "0x" << QString(data[2*a]) << QString(data[2*a+1]) << ',';
167 if (a % 12 == 11)
168 output << '\n' << indent;
169 else
170 output << ' ';
171 }
172 output << "0x" << QString(data[2*a]) << QString(data[2*a+1]) << '\n';
173 output << "};\n\n";
174 }
175}
176
177void WriteIconData::writeImage(QIODevice &output, DomImage *image)
178{
179 QByteArray array = transformImageData(image->elementData()->text());
180 output.write(array, array.size());
181}
182
183} // namespace CPP
184
185QT_END_NAMESPACE
Note: See TracBrowser for help on using the repository browser.