| cec3b5da |
Poul Sander |
2026-02-19 19:17 |
1 |
#include "thumbnailworker.h" |
| cec3b5da |
Poul Sander |
2026-02-19 19:17 |
2 |
#include <QImageReader> |
| cfe4a67b |
Poul Sander |
2026-02-19 23:06 |
3 |
#include <QPainter> |
| cec3b5da |
Poul Sander |
2026-02-19 19:17 |
4 |
|
| 6fcd6c90 |
Poul Sander |
2026-02-19 21:12 |
5 |
ThumbnailWorker::ThumbnailWorker(const QString &path, |
| 6fcd6c90 |
Poul Sander |
2026-02-19 21:12 |
6 |
int row, |
| 6fcd6c90 |
Poul Sander |
2026-02-19 21:12 |
7 |
std::atomic_bool *cancelFlag) |
| 6fcd6c90 |
Poul Sander |
2026-02-19 21:12 |
8 |
: m_path(path), |
| 6fcd6c90 |
Poul Sander |
2026-02-19 21:12 |
9 |
m_row(row), |
| 6fcd6c90 |
Poul Sander |
2026-02-19 21:12 |
10 |
m_cancelFlag(cancelFlag) |
| cec3b5da |
Poul Sander |
2026-02-19 19:17 |
11 |
{ |
| cec3b5da |
Poul Sander |
2026-02-19 19:17 |
12 |
setAutoDelete(true); |
| cec3b5da |
Poul Sander |
2026-02-19 19:17 |
13 |
} |
| cec3b5da |
Poul Sander |
2026-02-19 19:17 |
14 |
|
| cec3b5da |
Poul Sander |
2026-02-19 19:17 |
15 |
void ThumbnailWorker::run() |
| cec3b5da |
Poul Sander |
2026-02-19 19:17 |
16 |
{ |
| 6fcd6c90 |
Poul Sander |
2026-02-19 21:12 |
17 |
if (*m_cancelFlag) |
| 6fcd6c90 |
Poul Sander |
2026-02-19 21:12 |
18 |
return; |
| 6fcd6c90 |
Poul Sander |
2026-02-19 21:12 |
19 |
|
| cec3b5da |
Poul Sander |
2026-02-19 19:17 |
20 |
QImageReader reader(m_path); |
| cec3b5da |
Poul Sander |
2026-02-19 19:17 |
21 |
reader.setAutoTransform(true); |
| cec3b5da |
Poul Sander |
2026-02-19 19:17 |
22 |
|
| 7889168c |
Poul Sander |
2026-02-26 18:27 |
23 |
// Scale during decoding to avoid hitting the allocation limit |
| 7889168c |
Poul Sander |
2026-02-26 18:27 |
24 |
// on very large images (Qt 6 defaults to 128 MB). |
| 7889168c |
Poul Sander |
2026-02-26 18:27 |
25 |
QSize fullSize = reader.size(); |
| 7889168c |
Poul Sander |
2026-02-26 18:27 |
26 |
if (fullSize.isValid()) { |
| 7889168c |
Poul Sander |
2026-02-26 18:27 |
27 |
QSize target = fullSize.scaled(128, 128, Qt::KeepAspectRatio); |
| 7889168c |
Poul Sander |
2026-02-26 18:27 |
28 |
reader.setScaledSize(target); |
| 7889168c |
Poul Sander |
2026-02-26 18:27 |
29 |
} |
| 7889168c |
Poul Sander |
2026-02-26 18:27 |
30 |
|
| cec3b5da |
Poul Sander |
2026-02-19 19:17 |
31 |
QImage image = reader.read(); |
| 6fcd6c90 |
Poul Sander |
2026-02-19 21:12 |
32 |
|
| 6fcd6c90 |
Poul Sander |
2026-02-19 21:12 |
33 |
if (*m_cancelFlag) |
| 6fcd6c90 |
Poul Sander |
2026-02-19 21:12 |
34 |
return; |
| cec3b5da |
Poul Sander |
2026-02-19 19:17 |
35 |
|
| cec3b5da |
Poul Sander |
2026-02-19 19:17 |
36 |
if (!image.isNull()) |
| cec3b5da |
Poul Sander |
2026-02-19 19:17 |
37 |
{ |
| cfe4a67b |
Poul Sander |
2026-02-19 23:06 |
38 |
// Scale to fit within 128x128 keeping aspect ratio |
| 7889168c |
Poul Sander |
2026-02-26 18:27 |
39 |
// (may already be the right size if setScaledSize succeeded) |
| b614a581 |
Poul Sander |
2026-02-19 21:18 |
40 |
QImage scaled = image.scaled(128, 128, |
| cfe4a67b |
Poul Sander |
2026-02-19 23:06 |
41 |
Qt::KeepAspectRatio, |
| b614a581 |
Poul Sander |
2026-02-19 21:18 |
42 |
Qt::SmoothTransformation); |
| cfe4a67b |
Poul Sander |
2026-02-19 23:06 |
43 |
// Center on a 128x128 canvas with transparent/white fill |
| cfe4a67b |
Poul Sander |
2026-02-19 23:06 |
44 |
QImage thumb(128, 128, QImage::Format_ARGB32_Premultiplied); |
| cfe4a67b |
Poul Sander |
2026-02-19 23:06 |
45 |
thumb.fill(Qt::transparent); |
| cfe4a67b |
Poul Sander |
2026-02-19 23:06 |
46 |
int x = (128 - scaled.width()) / 2; |
| cfe4a67b |
Poul Sander |
2026-02-19 23:06 |
47 |
int y = (128 - scaled.height()) / 2; |
| cfe4a67b |
Poul Sander |
2026-02-19 23:06 |
48 |
QPainter painter(&thumb); |
| cfe4a67b |
Poul Sander |
2026-02-19 23:06 |
49 |
painter.drawImage(x, y, scaled); |
| cfe4a67b |
Poul Sander |
2026-02-19 23:06 |
50 |
painter.end(); |
| 6fcd6c90 |
Poul Sander |
2026-02-19 21:12 |
51 |
emit finished(m_row, thumb); |
| cec3b5da |
Poul Sander |
2026-02-19 19:17 |
52 |
} |
| cec3b5da |
Poul Sander |
2026-02-19 19:17 |
53 |
} |
|
|
1970-01-01 00:00 |
54 |
|