util/s60theme/s60themeconvert.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 "s60themeconvert.h"
       
    43 
       
    44 #include <QtGui>
       
    45 #include <QtWebKit>
       
    46 
       
    47 static const int pictureSize = 256;
       
    48 
       
    49 void dumpPartPictures(const QHash<QString, QPicture> &partPictures) {
       
    50     foreach (const QString &partKey, partPictures.keys()) {
       
    51         QPicture partPicture = partPictures.value(partKey);
       
    52         qDebug() << partKey << partPicture.boundingRect();
       
    53         QImage image(partPicture.boundingRect().size(), QImage::Format_ARGB32);
       
    54         image.fill(Qt::transparent);
       
    55         QPainter p(&image);
       
    56         partPicture.play(&p);
       
    57         image.save(partKey + QString::fromLatin1(".png"));
       
    58     }
       
    59 }
       
    60 
       
    61 void dumpColors(const QHash<QPair<QString, int>, QColor> &colors) {
       
    62     foreach (const QColor &color, colors.values()) {
       
    63         const QPair<QString, int> key = colors.key(color);
       
    64         qDebug() << key << color;
       
    65     }
       
    66 }
       
    67 
       
    68 class WebKitSVGRenderer : public QWebView
       
    69 {
       
    70     Q_OBJECT
       
    71 
       
    72 public:
       
    73     WebKitSVGRenderer(QWidget *parent = 0);
       
    74     QPicture svgToQPicture(const QString &svgFileName);
       
    75 
       
    76 private slots:
       
    77     void loadFinishedSlot(bool ok);
       
    78 
       
    79 private:
       
    80     QEventLoop m_loop;
       
    81     QPicture m_result;
       
    82 };
       
    83 
       
    84 WebKitSVGRenderer::WebKitSVGRenderer(QWidget *parent)
       
    85     : QWebView(parent)
       
    86 {
       
    87     connect(this, SIGNAL(loadFinished(bool)), SLOT(loadFinishedSlot(bool)));
       
    88     setFixedSize(pictureSize, pictureSize);
       
    89     QPalette pal = palette();
       
    90     pal.setColor(QPalette::Base, Qt::transparent);
       
    91     setPalette(pal);
       
    92 }
       
    93 
       
    94 QPicture WebKitSVGRenderer::svgToQPicture(const QString &svgFileName)
       
    95 {
       
    96     load(QUrl::fromLocalFile(svgFileName));
       
    97     m_loop.exec();
       
    98     return m_result;
       
    99 }
       
   100 
       
   101 void WebKitSVGRenderer::loadFinishedSlot(bool ok)
       
   102 {
       
   103     // crude error-checking
       
   104     if (!ok)
       
   105         qDebug() << "Failed loading " << qPrintable(url().toString());
       
   106 
       
   107     page()->mainFrame()->evaluateJavaScript(QString::fromLatin1(
       
   108         "document.rootElement.preserveAspectRatio.baseVal.align = SVGPreserveAspectRatio.SVG_PRESERVEASPECTRATIO_NONE;"
       
   109         "document.rootElement.style.width = '100%';"
       
   110         "document.rootElement.style.height = '100%';"
       
   111         "document.rootElement.width.baseVal.newValueSpecifiedUnits(SVGLength.SVG_LENGTHTYPE_PERCENTAGE, 100);"
       
   112         "document.rootElement.height.baseVal.newValueSpecifiedUnits(SVGLength.SVG_LENGTHTYPE_PERCENTAGE, 100);"
       
   113     ));
       
   114 
       
   115     m_result = QPicture(); // "Clear"
       
   116     QPainter p(&m_result);
       
   117     page()->mainFrame()->render(&p);
       
   118     p.end();
       
   119     m_result.setBoundingRect(QRect(0, 0, pictureSize, pictureSize));
       
   120 
       
   121     m_loop.exit();
       
   122 }
       
   123 
       
   124 QPair<QString, int> colorIdPair(const QString &colorID)
       
   125 {
       
   126     QPair<QString, int> result;
       
   127     QString idText = colorID;
       
   128     idText.remove(QRegExp(QString::fromLatin1("[0-9]")));
       
   129     if (QS60Style::colorListKeys().contains(idText)) {
       
   130         QString idNumber = colorID;
       
   131         idNumber.remove(QRegExp(QString::fromLatin1("[a-zA-Z]")));
       
   132         result.first = idText;
       
   133         result.second = idNumber.toInt();
       
   134     }
       
   135     return result;
       
   136 }
       
   137 
       
   138 bool parseTdfFile(const QString &tdfFile,
       
   139         QHash<QString, QString> &partSvgs,
       
   140         QHash<QPair<QString, int>, QColor> &colors)
       
   141 {
       
   142     QFile file(tdfFile);
       
   143     if (!file.open(QIODevice::ReadOnly))
       
   144         return false;
       
   145 
       
   146     const QLatin1String elementKey("element");
       
   147     const QLatin1String partKey("part");
       
   148     const QLatin1String elementIdKey("id");
       
   149     const QLatin1String layerKey("layer");
       
   150     const QLatin1String layerFileNameKey("filename");
       
   151     const QLatin1String layerColourrgbKey("colourrgb");
       
   152     const QString annoyingPrefix = QString::fromLatin1("S60_2_6%");
       
   153 
       
   154     QXmlStreamReader reader(&file);
       
   155     QString partId;
       
   156     QPair<QString, int> colorId;
       
   157     // Somebody with a sense of aesthetics may implement proper XML parsing, here.
       
   158     while (!reader.atEnd()) {
       
   159         const QXmlStreamReader::TokenType token = reader.readNext();
       
   160         switch (token) {
       
   161             case QXmlStreamReader::StartElement:
       
   162                 if (reader.name() == elementKey || reader.name() == partKey) {
       
   163                     QString id = reader.attributes().value(elementIdKey).toString();
       
   164                     if (QS60Style::partKeys().contains(id))
       
   165                         partId = id;
       
   166                     else if (!id.isEmpty() && id.at(id.length()-1).isDigit())
       
   167                         colorId = colorIdPair(id);
       
   168                     else if (QS60Style::partKeys().contains(id.mid(annoyingPrefix.length())))
       
   169                         partId = id.mid(annoyingPrefix.length());
       
   170                 } else if(reader.name() == layerKey) {
       
   171                     if (!partId.isEmpty()) {
       
   172                         const QString svgFile = reader.attributes().value(layerFileNameKey).toString();
       
   173                         partSvgs.insert(partId, svgFile);
       
   174                         partId.clear();
       
   175                     } else if (!colorId.first.isEmpty()) {
       
   176                         const QColor colorValue(reader.attributes().value(layerColourrgbKey).toString().toInt(NULL, 16));
       
   177                         colors.insert(colorId, colorValue);
       
   178                         colorId.first.clear();
       
   179                     }
       
   180                 }
       
   181                 break;
       
   182             case QXmlStreamReader::EndElement:
       
   183                 if (reader.tokenString() == elementKey || reader.name() == partKey)
       
   184                     partId.clear();
       
   185                 break;
       
   186             default:
       
   187                 break;
       
   188         }
       
   189     }
       
   190     return true;
       
   191 }
       
   192 
       
   193 bool loadThemeFromTdf(const QString &tdfFile,
       
   194         QHash<QString, QPicture> &partPictures,
       
   195         QHash<QPair<QString, int>, QColor> &colors)
       
   196 {
       
   197     QHash<QString, QString> parsedPartSvgs;
       
   198     QHash<QString, QPicture> parsedPartPictures;
       
   199     QHash<QPair<QString, int>, QColor> parsedColors;
       
   200     bool success = parseTdfFile(tdfFile, parsedPartSvgs, parsedColors);
       
   201     if (!success)
       
   202         return false;
       
   203     const QString tdfBasePath = QFileInfo(tdfFile).absolutePath();
       
   204     WebKitSVGRenderer renderer;
       
   205     foreach(const QString& partKey, parsedPartSvgs.keys()) {
       
   206         const QString tdfFullName =
       
   207             tdfBasePath + QDir::separator() + parsedPartSvgs.value(partKey);
       
   208         if (!QFile(tdfFullName).exists())
       
   209             qWarning() << "Could not find part:" << parsedPartSvgs.value(partKey);
       
   210         const QPicture partPicture = renderer.svgToQPicture(tdfFullName);
       
   211         parsedPartPictures.insert(partKey, partPicture);
       
   212     }
       
   213 //    dumpPartPictures(parsedPartPictures);
       
   214 //    dumpColors(colors);
       
   215     partPictures = parsedPartPictures;
       
   216     colors = parsedColors;
       
   217     return true;
       
   218 }
       
   219 
       
   220 bool S60ThemeConvert::convertTdfToBlob(const QString &themeTdf, const QString &themeBlob)
       
   221 {
       
   222     QHash<QString, QPicture> partPictures;
       
   223     QHash<QPair<QString, int>, QColor> colors;
       
   224 
       
   225     if (!::loadThemeFromTdf(themeTdf, partPictures, colors))
       
   226         return false;
       
   227 
       
   228     QS60Style style;
       
   229     style.setS60Theme(partPictures, colors);
       
   230     return style.saveS60ThemeToBlob(themeBlob);
       
   231 }
       
   232 
       
   233 bool parseDesignFile(const QString &designFile,
       
   234         QHash<QPair<QString, int>, QColor> &colors)
       
   235 {
       
   236     const QLatin1String elementKey("element");
       
   237     const QLatin1String elementIdKey("id");
       
   238     const QLatin1String colorKey("defaultcolour_rgb");
       
   239     QFile file(designFile);
       
   240     if (!file.open(QIODevice::ReadOnly))
       
   241         return false;
       
   242     QXmlStreamReader reader(&file);
       
   243     QPair<QString, int> colorId;
       
   244     // Somebody with a sense of aesthetics may implement proper XML parsing, here.
       
   245     while (!reader.atEnd()) {
       
   246         const QXmlStreamReader::TokenType token = reader.readNext();
       
   247         switch (token) {
       
   248             case QXmlStreamReader::StartElement:
       
   249                 if (reader.name() == elementKey) {
       
   250                     const QString colorString = reader.attributes().value(colorKey).toString();
       
   251                     if (!colorString.isEmpty()) {
       
   252                         const QString colorId = reader.attributes().value(elementIdKey).toString();
       
   253                         colors.insert(colorIdPair(colorId), colorString.toInt(NULL, 16));
       
   254                     }
       
   255                 }
       
   256             default:
       
   257                 break;
       
   258         }
       
   259     }
       
   260     return true;
       
   261 }
       
   262 
       
   263 bool loadDefaultTheme(const QString &themePath,
       
   264         QHash<QString, QPicture> &partPictures,
       
   265         QHash<QPair<QString, int>, QColor> &colors)
       
   266 {
       
   267     const QDir dir(themePath);
       
   268     if (!dir.exists())
       
   269         return false;
       
   270 
       
   271     if (!parseDesignFile(themePath + QDir::separator() + QString::fromLatin1("defaultdesign.xml"), colors))
       
   272         return false;
       
   273 
       
   274     WebKitSVGRenderer renderer;
       
   275     foreach (const QString &partKey, QS60Style::partKeys()) {
       
   276         const QString partFileName = partKey + QLatin1String(".svg");		
       
   277         const QString partFile(dir.absolutePath() + QDir::separator() + partFileName);
       
   278         if (!QFile::exists(partFile)) {
       
   279             qWarning() << "Could not find part:" << partFileName;
       
   280             continue;
       
   281         }
       
   282         const QPicture partPicture = renderer.svgToQPicture(partFile);
       
   283         partPictures.insert(partKey, partPicture);
       
   284     }
       
   285     return true;
       
   286 }
       
   287 
       
   288 bool S60ThemeConvert::convertDefaultThemeToBlob(const QString &themePath, const QString &themeBlob)
       
   289 {
       
   290     QHash<QString, QPicture> partPictures;
       
   291     QHash<QPair<QString, int>, QColor> colors;
       
   292 
       
   293     if (!::loadDefaultTheme(themePath, partPictures, colors))
       
   294         return false;
       
   295 
       
   296     QS60Style style;
       
   297     style.setS60Theme(partPictures, colors);
       
   298     return style.saveS60ThemeToBlob(themeBlob);
       
   299 }
       
   300 
       
   301 #include "s60themeconvert.moc"