GUIFramework 1.1.0
Framework for desktop GUI applications in C++.
Loading...
Searching...
No Matches
BaseLoadableHolder.cpp
Go to the documentation of this file.
2
3#include "Utility/Utility.h"
4
5using namespace std;
6
7namespace gui_framework
8{
9 namespace utility
10 {
11 BaseLoadableHolder::imageData::imageData(uint16_t index, imageType type, any&& data) :
12 index(index),
13 type(type),
14 data(move(data))
15 {
16
17 }
18
20 index(other.index),
21 type(other.type),
22 data(other.data)
23 {
24 const_cast<imageData&>(other).data.reset();
25 }
26
28 index(other.index),
29 type(other.type),
30 data(move(other.data))
31 {
32 other.data.reset();
33 }
34
36 {
37 index = other.index;
38 type = other.type;
39 data = other.data;
40
41 const_cast<imageData&>(other).data.reset();
42
43 return *this;
44 }
45
47 {
48 index = other.index;
49 type = other.type;
50 data = move(other.data);
51
52 other.data.reset();
53
54 return *this;
55 }
56
58 {
59 if (!data.has_value())
60 {
61 return;
62 }
63
64 switch (type)
65 {
66 case imageType::bitmap:
67 DeleteObject(any_cast<HBITMAP>(data));
68
69 break;
70 case imageType::icon:
71 DestroyIcon(any_cast<HICON>(data));
72
73 break;
74 case imageType::cursor:
75 DestroyCursor(any_cast<HCURSOR>(data));
76
77 break;
78 }
79 }
80
81 json::JSONBuilder BaseLoadableHolder::getStructure() const
82 {
83 using json::utility::jsonObject;
84
85 uint32_t codepage = ISerializable::getCodepage();
86 json::JSONBuilder builder(codepage);
87 jsonObject object;
88 vector<jsonObject> jsonImages;
89
90 object.data.push_back({ "imagesWidth"s, static_cast<uint64_t>(imagesWidth) });
91 object.data.push_back({ "imagesHeight"s, static_cast<uint64_t>(imagesHeight) });
92
93 for (const auto& [path, data] : images)
94 {
95 jsonObject image;
96
97 image.data.push_back({ "pathToImage"s, utility::to_string(path, codepage) });
98 image.data.push_back({ "type"s, static_cast<uint64_t>(data.type) });
99
100 json::utility::appendArray(move(image), jsonImages);
101 }
102
103 object.data.push_back({ "images"s, move(jsonImages) });
104
105 builder.append("imageHolder"s, move(object));
106
107 return builder;
108 }
109
110 uint16_t BaseLoadableHolder::insertImage(const filesystem::path& pathToImage, imageType type)
111 {
112 HBITMAP image = static_cast<HBITMAP>(LoadImageW(nullptr, pathToImage.wstring().data(), static_cast<uint32_t>(type), imagesWidth, imagesHeight, LR_LOADFROMFILE));
113 uint16_t resultIndex;
114
115 switch (type)
116 {
117 case imageType::bitmap:
118 resultIndex = images.emplace(pathToImage, imageData(ImageList_Add(imageList, image, NULL), type, image)).first->second.index;
119
120 break;
121 case imageType::icon:
122 resultIndex = images.emplace(pathToImage, imageData(ImageList_AddIcon(imageList, reinterpret_cast<HICON>(image)), type, reinterpret_cast<HICON>(image))).first->second.index;
123
124 break;
125 case imageType::cursor:
126 resultIndex = images.emplace(pathToImage, imageData(ImageList_AddIcon(imageList, reinterpret_cast<HCURSOR>(image)), type, reinterpret_cast<HCURSOR>(image))).first->second.index;
127
128 break;
129
130 default:
131 resultIndex = static_cast<uint16_t>(-1);
132
133 DeleteObject(image);
134
135 break;
136 }
137
138 return resultIndex;
139 }
140
142 imageList(ImageList_Create(imagesWidth, imagesHeight, ILC_COLOR32, 0, static_cast<int>(count))),
145 {
146
147 }
148
149 void BaseLoadableHolder::removeImage(const filesystem::path& pathToImage)
150 {
151 auto it = images.find(pathToImage);
152
153 if (it != images.end())
154 {
155 ImageList_Remove(imageList, it->second.index);
156
157 images.erase(it);
158 }
159 }
160
161 void BaseLoadableHolder::removeImage(uint16_t imageIndex)
162 {
163 for (const auto& [key, value] : images)
164 {
165 if (value.index == imageIndex)
166 {
167 images.erase(key);
168
169 ImageList_Remove(imageList, imageIndex);
170
171 break;
172 }
173 }
174 }
175
176 bool BaseLoadableHolder::contains(const filesystem::path& pathToImage) const
177 {
178 return images.contains(pathToImage);
179 }
180
182 {
183 return imagesWidth;
184 }
185
187 {
188 return imagesHeight;
189 }
190
191 uint16_t BaseLoadableHolder::getImageIndex(const filesystem::path& pathToImage) const
192 {
193 return images.at(pathToImage).index;
194 }
195
196 BaseLoadableHolder::imageType BaseLoadableHolder::getImageType(const filesystem::path& pathToImage) const
197 {
198 return images.at(pathToImage).type;
199 }
200
202 {
203 for (const auto& [key, value] : images)
204 {
205 if (value.index == imageIndex)
206 {
207 return value.type;
208 }
209 }
210
211 throw out_of_range(format("Can't find imageType at {} index"sv, imageIndex));
212 }
213
215 {
216 return imageList;
217 }
218
219 uint16_t BaseLoadableHolder::operator [] (const filesystem::path& pathToImage) const
220 {
221 return images.at(pathToImage).index;
222 }
223
224 filesystem::path BaseLoadableHolder::operator [] (uint16_t index) const
225 {
226 for (const auto& [key, value] : images)
227 {
228 if (value.index == index)
229 {
230 return key;
231 }
232 }
233
234 throw out_of_range(format("Can't find image at {} index"sv, index));
235 }
236
238 {
239 vector<filesystem::path> imagesPaths;
240
241 imagesPaths.reserve(images.size());
242
243 for (const auto& [path, data] : images)
244 {
245 imagesPaths.push_back(path);
246 }
247
248 return iterators::loadable_forward_iterator(move(imagesPaths), 0);
249 }
250
252 {
253 vector<filesystem::path> imagesPaths;
254
255 imagesPaths.reserve(images.size());
256
257 for (const auto& [path, data] : images)
258 {
259 imagesPaths.push_back(path);
260 }
261
262 return iterators::loadable_const_forward_iterator(move(imagesPaths), 0);
263 }
264
269
274
275 pair<string, json::utility::jsonObject::variantType>& BaseLoadableHolder::loadBaseLoadableHolderStructure(json::utility::jsonObject& current) const
276 {
277 return current.data.emplace_back(make_pair("imageHolder"s, move(this->getStructure()["imageHolder"])));
278 }
279
281 {
282 ImageList_Destroy(imageList);
283 }
284 }
285}
IBaseConstForwardIterator implementation for BaseLoadableHolder.
IBaseForwardIterator implementation for BaseLoadableHolder.
virtual iterators::loadable_const_forward_iterator cend() const noexcept final override
virtual uint16_t getImagesWidth() const final
virtual iterators::loadable_const_forward_iterator cbegin() const noexcept final override
virtual uint16_t insertImage(const std::filesystem::path &pathToImage, imageType type) final
BaseLoadableHolder(uint16_t imagesWidth, uint16_t imagesHeight, size_t count=standard_sizes::defaultImagesCount)
virtual imageType getImageType(const std::filesystem::path &pathToImage) const final
std::unordered_map< std::wstring, imageData > images
virtual iterators::loadable_forward_iterator begin() noexcept final override
Can't be modified.
virtual void removeImage(const std::filesystem::path &pathToImage) final
virtual uint16_t operator[](const std::filesystem::path &pathToImage) const final
virtual HIMAGELIST getImageList() const final
virtual uint16_t getImagesHeight() const final
virtual std::pair< std::string, json::utility::jsonObject::variantType > & loadBaseLoadableHolderStructure(json::utility::jsonObject &current) const final
virtual uint16_t getImageIndex(const std::filesystem::path &pathToImage) const final
virtual bool contains(const std::filesystem::path &pathToImage) const final
virtual iterators::loadable_forward_iterator end() noexcept final override
string to_string(wstring_view stringToConvert, uint32_t codepage)
Definition Utility.cpp:41
imageData(uint16_t index, imageType type, std::any &&data)