Skip to content

Commit

Permalink
Add tile cache, avoid flickering
Browse files Browse the repository at this point in the history
  • Loading branch information
jmlich committed Feb 2, 2025
1 parent e3d9e8f commit 45a2f09
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 4 deletions.
4 changes: 1 addition & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,7 @@ add_definitions( -DGIT_VERSION="${GIT_VERSION}" )

find_package(Qt5 COMPONENTS Core Quick Xml Widgets Charts LinguistTools REQUIRED)
find_package(Qt5QuickCompiler)
qtquick_compiler_add_resources(RESOURCES viewer.qrc)
#qt5_add_resources(RESOURCES viewer.qrc)

SET(RESOURCES viewer.qrc)

file(GLOB TS_FILES i18n/*.ts)

Expand Down
39 changes: 38 additions & 1 deletion src/qml/PinchMap.qml
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,7 @@ Rectangle {
var l = getCenter()
longitude = l[1]
latitude = l[0]
cleanCache();
canvas.requestPaint()

}
Expand Down Expand Up @@ -495,9 +496,45 @@ Rectangle {
}

function tileUrl(tx, ty) {
return tileUrlMultiple(tx, ty, url, true);
var imageUrl = tileUrlMultiple(tx, ty, url, true)
for (var i = 0; i < imageCache.length; i++) {
if (imageCache[i].cacheUrl === imageUrl) {
// console.log("Cache hit:", imageUrl)
imageCache[i].lastHit = new Date();
return imageCache[i].source
}
}

console.log("cache miss ("+imageCache.length+"): " + imageUrl )
var newImage = Qt.createQmlObject(
'import QtQuick 2.15;
Image {
property var lastHit: new Date();
property string cacheUrl: "'+imageUrl+'";
visible: false;
source: "' + imageUrl + '"
}', parent, "dynamicImage")
imageCache.push(newImage)
return newImage.source;
}

function cleanCache() {
if (imageCache.length < 100) {
return;
}
var someTimeAgo = new Date().getTime() - 60000;

for (var i = 0; i < imageCache.length; i++) {
if (imageCache[i].lastHit.getTime() < someTimeAgo) {
// console.log("Removing stale cache item: " + imageCache[i].cacheUrl);
imageCache[i].destroy();
imageCache.splice(i, 1);
}
}
console.log("after cleanup imageCache.length: " + imageCache.length)
}

property var imageCache: []

function tileUrlMultiple(tx, ty, baseUrl, first) {

Expand Down

0 comments on commit 45a2f09

Please sign in to comment.