~oif-team/ubuntu/natty/qt4-x11/xi2.1

« back to all changes in this revision

Viewing changes to src/tools/uic/writeicondata.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-08-24 04:09:09 UTC
  • Revision ID: james.westby@ubuntu.com-20050824040909-xmxe9jfr4a0w5671
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
**
 
3
** Copyright (C) 1992-2005 Trolltech AS. All rights reserved.
 
4
**
 
5
** This file is part of the tools applications of the Qt Toolkit.
 
6
**
 
7
** This file may be distributed under the terms of the Q Public License
 
8
** as defined by Trolltech AS of Norway and appearing in the file
 
9
** LICENSE.QPL included in the packaging of this file.
 
10
**
 
11
** This file may be distributed and/or modified under the terms of the
 
12
** GNU General Public License version 2 as published by the Free Software
 
13
** Foundation and appearing in the file LICENSE.GPL included in the
 
14
** packaging of this file.
 
15
**
 
16
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
 
17
**   information about Qt Commercial License Agreements.
 
18
** See http://www.trolltech.com/qpl/ for QPL licensing information.
 
19
** See http://www.trolltech.com/gpl/ for GPL licensing information.
 
20
**
 
21
** Contact info@trolltech.com if any conditions of this licensing are
 
22
** not clear to you.
 
23
**
 
24
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 
25
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 
26
**
 
27
****************************************************************************/
 
28
 
 
29
#include <qtextstream.h>
 
30
 
 
31
#include "writeicondata.h"
 
32
#include "driver.h"
 
33
#include "ui4.h"
 
34
#include "uic.h"
 
35
 
 
36
static QByteArray unzipXPM(QString data, ulong& length)
 
37
{
 
38
    const int lengthOffset = 4;
 
39
    int baSize = data.length() / 2 + lengthOffset;
 
40
    uchar *ba = new uchar[baSize];
 
41
    for (int i = lengthOffset; i < baSize; ++i) {
 
42
        char h = data[2 * (i-lengthOffset)].toLatin1();
 
43
        char l = data[2 * (i-lengthOffset) + 1].toLatin1();
 
44
        uchar r = 0;
 
45
        if (h <= '9')
 
46
            r += h - '0';
 
47
        else
 
48
            r += h - 'a' + 10;
 
49
        r = r << 4;
 
50
        if (l <= '9')
 
51
            r += l - '0';
 
52
        else
 
53
            r += l - 'a' + 10;
 
54
        ba[i] = r;
 
55
    }
 
56
    // qUncompress() expects the first 4 bytes to be the expected length of the
 
57
    // uncompressed data
 
58
    ba[0] = (length & 0xff000000) >> 24;
 
59
    ba[1] = (length & 0x00ff0000) >> 16;
 
60
    ba[2] = (length & 0x0000ff00) >> 8;
 
61
    ba[3] = (length & 0x000000ff);
 
62
    QByteArray baunzip = qUncompress(ba, baSize);
 
63
    delete[] ba;
 
64
    return baunzip;
 
65
}
 
66
 
 
67
WriteIconData::WriteIconData(Uic *uic)
 
68
    : driver(uic->driver()), output(uic->output()), option(uic->option())
 
69
{
 
70
}
 
71
 
 
72
void WriteIconData::acceptUI(DomUI *node)
 
73
{
 
74
    TreeWalker::acceptUI(node);
 
75
}
 
76
 
 
77
void WriteIconData::acceptImages(DomImages *images)
 
78
{
 
79
    TreeWalker::acceptImages(images);
 
80
}
 
81
 
 
82
void WriteIconData::acceptImage(DomImage *image)
 
83
{
 
84
    QString img = image->attributeName() + QLatin1String("_data");
 
85
    QString data = image->elementData()->text();
 
86
    QString fmt = image->elementData()->attributeFormat();
 
87
    int size = image->elementData()->attributeLength();
 
88
 
 
89
    if (fmt == QLatin1String("XPM.GZ")) {
 
90
        ulong length = size;
 
91
        QByteArray baunzip = unzipXPM(data, length);
 
92
        length = baunzip.size();
 
93
        // shouldn't we test the initial 'length' against the
 
94
        // resulting 'length' to catch corrupt UIC files?
 
95
        int a = 0;
 
96
        int column = 0;
 
97
        bool inQuote = false;
 
98
        output << option.indent << "static const char* const " << img << "[] = { " << endl;
 
99
        while (baunzip[a] != '\"')
 
100
            a++;
 
101
        for (; a < (int) length; a++) {
 
102
            output << baunzip[a];
 
103
            if (baunzip[a] == '\n') {
 
104
                column = 0;
 
105
            } else if (baunzip[a] == '"') {
 
106
                inQuote = !inQuote;
 
107
            }
 
108
 
 
109
            if (column++ >= 511 && inQuote) {
 
110
                output << "\"\n\""; // be nice with MSVC & Co.
 
111
                column = 1;
 
112
            }
 
113
        }
 
114
        output << endl;
 
115
    } else {
 
116
        output << option.indent << "static const unsigned char " << img << "[] = { \n";
 
117
        output << option.indent;
 
118
        int a ;
 
119
        for (a = 0; a < (int) (data.length()/2)-1; a++) {
 
120
            output << "0x" << QString(data[2*a]) << QString(data[2*a+1]) << ",";
 
121
            if (a % 12 == 11)
 
122
                output << "\n" << option.indent;
 
123
            else
 
124
                output << " ";
 
125
        }
 
126
        output << "0x" << QString(data[2*a]) << QString(data[2*a+1]) << "\n";
 
127
        output << "};\n\n";
 
128
    }
 
129
}
 
130