KOSMIndoorMap

texturecache.cpp
1/*
2 SPDX-FileCopyrightText: 2023 Volker Krause <vkrause@kde.org>
3 SPDX-License-Identifier: LGPL-2.0-or-later
4*/
5
6#include "texturecache_p.h"
7#include "logging.h"
8
9#include <QDebug>
10#include <QFile>
11#include <QGuiApplication>
12#include <QImageReader>
13
14using namespace KOSMIndoorMap;
15
16TextureCache::TextureCache() = default;
17TextureCache::~TextureCache() = default;
18
19QImage TextureCache::image(const QString &name) const
20{
21 auto it = std::lower_bound(m_cache.begin(), m_cache.end(), name, [](const auto &lhs, const auto &rhs) { return lhs.name < rhs; });
22 if (it != m_cache.end() && (*it).name == name) {
23 return (*it).image;
24 }
25
26 CacheEntry entry;
27 entry.name = name;
28
29 const QString fileName = QLatin1String(":/org.kde.kosmindoormap/assets/textures/") + name;
30 if (name.endsWith(QLatin1String(".svg"))) {
31 QImageReader imgReader(fileName, "svg");
32 imgReader.setScaledSize(imgReader.size() * qGuiApp->devicePixelRatio());
33 entry.image = imgReader.read();
34 entry.image.setDevicePixelRatio(qGuiApp->devicePixelRatio());
35 } else {
36 // TODO high dpi raster image loading
37 // QImageReader is supposed to do that transparently, but that doesn't seem to work here?
38 QImageReader imgReader(fileName);
39 entry.image = imgReader.read();
40 }
41
42 if (entry.image.isNull()) {
43 qCWarning(Log) << "failed to load texture:" << name;
44 } else {
45 qCDebug(Log) << "loaded texture:" << entry.name << entry.image;
46 }
47
48 it = m_cache.insert(it, std::move(entry));
49 return (*it).image;
50}
OSM-based multi-floor indoor maps for buildings.
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri May 3 2024 11:50:52 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.