/src/kio-extras/thumbnail/svgcreator.cpp
Line | Count | Source |
1 | | /* This file is part of the KDE libraries |
2 | | SPDX-FileCopyrightText: 2006 Pascal Létourneau <pascal.letourneau@kdemail.net> |
3 | | |
4 | | SPDX-License-Identifier: LGPL-2.0-or-later |
5 | | */ |
6 | | |
7 | | #include "svgcreator.h" |
8 | | |
9 | | #include <QImage> |
10 | | #include <QPainter> |
11 | | #include <QSvgRenderer> |
12 | | |
13 | | #include <KPluginFactory> |
14 | | |
15 | 0 | K_PLUGIN_CLASS_WITH_JSON(SvgCreator, "svgthumbnail.json") Unexecuted instantiation: svgthumbnail_factory::tr(char const*, char const*, int) Unexecuted instantiation: svgthumbnail_factory::~svgthumbnail_factory() |
16 | 0 |
|
17 | 0 | SvgCreator::SvgCreator(QObject *parent, const QVariantList &args) |
18 | 21.3k | : KIO::ThumbnailCreator(parent, args) |
19 | 21.3k | { |
20 | 21.3k | } |
21 | | |
22 | | KIO::ThumbnailResult SvgCreator::create(const KIO::ThumbnailRequest &request) |
23 | 21.3k | { |
24 | 21.3k | QSvgRenderer r(request.url().toLocalFile()); |
25 | 21.3k | if (!r.isValid()) |
26 | 18.1k | return KIO::ThumbnailResult::fail(); |
27 | | |
28 | | // render using the correct ratio |
29 | 3.23k | const QSize defaultSize = r.defaultSize(); |
30 | 3.23k | double ratio = 1.0; |
31 | 3.23k | if (defaultSize.width() > 0 && defaultSize.height() > 0) { |
32 | 2.80k | if (defaultSize.height() < request.targetSize().height() && defaultSize.width() < request.targetSize().width()) { |
33 | | // scale to output size if size is smaller than output |
34 | 2.05k | ratio = 1.0 * defaultSize.height() / defaultSize.width(); |
35 | 2.05k | } else { |
36 | 755 | ratio = static_cast<double>(defaultSize.height()) / static_cast<double>(defaultSize.width()); |
37 | 755 | } |
38 | 2.80k | } |
39 | | |
40 | 3.23k | int width = request.targetSize().width() * request.devicePixelRatio(); |
41 | 3.23k | int height = request.targetSize().height() * request.devicePixelRatio(); |
42 | | |
43 | 3.23k | if (defaultSize.height() > defaultSize.width()) |
44 | 622 | height = qRound(width / ratio); |
45 | 2.61k | else |
46 | 2.61k | width = qRound(height / ratio); |
47 | | |
48 | 3.23k | QImage i(width, height, QImage::Format_ARGB32_Premultiplied); |
49 | 3.23k | i.fill(0); |
50 | 3.23k | QPainter p(&i); |
51 | 3.23k | r.render(&p, QRectF(QPointF(0, 0), QSizeF(width, height))); |
52 | | |
53 | 3.23k | return KIO::ThumbnailResult::pass(i); |
54 | 21.3k | } |
55 | | |
56 | | #include "moc_svgcreator.cpp" |
57 | | #include "svgcreator.moc" |