From 38be0d13830efd2d98281c645c3a60afe05ffece Mon Sep 17 00:00:00 2001 From: Qt by Nokia Date: Wed, 27 Apr 2011 12:05:43 +0200 Subject: Initial import from the monolithic Qt. This is the beginning of revision history for this module. If you want to look at revision history older than this, please refer to the Qt Git wiki for how to use Git history grafting. At the time of writing, this wiki is located here: http://qt.gitorious.org/qt/pages/GitIntroductionWithQt If you have already performed the grafting and you don't see any history beyond this commit, try running "git log" with the "--follow" argument. Branched from the monolithic repo, Qt master branch, at commit 896db169ea224deb96c59ce8af800d019de63f12 --- util/s60theme/README | 31 ++++ util/s60theme/main.cpp | 78 ++++++++++ util/s60theme/s60theme.pro | 12 ++ util/s60theme/s60themeconvert.cpp | 312 ++++++++++++++++++++++++++++++++++++++ util/s60theme/s60themeconvert.h | 54 +++++++ 5 files changed, 487 insertions(+) create mode 100644 util/s60theme/README create mode 100644 util/s60theme/main.cpp create mode 100644 util/s60theme/s60theme.pro create mode 100644 util/s60theme/s60themeconvert.cpp create mode 100644 util/s60theme/s60themeconvert.h (limited to 'util/s60theme') diff --git a/util/s60theme/README b/util/s60theme/README new file mode 100644 index 0000000000..da4e81a6a9 --- /dev/null +++ b/util/s60theme/README @@ -0,0 +1,31 @@ +'s60theme' is a commandline tool which converts Carbide.ui themes into +an intermediate binary format that can be read by the simulated +QS60Style. So, for example Designer (standalone or in Carbide) will +be able to display a realistic S60 Ui. + +The intermediate binary format consists of hashes of QPictures and +QColors, streamed to a QByteArray and compressed. QS60Style could not load +the Carbide.ui theme directly because SVG handling is unfortunately +not part of QtGui, and would require a dependency on the QtSvg module. + +Also, 's60theme' uses QWebkit to parse the SVG graphics (inspired by +Ariya's 'WebKit-based SVG rasterizer' labs post). QtSvg had some issues +with the SVGs that come with Carbide.ui. + +Usage examples: +> s60theme "com.nokia.tools.theme.s60.50_3.4.0.0\config\model" Default.blob + (Reads the default 's60.50' theme and saves it as 'Default.blob') + +> s60theme "Eclipse\Examples\Haze\Haze.tdf" Haze.blob + (Reads the Haze theme and saves it as 'Haze.blob') + +To use the blob in a Qt application, get an instance of a QS60Style and call + style->loadS60ThemeFromBlob("Theme.blob"); + +The simulated QS60Style will, in its constructor by default, try to load a +":/s60Stylethemes/Default.blob". If your application has that a resource with +exactly that filename, it will be used by default. +'Default.blob' is not included in the current Qt source package. But it can +easily be created with 's60theme' and a fresh install of Carbide.ui + +Visit http://www.forum.nokia.com for details about Carbide.ui diff --git a/util/s60theme/main.cpp b/util/s60theme/main.cpp new file mode 100644 index 0000000000..0e54582c88 --- /dev/null +++ b/util/s60theme/main.cpp @@ -0,0 +1,78 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the tools applications of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include +#include "s60themeconvert.h" + +int help() +{ + qDebug() << "Usage: s60theme [modeldir|theme.tdf] output.blob"; + qDebug() << ""; + qDebug() << "Options:"; + qDebug() << " modeldir: Theme 'model' directory in Carbide.ui tree"; + qDebug() << " theme.tdf: Theme .tdf file"; + qDebug() << " output.blob: Theme blob output filename"; + qDebug() << ""; + qDebug() << "s60theme takes an S60 theme from Carbide.ui and converts"; + qDebug() << "it into a compact, binary format, that can be directly loaded by"; + qDebug() << "the QtS60Style."; + qDebug() << ""; + qDebug() << "Visit http://www.forum.nokia.com for details about Carbide.ui"; + return 1; +} + +int main(int argc, char *argv[]) +{ + if (argc != 3) + return help(); + + QApplication app(argc, argv); + + const QString input = QString::fromLatin1(argv[1]); + const QFileInfo inputInfo(input); + const QString output = QString::fromLatin1(argv[2]); + if (inputInfo.isDir()) + return S60ThemeConvert::convertDefaultThemeToBlob(input, output) ? 0 : 1; + else if (inputInfo.suffix().compare(QString::fromLatin1("tdf"), Qt::CaseInsensitive) == 0) + return S60ThemeConvert::convertTdfToBlob(input, output) ? 0 : 1; + + return help(); +} diff --git a/util/s60theme/s60theme.pro b/util/s60theme/s60theme.pro new file mode 100644 index 0000000000..83c0cf2357 --- /dev/null +++ b/util/s60theme/s60theme.pro @@ -0,0 +1,12 @@ +SOURCES += \ + s60themeconvert.cpp \ + main.cpp + +HEADERS += \ + s60themeconvert.h + +QT += \ + webkit + +CONFIG += \ + console diff --git a/util/s60theme/s60themeconvert.cpp b/util/s60theme/s60themeconvert.cpp new file mode 100644 index 0000000000..8fe37fe970 --- /dev/null +++ b/util/s60theme/s60themeconvert.cpp @@ -0,0 +1,312 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the tools applications of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "s60themeconvert.h" + +#include +#include + +static const int pictureSize = 256; +static const char* const msgPartNotInTdf = " Warning: The .tdf file does not have a part for "; +static const char* const msgSvgNotFound = " Fatal: Could not find part .svg "; + +void dumpPartPictures(const QHash &partPictures) { + foreach (const QString &partKey, partPictures.keys()) { + QPicture partPicture = partPictures.value(partKey); + qDebug() << partKey << partPicture.boundingRect(); + QImage image(partPicture.boundingRect().size(), QImage::Format_ARGB32); + image.fill(Qt::transparent); + QPainter p(&image); + partPicture.play(&p); + image.save(partKey + QString::fromLatin1(".png")); + } +} + +void dumpColors(const QHash, QColor> &colors) { + foreach (const QColor &color, colors.values()) { + const QPair key = colors.key(color); + qDebug() << key << color; + } +} + +class WebKitSVGRenderer : public QWebView +{ + Q_OBJECT + +public: + WebKitSVGRenderer(QWidget *parent = 0); + QPicture svgToQPicture(const QString &svgFileName); + +private slots: + void loadFinishedSlot(bool ok); + +private: + QEventLoop m_loop; + QPicture m_result; +}; + +WebKitSVGRenderer::WebKitSVGRenderer(QWidget *parent) + : QWebView(parent) +{ + + connect(this, SIGNAL(loadFinished(bool)), SLOT(loadFinishedSlot(bool))); + setFixedSize(pictureSize, pictureSize); + QPalette pal = palette(); + pal.setColor(QPalette::Base, Qt::transparent); + setPalette(pal); +} + +QPicture WebKitSVGRenderer::svgToQPicture(const QString &svgFileName) +{ + load(QUrl::fromLocalFile(svgFileName)); + m_loop.exec(); + return m_result; +} + +void WebKitSVGRenderer::loadFinishedSlot(bool ok) +{ + // crude error-checking + if (!ok) + qDebug() << "Failed loading " << qPrintable(url().toString()); + + page()->mainFrame()->evaluateJavaScript(QString::fromLatin1( + "document.rootElement.preserveAspectRatio.baseVal.align = SVGPreserveAspectRatio.SVG_PRESERVEASPECTRATIO_NONE;" + "document.rootElement.style.width = '100%';" + "document.rootElement.style.height = '100%';" + "document.rootElement.width.baseVal.newValueSpecifiedUnits(SVGLength.SVG_LENGTHTYPE_PERCENTAGE, 100);" + "document.rootElement.height.baseVal.newValueSpecifiedUnits(SVGLength.SVG_LENGTHTYPE_PERCENTAGE, 100);" + )); + + m_result = QPicture(); // "Clear" + QPainter p(&m_result); + page()->mainFrame()->render(&p); + p.end(); + m_result.setBoundingRect(QRect(0, 0, pictureSize, pictureSize)); + + m_loop.exit(); +} + +QPair colorIdPair(const QString &colorID) +{ + QPair result; + QString idText = colorID; + idText.remove(QRegExp(QString::fromLatin1("[0-9]"))); + if (QS60Style::colorListKeys().contains(idText)) { + QString idNumber = colorID; + idNumber.remove(QRegExp(QString::fromLatin1("[a-zA-Z]"))); + result.first = idText; + result.second = idNumber.toInt(); + } + return result; +} + +bool parseTdfFile(const QString &tdfFile, + QHash &partSvgs, + QHash, QColor> &colors) +{ + QFile file(tdfFile); + if (!file.open(QIODevice::ReadOnly)) + return false; + + const QLatin1String elementKey("element"); + const QLatin1String partKey("part"); + const QLatin1String elementIdKey("id"); + const QLatin1String layerKey("layer"); + const QLatin1String layerFileNameKey("filename"); + const QLatin1String layerColourrgbKey("colourrgb"); + const QString annoyingPrefix = QString::fromLatin1("S60_2_6%"); + + QXmlStreamReader reader(&file); + QString partId; + QPair colorId; + // Somebody with a sense of aesthetics may implement proper XML parsing, here. + while (!reader.atEnd()) { + const QXmlStreamReader::TokenType token = reader.readNext(); + switch (token) { + case QXmlStreamReader::StartElement: + if (reader.name() == elementKey || reader.name() == partKey) { + QString id = reader.attributes().value(elementIdKey).toString(); + if (QS60Style::partKeys().contains(id)) + partId = id; + else if (!id.isEmpty() && id.at(id.length()-1).isDigit()) + colorId = colorIdPair(id); + else if (QS60Style::partKeys().contains(id.mid(annoyingPrefix.length()))) + partId = id.mid(annoyingPrefix.length()); + } else if(reader.name() == layerKey) { + if (!partId.isEmpty()) { + const QString svgFile = reader.attributes().value(layerFileNameKey).toString(); + partSvgs.insert(partId, svgFile); + partId.clear(); + } else if (!colorId.first.isEmpty()) { + const QColor colorValue(reader.attributes().value(layerColourrgbKey).toString().toInt(NULL, 16)); + colors.insert(colorId, colorValue); + colorId.first.clear(); + } + } + break; + case QXmlStreamReader::EndElement: + if (reader.tokenString() == elementKey || reader.name() == partKey) + partId.clear(); + break; + default: + break; + } + } + return true; +} + +bool loadThemeFromTdf(const QString &tdfFile, + QHash &partPictures, + QHash, QColor> &colors) +{ + QHash parsedPartSvgs; + QHash parsedPartPictures; + QHash, QColor> parsedColors; + bool success = parseTdfFile(tdfFile, parsedPartSvgs, parsedColors); + if (!success) + return false; + const QString tdfBasePath = QFileInfo(tdfFile).absolutePath(); + WebKitSVGRenderer renderer; + foreach (const QString &partKey, QS60Style::partKeys()) { + qDebug() << partKey; + QString tdfFullName; + if (parsedPartSvgs.contains(partKey)) { + tdfFullName = tdfBasePath + QDir::separator() + parsedPartSvgs.value(partKey); + } else { + qWarning() << msgPartNotInTdf << partKey; + tdfFullName = tdfBasePath + QDir::separator() + partKey + QLatin1String(".svg"); + } + if (!QFile(tdfFullName).exists()) { + qWarning() << msgSvgNotFound << QDir::toNativeSeparators(tdfFullName); + return false; + } + const QPicture partPicture = renderer.svgToQPicture(tdfFullName); + parsedPartPictures.insert(partKey, partPicture); + } +// dumpPartPictures(parsedPartPictures); +// dumpColors(colors); + partPictures = parsedPartPictures; + colors = parsedColors; + return true; +} + +bool S60ThemeConvert::convertTdfToBlob(const QString &themeTdf, const QString &themeBlob) +{ + QHash partPictures; + QHash, QColor> colors; + + if (!::loadThemeFromTdf(themeTdf, partPictures, colors)) + return false; + + QS60Style style; + style.setS60Theme(partPictures, colors); + return style.saveS60ThemeToBlob(themeBlob); +} + +bool parseDesignFile(const QString &designFile, + QHash, QColor> &colors) +{ + const QLatin1String elementKey("element"); + const QLatin1String elementIdKey("id"); + const QLatin1String colorKey("defaultcolour_rgb"); + QFile file(designFile); + if (!file.open(QIODevice::ReadOnly)) + return false; + QXmlStreamReader reader(&file); + QPair colorId; + // Somebody with a sense of aesthetics may implement proper XML parsing, here. + while (!reader.atEnd()) { + const QXmlStreamReader::TokenType token = reader.readNext(); + switch (token) { + case QXmlStreamReader::StartElement: + if (reader.name() == elementKey) { + const QString colorString = reader.attributes().value(colorKey).toString(); + if (!colorString.isEmpty()) { + const QString colorId = reader.attributes().value(elementIdKey).toString(); + colors.insert(colorIdPair(colorId), colorString.toInt(NULL, 16)); + } + } + default: + break; + } + } + return true; +} + +bool loadDefaultTheme(const QString &themePath, + QHash &partPictures, + QHash, QColor> &colors) +{ + const QDir dir(themePath); + if (!dir.exists()) + return false; + + if (!parseDesignFile(themePath + QDir::separator() + QString::fromLatin1("defaultdesign.xml"), colors)) + return false; + + WebKitSVGRenderer renderer; + foreach (const QString &partKey, QS60Style::partKeys()) { + const QString partFileName = partKey + QLatin1String(".svg"); + const QString partFile(dir.absolutePath() + QDir::separator() + partFileName); + if (!QFile::exists(partFile)) { + qWarning() << msgSvgNotFound << partFileName; + return false; + } + const QPicture partPicture = renderer.svgToQPicture(partFile); + partPictures.insert(partKey, partPicture); + } + return true; +} + +bool S60ThemeConvert::convertDefaultThemeToBlob(const QString &themePath, const QString &themeBlob) +{ + QHash partPictures; + QHash, QColor> colors; + + if (!::loadDefaultTheme(themePath, partPictures, colors)) + return false; + + QS60Style style; + style.setS60Theme(partPictures, colors); + return style.saveS60ThemeToBlob(themeBlob); +} + +#include "s60themeconvert.moc" diff --git a/util/s60theme/s60themeconvert.h b/util/s60theme/s60themeconvert.h new file mode 100644 index 0000000000..123e094a15 --- /dev/null +++ b/util/s60theme/s60themeconvert.h @@ -0,0 +1,54 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the tools applications of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef S60THEMECONVERT_H +#define S60THEMECONVERT_H + +#include + +class S60ThemeConvert +{ +public: + static bool convertTdfToBlob(const QString &themeTdf, const QString &themeBlob); + static bool convertDefaultThemeToBlob(const QString &themePath, const QString &themeBlob); +}; + +#endif // S60THEMECONVERT_H -- cgit v1.2.3