| 1 |
/* ============================================================ |
| 2 |
* |
| 3 |
* This file is a part of digiKam project |
| 4 |
* https://www.digikam.org |
| 5 |
* |
| 6 |
* Date : 2007-06-05 |
| 7 |
* Description : Thumbnail loading |
| 8 |
* |
| 9 |
* SPDX-FileCopyrightText: 2006-2011 by Marcel Wiesweg <marcel dot wiesweg at gmx dot de> |
| 10 |
* SPDX-FileCopyrightText: 2005-2026 by Gilles Caulier <caulier dot gilles at gmail dot com> |
| 11 |
* SPDX-FileCopyrightText: 2015 by Mohamed_Anwer <m_dot_anwer at gmx dot com> |
| 12 |
* |
| 13 |
* SPDX-License-Identifier: GPL-2.0-or-later |
| 14 |
* |
| 15 |
* ============================================================ */ |
| 16 |
|
| 17 |
#include "thumbnailloadthread_p.h" |
| 18 |
|
| 19 |
namespace Digikam |
| 20 |
{ |
| 21 |
|
| 22 |
Q_GLOBAL_STATIC(ThumbnailLoadThreadStaticPriv, static_d) |
| 23 |
Q_GLOBAL_STATIC(ThumbnailLoadThread, defaultObject) |
| 24 |
Q_GLOBAL_STATIC(ThumbnailLoadThread, defaultIconViewObject) |
| 25 |
|
| 26 |
// --- Creating loading descriptions --- |
| 27 |
|
| 28 |
LoadingDescription ThumbnailLoadThread::Private::createLoadingDescription(const ThumbnailIdentifier& identifier, |
| 29 |
int size, |
| 30 |
bool setLastDescription) |
| 31 |
{ |
| 32 |
size = thumbnailSizeForPixmapSize(size); |
| 33 |
|
| 34 |
LoadingDescription description(identifier.filePath, PreviewSettings(), size, |
| 35 |
LoadingDescription::NoColorConversion, |
| 36 |
LoadingDescription::PreviewParameters::Thumbnail); |
| 37 |
description.previewParameters.storageReference = identifier.id; |
| 38 |
|
| 39 |
if (IccSettings::instance()->useManagedPreviews()) |
| 40 |
{ |
| 41 |
description.postProcessingParameters.colorManagement = LoadingDescription::ConvertForDisplay; |
| 42 |
description.postProcessingParameters.setProfile(static_d->profile); |
| 43 |
} |
| 44 |
|
| 45 |
if (setLastDescription) |
| 46 |
{ |
| 47 |
lastDescriptions.clear(); |
| 48 |
lastDescriptions << description; |
| 49 |
} |
| 50 |
|
| 51 |
return description; |
| 52 |
} |
| 53 |
|
| 54 |
LoadingDescription ThumbnailLoadThread::Private::createLoadingDescription(const ThumbnailIdentifier& identifier, |
| 55 |
int size, |
| 56 |
const QRect& detailRect, |
| 57 |
bool setLastDescription) |
| 58 |
{ |
| 59 |
size = thumbnailSizeForPixmapSize(size); |
| 60 |
|
| 61 |
LoadingDescription description(identifier.filePath, PreviewSettings(), size, |
| 62 |
LoadingDescription::NoColorConversion, |
| 63 |
LoadingDescription::PreviewParameters::DetailThumbnail); |
| 64 |
|
| 65 |
description.previewParameters.storageReference = identifier.id; |
| 66 |
description.previewParameters.extraParameter = detailRect; |
| 67 |
|
| 68 |
if (IccSettings::instance()->useManagedPreviews()) |
| 69 |
{ |
| 70 |
description.postProcessingParameters.colorManagement = LoadingDescription::ConvertForDisplay; |
| 71 |
description.postProcessingParameters.setProfile(static_d->profile); |
| 72 |
} |
| 73 |
|
| 74 |
if (setLastDescription) |
| 75 |
{ |
| 76 |
lastDescriptions.clear(); |
| 77 |
lastDescriptions << description; |
| 78 |
} |
| 79 |
|
| 80 |
return description; |
| 81 |
} |
| 82 |
|
| 83 |
// --------------------------------------------------------------------------- |
| 84 |
|
| 85 |
ThumbnailLoadThread::ThumbnailLoadThread(QObject* const parent) |
| 86 |
: ManagedLoadSaveThread(parent), |
| 87 |
d (new Private) |
| 88 |
{ |
| 89 |
static_d->firstThreadCreated = true; |
| 90 |
d->creator = new ThumbnailCreator(static_d->storageMethod); |
| 91 |
|
| 92 |
if (static_d->provider) |
| 93 |
{ |
| 94 |
d->creator->setThumbnailInfoProvider(static_d->provider); |
| 95 |
} |
| 96 |
|
| 97 |
d->creator->setOnlyLargeThumbnails(true); |
| 98 |
d->creator->setRemoveAlphaChannel(true); |
| 99 |
|
| 100 |
connect(this, SIGNAL(thumbnailsAvailable()), |
| 101 |
this, SLOT(slotThumbnailsAvailable())); |
| 102 |
} |
| 103 |
|
| 104 |
ThumbnailLoadThread::~ThumbnailLoadThread() |
| 105 |
{ |
| 106 |
shutDown(); |
| 107 |
|
| 108 |
delete d->creator; |
| 109 |
delete d; |
| 110 |
} |
| 111 |
|
| 112 |
ThumbnailLoadThread* ThumbnailLoadThread::defaultIconViewThread() |
| 113 |
{ |
| 114 |
return defaultIconViewObject; |
| 115 |
} |
| 116 |
|
| 117 |
ThumbnailLoadThread* ThumbnailLoadThread::defaultThread() |
| 118 |
{ |
| 119 |
return defaultObject; |
| 120 |
} |
| 121 |
|
| 122 |
void ThumbnailLoadThread::cleanUp() |
| 123 |
{ |
| 124 |
// NOTE: Nothing to do with Qt5 and Q_GLOBAL_STATIC. Qt clean up all automatically at end of application instance. |
| 125 |
// But stopping all running tasks to prevent a crash at end. |
| 126 |
|
| 127 |
defaultIconViewThread()->stopAllTasks(); |
| 128 |
defaultThread()->stopAllTasks(); |
| 129 |
|
| 130 |
defaultIconViewThread()->wait(); |
| 131 |
defaultThread()->wait(); |
| 132 |
} |
| 133 |
|
| 134 |
void ThumbnailLoadThread::initializeNoThumbnailStorage() |
| 135 |
{ |
| 136 |
if (static_d->firstThreadCreated) |
| 137 |
{ |
| 138 |
qCDebug(DIGIKAM_GENERAL_LOG) << "Call initializeNoThumbnailStorage at application start. " |
| 139 |
"There are already thumbnail loading threads created, " |
| 140 |
"and these will not be switched to disable storage."; |
| 141 |
} |
| 142 |
|
| 143 |
qCDebug(DIGIKAM_GENERAL_LOG) << "No storage of thumbnails in the disk cache"; |
| 144 |
static_d->storageMethod = ThumbnailCreator::NoThumbnailStorage; |
| 145 |
} |
| 146 |
|
| 147 |
void ThumbnailLoadThread::initializeThumbnailDatabase(const DbEngineParameters& params, |
| 148 |
ThumbnailInfoProvider* const provider) |
| 149 |
{ |
| 150 |
if (static_d->firstThreadCreated) |
| 151 |
{ |
| 152 |
qCDebug(DIGIKAM_GENERAL_LOG) << "Call initializeThumbnailDatabase at application start. " |
| 153 |
"There are already thumbnail loading threads created, " |
| 154 |
"and these will not be switched to use the database."; |
| 155 |
} |
| 156 |
|
| 157 |
ThumbsDbAccess::setParameters(params); |
| 158 |
|
| 159 |
if (ThumbsDbAccess::checkReadyForUse(nullptr)) |
| 160 |
{ |
| 161 |
qCDebug(DIGIKAM_GENERAL_LOG) << "Thumbnails database ready for use"; |
| 162 |
static_d->storageMethod = ThumbnailCreator::ThumbnailDatabase; |
| 163 |
static_d->provider = provider; |
| 164 |
} |
| 165 |
else |
| 166 |
{ |
| 167 |
QMessageBox::information(qApp->activeWindow(), |
| 168 |
i18nc("@title:window", |
| 169 |
"Failed to Initialize Thumbnails Database"), |
| 170 |
i18n("Error message: %1", ThumbsDbAccess().lastError())); |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
void ThumbnailLoadThread::setDisplayingWidget(QWidget* const widget) |
| 175 |
{ |
| 176 |
static_d->profile = IccManager::displayProfile(widget); |
| 177 |
} |
| 178 |
|
| 179 |
void ThumbnailLoadThread::setThumbnailSize(int size, bool forFace) |
| 180 |
{ |
| 181 |
d->size = size; |
| 182 |
|
| 183 |
if (forFace) |
| 184 |
{ |
| 185 |
d->creator->setThumbnailSize(size); |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
int ThumbnailLoadThread::maximumThumbnailSize() |
| 190 |
{ |
| 191 |
return ThumbnailSize::maxThumbsSize(); |
| 192 |
} |
| 193 |
|
| 194 |
int ThumbnailLoadThread::maximumThumbnailPixmapSize(bool highlight) |
| 195 |
{ |
| 196 |
if (highlight) |
| 197 |
{ |
| 198 |
return ThumbnailSize::maxThumbsSize(); |
| 199 |
} |
| 200 |
else |
| 201 |
{ |
| 202 |
return ThumbnailSize::maxThumbsSize() + 2; // see slotThumbnailLoaded |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
void ThumbnailLoadThread::setSendSurrogatePixmap(bool send) |
| 207 |
{ |
| 208 |
d->sendSurrogate = send; |
| 209 |
} |
| 210 |
|
| 211 |
void ThumbnailLoadThread::setPixmapRequested(bool wantPixmap) |
| 212 |
{ |
| 213 |
d->wantPixmap = wantPixmap; |
| 214 |
} |
| 215 |
|
| 216 |
void ThumbnailLoadThread::setHighlightPixmap(bool highlight) |
| 217 |
{ |
| 218 |
d->highlight = highlight; |
| 219 |
} |
| 220 |
|
| 221 |
ThumbnailCreator* ThumbnailLoadThread::thumbnailCreator() const |
| 222 |
{ |
| 223 |
return d->creator; |
| 224 |
} |
| 225 |
|
| 226 |
int ThumbnailLoadThread::thumbnailToPixmapSize(int size) const |
| 227 |
{ |
| 228 |
return d->pixmapSizeForThumbnailSize(size); |
| 229 |
} |
| 230 |
|
| 231 |
int ThumbnailLoadThread::thumbnailToPixmapSize(bool withHighlight, int size) |
| 232 |
{ |
| 233 |
if (withHighlight && (size >= 10)) |
| 234 |
{ |
| 235 |
return (size + 2); |
| 236 |
} |
| 237 |
|
| 238 |
return size; |
| 239 |
} |
| 240 |
|
| 241 |
int ThumbnailLoadThread::pixmapToThumbnailSize(int size) const |
| 242 |
{ |
| 243 |
return d->thumbnailSizeForPixmapSize(size); |
| 244 |
} |
| 245 |
|
| 246 |
bool ThumbnailLoadThread::find(const ThumbnailIdentifier& identifier, |
| 247 |
int size, |
| 248 |
QPixmap* retPixmap, |
| 249 |
bool emitSignal, |
| 250 |
const QRect& detailRect, |
| 251 |
bool onlyStorage) |
| 252 |
{ |
| 253 |
QPixmap pix; |
| 254 |
LoadingDescription description; |
| 255 |
|
| 256 |
if (detailRect.isNull()) |
| 257 |
{ |
| 258 |
description = d->createLoadingDescription(identifier, size); |
| 259 |
} |
| 260 |
else |
| 261 |
{ |
| 262 |
description = d->createLoadingDescription(identifier, size, detailRect); |
| 263 |
} |
| 264 |
|
| 265 |
if (onlyStorage) |
| 266 |
{ |
| 267 |
description.previewParameters.flags |= LoadingDescription::PreviewParameters::OnlyFromStorage; |
| 268 |
} |
| 269 |
|
| 270 |
QString cacheKey = description.cacheKey(); |
| 271 |
|
| 272 |
{ |
| 273 |
LoadingCache* const cache = LoadingCache::cache(); |
| 274 |
LoadingCache::CacheLock lock(cache); |
| 275 |
const QPixmap* cachePix = cache->retrieveThumbnailPixmap(cacheKey); |
| 276 |
|
| 277 |
if (cachePix) |
| 278 |
{ |
| 279 |
pix = *cachePix; |
| 280 |
} |
| 281 |
} |
| 282 |
|
| 283 |
if (!pix.isNull()) |
| 284 |
{ |
| 285 |
if (retPixmap) |
| 286 |
{ |
| 287 |
*retPixmap = pix; |
| 288 |
} |
| 289 |
|
| 290 |
if (emitSignal) |
| 291 |
{ |
| 292 |
load(description); |
| 293 |
|
| 294 |
Q_EMIT signalThumbnailLoaded(description, pix); |
| 295 |
} |
| 296 |
|
| 297 |
return true; |
| 298 |
} |
| 299 |
|
| 300 |
{ |
| 301 |
// If there is a result waiting for conversion to pixmap, return false - pixmap will come shortly |
| 302 |
|
| 303 |
QMutexLocker lock(&d->resultsMutex); |
| 304 |
|
| 305 |
if (d->collectedResults.contains(cacheKey)) |
| 306 |
{ |
| 307 |
return false; |
| 308 |
} |
| 309 |
} |
| 310 |
|
| 311 |
load(description); |
| 312 |
|
| 313 |
return false; |
| 314 |
} |
| 315 |
|
| 316 |
// --- Normal thumbnails --- |
| 317 |
|
| 318 |
bool ThumbnailLoadThread::find(const ThumbnailIdentifier& identifier, |
| 319 |
QPixmap& retPixmap, int size, bool onlyStorage) |
| 320 |
{ |
| 321 |
return find(identifier, size, &retPixmap, false, QRect(), onlyStorage); |
| 322 |
} |
| 323 |
|
| 324 |
bool ThumbnailLoadThread::find(const ThumbnailIdentifier& identifier, QPixmap& retPixmap) |
| 325 |
{ |
| 326 |
return find(identifier, retPixmap, d->size); |
| 327 |
} |
| 328 |
|
| 329 |
void ThumbnailLoadThread::find(const ThumbnailIdentifier& identifier) |
| 330 |
{ |
| 331 |
find(identifier, d->size); |
| 332 |
} |
| 333 |
|
| 334 |
void ThumbnailLoadThread::find(const ThumbnailIdentifier& identifier, int size) |
| 335 |
{ |
| 336 |
find(identifier, size, nullptr, true, QRect()); |
| 337 |
} |
| 338 |
|
| 339 |
void ThumbnailLoadThread::findGroup(QList<ThumbnailIdentifier>& identifiers) |
| 340 |
{ |
| 341 |
findGroup(identifiers, d->size); |
| 342 |
} |
| 343 |
|
| 344 |
void ThumbnailLoadThread::findGroup(QList<ThumbnailIdentifier>& identifiers, int size) |
| 345 |
{ |
| 346 |
if (!checkSize(size)) |
| 347 |
{ |
| 348 |
return; |
| 349 |
} |
| 350 |
|
| 351 |
QList<LoadingDescription> descriptions = d->makeDescriptions(identifiers, size); |
| 352 |
ManagedLoadSaveThread::prependThumbnailGroup(descriptions); |
| 353 |
} |
| 354 |
|
| 355 |
// --- Detail thumbnails --- |
| 356 |
|
| 357 |
bool ThumbnailLoadThread::find(const ThumbnailIdentifier& identifier, const QRect& rect, QPixmap& pixmap) |
| 358 |
{ |
| 359 |
return find(identifier, rect, pixmap, d->size); |
| 360 |
} |
| 361 |
|
| 362 |
bool ThumbnailLoadThread::find(const ThumbnailIdentifier& identifier, |
| 363 |
const QRect& rect, QPixmap& pixmap, int size, bool onlyStorage) |
| 364 |
{ |
| 365 |
return find(identifier, size, &pixmap, false, rect, onlyStorage); |
| 366 |
} |
| 367 |
|
| 368 |
void ThumbnailLoadThread::find(const ThumbnailIdentifier& identifier, const QRect& rect) |
| 369 |
{ |
| 370 |
find(identifier, rect, d->size); |
| 371 |
} |
| 372 |
|
| 373 |
void ThumbnailLoadThread::find(const ThumbnailIdentifier& identifier, const QRect& rect, int size) |
| 374 |
{ |
| 375 |
find(identifier, size, nullptr, true, rect); |
| 376 |
} |
| 377 |
|
| 378 |
void ThumbnailLoadThread::findGroup(const QList<QPair<ThumbnailIdentifier, QRect> >& idsAndRects) |
| 379 |
{ |
| 380 |
findGroup(idsAndRects, d->size); |
| 381 |
} |
| 382 |
|
| 383 |
void ThumbnailLoadThread::findGroup(const QList<QPair<ThumbnailIdentifier, QRect> >& idsAndRects, int size) |
| 384 |
{ |
| 385 |
if (!checkSize(size)) |
| 386 |
{ |
| 387 |
return; |
| 388 |
} |
| 389 |
|
| 390 |
QList<LoadingDescription> descriptions = d->makeDescriptions(idsAndRects, size); |
| 391 |
ManagedLoadSaveThread::prependThumbnailGroup(descriptions); |
| 392 |
} |
| 393 |
|
| 394 |
bool ThumbnailLoadThread::findBuffered(const ThumbnailIdentifier& identifier, |
| 395 |
const QRect& rect, QPixmap& pixmap, int size) |
| 396 |
{ |
| 397 |
LoadingDescription description; |
| 398 |
|
| 399 |
if (rect.isNull()) |
| 400 |
{ |
| 401 |
description = d->createLoadingDescription(identifier, size); |
| 402 |
} |
| 403 |
else |
| 404 |
{ |
| 405 |
description = d->createLoadingDescription(identifier, size, rect); |
| 406 |
} |
| 407 |
|
| 408 |
QString cacheKey = description.cacheKey(); |
| 409 |
|
| 410 |
{ |
| 411 |
LoadingCache* const cache = LoadingCache::cache(); |
| 412 |
LoadingCache::CacheLock lock(cache); |
| 413 |
const QPixmap* cachePix = cache->retrieveBufferedTPixmap(cacheKey); |
| 414 |
|
| 415 |
if (cachePix) |
| 416 |
{ |
| 417 |
pixmap = *cachePix; |
| 418 |
|
| 419 |
return true; |
| 420 |
} |
| 421 |
} |
| 422 |
|
| 423 |
return false; |
| 424 |
} |
| 425 |
|
| 426 |
// --- Preloading --- |
| 427 |
|
| 428 |
void ThumbnailLoadThread::preload(const ThumbnailIdentifier& identifier) |
| 429 |
{ |
| 430 |
preload(identifier, d->size); |
| 431 |
} |
| 432 |
|
| 433 |
void ThumbnailLoadThread::preload(const ThumbnailIdentifier& identifier, int size) |
| 434 |
{ |
| 435 |
LoadingDescription description = d->createLoadingDescription(identifier, size); |
| 436 |
|
| 437 |
if (d->checkDescription(description)) |
| 438 |
{ |
| 439 |
load(description, true); |
| 440 |
} |
| 441 |
} |
| 442 |
|
| 443 |
void ThumbnailLoadThread::preloadGroup(QList<ThumbnailIdentifier>& identifiers) |
| 444 |
{ |
| 445 |
preloadGroup(identifiers, d->size); |
| 446 |
} |
| 447 |
|
| 448 |
void ThumbnailLoadThread::preloadGroup(QList<ThumbnailIdentifier>& identifiers, int size) |
| 449 |
{ |
| 450 |
if (!checkSize(size)) |
| 451 |
{ |
| 452 |
return; |
| 453 |
} |
| 454 |
|
| 455 |
QList<LoadingDescription> descriptions = d->makeDescriptions(identifiers, size); |
| 456 |
ManagedLoadSaveThread::preloadThumbnailGroup(descriptions); |
| 457 |
} |
| 458 |
|
| 459 |
void ThumbnailLoadThread::pregenerateGroup(const QList<ThumbnailIdentifier>& identifiers) |
| 460 |
{ |
| 461 |
pregenerateGroup(identifiers, d->size); |
| 462 |
} |
| 463 |
|
| 464 |
void ThumbnailLoadThread::pregenerateGroup(const QList<ThumbnailIdentifier>& identifiers, int size) |
| 465 |
{ |
| 466 |
if (!checkSize(size)) |
| 467 |
{ |
| 468 |
return; |
| 469 |
} |
| 470 |
|
| 471 |
QList<LoadingDescription> descriptions = d->makeDescriptions(identifiers, size); |
| 472 |
|
| 473 |
for (int i = 0 ; i < descriptions.size() ; ++i) |
| 474 |
{ |
| 475 |
descriptions[i].previewParameters.flags |= LoadingDescription::PreviewParameters::OnlyPregenerate; |
| 476 |
} |
| 477 |
|
| 478 |
ManagedLoadSaveThread::preloadThumbnailGroup(descriptions); |
| 479 |
} |
| 480 |
|
| 481 |
// --- Basic load() --- |
| 482 |
|
| 483 |
void ThumbnailLoadThread::load(const LoadingDescription& desc) |
| 484 |
{ |
| 485 |
load(desc, false); |
| 486 |
} |
| 487 |
|
| 488 |
void ThumbnailLoadThread::load(const LoadingDescription& description, bool preload) |
| 489 |
{ |
| 490 |
if (!checkSize(description.previewParameters.size)) |
| 491 |
{ |
| 492 |
return; |
| 493 |
} |
| 494 |
|
| 495 |
if (preload) |
| 496 |
{ |
| 497 |
ManagedLoadSaveThread::preloadThumbnail(description); |
| 498 |
} |
| 499 |
else |
| 500 |
{ |
| 501 |
ManagedLoadSaveThread::loadThumbnail(description); |
| 502 |
} |
| 503 |
} |
| 504 |
|
| 505 |
QList<LoadingDescription> ThumbnailLoadThread::lastDescriptions() const |
| 506 |
{ |
| 507 |
return d->lastDescriptions; |
| 508 |
} |
| 509 |
|
| 510 |
bool ThumbnailLoadThread::checkSize(int size) |
| 511 |
{ |
| 512 |
size = d->thumbnailSizeForPixmapSize(size); |
| 513 |
double dpr = qApp->devicePixelRatio(); |
| 514 |
int maxThumbSize = (dpr > 1.0) ? ThumbnailSize::MAX |
| 515 |
: ThumbnailSize::maxThumbsSize(); |
| 516 |
|
| 517 |
if (size <= 0) |
| 518 |
{ |
| 519 |
qCDebug(DIGIKAM_GENERAL_LOG) << "ThumbnailLoadThread::load: No thumbnail size specified. Refusing to load thumbnail."; |
| 520 |
return false; |
| 521 |
} |
| 522 |
else if (size > maxThumbSize) |
| 523 |
{ |
| 524 |
qCDebug(DIGIKAM_GENERAL_LOG) << "ThumbnailLoadThread::load: Thumbnail size " << size |
| 525 |
<< " is larger than " << maxThumbSize << ". Refusing to load."; |
| 526 |
return false; |
| 527 |
} |
| 528 |
|
| 529 |
return true; |
| 530 |
} |
| 531 |
|
| 532 |
// --- Receiving --- |
| 533 |
|
| 534 |
/** |
| 535 |
* virtual method overridden from LoadSaveNotifier, implemented first by LoadSaveThread |
| 536 |
* called by ThumbnailTask from working thread |
| 537 |
*/ |
| 538 |
void ThumbnailLoadThread::thumbnailLoaded(const LoadingDescription& loadingDescription, const QImage& img) |
| 539 |
{ |
| 540 |
// call parent to send signalThumbnailLoaded(LoadingDescription, QImage) - signal is part of public API |
| 541 |
|
| 542 |
ManagedLoadSaveThread::thumbnailLoaded(loadingDescription, img); |
| 543 |
|
| 544 |
if (!d->wantPixmap) |
| 545 |
{ |
| 546 |
return; |
| 547 |
} |
| 548 |
|
| 549 |
// Store result in our list and fire one signal |
| 550 |
// This means there can be several results per pixmap, |
| 551 |
// to speed up cases where inter-thread communication is the limiting factor |
| 552 |
|
| 553 |
QMutexLocker lock(&d->resultsMutex); |
| 554 |
d->collectedResults.insert(loadingDescription.cacheKey(), ThumbnailResult(loadingDescription, img)); |
| 555 |
|
| 556 |
// only sent signal when flag indicates there is no signal on the way currently |
| 557 |
|
| 558 |
if (!d->notifiedForResults) |
| 559 |
{ |
| 560 |
d->notifiedForResults = true; |
| 561 |
|
| 562 |
Q_EMIT thumbnailsAvailable(); |
| 563 |
} |
| 564 |
} |
| 565 |
|
| 566 |
void ThumbnailLoadThread::slotThumbnailsAvailable() |
| 567 |
{ |
| 568 |
// harvest collected results safely into our thread |
| 569 |
|
| 570 |
QList<ThumbnailResult> results; |
| 571 |
{ |
| 572 |
QMutexLocker lock(&d->resultsMutex); |
| 573 |
results = d->collectedResults.values(); |
| 574 |
d->collectedResults.clear(); |
| 575 |
|
| 576 |
// reset flag so that for next result, the signal is sent again |
| 577 |
|
| 578 |
d->notifiedForResults = false; |
| 579 |
} |
| 580 |
|
| 581 |
for (const ThumbnailResult& result : std::as_const(results)) |
| 582 |
{ |
| 583 |
slotThumbnailLoaded(result.loadingDescription, result.image); |
| 584 |
} |
| 585 |
} |
| 586 |
|
| 587 |
void ThumbnailLoadThread::slotThumbnailLoaded(const LoadingDescription& description, const QImage& thumb) |
| 588 |
{ |
| 589 |
QPixmap pix; |
| 590 |
|
| 591 |
if (thumb.isNull()) |
| 592 |
{ |
| 593 |
pix = surrogatePixmap(description); |
| 594 |
} |
| 595 |
else |
| 596 |
{ |
| 597 |
int w = thumb.width(); |
| 598 |
int h = thumb.height(); |
| 599 |
|
| 600 |
// highlight only when requested and when thumbnail |
| 601 |
// width and height are greater than 10 |
| 602 |
|
| 603 |
if (d->highlight && ((w >= 10) && (h >= 10))) |
| 604 |
{ |
| 605 |
pix = QPixmap(w + 2, h + 2); |
| 606 |
QPainter p(&pix); |
| 607 |
p.setPen(QPen(Qt::black, 1)); |
| 608 |
p.drawRect(0, 0, w + 1, h + 1); |
| 609 |
p.drawImage(1, 1, thumb); |
| 610 |
} |
| 611 |
else |
| 612 |
{ |
| 613 |
pix = QPixmap::fromImage(thumb); |
| 614 |
} |
| 615 |
} |
| 616 |
|
| 617 |
// put into cache |
| 618 |
|
| 619 |
if (!pix.isNull()) |
| 620 |
{ |
| 621 |
LoadingCache* const cache = LoadingCache::cache(); |
| 622 |
LoadingCache::CacheLock lock(cache); |
| 623 |
cache->putThumbnail(description.cacheKey(), pix, description.filePath); |
| 624 |
} |
| 625 |
|
| 626 |
Q_EMIT signalThumbnailLoaded(description, pix); |
| 627 |
} |
| 628 |
|
| 629 |
QPixmap ThumbnailLoadThread::surrogatePixmap(const LoadingDescription& description) |
| 630 |
{ |
| 631 |
if (!d->sendSurrogate) |
| 632 |
{ |
| 633 |
return QPixmap(); |
| 634 |
} |
| 635 |
|
| 636 |
QPixmap pix; |
| 637 |
|
| 638 |
QMimeType mimeType = QMimeDatabase().mimeTypeForFile(description.filePath); |
| 639 |
|
| 640 |
if (mimeType.isValid()) |
| 641 |
{ |
| 642 |
pix = QIcon::fromTheme(mimeType.genericIconName()).pixmap(128); |
| 643 |
} |
| 644 |
|
| 645 |
if (pix.isNull()) |
| 646 |
{ |
| 647 |
pix = QIcon::fromTheme(QLatin1String("application-x-zerosize")).pixmap(128); |
| 648 |
} |
| 649 |
|
| 650 |
if (pix.isNull()) |
| 651 |
{ |
| 652 |
// give up |
| 653 |
return QPixmap(); |
| 654 |
} |
| 655 |
|
| 656 |
// Resize icon to the right size depending of current settings. |
| 657 |
|
| 658 |
QSize size(pix.size()); |
| 659 |
size.scale(description.previewParameters.size, description.previewParameters.size, Qt::KeepAspectRatio); |
| 660 |
|
| 661 |
if (!pix.isNull() && (size.width() < pix.width()) && (size.height() < pix.height())) |
| 662 |
{ |
| 663 |
// only scale down |
| 664 |
// do not scale up, looks bad |
| 665 |
|
| 666 |
pix = pix.scaled(size, Qt::IgnoreAspectRatio, Qt::SmoothTransformation); |
| 667 |
} |
| 668 |
|
| 669 |
return pix; |
| 670 |
} |
| 671 |
|
| 672 |
// --- Utilities --- |
| 673 |
|
| 674 |
void ThumbnailLoadThread::storeDetailThumbnail(const QString& filePath, const QRect& detailRect, const QImage& image, bool isFace) |
| 675 |
{ |
| 676 |
Q_UNUSED(isFace); |
| 677 |
d->creator->storeDetailThumbnail(filePath, detailRect, image); |
| 678 |
} |
| 679 |
|
| 680 |
int ThumbnailLoadThread::storedSize() const |
| 681 |
{ |
| 682 |
return d->creator->storedSize(); |
| 683 |
} |
| 684 |
|
| 685 |
void ThumbnailLoadThread::deleteThumbnail(const QString& filePath) |
| 686 |
{ |
| 687 |
{ |
| 688 |
LoadingCache* const cache = LoadingCache::cache(); |
| 689 |
LoadingCache::CacheLock lock(cache); |
| 690 |
QStringList possibleKeys = LoadingDescription::possibleThumbnailCacheKeys(filePath); |
| 691 |
|
| 692 |
for (const QString& cacheKey : std::as_const(possibleKeys)) |
| 693 |
{ |
| 694 |
cache->removeThumbnail(cacheKey); |
| 695 |
} |
| 696 |
} |
| 697 |
|
| 698 |
ThumbnailCreator creator(static_d->storageMethod); |
| 699 |
|
| 700 |
if (static_d->provider) |
| 701 |
{ |
| 702 |
creator.setThumbnailInfoProvider(static_d->provider); |
| 703 |
} |
| 704 |
|
| 705 |
creator.deleteThumbnailsFromDisk(filePath); |
| 706 |
} |
| 707 |
|
| 708 |
ThumbnailImageCatcher::ThumbnailImageCatcher(QObject* const parent) |
| 709 |
: QObject(parent), |
| 710 |
d (new Private) |
| 711 |
{ |
| 712 |
} |
| 713 |
|
| 714 |
ThumbnailImageCatcher::ThumbnailImageCatcher(ThumbnailLoadThread* const thread, QObject* const parent) |
| 715 |
: QObject(parent), |
| 716 |
d (new Private) |
| 717 |
{ |
| 718 |
setThumbnailLoadThread(thread); |
| 719 |
} |
| 720 |
|
| 721 |
ThumbnailImageCatcher::~ThumbnailImageCatcher() |
| 722 |
{ |
| 723 |
delete d; |
| 724 |
} |
| 725 |
|
| 726 |
ThumbnailLoadThread* ThumbnailImageCatcher::thread() const |
| 727 |
{ |
| 728 |
return d->thread; |
| 729 |
} |
| 730 |
|
| 731 |
void ThumbnailImageCatcher::setThumbnailLoadThread(ThumbnailLoadThread* const thread) |
| 732 |
{ |
| 733 |
if (d->thread == thread) |
| 734 |
{ |
| 735 |
return; |
| 736 |
} |
| 737 |
|
| 738 |
d->state = Private::Inactive; |
| 739 |
|
| 740 |
if (d->thread) |
| 741 |
{ |
| 742 |
disconnect(d->thread, SIGNAL(signalQImageThumbnailLoaded(LoadingDescription,QImage)), |
| 743 |
this, SLOT(slotThumbnailLoaded(LoadingDescription,QImage))); |
| 744 |
} |
| 745 |
|
| 746 |
d->thread = thread; |
| 747 |
|
| 748 |
{ |
| 749 |
QMutexLocker lock(&d->mutex); |
| 750 |
d->reset(); |
| 751 |
} |
| 752 |
|
| 753 |
if (d->thread) |
| 754 |
{ |
| 755 |
connect(d->thread, SIGNAL(signalQImageThumbnailLoaded(LoadingDescription,QImage)), |
| 756 |
this, SLOT(slotThumbnailLoaded(LoadingDescription,QImage)), |
| 757 |
Qt::DirectConnection); |
| 758 |
} |
| 759 |
} |
| 760 |
|
| 761 |
void ThumbnailImageCatcher::setActive(bool active) |
| 762 |
{ |
| 763 |
if (d->active == active) |
| 764 |
{ |
| 765 |
return; |
| 766 |
} |
| 767 |
|
| 768 |
if (!active) |
| 769 |
{ |
| 770 |
cancel(); |
| 771 |
} |
| 772 |
|
| 773 |
QMutexLocker lock(&d->mutex); |
| 774 |
d->active = active; |
| 775 |
d->reset(); |
| 776 |
} |
| 777 |
|
| 778 |
void ThumbnailImageCatcher::cancel() |
| 779 |
{ |
| 780 |
QMutexLocker lock(&d->mutex); |
| 781 |
|
| 782 |
if (d->state == Private::Waiting) |
| 783 |
{ |
| 784 |
d->state = Private::Quitting; |
| 785 |
d->condVar.wakeOne(); |
| 786 |
} |
| 787 |
} |
| 788 |
|
| 789 |
void ThumbnailImageCatcher::slotThumbnailLoaded(const LoadingDescription& description, const QImage& image) |
| 790 |
{ |
| 791 |
// We are in the thumbnail thread here, DirectConnection! |
| 792 |
|
| 793 |
QMutexLocker lock(&d->mutex); |
| 794 |
|
| 795 |
switch (d->state) |
| 796 |
{ |
| 797 |
case Private::Inactive: |
| 798 |
{ |
| 799 |
break; |
| 800 |
} |
| 801 |
|
| 802 |
case Private::Accepting: |
| 803 |
{ |
| 804 |
d->intermediate << Private::CatcherResult(description, image); |
| 805 |
break; |
| 806 |
} |
| 807 |
|
| 808 |
case Private::Waiting: |
| 809 |
{ |
| 810 |
d->harvest(description, image); |
| 811 |
break; |
| 812 |
} |
| 813 |
|
| 814 |
case Private::Quitting: |
| 815 |
{ |
| 816 |
break; |
| 817 |
} |
| 818 |
} |
| 819 |
} |
| 820 |
|
| 821 |
int ThumbnailImageCatcher::enqueue() |
| 822 |
{ |
| 823 |
QList<LoadingDescription> descriptions = d->thread->lastDescriptions(); |
| 824 |
|
| 825 |
QMutexLocker lock(&d->mutex); |
| 826 |
|
| 827 |
for (const LoadingDescription& description : std::as_const(descriptions)) |
| 828 |
{ |
| 829 |
d->tasks << Private::CatcherResult(description); |
| 830 |
} |
| 831 |
|
| 832 |
return descriptions.size(); |
| 833 |
} |
| 834 |
|
| 835 |
QList<QImage> ThumbnailImageCatcher::waitForThumbnails() |
| 836 |
{ |
| 837 |
if (!d->thread || d->tasks.isEmpty() || !d->active) |
| 838 |
{ |
| 839 |
return QList<QImage>(); |
| 840 |
} |
| 841 |
|
| 842 |
QMutexLocker lock(&d->mutex); |
| 843 |
d->state = Private::Waiting; |
| 844 |
|
| 845 |
// first, handle results received between request and calling this method |
| 846 |
|
| 847 |
for (const Private::CatcherResult& result : std::as_const(d->intermediate)) |
| 848 |
{ |
| 849 |
d->harvest(result.description, result.image); |
| 850 |
} |
| 851 |
|
| 852 |
d->intermediate.clear(); |
| 853 |
|
| 854 |
// Now wait for the rest to arrive. If already finished, state will be Quitting |
| 855 |
|
| 856 |
while (d->state == Private::Waiting) |
| 857 |
{ |
| 858 |
d->condVar.wait(&d->mutex); |
| 859 |
} |
| 860 |
|
| 861 |
QList<QImage> result; |
| 862 |
|
| 863 |
for (const Private::CatcherResult& task : std::as_const(d->tasks)) |
| 864 |
{ |
| 865 |
result << task.image; |
| 866 |
} |
| 867 |
|
| 868 |
d->reset(); |
| 869 |
|
| 870 |
return result; |
| 871 |
} |
| 872 |
|
| 873 |
} // namespace Digikam |
| 874 |
|
| 875 |
#include "moc_thumbnailloadthread.cpp" |