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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162 | /* ============================================================
*
* This file is a part of digiKam project
* https://www.digikam.org
*
* Date : 2008-03-13
* Description : Image files selection dialog - list view icon provider.
*
* SPDX-FileCopyrightText: 2008-2025 by Gilles Caulier <caulier dot gilles at gmail dot com>
*
* SPDX-License-Identifier: GPL-2.0-or-later
*
* ============================================================ */
#include "imagedialog_p.h"
namespace Digikam
{
class Q_DECL_HIDDEN ImageDialogIconProvider::Private
{
public:
Private() = default;
public:
QMutex mutex;
QTimer* timer = nullptr;
ThumbnailLoadThread* thread = nullptr; ///< The separated thread to render thumbnail images.
QHash<QString, QIcon> iconHash;
};
ImageDialogIconProvider::ImageDialogIconProvider()
: QObject (),
QFileIconProvider(),
d (new Private)
{
d->timer = new QTimer(this);
d->timer->setInterval(100);
d->timer->setSingleShot(true);
d->thread = new ThumbnailLoadThread;
d->thread->setPixmapRequested(true);
d->thread->setThumbnailSize(256);
connect(d->timer, SIGNAL(timeout()),
this, SIGNAL(signalThumbnailRefresh()));
connect(d->thread, SIGNAL(signalQImageThumbnailLoaded(LoadingDescription,QImage)),
this, SLOT(slotThumbnailLoaded(LoadingDescription,QImage)));
}
ImageDialogIconProvider::~ImageDialogIconProvider()
{
d->thread->stopAllTasks();
d->thread->wait();
d->timer->stop();
delete d->thread;
delete d;
}
QIcon ImageDialogIconProvider::icon(const QFileInfo& info) const
{
// We will only process image files.
if (info.isFile() && !info.isSymLink() && !info.isRoot())
{
QString path = info.absoluteFilePath();
if (!path.isEmpty())
{
{
QMutexLocker locker(&d->mutex);
if (d->iconHash.contains(path))
{
return d->iconHash.value(path);
}
}
QString mimeType = QMimeDatabase().mimeTypeForFile(path).name();
QString suffix = info.suffix().toUpper();
if (
mimeType.startsWith(QLatin1String("image/")) ||
(suffix == QLatin1String("PGF")) ||
(suffix == QLatin1String("JXL")) ||
(suffix == QLatin1String("AVIF")) ||
(suffix == QLatin1String("KRA")) ||
(suffix == QLatin1String("CR3")) ||
(suffix == QLatin1String("HIF")) ||
(suffix == QLatin1String("HEIC")) ||
(suffix == QLatin1String("HEIF"))
)
{
qCDebug(DIGIKAM_GENERAL_LOG) << "request thumb icon for " << path;
d->thread->find(ThumbnailIdentifier(path));
}
}
}
// For non-iages, we will use default provider implementation.
return QFileIconProvider::icon(info);
}
#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
QIcon ImageDialogIconProvider::icon(QAbstractFileIconProvider::IconType type) const
#else
QIcon ImageDialogIconProvider::icon(IconType type) const
#endif
{
return QFileIconProvider::icon(type);
}
void ImageDialogIconProvider::slotThumbnailLoaded(const LoadingDescription& desc, const QImage& img)
{
// Resize and center pixmap on target icon.
if (!img.isNull())
{
QPixmap pix = QPixmap::fromImage(img);
pix = pix.scaled(QSize(256, 256),
Qt::KeepAspectRatio,
Qt::FastTransformation);
QPixmap icon(QSize(256, 256));<--- Shadow variable
icon.fill(Qt::transparent);
QPainter p(&icon);
p.drawPixmap((icon.width() - pix.width() ) / 2,
(icon.height() - pix.height()) / 2,
pix);
QMutexLocker locker(&d->mutex);
d->iconHash.insert(desc.filePath, icon);
}
else
{
QMutexLocker locker(&d->mutex);
d->iconHash.insert(desc.filePath, QIcon::fromTheme(QLatin1String("image-missing")));
}
if (!d->timer->isActive())
{
d->timer->start();
}
}
} // namespace Digikam
|