Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qquickimagepreviewprovider.cpp
Go to the documentation of this file.
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
5#include <QtCore/qmutex.h>
6#include <QtCore/qdebug.h>
7
8#include <mutex>
9
10QT_BEGIN_NAMESPACE
11
12namespace {
13
15{
16 void clear()
17 {
18 std::lock_guard guard(mutex);
19 records.clear();
20 }
21
22 void registerImage(QUuid instance, QString id, QImage preview)
23 {
24 // we only keep the most recent preview for each instances
25 std::lock_guard guard(mutex);
26 records.insert_or_assign(instance, Record{ std::move(id), std::move(preview) });
27 }
28
29 QImage getImage(const QString &id, QSize *size, const QSize &requestedSize)
30 {
31 QImage preview = [&] {
32 std::lock_guard guard(mutex);
33 for (const auto &record : records) {
34 if (record.second.id == id)
35 return record.second.image;
36 }
37 return QImage();
38 }();
39
40 if (preview.isNull())
41 return QImage();
42
43 if (requestedSize.isValid())
44 preview = preview.scaled(requestedSize, Qt::KeepAspectRatio, Qt::SmoothTransformation);
45
46 if (size)
47 *size = preview.size();
48
49 return preview;
50 }
51
52 void cleanupInstance(QUuid instance)
53 {
54 std::lock_guard guard(mutex);
55 records.erase(instance);
56 }
57
58 struct Record
59 {
62 };
63
66};
67
68Q_GLOBAL_STATIC(QQuickImagePreviewProviderPrivate, previewProviderSingleton)
69
70} // namespace
71
72QQuickImagePreviewProvider::QQuickImagePreviewProvider()
73: QQuickImageProvider(QQuickImageProvider::Image)
74{
75}
76
77QQuickImagePreviewProvider::~QQuickImagePreviewProvider()
78{
79 previewProviderSingleton()->clear();
80}
81
82QImage QQuickImagePreviewProvider::requestImage(const QString &id, QSize *size, const QSize& requestedSize)
83{
84 return previewProviderSingleton()->getImage(id, size, requestedSize);
85}
86
87void QQuickImagePreviewProvider::registerPreview(QUuid captureInstance, QString id, QImage preview)
88{
89 previewProviderSingleton()->registerImage(captureInstance, std::move(id), std::move(preview));
90}
91
92void QQuickImagePreviewProvider::cleanupInstance(QUuid captureInstance)
93{
94 previewProviderSingleton()->cleanupInstance(captureInstance);
95}
96
97QT_END_NAMESPACE
QImage getImage(const QString &id, QSize *size, const QSize &requestedSize)
void registerImage(QUuid instance, QString id, QImage preview)