1 | // Copyright (C) 2021 The Qt Company Ltd. |
---|---|
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
3 | |
4 | #include "qquickimagepreviewprovider_p.h" |
5 | #include <QtCore/qmutex.h> |
6 | #include <QtCore/qdebug.h> |
7 | |
8 | QT_BEGIN_NAMESPACE |
9 | |
10 | struct QQuickImagePreviewProviderPrivate |
11 | { |
12 | QString id; |
13 | QImage image; |
14 | QMutex mutex; |
15 | }; |
16 | |
17 | Q_GLOBAL_STATIC(QQuickImagePreviewProviderPrivate, priv) |
18 | |
19 | QQuickImagePreviewProvider::QQuickImagePreviewProvider() |
20 | : QQuickImageProvider(QQuickImageProvider::Image) |
21 | { |
22 | } |
23 | |
24 | QQuickImagePreviewProvider::~QQuickImagePreviewProvider() |
25 | { |
26 | QQuickImagePreviewProviderPrivate *d = priv(); |
27 | QMutexLocker lock(&d->mutex); |
28 | d->id.clear(); |
29 | d->image = QImage(); |
30 | } |
31 | |
32 | QImage QQuickImagePreviewProvider::requestImage(const QString &id, QSize *size, const QSize& requestedSize) |
33 | { |
34 | QQuickImagePreviewProviderPrivate *d = priv(); |
35 | QMutexLocker lock(&d->mutex); |
36 | |
37 | if (d->id != id) |
38 | return QImage(); |
39 | |
40 | QImage res = d->image; |
41 | if (!requestedSize.isEmpty()) |
42 | res = res.scaled(s: requestedSize, aspectMode: Qt::KeepAspectRatio, mode: Qt::SmoothTransformation); |
43 | |
44 | if (size) |
45 | *size = res.size(); |
46 | |
47 | return res; |
48 | } |
49 | |
50 | void QQuickImagePreviewProvider::registerPreview(const QString &id, const QImage &preview) |
51 | { |
52 | //only the last preview is kept |
53 | QQuickImagePreviewProviderPrivate *d = priv(); |
54 | QMutexLocker lock(&d->mutex); |
55 | d->id = id; |
56 | d->image = preview; |
57 | } |
58 | |
59 | QT_END_NAMESPACE |
60 |