src/tools/uic/cpp/cppwriteicondata.cpp
changeset 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 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 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    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 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    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 
       
    49 QT_BEGIN_NAMESPACE
       
    50 
       
    51 namespace CPP {
       
    52 
       
    53 static 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 
       
    77 static 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 
       
   100 WriteIconData::WriteIconData(Uic *uic)
       
   101     : driver(uic->driver()), output(uic->output()), option(uic->option())
       
   102 {
       
   103 }
       
   104 
       
   105 void WriteIconData::acceptUI(DomUI *node)
       
   106 {
       
   107     TreeWalker::acceptUI(node);
       
   108 }
       
   109 
       
   110 void WriteIconData::acceptImages(DomImages *images)
       
   111 {
       
   112     TreeWalker::acceptImages(images);
       
   113 }
       
   114 
       
   115 void WriteIconData::acceptImage(DomImage *image)
       
   116 {
       
   117     writeImage(output, option.indent, image);
       
   118 }
       
   119 
       
   120 void 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 
       
   173 void 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 
       
   181 QT_END_NAMESPACE