36 if (this->dirty !=
dirty)
42 assert((
item_map.find(i->id) ==
item_map.end() &&
"Item already exists in model"));
45 item_map.insert(make_pair(i->id, i));
47 assert(i->category_map.find(c->id) == i->category_map.end());
48 assert(c->item_map.find(i->id) == c->item_map.end());
50 i->categories.push_back(c);
51 i->category_map.insert(make_pair(c->id, c));
52 c->items.push_back(i);
53 c->item_map.insert(make_pair(i->id, i));
54 assert(c->items.size() == c->item_map.size());
55 assert(i->categories.size() == i->category_map.size());
63 std::shared_ptr<Category> c)
65 auto item_iter =
item_map.find(item_id);
68 cerr <<
"Item " << item_id <<
" not found in item list" << endl;
73 auto cat_item_iter = c->item_map.find(item_id);
75 if (cat_item_iter != c->item_map.end())
76 cerr <<
"Item " << item_id <<
", \"" << cat_item_iter->second->name
77 <<
"\" already belongs to category "
78 << c->id <<
", \"" << c->name <<
'"' << endl;
80 assert(cat_item_iter == c->item_map.end());
81 if (cat_item_iter == c->item_map.end()) {
82 shared_ptr<Item> item = item_iter->second;
83 assert(item->category_map.find(c->id) == item->category_map.end());
85 c->items.push_back(item);
86 c->item_map.insert(make_pair(item_iter->first, item));
87 item->categories.push_back(c);
88 item->category_map.insert(make_pair(c->id, c));
98 bool success = w.
save_file(filename, *
this);
108 item_map.insert(std::make_pair(i->id, i));
112 i->categories.push_back(c);
113 i->category_map.insert(make_pair(c->id, c));
114 c->items.push_back(i);
115 c->item_map.insert(make_pair(i->id, i));
130 shared_ptr<Category> category = map_iter->second;
131 for (
auto& item : category->items) {
132 item->categories.erase(
134 item->categories.begin(), item->categories.end(),
136 return (!c.expired() && c.lock()->id == id);
138 item->categories.end());
151 c->items.erase(remove_if(c->items.begin(), c->items.end(),
152 [=](
const auto& n) { return (n->id == id); }),
154 c->item_map.erase(
id);
157 [=](
const auto& n) { return (n->id == id); }),
170 auto category = pos->second;
171 for (
const auto& item :
items) {
172 erase_if(item->categories,
173 [=] (
const auto& cat) { return cat.expired() || cat.lock()->id == category->id; });
174 item->category_map.erase(category->id);
175 category->item_map.erase(item->id);
176 erase_if(category->items,
177 [=] (
const auto& i) { return item->id == i->id; });
187 c->items.erase(remove_if(c->items.begin(), c->items.end(),
188 [=](
const auto& item) { return item->id == id; }),
190 c->item_map.erase(
id);
238 std::shared_ptr<Item> item;
254 for (
const auto& i :
items) {
263 i->checked = !i->checked;
272 vector<shared_ptr<Item>> r;
278 r = c->second->items;
289 vector<shared_ptr<Item>> f;
290 for (
const auto& i : r) {
319 category->items.erase(
320 remove_if(category->items.begin(), category->items.end(),
324 category->items.end());
325 auto iter = category->item_map.begin();
326 auto end_iter = category->item_map.end();
327 for (; iter != end_iter; ) {
328 if (iter->second->checked ==
true) {
330 auto item = iter->second;
331 item->category_map.erase(category->id);
332 item->categories.erase(
333 remove_if(item->categories.begin(), item->categories.end(),
335 return !c.expired() && c.lock()->id == category->id;
337 item->categories.end());
338 iter = category->item_map.erase(iter);
355 category->items.erase(
356 remove_if(category->items.begin(), category->items.end(),
360 category->items.end());
362 auto iter = category->item_map.begin();
363 auto end_iter = category->item_map.end();
364 for (; iter != end_iter; ) {
365 if (iter->second->checked ==
true) {
366 iter = category->item_map.erase(iter);
374 if (iter->second->checked) {
393 bool modified =
false;
395 bool category_modified =
false;
396 for (
const auto& i :
items) {
397 const auto r = c->item_map.insert(make_pair(i->id, i));
400 category_modified =
true;
401 c->items.push_back(i);
402 const auto r2 = i->category_map.insert(make_pair(c->id, c));
404 i->categories.push_back(c);
407 if (category_modified)
418 std::vector<shared_ptr<Item>> source_items{};
419 for (
const auto& i :
items) {
421 source_items.push_back(i);
423 std::vector<shared_ptr<Category>> target_categories{};
424 for (
auto cid : cids) {
427 target_categories.push_back(category);
435 std::vector<shared_ptr<Item>> source_items{item};
436 std::vector<shared_ptr<Category>> target_categories{};
437 for (
auto cid : cids) {
440 target_categories.push_back(category);
448 [](shared_ptr<Item> a, shared_ptr<Item> b) {
456 [](shared_ptr<Category> a, shared_ptr<Category>b) {
464 std::sort(c->items.begin(), c->items.end(),
465 [](shared_ptr<Item> a, shared_ptr<Item> b) {
473 std::sort(i->categories.begin(), i->categories.end(),
474 [](weak_ptr<Category> a, weak_ptr<Category> b) {
491 <<
" categories." << endl;
492 cout <<
"Categories:\n";
493 for (
const shared_ptr<Category>& c :
categories) {
494 cout << c->id <<
'\t' << c->name <<
'\n';
495 for (
const shared_ptr<Item>& i : c->items) {
496 cout <<
'\t' << i->id <<
'\t' << i->name <<
'\n';
500 for (
const shared_ptr<Item>& i :
items) {
501 cout << i->id <<
'\t' << i->name <<
'\n';
502 for (
const weak_ptr<Category>& cat : i->categories) {
503 assert(!cat.expired());
504 shared_ptr c = cat.lock();
518 for (
auto ci : c->items) {
519 assert(c->item_map.find(ci->id) != c->item_map.end());
521 assert(ci->category_map.find(c->id) != ci->category_map.end());
525 for (
auto i :
items) {
528 for (
auto ic : i->categories) {
529 assert(!ic.expired());
530 assert(i->category_map.find(ic.lock()->id) != i->category_map.end());
541 for (
auto i :
items) {
543 item_map.insert(make_pair(i->id, i));
548 for (
auto i : c->items) {
549 c->item_map.insert(make_pair(i->id, i));
564 for (
auto i :
items) {
565 i->category_map.clear();
567 i->category_map.insert(make_pair(c->id, c));
Represents a Category having a many-to-many relationship to zero or more Item instances.
Represents a Item having a many-to-many relationship to zero or more Category instances.
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.
const std::shared_ptr< Item > get_item(int32_t id) const
Gets an Item by ID.
void validate() const
Validates the model ensuring all the maps are consistent with each of the corresponding lists.
bool save(const std::string &filename)
saves the Model to a file.
void change_all_current_items_checked_states(check_action action)
Switches the checked state of all items in the currently selected Category.
void renumber()
Re-assigns IDs to all of the categories and items.
void add_category(std::shared_ptr< Category > category)
Adds the passed category to the model.
void sort()
Sorts everything within the model.
const std::vector< std::shared_ptr< Item > > get_all_items_for_current_selected_category() const
Returns all items for the currently selected Category.
auto get_item_count() const
The count of items in the model.
void remove_all_current_checked_items()
Removes all checked items from the current category.
int32_t max_item_id
The highest item ID in use.
void set_dirty(bool dirty=true)
Sets dirty flag.
state_filter filter
Indicates filtering state of checked items.
const std::shared_ptr< Category > get_category(int32_t id) const
Gets a Category by ID.
void remove_item(int32_t id)
Removes references to the item from the currently selected Category.
void sort_item_categories()
Sorts all the categories assicated with each item.
void renumber_categories()
Re-assigns IDs to all the categories.
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.
std::shared_ptr< Category > get_current_category()
Returns the current Category or nullptr if not found.
void show_model() const
Dumps details of the entire model for debugging.
void copy_item_to_categories(int32_t item_id, std::vector< int32_t > cids)
Copies the specified item to the list of categories.
void delete_category(int32_t id)
Deletes the Category having the passed ID.
int32_t selected_category
The currently selected category.
int32_t max_category_id
The highest category ID in use.
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...
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...
void sort_category_items()
Sorts all the items associated with each category.
void sort_items()
Sorts all the model's items.
bool dirty
Indicates that the Model has been modified since it was last saved.
check_action
Specifies what state Item checkmarks must be changed to.
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...
void delete_item(int32_t id)
Deletes the Item having the passed ID.
auto get_category_count() const
The count of categories in the model.
std::vector< std::shared_ptr< Item > > items
The list of items.
std::vector< std::shared_ptr< Category > > categories
The list of categories.
static const int32_t no_category
Indicates no filtering by category.
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...
std::shared_ptr< Item > set_item_checked(const int32_t id, const bool state)
Updates the checked state of the passed Item.
std::map< int32_t, std::shared_ptr< Category > > category_map
A map of all categories keyed by ID.
std::shared_ptr< Item > toggle_item_checked(const int32_t id)
Toggles the checked state of the passed Item.
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.
std::map< int32_t, std::shared_ptr< Item > > item_map
A map of all items keyed by ID.
void delete_all_current_checked_items()
Deletes all checked items.
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,...
void select_category(int32_t id)
Sets the selected Category to that of the passed ID.
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...
@ unchecked
Show only unchecked items.
@ all
Show all items, i.e. no filtering.
@ checked
Show only checked items.
void sort_categories()
Sorts all the model's categories.
void renumber_items()
Re-assigns IDs to all the items.
Saves the Model in XML format to a file.
static bool save_file(const std::string &filename, const Model &model)
Saves the Model in XML format to the specified file.