Nix (Dev) 3.5.10
dev - 3.5.10 - 1af9301
Loading...
Searching...
No Matches
GroupMenuModel.hpp
Go to the documentation of this file.
1#ifndef IV_SRC_MODEL_DIAMAR_GROUPMENUMODEL_HPP_
2#define IV_SRC_MODEL_DIAMAR_GROUPMENUMODEL_HPP_
3
4#include "core/types.hpp"
6
7#include <algorithm>
8#include <cstdint>
9#include <iostream>
10#include <map>
11#include <ranges>
12#include <string>
13#include <utility>
14#include <vector>
15
16namespace iv::diamar
17{
18class Mimic;
19}
20
21namespace iv::model
22{
23
24enum class Type
25{
26 Action,
28};
29
30template<typename T>
32{
33public:
35 {
36 }
37
38 ItemModel(iv::types::groupItemId _id, Type _type, std::string _label, uint8_t _order, const T &_actionItem = T())
39 : id(std::move(_id)), type(_type), label(std::move(_label)), order(_order), actionItem(_actionItem)
40 {
41 }
42
43 bool operator<(const ItemModel &other) const
44 {
45 return order < other.order;
46 }
47
48 void removeAction(const T &actionToRemove)
49 {
50 if (type == Type::Dropdown)
51 {
52 std::vector<iv::types::groupItemId> actionsToRemove;
53 for (auto &[itemId, item]: subOptions)
54 {
55 switch (item.type)
56 {
57 case Type::Action:
58 if constexpr (std::is_same_v<std::weak_ptr<typename T::element_type>, T>)
59 {
60 if (item.actionItem.lock() == actionToRemove.lock())
61 {
62 actionsToRemove.push_back(itemId);
63 }
64 }
65 else if (item.actionItem == actionToRemove)
66 {
67 actionsToRemove.push_back(itemId);
68 }
69 break;
70
71 case Type::Dropdown:
72 item.removeAction(actionToRemove);
73 break;
74 }
75 }
76
77 for (const auto &actionId: actionsToRemove)
78 {
79 subOptions.erase(actionId);
80 }
81 }
82 }
83
84 [[nodiscard]] std::vector<T> flattenActions() const
85 {
86 std::vector<T> actions;
87
88 if (type == Type::Action)
89 {
90 actions.push_back(actionItem);
91 }
92 else
93 {
94 std::map<iv::types::order, ItemModel<T>> orderedSubOptions;
95 for (const auto &[itemId, item]: subOptions)
96 {
97 orderedSubOptions.emplace(item.order, item);
98 }
99
100 for (const auto &subOption: orderedSubOptions | std::views::values)
101 {
102 std::vector<T> subActions = subOption.flattenActions();
103 actions.insert(std::end(actions), std::begin(subActions), std::end(subActions));
104 }
105 }
106
107 return actions;
108 }
109
110 // private:
113 std::string label;
115 T actionItem; // Holds the object associated
116 std::map<iv::types::groupItemId, ItemModel<T>> subOptions;// Only for DropdownItems
117};
118
119template<typename T>
121{
122public:
123 // Add direct action item
124 void addActionItem(const iv::types::groupItemId &id, const std::string &label, const T &actionItem, uint8_t order)
125 {
126 ItemModel<T> action(id, Type::Action, label, order, actionItem);
127 items.emplace(id, action);
128 }
129
130 // Add an empty dropdown item
131 void addEmptyDropdownItem(const iv::types::groupItemId &id, const std::string &label, uint8_t order)
132 {
133 ItemModel<T> dropdown(id, Type::Dropdown, label, order);
134 items.emplace(id, dropdown);
135 }
136
137 // Add dropdown item
138 void addDropdownItem(const iv::types::groupItemId &id, const std::string &label,
139 const std::vector<ItemModel<T>> &subOptions)
140 {
141 ItemModel<T> dropdown(id, Type::Dropdown, label);
142 dropdown.subOptions = subOptions;
143 items.emplace(id, dropdown);
144 }
145
147 const std::string &label, const T &actionItem, uint8_t order)
148 {
149 if (items.contains(dropdownId) and items[dropdownId].type == Type::Dropdown)
150 {
151 iv::model::ItemModel<T> &dropdownItem = items[dropdownId];
152 ItemModel<T> action(id, Type::Action, label, order, actionItem);
153 dropdownItem.subOptions.emplace(action.id, action);
154 }
155 else
156 {
157 std::cerr << "Dropdown item with label '" << dropdownId << "' not found." << std::endl;
158 }
159 }
160
161 // Add an option to an existing dropdown item
162 void addOptionToDropdownItem(const iv::types::groupItemId &dropdownId, const ItemModel<T> &newOption)
163 {
164 if (items.contains(dropdownId) and items[dropdownId].type == Type::Dropdown)
165 {
166 iv::model::ItemModel<T> &dropdownItem = items[dropdownId];
167 dropdownItem.subOptions.emplace(newOption.id, newOption);
168 }
169 else
170 {
171 std::cerr << "Dropdown item with label '" << dropdownId << "' not found." << std::endl;
172 }
173 }
174
175 // Remove an item from the main menu
177 {
178 if (items.contains(itemId))
179 {
180 items.erase(itemId);
181 std::cout << "Item '" << itemId << "' removed from the menu." << std::endl;
182 }
183 else
184 {
185 std::cerr << "Item '" << itemId << "' not found in the menu." << std::endl;
186 }
187 }
188
189 // Remove a dropdown item (and all its options)
191 {
192 if (items.contains(dropdownId) and items[dropdownId].type == Type::Dropdown)
193 {
194 items.erase(dropdownId);
195 std::cout << "Dropdown item '" << dropdownId << "' removed from the menu." << std::endl;
196 }
197 else
198 {
199 std::cerr << "Dropdown item '" << dropdownId << "' not found in the menu." << std::endl;
200 }
201 }
202
203 // Remove an option from an existing dropdown item
205 {
206 if (items.contains(dropdownId) and items[dropdownId].type == Type::Dropdown)
207 {
208 iv::model::ItemModel<T> &dropdownItem = items[dropdownId];
209
210 if (dropdownItem.subOptions.contains(optionId))
211 {
212 dropdownItem.subOptions.erase(optionId);
213 std::cout << "Option '" << optionId << "' removed from dropdown '" << dropdownId << "'." << std::endl;
214 }
215 else
216 {
217 std::cerr << "Option '" << optionId << "' not found in dropdown '" << dropdownId << "'." << std::endl;
218 }
219 }
220 else
221 {
222 std::cerr << "Dropdown item with label '" << dropdownId << "' not found." << std::endl;
223 }
224 }
225
226 void removeAction(const T &action)
227 {
228 std::vector<iv::types::groupItemId> itemsToRemove;
229 for (auto &[itemId, item]: items)
230 {
231 switch (item.type)
232 {
233 case Type::Action:
234 if constexpr (std::is_same_v<std::weak_ptr<typename T::element_type>, T>)
235 {
236 if (item.actionItem.lock() == action.lock())
237 {
238 itemsToRemove.push_back(itemId);
239 }
240 }
241 else if (item.actionItem == action)
242 {
243 itemsToRemove.push_back(itemId);
244 }
245 break;
246
247 case Type::Dropdown:
248 item.removeAction(action);
249 break;
250 }
251 }
252
253 for (const auto &itemId: itemsToRemove)
254 {
255 items.erase(itemId);
256 }
257 }
258
260 {
261 std::map<std::pair<iv::types::order, iv::types::groupItemId>, iv::model::ItemModel<T> *> sortedItems;
262 for (auto &[itemId, item]: items)
263 {
264 sortedItems.emplace(std::make_pair(item.order, itemId), &item);
265 }
266
267 iv::types::order optionOrder {0};
268 for (const auto &[order, item]: sortedItems)
269 {
270 item->order = optionOrder;
271 ++optionOrder;
272 }
273 }
274
275 // List only labels of the dropdown items (roots, not the children)
276 [[nodiscard]] std::vector<std::string> listDropdownLabels() const
277 {
278 std::vector<std::string> labels;
279
280 for (const auto &item: items | std::views::values)
281 {
282 if (item.type == Type::Dropdown)
283 {
284 labels.push_back(item.label);
285 }
286 }
287
288 return labels;
289 }
290
291 [[nodiscard]] std::vector<T> flattenActions() const
292 {
293 std::map<iv::types::order, ItemModel<T>> orderedItems;
294 for (const ItemModel<T> &item: items | std::views::values)
295 {
296 orderedItems.emplace(item.order, item);
297 }
298
299 std::vector<T> actions;
300 for (const auto &item: orderedItems | std::views::values)
301 {
302 std::vector<T> itemActions = item.flattenActions();
303 actions.insert(std::end(actions), std::begin(itemActions), std::end(itemActions));
304 }
305
306 return actions;
307 }
308
309 void save(iv::file::xml::node &node) const
310 {
311 static_assert(sizeof(T) == -1, "GroupMenuModel::save() debe ser implementada para este tipo especĂ­fico.");
312 }
313
314 void load(const iv::file::xml::node &node)
315 {
316 static_assert(sizeof(T) == -1, "GroupMenuModel::load() debe ser implementada para este tipo especĂ­fico.");
317 }
318
319 // private:
320 std::map<iv::types::groupItemId, ItemModel<T>> items;
321
322private:
323 struct Sections
324 {
325 static constexpr std::string_view items {"Items"};
326 static constexpr std::string_view item {"Item"};
327 static constexpr std::string_view subOptions {"SubOptions"};
328 static constexpr std::string_view subOption {"SubOption"};
329 };
330};
331
332namespace Mimic
333{
334struct Keys
335{
336 static constexpr std::string_view groupName {"Name"};
337 static constexpr std::string_view groupOrder {"Order"};
338 static constexpr std::string_view mimicId {"MimicId"};
339 static constexpr std::string_view mimicOrder {"Order"};
340};
342{
343 static constexpr std::string_view mimicGroups {"MimicGroups"};
344 static constexpr std::string_view group {"Group"};
345 static constexpr std::string_view mimic {"Mimic"};
346};
348{
349 static constexpr uint8_t order {0};
350 static constexpr std::string string {};
351};
352}// namespace Mimic
353
354template<>
356
357template<>
359
360}// namespace iv::model
361
362#endif//IV_SRC_MODEL_DIAMAR_GROUPMENUMODEL_HPP_
Definition xmlFile.hpp:15
Definition GroupMenuModel.hpp:121
void addOptionToDropdownItem(const iv::types::groupItemId &dropdownId, const ItemModel< T > &newOption)
Definition GroupMenuModel.hpp:162
void addActionItem(const iv::types::groupItemId &id, const std::string &label, const T &actionItem, uint8_t order)
Definition GroupMenuModel.hpp:124
void addActionToDropdownItem(const iv::types::groupItemId &dropdownId, const iv::types::groupItemId &id, const std::string &label, const T &actionItem, uint8_t order)
Definition GroupMenuModel.hpp:146
std::vector< std::string > listDropdownLabels() const
Definition GroupMenuModel.hpp:276
void load(const iv::file::xml::node &node)
Definition GroupMenuModel.hpp:314
void addDropdownItem(const iv::types::groupItemId &id, const std::string &label, const std::vector< ItemModel< T > > &subOptions)
Definition GroupMenuModel.hpp:138
std::map< iv::types::groupItemId, ItemModel< T > > items
Definition GroupMenuModel.hpp:320
void collapseOptionsOrder()
Definition GroupMenuModel.hpp:259
void removeOptionFromDropdownItem(const iv::types::groupItemId &dropdownId, const iv::types::groupItemId &optionId)
Definition GroupMenuModel.hpp:204
void addEmptyDropdownItem(const iv::types::groupItemId &id, const std::string &label, uint8_t order)
Definition GroupMenuModel.hpp:131
void removeItem(const iv::types::groupItemId &itemId)
Definition GroupMenuModel.hpp:176
void save(iv::file::xml::node &node) const
Definition GroupMenuModel.hpp:309
void removeDropdownItem(const iv::types::groupItemId &dropdownId)
Definition GroupMenuModel.hpp:190
void removeAction(const T &action)
Definition GroupMenuModel.hpp:226
std::vector< T > flattenActions() const
Definition GroupMenuModel.hpp:291
Definition GroupMenuModel.hpp:32
Type type
Definition GroupMenuModel.hpp:112
T actionItem
Definition GroupMenuModel.hpp:115
ItemModel()
Definition GroupMenuModel.hpp:34
std::string label
Definition GroupMenuModel.hpp:113
iv::types::order order
Definition GroupMenuModel.hpp:114
std::map< iv::types::groupItemId, ItemModel< T > > subOptions
Definition GroupMenuModel.hpp:116
void removeAction(const T &actionToRemove)
Definition GroupMenuModel.hpp:48
bool operator<(const ItemModel &other) const
Definition GroupMenuModel.hpp:43
ItemModel(iv::types::groupItemId _id, Type _type, std::string _label, uint8_t _order, const T &_actionItem=T())
Definition GroupMenuModel.hpp:38
iv::types::groupItemId id
Definition GroupMenuModel.hpp:111
std::vector< T > flattenActions() const
Definition GroupMenuModel.hpp:84
Definition DataLoggerManager.cpp:10
@ Mimic
Definition MenuMimicsOptions.cpp:24
Definition DiamarModel.cpp:14
Type
Definition GroupMenuModel.hpp:25
std::string groupItemId
Definition types.hpp:71
uint64_t order
Definition types.hpp:90
Definition GroupMenuModel.hpp:324
static constexpr std::string_view items
Definition GroupMenuModel.hpp:325
static constexpr std::string_view subOptions
Definition GroupMenuModel.hpp:327
static constexpr std::string_view item
Definition GroupMenuModel.hpp:326
static constexpr std::string_view subOption
Definition GroupMenuModel.hpp:328
Definition GroupMenuModel.hpp:348
static constexpr uint8_t order
Definition GroupMenuModel.hpp:349
Definition GroupMenuModel.hpp:335
static constexpr std::string_view groupName
Definition GroupMenuModel.hpp:336
static constexpr std::string_view mimicId
Definition GroupMenuModel.hpp:338
static constexpr std::string_view groupOrder
Definition GroupMenuModel.hpp:337
static constexpr std::string_view mimicOrder
Definition GroupMenuModel.hpp:339
Definition GroupMenuModel.hpp:342
static constexpr std::string_view mimic
Definition GroupMenuModel.hpp:345
static constexpr std::string_view mimicGroups
Definition GroupMenuModel.hpp:343
static constexpr std::string_view group
Definition GroupMenuModel.hpp:344