-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpreviewimageprovider.cpp
More file actions
65 lines (53 loc) · 2.3 KB
/
previewimageprovider.cpp
File metadata and controls
65 lines (53 loc) · 2.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include "previewimageprovider.h"
PreviewImageProvider *PreviewImageProvider::s_instance = nullptr;
PreviewImageProvider::PreviewImageProvider()
: QQuickImageProvider(QQuickImageProvider::Image) {
s_instance = this;
}
PreviewImageProvider *PreviewImageProvider::instance() {
return s_instance;
}
QImage PreviewImageProvider::requestImage(const QString &id, QSize *size, const QSize &requestedSize) {
QMutexLocker locker(&m_mutex);
// ID might have extra parameters like timestamps, we just want the key
// For now assuming ID is exact key.
// In our usage we will use "preview_TIMESTAMP" or similar unique key.
QImage image;
if (m_images.contains(id)) {
image = m_images.value(id);
} else {
// Return a transparent placeholder or valid empty image
return QImage(requestedSize.width() > 0 ? requestedSize.width() : 1,
requestedSize.height() > 0 ? requestedSize.height() : 1,
QImage::Format_ARGB32_Premultiplied);
}
if (size) {
*size = image.size();
}
if (requestedSize.width() > 0 && requestedSize.height() > 0) {
return image.scaled(requestedSize, Qt::KeepAspectRatio, Qt::SmoothTransformation);
}
return image;
}
void PreviewImageProvider::storeImage(const QString &id, const QImage &image) {
QMutexLocker locker(&m_mutex);
m_images.insert(id, image);
// Cleanup old images?
// For this simple usage where we overwrite often or use unique IDs,
// we might want to clear if the map gets too big, or if we are reusing IDs.
// Let's implement a simple size cap: if > 10 images, remove oldest?
// But QMap isn't ordered by insertion.
// Since we are likely using one ID per player or one ID that changes,
// let's rely on CustomMediaPlayer reusing the ID or generating new ones.
// If CustomMediaPlayer reuses "preview" ID it won't force refresh in QML unless URL changes.
// So CustomMediaPlayer will use "preview_UNIQUE".
// We should clear the map periodically or just clear it here if it's too big.
if (m_images.size() > 20) {
m_images.clear();
m_images.insert(id, image); // Keep the new one
}
}
void PreviewImageProvider::clearImages() {
QMutexLocker locker(&m_mutex);
m_images.clear();
}