Skip to content

Commit

Permalink
When file load is requested, take advantage of in-progress preloads i…
Browse files Browse the repository at this point in the history
…f possible
  • Loading branch information
jdpurcell committed Feb 27, 2024
1 parent 8473768 commit 4516f32
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/qvimagecore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ void QVImageCore::loadFile(const QString &fileName, bool isReloading)
delete cachedData;
loadPixmap(readData);
}
else if (preloadsInProgress.contains(sanitaryFileName))
{
waitingOnPreloadPath = sanitaryFileName;
}
else
{
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
Expand Down Expand Up @@ -480,9 +484,21 @@ void QVImageCore::requestCachingFile(const QString &filePath, const QColorSpace
if (imgFile.size()/1024 > QVImageCore::pixmapCache.maxCost()/2)
return;

preloadsInProgress.insert(filePath);

auto *cacheFutureWatcher = new QFutureWatcher<ReadData>();
connect(cacheFutureWatcher, &QFutureWatcher<ReadData>::finished, this, [cacheFutureWatcher, this](){
addToCache(cacheFutureWatcher->result());
const ReadData readData = cacheFutureWatcher->result();
if (waitingOnPreloadPath == readData.absoluteFilePath)
{
loadPixmap(readData);
waitingOnPreloadPath = {};
}
else
{
addToCache(readData);
}
preloadsInProgress.remove(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 @@ -127,6 +127,8 @@ class QVImageCore : public QObject
quint32 baseRandomSortSeed {static_cast<quint32>(std::chrono::system_clock::now().time_since_epoch().count())};

QStringList lastFilesPreloaded;
QSet<QString> preloadsInProgress;
QString waitingOnPreloadPath;

int largestDimension {0};

Expand Down

0 comments on commit 4516f32

Please sign in to comment.