/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 | 47.5k | : KIO::ThumbnailCreator(parent, args) |
19 | 47.5k | { |
20 | 47.5k | } |
21 | | |
22 | | KIO::ThumbnailResult SvgCreator::create(const KIO::ThumbnailRequest &request) |
23 | 47.5k | { |
24 | 47.5k | QSvgRenderer r(request.url().toLocalFile()); |
25 | 47.5k | if (!r.isValid()) |
26 | 36.8k | return KIO::ThumbnailResult::fail(); |
27 | | |
28 | 10.6k | const QSize targetSize = request.targetSize() * request.devicePixelRatio(); |
29 | 10.6k | QSize renderSize = targetSize; |
30 | | |
31 | 10.6k | const QSize defaultSize = r.defaultSize(); |
32 | 10.6k | if (defaultSize.isValid() && !defaultSize.isEmpty()) { |
33 | 6.73k | const QSize scaled = defaultSize.scaled(targetSize, Qt::KeepAspectRatio); |
34 | 6.73k | if (!scaled.isEmpty()) { |
35 | 5.09k | renderSize = scaled; |
36 | 5.09k | } |
37 | 6.73k | } |
38 | | |
39 | | |
40 | 10.6k | QImage img(renderSize, QImage::Format_ARGB32_Premultiplied); |
41 | 10.6k | img.fill(Qt::transparent); |
42 | | |
43 | 10.6k | QPainter p(&img); |
44 | 10.6k | r.render(&p, QRectF(QPointF(0, 0), renderSize)); |
45 | | |
46 | 10.6k | return KIO::ThumbnailResult::pass(img); |
47 | 47.5k | } |
48 | | |
49 | | #include "moc_svgcreator.cpp" |
50 | | #include "svgcreator.moc" |