Kitlist
A list manager for maintaining kit lists
Loading...
Searching...
No Matches
Model Class Reference

Maintains the data model. More...

#include "model.hpp"

Public Types

enum  state_filter { all , checked , unchecked }
 Constants for filtering items depending on their checked state. More...
enum  check_action { toggle , check , uncheck }
 Specifies what state Item checkmarks must be changed to. More...

Public Member Functions

 Model ()
 Default constructor.
virtual ~Model ()
 Destructor.
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, but using only the item's ID as a reference.
std::shared_ptr< Categoryset_category_name (const int32_t id, const std::string &name)
 Updates the name of passed Category.
std::shared_ptr< Itemset_item_name (const int32_t id, const std::string &name)
 Updates the name of the passed Item.
std::shared_ptr< Itemset_item_checked (const int32_t id, const bool state)
 Updates the checked state of the passed Item.
bool save (const std::string &filename)
 saves the Model to a file.
void set_dirty (bool dirty=true)
 Sets dirty flag.
bool is_dirty () const
 Whether the model has been modified since it was last saved.
auto get_item_count () const
 The count of items in the model.
std::vector< std::shared_ptr< Item > >::size_type get_current_checked_item_count () const
auto get_category_count () const
 The count of categories in the model.
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 it to the Item.
void delete_category (int32_t id)
 Deletes the Category having the passed ID.
void delete_item (int32_t id)
 Deletes the Item having the passed ID.
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.
void remove_item (int32_t id)
 Removes references to the item from the currently selected Category.
int32_t new_category (const std::string &name)
 Creates a new Category in the model using the passed parameters, incrementing max_category_id and assigning it to the Category.
void add_category (std::shared_ptr< Category > category)
 Adds the passed category to the model.
const std::shared_ptr< Itemget_item (int32_t id) const
 Gets an Item by ID.
const std::shared_ptr< Categoryget_category (int32_t id) const
 Gets a Category by ID.
std::shared_ptr< Categoryget_current_category ()
 Returns the current Category or nullptr if not found.
const std::shared_ptr< Itemchange_checked_state (int32_t id, check_action action)
 Switches the checked state of the Item referenced by the specified ID according to the specified action.
void change_all_current_items_checked_states (check_action action)
 Switches the checked state of all items in the currently selected Category.
const std::vector< std::shared_ptr< Item > > get_all_items_for_current_selected_category () const
 Returns all items for the currently selected Category.
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 checked status (filter).
auto count_filter_items_for_current_selected_category () const
 Returns a count of the filtered items in the currently selected category.
void remove_all_current_checked_items ()
 Removes all checked items from the current category.
void delete_all_current_checked_items ()
 Deletes all checked items.
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.
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.
void copy_item_to_categories (int32_t item_id, std::vector< int32_t > cids)
 Copies the specified item to the list of categories.
void select_category (int32_t id)
 Sets the selected Category to that of the passed ID.
int32_t get_selected_category () const
 The ID of the currently selected Category.
const std::vector< std::shared_ptr< Category > > get_categories () const
 The list of categories.
const std::vector< std::shared_ptr< Item > > get_all_items () const
 Gets all items, without filtering.
void set_filter (state_filter state)
 Sets filtering of checked items.
state_filter get_filter () const
 The current state_filter.
void sort_items ()
 Sorts all the model's items.
void sort_categories ()
 Sorts all the model's categories.
void sort_category_items ()
 Sorts all the items associated with each category.
void sort_item_categories ()
 Sorts all the categories assicated with each item.
void sort ()
 Sorts everything within the model.
void show_model () const
 Dumps details of the entire model for debugging.
void validate () const
 Validates the model ensuring all the maps are consistent with each of the corresponding lists.
void renumber_items ()
 Re-assigns IDs to all the items.
void renumber_categories ()
 Re-assigns IDs to all the categories.
void renumber ()
 Re-assigns IDs to all of the categories and items.

Static Public Attributes

static const int32_t no_category = -1
 Indicates no filtering by category.

Protected Member Functions

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 the Category.

Private Member Functions

std::shared_ptr< Itemtoggle_item_checked (const int32_t id)
 Toggles the checked state of the passed Item.

Private Attributes

bool dirty
 Indicates that the Model has been modified since it was last saved.
std::vector< std::shared_ptr< Item > > items
 The list of items.
std::vector< std::shared_ptr< Category > > categories
 The list of categories.
std::map< int32_t, std::shared_ptr< Item > > item_map
 A map of all items keyed by ID.
std::map< int32_t, std::shared_ptr< Category > > category_map
 A map of all categories keyed by ID.
int32_t max_item_id
 The highest item ID in use.
int32_t max_category_id
 The highest category ID in use.
int32_t selected_category
 The currently selected category.
state_filter filter
 Indicates filtering state of checked items.

Friends

class KitParser

Detailed Description

Maintains the data model.

Manages the category and item lists and their relationships.

Definition at line 40 of file model.hpp.

Member Enumeration Documentation

◆ check_action

Specifies what state Item checkmarks must be changed to.

Enumerator
toggle 

Specifies that checkmarks must be toggled.

check 

Specifies that checkmarks must be switched on.

uncheck 

Specifies that checkmarks must be switched off.

Definition at line 117 of file model.hpp.

◆ state_filter

Constants for filtering items depending on their checked state.

Enumerator
all 

Show all items, i.e. no filtering.

checked 

Show only checked items.

unchecked 

Show only unchecked items.

Definition at line 104 of file model.hpp.

Constructor & Destructor Documentation

◆ Model()

Model::Model ( )
inline

Default constructor.

Definition at line 158 of file model.hpp.

◆ ~Model()

virtual Model::~Model ( )
inlinevirtual

Destructor.

Definition at line 165 of file model.hpp.

Member Function Documentation

◆ add_category()

void Model::add_category ( std::shared_ptr< Category > category)

Adds the passed category to the model.

Parameters
categoryA shared_ptr to the Category.

Definition at line 207 of file model.cpp.

◆ add_item()

void Model::add_item ( std::shared_ptr< Item > item,
std::shared_ptr< Category > category = nullptr )
protected

Adds the passed Item to the model, updating item and category maps appropriately of both the Item and the Category.

Parameters
itemA std::shared_ptr to the Item to add to the model.
categoryAn optional std::shared_ptr to an existing Category in the model.
Remarks
This method expects the item's assigned ID to be valid and updates max_item_id to be equal or greater to the item's ID. This is intended for an import operation such as KitParser::parse_items where all ID's are valid and define many-to-many relationships between items and categories.

Definition at line 40 of file model.cpp.

◆ associate_item_with_category()

void Model::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, but using only the item's ID as a reference.

Parameters
item_idThe ID of the Item within the model.
categoryA std::shared_ptr to the Category within the model.

Definition at line 62 of file model.cpp.

◆ change_all_current_items_checked_states()

void Model::change_all_current_items_checked_states ( check_action action)

Switches the checked state of all items in the currently selected Category.

All items are processed without any filtering by their checked state.

Parameters
actionThe type of check_action to take.

Definition at line 251 of file model.cpp.

◆ change_checked_state()

const std::shared_ptr< Item > Model::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 action.

Parameters
idThe ID of the Item to be updated.
actionThe type of check_action to take.
Returns
A pointer to the updated Item or nullptr if the Item does not exist.

Definition at line 234 of file model.cpp.

◆ copy_checked_items_to_categories()

void Model::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.

Parameters
cidsA list of category IDs to copy all the checked items to.

Definition at line 415 of file model.cpp.

◆ copy_item_to_categories()

void Model::copy_item_to_categories ( int32_t item_id,
std::vector< int32_t > cids )

Copies the specified item to the list of categories.

Parameters
item_idThe ID of the item to be copies.
cidsA list of category IDs to copy all the checked items to.

Definition at line 432 of file model.cpp.

◆ copy_items_to_categories()

void Model::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.

Only the items complying with the specified filter are copied to categories. The default of Model::all copies all items, otherwise only items matching the specified filter are copied.

Parameters
itemsThe source list of Item instance to copy to categories.
categoriesThe target list of Category instances.

Definition at line 391 of file model.cpp.

◆ count_filter_items_for_current_selected_category()

auto Model::count_filter_items_for_current_selected_category ( ) const
inline

Returns a count of the filtered items in the currently selected category.

If selected_category is no_category then all items are returned subject to the filter state.

Definition at line 457 of file model.hpp.

◆ delete_all_current_checked_items()

void Model::delete_all_current_checked_items ( )

Deletes all checked items.

If there is a current category selected, then only those items in the current category are deleted.

Definition at line 347 of file model.cpp.

◆ delete_category()

void Model::delete_category ( int32_t id)

Deletes the Category having the passed ID.

The model relationships are updated appropriately.

Parameters
idThe ID of the Category to delete.

Definition at line 125 of file model.cpp.

◆ delete_item()

void Model::delete_item ( int32_t id)

Deletes the Item having the passed ID.

The model relationships are updated appropriately.

Parameters
idThe ID of the Item to delete.

Definition at line 148 of file model.cpp.

◆ filter_items_for_current_selected_category()

const vector< shared_ptr< Item > > Model::filter_items_for_current_selected_category ( ) const

Filters the list of Item instances based on the currently selected category (selected_category) and checked status (filter).

If selected_category is no_category then all items are returned subject to the filter state.

Returns
a filtered list of Item instances.

Definition at line 283 of file model.cpp.

◆ get_all_items()

const std::vector< std::shared_ptr< Item > > Model::get_all_items ( ) const
inline

Gets all items, without filtering.

Returns
all items, i.e. without any filtering by checked state or current Category.

Definition at line 537 of file model.hpp.

◆ get_all_items_for_current_selected_category()

const vector< shared_ptr< Item > > Model::get_all_items_for_current_selected_category ( ) const

Returns all items for the currently selected Category.

All items are returned without any filtering by their checked state.

See also
selected_category

Definition at line 270 of file model.cpp.

◆ get_categories()

const std::vector< std::shared_ptr< Category > > Model::get_categories ( ) const
inline

The list of categories.

Returns
All categories.

Definition at line 527 of file model.hpp.

◆ get_category()

const std::shared_ptr< Category > Model::get_category ( int32_t id) const

Gets a Category by ID.

Parameters
idThe ID of the Category.
Returns
The Category with the passed ID, or nullptr if it does not exist in the model.

Definition at line 226 of file model.cpp.

◆ get_category_count()

auto Model::get_category_count ( ) const
inline

The count of categories in the model.

Definition at line 295 of file model.hpp.

◆ get_current_category()

std::shared_ptr< Category > Model::get_current_category ( )
inline

Returns the current Category or nullptr if not found.

Definition at line 401 of file model.hpp.

◆ get_current_checked_item_count()

std::vector< std::shared_ptr< Item > >::size_type Model::get_current_checked_item_count ( ) const
inline

Returns the count of items in the currently selected Category.

The count disregards the current filter.

Definition at line 283 of file model.hpp.

◆ get_filter()

state_filter Model::get_filter ( ) const
inline

The current state_filter.

Definition at line 547 of file model.hpp.

◆ get_item()

const std::shared_ptr< Item > Model::get_item ( int32_t id) const

Gets an Item by ID.

Parameters
idThe ID of the Item.
Returns
The Item with the passed ID, or nullptr if it does not exist in the model.

Definition at line 218 of file model.cpp.

◆ get_item_count()

auto Model::get_item_count ( ) const
inline

The count of items in the model.

Definition at line 274 of file model.hpp.

◆ get_selected_category()

int32_t Model::get_selected_category ( ) const
inline

The ID of the currently selected Category.

Definition at line 518 of file model.hpp.

◆ is_dirty()

bool Model::is_dirty ( ) const
inline

Whether the model has been modified since it was last saved.

Returns
true if the Model has been modified since it was last saved.

Definition at line 267 of file model.hpp.

◆ new_category()

int32_t Model::new_category ( const std::string & name)

Creates a new Category in the model using the passed parameters, incrementing max_category_id and assigning it to the Category.

Parameters
nameThe category's name.
Returns
The ID of the newly created category.

Definition at line 195 of file model.cpp.

◆ new_item()

int32_t Model::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 it to the Item.

Parameters
nameThe item's name.
checkedAn optional boolean, true indicating it is checked. Defaults to false.
Returns
The ID of the newly created item.

Definition at line 104 of file model.cpp.

◆ remove_all_current_checked_items()

void Model::remove_all_current_checked_items ( )

Removes all checked items from the current category.

Definition at line 308 of file model.cpp.

◆ remove_item()

void Model::remove_item ( int32_t id)

Removes references to the item from the currently selected Category.

The model relationships are updated appropriately.

Remarks
The item itself is not deleted from the model and therefore will still be returned by a call to get_all_items.

Definition at line 183 of file model.cpp.

◆ remove_items()

void Model::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.

All the model relationships are updated appropriately.

Parameters
itemsA list of shared_ptr to the Item instances being removed from the currently selected Category.
Remarks
The items themselves are not deleted from the model and therefore will still be returned by a call to get_all_items.

Definition at line 164 of file model.cpp.

◆ renumber()

void Model::renumber ( )

Re-assigns IDs to all of the categories and items.

Definition at line 574 of file model.cpp.

◆ renumber_categories()

void Model::renumber_categories ( )

Re-assigns IDs to all the categories.

Definition at line 555 of file model.cpp.

◆ renumber_items()

void Model::renumber_items ( )

Re-assigns IDs to all the items.

Definition at line 536 of file model.cpp.

◆ save()

bool Model::save ( const std::string & filename)

saves the Model to a file.

Parameters
filenameThe name of the file to save the model to.
Returns
true if the save succeeded.

Definition at line 95 of file model.cpp.

◆ select_category()

void Model::select_category ( int32_t id)
inline

Sets the selected Category to that of the passed ID.

Parameters
idThe ID of the Category

Definition at line 511 of file model.hpp.

◆ set_category_name()

std::shared_ptr< Category > Model::set_category_name ( const int32_t id,
const std::string & name )
inline

Updates the name of passed Category.

Implemented such that only calls from an implementation of KitListBaseApp can modify the model and therefore have an opportunity to be aware of the change, updating the user interface appropriately.

Parameters
idThe ID of the Category to be updated.
nameThe new name of the Category.
Returns
A pointer to the Category or nullptr if it does not exist.

Definition at line 191 of file model.hpp.

◆ set_dirty()

void Model::set_dirty ( bool dirty = true)

Sets dirty flag.

Also calls KitListBaseApp::model_state_changed() notifying the change of state.

Parameters
dirtytrue if the model has been altered.

Definition at line 34 of file model.cpp.

◆ set_filter()

void Model::set_filter ( state_filter state)
inline

Sets filtering of checked items.

See also
state_filter.

Definition at line 542 of file model.hpp.

◆ set_item_checked()

std::shared_ptr< Item > Model::set_item_checked ( const int32_t id,
const bool state )
inline

Updates the checked state of the passed Item.

Implemented such that only calls from an implementation of KitListBaseApp can modify the model and therefore have an opportunity to be aware of the change, updating the user interface appropriately.

Parameters
idThe ID of the Item to be updated.
stateThe new state of the Item.
Returns
A pointer to the Item or nullptr if it does not exist.

Definition at line 233 of file model.hpp.

◆ set_item_name()

std::shared_ptr< Item > Model::set_item_name ( const int32_t id,
const std::string & name )
inline

Updates the name of the passed Item.

Implemented such that only calls from an implementation of KitListBaseApp can modify the model and therefore have an opportunity to be aware of the change, updating the user interface appropriately.

Parameters
idThe ID of the Item to be updated.
nameThe new name of the Item.
Returns
A pointer to the Item or nullptr if it does not exist.

Definition at line 212 of file model.hpp.

◆ show_model()

void Model::show_model ( ) const

Dumps details of the entire model for debugging.

Definition at line 487 of file model.cpp.

◆ sort()

void Model::sort ( )

Sorts everything within the model.

Definition at line 479 of file model.cpp.

◆ sort_categories()

void Model::sort_categories ( )

Sorts all the model's categories.

Definition at line 453 of file model.cpp.

◆ sort_category_items()

void Model::sort_category_items ( )

Sorts all the items associated with each category.

Definition at line 461 of file model.cpp.

◆ sort_item_categories()

void Model::sort_item_categories ( )

Sorts all the categories assicated with each item.

Definition at line 470 of file model.cpp.

◆ sort_items()

void Model::sort_items ( )

Sorts all the model's items.

Definition at line 445 of file model.cpp.

◆ toggle_item_checked()

std::shared_ptr< Item > Model::toggle_item_checked ( const int32_t id)
inlineprivate

Toggles the checked state of the passed Item.

Implemented such that only calls from an implementation of KitListBaseApp can modify the model and therefore have an opportunity to be aware of the change, updating the user interface appropriately.

Parameters
idThe ID of the Item to be updated.
Returns
A pointer to the Item or nullptr if it does not exist.

Definition at line 89 of file model.hpp.

◆ validate()

void Model::validate ( ) const

Validates the model ensuring all the maps are consistent with each of the corresponding lists.

Definition at line 509 of file model.cpp.

◆ KitParser

friend class KitParser
friend

Definition at line 129 of file model.hpp.

Member Data Documentation

◆ categories

std::vector<std::shared_ptr<Category> > Model::categories
private

The list of categories.

Definition at line 49 of file model.hpp.

◆ category_map

std::map<int32_t, std::shared_ptr<Category> > Model::category_map
private

A map of all categories keyed by ID.

Definition at line 55 of file model.hpp.

◆ dirty

bool Model::dirty
private

Indicates that the Model has been modified since it was last saved.

Definition at line 43 of file model.hpp.

◆ filter

state_filter Model::filter
private

Indicates filtering state of checked items.

See also
state_filter.

Definition at line 125 of file model.hpp.

◆ item_map

std::map<int32_t, std::shared_ptr<Item> > Model::item_map
private

A map of all items keyed by ID.

Definition at line 52 of file model.hpp.

◆ items

std::vector<std::shared_ptr<Item> > Model::items
private

The list of items.

Definition at line 46 of file model.hpp.

◆ max_category_id

int32_t Model::max_category_id
private

The highest category ID in use.

Remarks
incremented each time a new category is created and used as it's ID, so if non-zero it will be a valid ID.

Definition at line 69 of file model.hpp.

◆ max_item_id

int32_t Model::max_item_id
private

The highest item ID in use.

Remarks
incremented each time a new item is created and used as it's ID, so if non-zero it will be a valid ID.

Definition at line 62 of file model.hpp.

◆ no_category

const int32_t Model::no_category = -1
static

Indicates no filtering by category.

Definition at line 153 of file model.hpp.

◆ selected_category

int32_t Model::selected_category
private

The currently selected category.

A value of no_category indicates that no category is selected, so all items will be shown.

Definition at line 77 of file model.hpp.


The documentation for this class was generated from the following files:
Copyright 2008-2025 Frank Dean