Kitlist
A list manager for maintaining kit lists
Loading...
Searching...
No Matches
model.hpp
Go to the documentation of this file.
1/*
2
3 This file is part of Kitlist, a program to maintain a simple list
4 of items and assign items to one or more categories.
5
6 Copyright (C) 2008-2025 Frank Dean
7
8 Kitlist is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12
13 Kitlist is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with Kitlist. If not, see <http://www.gnu.org/licenses/>.
20
21*/
22#ifndef MODEL_HPP
23#define MODEL_HPP
24
25#include "category.hpp"
26#include "item.hpp"
27#include <cstdint>
28#include <iostream>
29#include <map>
30#include <memory>
31#include <vector>
32
40class Model {
41
43 bool dirty;
44
46 std::vector<std::shared_ptr<Item>> items;
47
49 std::vector<std::shared_ptr<Category>> categories;
50
52 std::map<int32_t, std::shared_ptr<Item>> item_map;
53
55 std::map<int32_t, std::shared_ptr<Category>> category_map;
56
62 int32_t max_item_id;
63
70
78
89 std::shared_ptr<Item> toggle_item_checked(const int32_t id) {
90 auto item = get_item(id);
91 if (item) {
92 item->checked = !item->checked;
93 set_dirty();
94 }
95 return item;
96 }
97
98public:
99
112
122
123private:
126
127protected:
128
129 friend class KitParser;
130
148 void add_item(std::shared_ptr<Item> item, std::shared_ptr<Category> category = nullptr);
149
150public:
151
153 static const int32_t no_category;
154
161
165 virtual ~Model() {}
166
178 void associate_item_with_category(int32_t item_id, std::shared_ptr<Category> category);
179
191 std::shared_ptr<Category> set_category_name(const int32_t id,
192 const std::string& name) {
193 auto category = get_category(id);
194 if (category && category->name != name) {
195 category->name = name;
196 set_dirty();
197 }
198 return category;
199 }
200
212 std::shared_ptr<Item> set_item_name(const int32_t id,
213 const std::string& name) {
214 auto item = get_item(id);
215 if (item && item->name != name) {
216 item->name = name;
217 set_dirty();
218 }
219 return item;
220 }
221
233 std::shared_ptr<Item> set_item_checked(const int32_t id,
234 const bool state) {
235 auto item = get_item(id);
236 if (item && item->checked != state) {
237 item->checked = state;
238 set_dirty();
239 }
240 return item;
241 }
242
250 bool save(const std::string& filename);
251
260 void set_dirty(bool dirty = true);
261
267 bool is_dirty() const {
268 return dirty;
269 }
270
274 auto get_item_count() const {
275 return items.size();
276 }
277
283 std::vector<std::shared_ptr<Item>>::size_type get_current_checked_item_count() const {
284 std::vector<std::shared_ptr<Item>>::size_type c = 0;
285 const auto cat_items = get_all_items_for_current_selected_category();
286 for (const auto& item : cat_items)
287 if (item->checked)
288 c++;
289 return c;
290 }
291
295 auto get_category_count() const {
296 return categories.size();
297 }
298
312 int32_t new_item(const std::string& name, bool checked = false);
313
322 void delete_category(int32_t id);
323
332 void delete_item(int32_t id);
333
347 void remove_items(const std::vector<std::shared_ptr<Item>>& items);
348
358 void remove_item(int32_t id);
359
370 int32_t new_category(const std::string& name);
371
378 void add_category(std::shared_ptr<Category> category);
379
388 const std::shared_ptr<Item> get_item(int32_t id) const;
389
398 const std::shared_ptr<Category> get_category(int32_t id) const;
399
401 std::shared_ptr<Category> get_current_category() {
403 }
404
415 const std::shared_ptr<Item> change_checked_state(int32_t id,
416 check_action action);
417
427
435 const std::vector<std::shared_ptr<Item>>
437
448 const std::vector<std::shared_ptr<Item>>
450
460
465
473
486 std::vector<std::shared_ptr<Item>>& items,
487 std::vector<std::shared_ptr<Category>>& categories);
488
495 void copy_checked_items_to_categories(std::vector<int32_t> cids);
496
503 void copy_item_to_categories(int32_t item_id, std::vector<int32_t> cids);
504
511 void select_category(int32_t id) {
513 }
514
518 int32_t get_selected_category() const {
519 return selected_category;
520 }
521
527 const std::vector<std::shared_ptr<Category>> get_categories() const {
528 return categories;
529 }
530
537 const std::vector<std::shared_ptr<Item>> get_all_items() const {
538 return items;
539 }
540
543 filter = state;
544 }
545
548 return filter;
549 }
550
554 void sort_items();
555
559 void sort_categories();
560
564 void sort_category_items();
565
570
574 void sort();
575
579 void show_model() const;
580
585 void validate() const;
586
590 void renumber_items();
591
595 void renumber_categories();
596
600 void renumber();
601
602};
603
604#endif // MODEL_HPP
std::vector< std::shared_ptr< Item > >::size_type get_current_checked_item_count() const
Definition model.hpp:283
void remove_items(const std::vector< std::shared_ptr< Item > > &items)
Removes references to each of the Item instances in the list from the currently selected Category.
Definition model.cpp:164
const std::shared_ptr< Item > get_item(int32_t id) const
Gets an Item by ID.
Definition model.cpp:218
void validate() const
Validates the model ensuring all the maps are consistent with each of the corresponding lists.
Definition model.cpp:509
bool save(const std::string &filename)
saves the Model to a file.
Definition model.cpp:95
void change_all_current_items_checked_states(check_action action)
Switches the checked state of all items in the currently selected Category.
Definition model.cpp:251
void renumber()
Re-assigns IDs to all of the categories and items.
Definition model.cpp:574
void add_category(std::shared_ptr< Category > category)
Adds the passed category to the model.
Definition model.cpp:207
std::shared_ptr< Item > set_item_name(const int32_t id, const std::string &name)
Updates the name of the passed Item.
Definition model.hpp:212
void sort()
Sorts everything within the model.
Definition model.cpp:479
const std::vector< std::shared_ptr< Item > > get_all_items_for_current_selected_category() const
Returns all items for the currently selected Category.
Definition model.cpp:270
bool is_dirty() const
Whether the model has been modified since it was last saved.
Definition model.hpp:267
auto get_item_count() const
The count of items in the model.
Definition model.hpp:274
friend class KitParser
Definition model.hpp:129
void remove_all_current_checked_items()
Removes all checked items from the current category.
Definition model.cpp:308
int32_t max_item_id
The highest item ID in use.
Definition model.hpp:62
void set_filter(state_filter state)
Sets filtering of checked items.
Definition model.hpp:542
void set_dirty(bool dirty=true)
Sets dirty flag.
Definition model.cpp:34
state_filter filter
Indicates filtering state of checked items.
Definition model.hpp:125
const std::shared_ptr< Category > get_category(int32_t id) const
Gets a Category by ID.
Definition model.cpp:226
void remove_item(int32_t id)
Removes references to the item from the currently selected Category.
Definition model.cpp:183
void sort_item_categories()
Sorts all the categories assicated with each item.
Definition model.cpp:470
std::shared_ptr< Category > set_category_name(const int32_t id, const std::string &name)
Updates the name of passed Category.
Definition model.hpp:191
void renumber_categories()
Re-assigns IDs to all the categories.
Definition model.cpp:555
state_filter get_filter() const
The current state_filter.
Definition model.hpp:547
auto count_filter_items_for_current_selected_category() const
Returns a count of the filtered items in the currently selected category.
Definition model.hpp:457
const std::vector< std::shared_ptr< Category > > get_categories() const
The list of categories.
Definition model.hpp:527
void copy_items_to_categories(std::vector< std::shared_ptr< Item > > &items, std::vector< std::shared_ptr< Category > > &categories)
Copies all of the passed items to each of the passed categories, updating the model relationships.
Definition model.cpp:391
std::shared_ptr< Category > get_current_category()
Returns the current Category or nullptr if not found.
Definition model.hpp:401
void show_model() const
Dumps details of the entire model for debugging.
Definition model.cpp:487
void copy_item_to_categories(int32_t item_id, std::vector< int32_t > cids)
Copies the specified item to the list of categories.
Definition model.cpp:432
const std::vector< std::shared_ptr< Item > > get_all_items() const
Gets all items, without filtering.
Definition model.hpp:537
void delete_category(int32_t id)
Deletes the Category having the passed ID.
Definition model.cpp:125
int32_t get_selected_category() const
The ID of the currently selected Category.
Definition model.hpp:518
int32_t selected_category
The currently selected category.
Definition model.hpp:77
int32_t max_category_id
The highest category ID in use.
Definition model.hpp:69
const std::shared_ptr< Item > change_checked_state(int32_t id, check_action action)
Switches the checked state of the Item referenced by the specified ID according to the specified acti...
Definition model.cpp:234
const std::vector< std::shared_ptr< Item > > filter_items_for_current_selected_category() const
Filters the list of Item instances based on the currently selected category (selected_category) and c...
Definition model.cpp:283
void sort_category_items()
Sorts all the items associated with each category.
Definition model.cpp:461
void sort_items()
Sorts all the model's items.
Definition model.cpp:445
bool dirty
Indicates that the Model has been modified since it was last saved.
Definition model.hpp:43
check_action
Specifies what state Item checkmarks must be changed to.
Definition model.hpp:117
@ check
Definition model.hpp:119
@ uncheck
Definition model.hpp:120
@ toggle
Definition model.hpp:118
void add_item(std::shared_ptr< Item > item, std::shared_ptr< Category > category=nullptr)
Adds the passed Item to the model, updating item and category maps appropriately of both the Item and...
Definition model.cpp:40
void delete_item(int32_t id)
Deletes the Item having the passed ID.
Definition model.cpp:148
auto get_category_count() const
The count of categories in the model.
Definition model.hpp:295
std::vector< std::shared_ptr< Item > > items
The list of items.
Definition model.hpp:46
std::vector< std::shared_ptr< Category > > categories
The list of categories.
Definition model.hpp:49
static const int32_t no_category
Indicates no filtering by category.
Definition model.hpp:153
int32_t new_item(const std::string &name, bool checked=false)
Creates a new Item in the model using the passed parameters, incrementing max_item_id and assigning i...
Definition model.cpp:104
std::shared_ptr< Item > set_item_checked(const int32_t id, const bool state)
Updates the checked state of the passed Item.
Definition model.hpp:233
std::map< int32_t, std::shared_ptr< Category > > category_map
A map of all categories keyed by ID.
Definition model.hpp:55
std::shared_ptr< Item > toggle_item_checked(const int32_t id)
Toggles the checked state of the passed Item.
Definition model.hpp:89
void copy_checked_items_to_categories(std::vector< int32_t > cids)
Copies all currently checked items for the currently selected Category to the list of categories.
Definition model.cpp:415
std::map< int32_t, std::shared_ptr< Item > > item_map
A map of all items keyed by ID.
Definition model.hpp:52
void delete_all_current_checked_items()
Deletes all checked items.
Definition model.cpp:347
void associate_item_with_category(int32_t item_id, std::shared_ptr< Category > category)
Updates the relationships between an Item and a Category that are already in the model,...
Definition model.cpp:62
Model()
Default constructor.
Definition model.hpp:158
void select_category(int32_t id)
Sets the selected Category to that of the passed ID.
Definition model.hpp:511
int32_t new_category(const std::string &name)
Creates a new Category in the model using the passed parameters, incrementing max_category_id and ass...
Definition model.cpp:195
virtual ~Model()
Destructor.
Definition model.hpp:165
state_filter
Constants for filtering items depending on their checked state.
Definition model.hpp:104
@ unchecked
Show only unchecked items.
Definition model.hpp:110
@ all
Show all items, i.e. no filtering.
Definition model.hpp:106
@ checked
Show only checked items.
Definition model.hpp:108
void sort_categories()
Sorts all the model's categories.
Definition model.cpp:453
void renumber_items()
Re-assigns IDs to all the items.
Definition model.cpp:536
Copyright 2008-2025 Frank Dean