Kitlist
A list manager for maintaining kit lists
Loading...
Searching...
No Matches
kitlist_base_app.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 KITLIST_BASE_APP_HPP
23#define KITLIST_BASE_APP_HPP
24
25#include "model.hpp"
26#include <iostream>
27#include <memory>
28#include <string>
29#include <vector>
30
43
44 friend class Model;
45
47 std::unique_ptr<Model> model;
48
55 std::string filename;
56
66
67protected:
68
80 virtual void update_menu_state(bool is_dirty) {
81 (void)is_dirty; // unused parameter
82 }
83
91 virtual void category_entry_changed(const std::shared_ptr<Category> category) {}
92
99 virtual void item_entry_changed(const std::shared_ptr<Item> item) {}
100
106 virtual void category_name_changed(const std::shared_ptr<Category> category) {
108 category_entry_changed(category);
109 update_menu_state(model->is_dirty());
110 }
111
117 virtual void item_name_changed(const std::shared_ptr<Item> item) {
118 sort();
119 item_entry_changed(item);
120 update_menu_state(model->is_dirty());
121 }
122
128 virtual void item_checked_changed(const std::shared_ptr<Item> item) {
129 if (get_filter() != Model::all)
130 item_entry_changed(item);
131 update_menu_state(model->is_dirty());
132 }
133
141 virtual void model_state_changed(bool is_dirty) {
142 if (initialized)
144 }
145
146public:
147
150
152 virtual ~KitListBaseApp();
153
156 const std::string all_items_text = "-- Show all items --";
157
159 static const std::string DEFAULT_FILE_EXTENSION;
160
162 bool is_dirty() const {
163 return model->is_dirty();
164 }
165
166 void set_dirty(bool dirty = true) const {
167 model->set_dirty(dirty);
168 }
169
176 return model->get_current_checked_item_count();
177 }
178
180 void new_file() {
181 model.reset(new Model());
182 filename.clear();
183 }
184
198 virtual void load_file(const std::string& filename);
199
201 virtual bool save() {
202 if (filename.empty())
203 return false;
204 const bool retval = model->save(filename);
205 if (!model->is_dirty())
206 update_menu_state(false);
207 return retval;
208 }
209
210 virtual bool save(std::string filename) {
211 this->filename = filename;
212 return save();
213 }
214
226 const std::shared_ptr<Category> set_category_name(const int32_t id,
227 const std::string& name) {
228 auto category = model->set_category_name(id, name);
229 category_name_changed(category);
230 return category;
231 }
232
244 const std::shared_ptr<Item> set_item_name(const int32_t id,
245 const std::string& name) {
246 auto item = model->set_item_name(id, name);
247 if (item)
248 item_name_changed(item);
249 return item;
250 }
251
263 const std::shared_ptr<Item> set_item_checked(const int32_t id,
264 const bool state) {
265 auto item = model->set_item_checked(id, state);
266 if (item)
268 return item;
269 }
270
281 virtual const std::shared_ptr<Item> change_checked_state(
282 int32_t id,
283 Model::check_action action) {
284
285 return model->change_checked_state(id, action);
286 }
287
297 Model::check_action action) const {
298
299 model->change_all_current_items_checked_states(action);
300 }
301
307 void set_filename(const std::string& filename) {
308 this->filename = filename;
309 }
310
314 const std::string get_filename() const {
315 return filename;
316 }
317
325 virtual void initialization_complete() {
326 initialized = true;
327 }
328
342 int32_t new_item(const std::string& name, bool checked) const {
343 return model->new_item(name, checked);
344 }
345
354 void delete_category(int32_t id) const {
355 model->delete_category(id);
356 }
357
366 void delete_item(int32_t id) const {
367 model->delete_item(id);
368 }
369
383 void remove_items(const std::vector<std::shared_ptr<Item>>& items) const {
384 model->remove_items(items);
385 }
386
396 void remove_item(int32_t id) const {
397 model->remove_item(id);
398 }
399
410 int32_t new_category(const std::string& name) const {
411 return model->new_category(name);
412 }
413
422 const std::shared_ptr<Item> get_item(int32_t id) const {
423 return model->get_item(id);
424 }
425
433 const std::shared_ptr<Category> get_category(int32_t id) const {
434 return model->get_category(id);
435 }
436
444 const std::vector<std::shared_ptr<Item>>
446
447 return model->get_all_items_for_current_selected_category();
448 }
449
461 const std::vector<std::shared_ptr<Item>>
463
464 return model->filter_items_for_current_selected_category();
465 }
466
474 return model->count_filter_items_for_current_selected_category();
475 }
476
481 model->remove_all_current_checked_items();
482 }
483
491 model->delete_all_current_checked_items();
492 }
493
505 void associate_item_with_category(int32_t item_id, std::shared_ptr<Category> category) {
506 model->associate_item_with_category(item_id, category);
507 }
508
517 std::vector<std::shared_ptr<Item>>& items,
518 std::vector<std::shared_ptr<Category>>& categories) const {
519
520 model->copy_items_to_categories(items, categories);
521 }
522
529 void copy_checked_items_to_categories(std::vector<int32_t> cids) const {
530 model->copy_checked_items_to_categories(cids);
531 }
532
539 void copy_item_to_categories(int32_t item_id, std::vector<int32_t> cids) {
540 model->copy_item_to_categories(item_id, cids);
541 }
542
549 void select_category(int32_t id) {
550 model->select_category(id);
551 update_menu_state(model->is_dirty());
552 }
553
559 int32_t get_selected_category() const {
560 return model->get_selected_category();
561 }
562
568 const std::vector<std::shared_ptr<Category>> get_categories() const {
569 return model->get_categories();
570 }
571
578 const std::vector<std::shared_ptr<Item>> get_all_items() const {
579 return model->get_all_items();
580 }
581
583 void set_filter(Model::state_filter state) const {
584 model->set_filter(state);
585 }
586
592 return model->get_filter();
593 }
594
598 void sort_categories() const {
599 model->sort_categories();
600 }
601
605 void sort() const {
606 model->sort();
607 }
608
610 class file_not_found : public std::exception {
612 std::string message;
613 public:
615 file_not_found(std::string message) : std::exception(), message(message) {}
617 virtual const char* what() const throw() override {
618 return message.c_str();
619 }
620 };
621
622};
623
624#endif // KITLIST_BASE_APP_HPP
virtual const char * what() const override
Describes the exeption.
file_not_found(std::string message)
Constructor taking a message.
std::string message
The exception's message.
void set_dirty(bool dirty=true) const
auto count_filter_items_for_current_selected_category() const
Returns a count of the filtered items in the currently selected category.
void remove_items(const std::vector< std::shared_ptr< Item > > &items) const
Removes references to each of the Item instances in the list from 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 (Model::selected_category...
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_all_current_checked_items()
Deletes all checked items.
void sort() const
Sorts everything within the model.
virtual const std::shared_ptr< Item > change_checked_state(int32_t id, Model::check_action action)
Switches the checked state of the Item referenced by the specified ID according to the specified acti...
void new_file()
Create a new Model and sets the associated filename to be empty.
virtual void update_menu_state(bool is_dirty)
Called whenever there is a change to the Model state.
const std::shared_ptr< Category > set_category_name(const int32_t id, const std::string &name)
Updates the name of passed Category.
KitListBaseApp()
Default constructor.
void sort_categories() const
Sorts all the model's categories.
void copy_checked_items_to_categories(std::vector< int32_t > cids) const
Copies all currently checked items for the currently selected Category to the list of categories.
virtual void item_checked_changed(const std::shared_ptr< Item > item)
void set_filter(Model::state_filter state) const
Sets filtering of checked items.
void remove_item(int32_t id) const
Removes references to the item from the currently selected Category.
int32_t get_selected_category() const
Gets the ID of the currently selected Category.
const std::shared_ptr< Item > set_item_name(const int32_t id, const std::string &name)
Updates the name of the passed Item.
const std::string get_filename() const
virtual void category_name_changed(const std::shared_ptr< Category > category)
std::string filename
The current filename the Model was loaded from.
void copy_items_to_categories(std::vector< std::shared_ptr< Item > > &items, std::vector< std::shared_ptr< Category > > &categories) const
Copies all of the passed items to each of the passed categories, updating the model relationships.
virtual void item_entry_changed(const std::shared_ptr< Item > item)
Called when a Item instance's state has changed.
const std::shared_ptr< Item > set_item_checked(const int32_t id, const bool state)
Updates the checked state of the passed Item.
virtual void category_entry_changed(const std::shared_ptr< Category > category)
Called when a Category name has changed.
void delete_category(int32_t id) const
Deletes the Category having the passed ID.
int32_t new_item(const std::string &name, bool checked) const
creates a new Item in the model using the passed parameters, incrementing Model::max_item_id and assi...
virtual void load_file(const std::string &filename)
Loads the specified file.
const std::vector< std::shared_ptr< Item > > get_all_items() const
Gets all items, without filtering.
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,...
virtual void initialization_complete()
Sets initialized to true.
void select_category(int32_t id)
Sets the selected Category to that of the passed ID.
virtual void item_name_changed(const std::shared_ptr< Item > item)
bool initialized
Indicates that the application has finished it's initialization stage.
void set_filename(const std::string &filename)
Sets a new filename to associate with the Model.
void remove_all_current_checked_items()
Removes all checked items from the current category.
std::unique_ptr< Model > model
The application's data model.
virtual bool save(std::string filename)
static const std::string DEFAULT_FILE_EXTENSION
The application's default filename extension. .kit.
const std::vector< std::shared_ptr< Item > > get_all_items_for_current_selected_category() const
Returns all items for the currently selected Category.
virtual ~KitListBaseApp()
Destructor.
void delete_item(int32_t id) const
Deletes the Item having the passed ID.
auto get_current_checked_item_count() const
const std::shared_ptr< Category > get_category(int32_t id) const
Gets a Category by ID.
const std::string all_items_text
Constant string indicating the option to select no category and therefor display all items.
const std::vector< std::shared_ptr< Category > > get_categories() const
Gets the list of categories.
int32_t new_category(const std::string &name) const
Creates a new Category in the Model using the passed parameters, incrementing Model::max_category_id ...
const std::shared_ptr< Item > get_item(int32_t id) const
Gets an Item by ID.
virtual bool save()
Saves the current Model using the current filename.
void change_all_current_items_checked_states(Model::check_action action) const
Switches the checked state of all items in the currently selected Category.
virtual void model_state_changed(bool is_dirty)
Called whenever the Model state is changed.
Model::state_filter get_filter() const
The current filter.
bool is_dirty() const
check_action
Specifies what state Item checkmarks must be changed to.
Definition model.hpp:117
state_filter
Constants for filtering items depending on their checked state.
Definition model.hpp:104
@ all
Show all items, i.e. no filtering.
Definition model.hpp:106
Copyright 2008-2025 Frank Dean