game_launcher 1.1.0
Modern Linux game launcher with Steam / Proton / Wine integration
Loading...
Searching...
No Matches
qt_image_service.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// ==============================================================================
3// qt/qt_image_service.cpp
4// ==============================================================================
5// Qt implementation of IImageService
6// ==============================================================================
7
9
10#include <QFile>
11#include <QImageReader>
12
13bool QtImageService::loadImage(const std::string& path, int* width, int* height)
14{
15 std::lock_guard<std::mutex> lock(cacheMutex_);
16
17 // Check if already cached
18 auto it = cache_.find(path);
19 if (it != cache_.end())
20 {
21 if (width)
22 *width = it->second.width;
23 if (height)
24 *height = it->second.height;
25 return true;
26 }
27
28 // Check if file exists
29 QString qpath = QString::fromStdString(path);
30 if (!QFile::exists(qpath))
31 {
32 if (width)
33 *width = 0;
34 if (height)
35 *height = 0;
36 return false;
37 }
38
39 // Load the image
40 QPixmap pixmap(qpath);
41 if (pixmap.isNull())
42 {
43 if (width)
44 *width = 0;
45 if (height)
46 *height = 0;
47 return false;
48 }
49
50 // Cache the image
51 CachedImage cached;
52 cached.pixmap = pixmap;
53 cached.width = pixmap.width();
54 cached.height = pixmap.height();
55 cache_[path] = cached;
56
57 if (width)
58 *width = cached.width;
59 if (height)
60 *height = cached.height;
61
62 return true;
63}
64
65bool QtImageService::isImageLoaded(const std::string& path) const
66{
67 std::lock_guard<std::mutex> lock(cacheMutex_);
68 return cache_.find(path) != cache_.end();
69}
70
71void QtImageService::unloadImage(const std::string& path)
72{
73 std::lock_guard<std::mutex> lock(cacheMutex_);
74 cache_.erase(path);
75}
76
78{
79 std::lock_guard<std::mutex> lock(cacheMutex_);
80 cache_.clear();
81}
82
83bool QtImageService::getImageDimensions(const std::string& path, int* width, int* height) const
84{
85 std::lock_guard<std::mutex> lock(cacheMutex_);
86
87 auto it = cache_.find(path);
88 if (it == cache_.end())
89 {
90 if (width)
91 *width = 0;
92 if (height)
93 *height = 0;
94 return false;
95 }
96
97 if (width)
98 *width = it->second.width;
99 if (height)
100 *height = it->second.height;
101 return true;
102}
103
104QPixmap QtImageService::getPixmap(const std::string& path)
105{
106 std::lock_guard<std::mutex> lock(cacheMutex_);
107
108 auto it = cache_.find(path);
109 if (it != cache_.end())
110 {
111 return it->second.pixmap;
112 }
113
114 // Try to load if not cached
115 QString qpath = QString::fromStdString(path);
116 if (!QFile::exists(qpath))
117 {
118 return QPixmap();
119 }
120
121 QPixmap pixmap(qpath);
122 if (pixmap.isNull())
123 {
124 return QPixmap();
125 }
126
127 // Cache and return
128 CachedImage cached;
129 cached.pixmap = pixmap;
130 cached.width = pixmap.width();
131 cached.height = pixmap.height();
132 cache_[path] = cached;
133
134 return pixmap;
135}
136
137QPixmap QtImageService::getScaledPixmap(const std::string& path, int maxWidth, int maxHeight)
138{
139 QPixmap original = getPixmap(path);
140 if (original.isNull())
141 {
142 return QPixmap();
143 }
144
145 return original.scaled(maxWidth, maxHeight, Qt::KeepAspectRatio, Qt::SmoothTransformation);
146}
void unloadImage(const std::string &path) override
Unload a specific image and free resources.
bool loadImage(const std::string &path, int *width=nullptr, int *height=nullptr) override
Load an image from disk.
QPixmap getPixmap(const std::string &path)
void clearCache() override
Clear all cached images and free resources.
bool getImageDimensions(const std::string &path, int *width, int *height) const override
Get dimensions of a loaded image.
QPixmap getScaledPixmap(const std::string &path, int maxWidth, int maxHeight)
bool isImageLoaded(const std::string &path) const override
Check if an image is already loaded/cached.