aboutsummaryrefslogtreecommitdiffstats
path: root/src/virtualkeyboard/styles
diff options
context:
space:
mode:
authorJarkko Koivikko <jarkko.koivikko@code-q.fi>2015-04-14 14:21:24 +0300
committerJarkko Koivikko <jarkko.koivikko@code-q.fi>2015-06-17 12:32:58 +0300
commit85b64c67f4b948c3d3aefee237b7606a41ccccba (patch)
tree935b90025260e567621cc244197f915f5be2abfe /src/virtualkeyboard/styles
parent60309fd67daa902c3d06e834cbefed19abbdd9be (diff)
Add SVG image provider to Styles module
With SVG image provider, it is possible to request the SVG images scaled to specified size using the URL parameters. In normal case, like with the QML Image, this would not be necessary, since the Image allows setting the sourceSize property, thus allowing to scale the SVG to desired size. However, in special cases, such as with BorderImage, the sourceSize is not available for modification, thus makes it impossible to leverage SVG scaling capabilities. With this change, the SVG images can be fetched from the new image provider available at image://qtvkbsvg URL and with the desired size given as URL parameters, for example: "image://qtvkbsvg/path/to/resource/mySvg.svg?width=123&height=456" It is also possible to give only one dimension, in which case the other dimension is calculated by the image provider from the design aspect ratio of the SVG image. Change-Id: Ided3035d0d414462b2c3eeaba0b8543a4c5ceea9 Reviewed-by: Gatis Paeglis <gatis.paeglis@theqtcompany.com>
Diffstat (limited to 'src/virtualkeyboard/styles')
-rw-r--r--src/virtualkeyboard/styles/styles.pro4
-rw-r--r--src/virtualkeyboard/styles/styles_plugin.cpp7
-rw-r--r--src/virtualkeyboard/styles/styles_plugin.h1
-rw-r--r--src/virtualkeyboard/styles/svgimageprovider.cpp95
-rw-r--r--src/virtualkeyboard/styles/svgimageprovider.h34
5 files changed, 140 insertions, 1 deletions
diff --git a/src/virtualkeyboard/styles/styles.pro b/src/virtualkeyboard/styles/styles.pro
index 0f2fb269..03a8141f 100644
--- a/src/virtualkeyboard/styles/styles.pro
+++ b/src/virtualkeyboard/styles/styles.pro
@@ -1,16 +1,18 @@
TEMPLATE = lib
TARGET = qtvirtualkeyboardstylesplugin
INSTALL_PATH = $$[QT_INSTALL_QML]/QtQuick/Enterprise/VirtualKeyboard/Styles
-QT += qml quick
+QT += qml quick svg
CONFIG += plugin
target.path = $$INSTALL_PATH
INSTALLS += target
SOURCES += \
+ svgimageprovider.cpp \
styles_plugin.cpp
HEADERS += \
+ svgimageprovider.h \
styles_plugin.h
OTHER_FILES += \
diff --git a/src/virtualkeyboard/styles/styles_plugin.cpp b/src/virtualkeyboard/styles/styles_plugin.cpp
index 5b522d67..b20c6d01 100644
--- a/src/virtualkeyboard/styles/styles_plugin.cpp
+++ b/src/virtualkeyboard/styles/styles_plugin.cpp
@@ -17,6 +17,7 @@
****************************************************************************/
#include "styles_plugin.h"
+#include "svgimageprovider.h"
#include <qqml.h>
#include <QtCore/QLibraryInfo>
@@ -39,3 +40,9 @@ void StylesPlugin::registerTypes(const char *uri)
qmlRegisterType(QUrl(path + "KeyPanel.qml"), uri, 1, 0, "KeyPanel");
qmlRegisterType(QUrl(path + "SelectionListItem.qml"), uri, 1, 0, "SelectionListItem");
}
+
+void StylesPlugin::initializeEngine(QQmlEngine *engine, const char *uri)
+{
+ Q_UNUSED(uri)
+ engine->addImageProvider(QLatin1String("qtvkbsvg"), new SvgImageProvider());
+}
diff --git a/src/virtualkeyboard/styles/styles_plugin.h b/src/virtualkeyboard/styles/styles_plugin.h
index 97240d2d..733161c5 100644
--- a/src/virtualkeyboard/styles/styles_plugin.h
+++ b/src/virtualkeyboard/styles/styles_plugin.h
@@ -28,6 +28,7 @@ class StylesPlugin : public QQmlExtensionPlugin
public:
void registerTypes(const char *uri);
+ void initializeEngine(QQmlEngine *engine, const char *uri);
};
#endif // STYLES_PLUGIN_H
diff --git a/src/virtualkeyboard/styles/svgimageprovider.cpp b/src/virtualkeyboard/styles/svgimageprovider.cpp
new file mode 100644
index 00000000..665b37a3
--- /dev/null
+++ b/src/virtualkeyboard/styles/svgimageprovider.cpp
@@ -0,0 +1,95 @@
+/****************************************************************************
+**
+** Copyright (C) 2015 Digia Plc
+** All rights reserved.
+** For any questions to Digia, please use contact form at http://www.qt.io
+**
+** This file is part of the Qt Virtual Keyboard add-on for Qt Enterprise.
+**
+** Licensees holding valid Qt Enterprise licenses may use this file in
+** accordance with the Qt Enterprise License Agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and Digia.
+**
+** If you have questions regarding the use of this file, please use
+** contact form at http://www.qt.io
+**
+****************************************************************************/
+
+#include "svgimageprovider.h"
+#include <QImage>
+#include <QPixmap>
+#include <QSvgRenderer>
+#include <QPainter>
+
+SvgImageProvider::SvgImageProvider() :
+ QQuickImageProvider(QQuickImageProvider::Pixmap)
+{
+}
+
+SvgImageProvider::~SvgImageProvider()
+{
+}
+
+QPixmap SvgImageProvider::requestPixmap(const QString &id, QSize *size, const QSize &requestedSize)
+{
+ QSize imageSize(-1, -1);
+ QUrl request(id);
+ QString imagePath = ":/" + request.path();
+ if (request.hasQuery()) {
+ const QString query = request.query();
+ const QStringList paramList = query.split(QChar('&'), QString::SkipEmptyParts);
+ QVariantMap params;
+ foreach (const QString &param, paramList) {
+ QStringList keyValue = param.split(QChar('='), QString::SkipEmptyParts);
+ if (keyValue.length() == 2)
+ params[keyValue[0]] = keyValue[1];
+ }
+ if (params.contains("width")) {
+ bool ok = false;
+ int value = params["width"].toInt(&ok);
+ if (ok)
+ imageSize.setWidth(value);
+ }
+ if (params.contains("height")) {
+ bool ok = false;
+ int value = params["height"].toInt(&ok);
+ if (ok)
+ imageSize.setHeight(value);
+ }
+ } else {
+ imageSize = requestedSize;
+ }
+
+ QPixmap image;
+ if ((imageSize.width() > 0 || imageSize.height() > 0) && imagePath.endsWith(".svg")) {
+ QSvgRenderer renderer(imagePath);
+ QSize defaultSize(renderer.defaultSize());
+ if (defaultSize.isEmpty())
+ return image;
+ if (imageSize.width() <= 0 && imageSize.height() > 0) {
+ double aspectRatio = (double)defaultSize.width() / (double)defaultSize.height();
+ imageSize.setWidth(qRound(imageSize.height() * aspectRatio));
+ } else if (imageSize.width() > 0 && imageSize.height() <= 0) {
+ double aspectRatio = (double)defaultSize.width() / (double)defaultSize.height();
+ imageSize.setHeight(qRound(imageSize.width() / aspectRatio));
+ }
+ image = QPixmap(imageSize);
+ image.fill(Qt::transparent);
+ QPainter painter(&image);
+ renderer.render(&painter, image.rect());
+ } else {
+ image = QPixmap(imagePath);
+ imageSize = image.size();
+ }
+
+ QPixmap result;
+ if (requestedSize.isValid() && requestedSize != imageSize)
+ result = image.scaled(requestedSize, Qt::KeepAspectRatio);
+ else
+ result = image;
+
+ *size = result.size();
+
+ return result;
+}
diff --git a/src/virtualkeyboard/styles/svgimageprovider.h b/src/virtualkeyboard/styles/svgimageprovider.h
new file mode 100644
index 00000000..c0f1a9e3
--- /dev/null
+++ b/src/virtualkeyboard/styles/svgimageprovider.h
@@ -0,0 +1,34 @@
+/****************************************************************************
+**
+** Copyright (C) 2015 Digia Plc
+** All rights reserved.
+** For any questions to Digia, please use contact form at http://www.qt.io
+**
+** This file is part of the Qt Virtual Keyboard add-on for Qt Enterprise.
+**
+** Licensees holding valid Qt Enterprise licenses may use this file in
+** accordance with the Qt Enterprise License Agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and Digia.
+**
+** If you have questions regarding the use of this file, please use
+** contact form at http://www.qt.io
+**
+****************************************************************************/
+
+#ifndef SVGIMAGEPROVIDER_H
+#define SVGIMAGEPROVIDER_H
+
+#include <QQuickImageProvider>
+
+class SvgImageProvider : public QQuickImageProvider
+{
+public:
+ explicit SvgImageProvider();
+ ~SvgImageProvider();
+
+ QPixmap requestPixmap(const QString &id, QSize *size, const QSize &requestedSize);
+};
+
+#endif // SVGIMAGEPROVIDER_H
+