1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the Qt Virtual Keyboard module of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:GPL$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** GNU General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU |
19 | ** General Public License version 3 or (at your option) any later version |
20 | ** approved by the KDE Free Qt Foundation. The licenses are as published by |
21 | ** the Free Software Foundation and appearing in the file LICENSE.GPL3 |
22 | ** included in the packaging of this file. Please review the following |
23 | ** information to ensure the GNU General Public License requirements will |
24 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. |
25 | ** |
26 | ** $QT_END_LICENSE$ |
27 | ** |
28 | ****************************************************************************/ |
29 | |
30 | #include "svgimageprovider.h" |
31 | #include <QImage> |
32 | #include <QPixmap> |
33 | #include <QSvgRenderer> |
34 | #include <QPainter> |
35 | |
36 | SvgImageProvider::SvgImageProvider() : |
37 | QQuickImageProvider(QQuickImageProvider::Pixmap) |
38 | { |
39 | } |
40 | |
41 | SvgImageProvider::~SvgImageProvider() |
42 | { |
43 | } |
44 | |
45 | QPixmap SvgImageProvider::requestPixmap(const QString &id, QSize *size, const QSize &requestedSize) |
46 | { |
47 | QSize imageSize(-1, -1); |
48 | QUrl request(id); |
49 | QString imagePath = QLatin1String(":/" ) + request.path(); |
50 | if (request.hasQuery()) { |
51 | const QString query = request.query(); |
52 | const QStringList paramList = query.split(sep: QLatin1Char('&'), behavior: Qt::SkipEmptyParts); |
53 | QVariantMap params; |
54 | for (const QString ¶m : paramList) { |
55 | QStringList keyValue = param.split(sep: QLatin1Char('='), behavior: Qt::SkipEmptyParts); |
56 | if (keyValue.length() == 2) |
57 | params[keyValue[0]] = keyValue[1]; |
58 | } |
59 | const auto widthIt = params.constFind(akey: QLatin1String("width" )); |
60 | if (widthIt != params.cend()) { |
61 | bool ok = false; |
62 | int value = widthIt.value().toInt(ok: &ok); |
63 | if (ok) |
64 | imageSize.setWidth(value); |
65 | } |
66 | const auto heightIt = params.constFind(akey: QLatin1String("height" )); |
67 | if (heightIt != params.cend()) { |
68 | bool ok = false; |
69 | int value = heightIt.value().toInt(ok: &ok); |
70 | if (ok) |
71 | imageSize.setHeight(value); |
72 | } |
73 | } else { |
74 | imageSize = requestedSize; |
75 | } |
76 | |
77 | QPixmap image; |
78 | if ((imageSize.width() > 0 || imageSize.height() > 0) && imagePath.endsWith(s: QLatin1String(".svg" ))) { |
79 | QSvgRenderer renderer(imagePath); |
80 | QSize defaultSize(renderer.defaultSize()); |
81 | if (defaultSize.isEmpty()) |
82 | return image; |
83 | if (imageSize.width() <= 0 && imageSize.height() > 0) { |
84 | double aspectRatio = (double)defaultSize.width() / (double)defaultSize.height(); |
85 | imageSize.setWidth(qRound(d: imageSize.height() * aspectRatio)); |
86 | } else if (imageSize.width() > 0 && imageSize.height() <= 0) { |
87 | double aspectRatio = (double)defaultSize.width() / (double)defaultSize.height(); |
88 | imageSize.setHeight(qRound(d: imageSize.width() / aspectRatio)); |
89 | } |
90 | image = QPixmap(imageSize); |
91 | image.fill(fillColor: Qt::transparent); |
92 | QPainter painter(&image); |
93 | renderer.render(p: &painter, bounds: image.rect()); |
94 | } else { |
95 | image = QPixmap(imagePath); |
96 | imageSize = image.size(); |
97 | } |
98 | |
99 | QPixmap result; |
100 | if (requestedSize.isValid() && requestedSize != imageSize) |
101 | result = image.scaled(s: requestedSize, aspectMode: Qt::KeepAspectRatio); |
102 | else |
103 | result = image; |
104 | |
105 | *size = result.size(); |
106 | |
107 | return result; |
108 | } |
109 | |