Skip to content

Commit

Permalink
When loading files, take advantage of in-progress preloads if possible
Browse files Browse the repository at this point in the history
  • Loading branch information
jdpurcell committed Jul 6, 2024
1 parent c277a97 commit 573dd14
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
19 changes: 18 additions & 1 deletion src/qvimagecore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,11 @@ void QVImageCore::loadFile(const QString &fileName, bool isReloading)
delete cachedData;
loadPixmap(readData);
}
//or see if the preloader is already working on it
else if (preloadFilesInProgress.contains(sanitaryFileName))
{
waitingOnPreloadFile = sanitaryFileName;
}
else
{
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
Expand Down Expand Up @@ -512,9 +517,21 @@ void QVImageCore::requestCachingFile(const QString &filePath, const QColorSpace
if (imgFile.size()/1024 > QVImageCore::pixmapCache.maxCost()/2)
return;

preloadFilesInProgress.append(filePath);

auto *cacheFutureWatcher = new QFutureWatcher<ReadData>();
connect(cacheFutureWatcher, &QFutureWatcher<ReadData>::finished, this, [cacheFutureWatcher, this](){
addToCache(cacheFutureWatcher->result());
const ReadData readData = cacheFutureWatcher->result();
if (waitingOnPreloadFile == readData.absoluteFilePath)
{
loadPixmap(readData);
waitingOnPreloadFile = QString();
}
else
{
addToCache(readData);
}
preloadFilesInProgress.removeAll(readData.absoluteFilePath);
cacheFutureWatcher->deleteLater();
});

Expand Down
2 changes: 2 additions & 0 deletions src/qvimagecore.h
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,8 @@ class QVImageCore : public QObject
unsigned randomSortSeed;

QStringList lastFilesPreloaded;
QStringList preloadFilesInProgress;
QString waitingOnPreloadFile;

int largestDimension;

Expand Down

0 comments on commit 573dd14

Please sign in to comment.