Kitlist  1.1.0
kitmodel.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,2009 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 
23 #ifndef KIT_MODEL_H
24 #define KIT_MODEL_H 1
25 
26 #include "item.hpp"
27 #include "category.hpp"
28 #include <map>
29 #include <sigc++/signal.h>
30 
31 
38 class GuiState {
39  protected:
40  bool m_dirty;
41  bool m_deleted;
42  bool m_new;
43  public:
44  GuiState() : m_dirty(false), m_deleted(false), m_new(false) {}
45  bool is_dirty() { return m_dirty; }
46  void set_dirty(bool dirty = true) { m_dirty = dirty; }
47  bool is_deleted() { return m_deleted; }
48  void set_deleted(bool deleted) { m_deleted = deleted; }
49  void set_new_flag(bool flag) { m_new = flag; }
50  bool is_new() { return m_new; }
51  virtual void reset();
52 };
53 
54 class ItemCompareId;
55 
56 
60 class ModelItem : public Item, public GuiState {
61  public:
62  ModelItem() : Item(), GuiState() {}
63  friend class ModelItemCompareId;
64  virtual void set_checked(bool checked) { set_dirty(); Item::set_checked(checked); }
65 };
66 
67 
73  public:
75  int operator()(ModelItem* i1, ModelItem* i2) {
76  return (i1->get_id() < i2->get_id());
77  }
78 };
79 
80 
81 typedef std::vector<ModelItem*> ModelItemContainer;
82 typedef ModelItemContainer::iterator ModelItemIter;
83 
84 typedef std::map<long, ModelItem*> ItemMap;
85 typedef ItemMap::iterator ItemMapIter;
86 
87 typedef sigc::slot<bool, const ItemMap::iterator&> SlotForeachModelItemIter;
88 typedef sigc::slot<bool, ModelItem&> SlotForeachModelItem;
89 
90 
94 class ModelCategory : public Category, public GuiState {
95  protected:
100  public:
101  ModelCategory();
102  ~ModelCategory();
103  virtual ModelItemContainer* get_model_items();
104  virtual ItemContainer* get_items();
105  virtual ItemContainer* get_items(ItemFunctor& functor);
107  virtual ItemMap* get_removed_children() { return m_removed_children; }
109  virtual ItemMap* get_added_children() { return m_added_children; }
110  virtual void add_item(Item*);
111  virtual void remove_item(Item* item);
112  virtual void remove_items(ModelItemContainer* items);
113  virtual void reset();
114  virtual void purge();
115  friend class KitModel;
116 };
117 
118 
119 typedef std::vector<ModelCategory*> ModelCategoryContainer;
120 typedef ModelCategoryContainer::iterator ModelCategoryIter;
121 
122 typedef std::map<long, ModelCategory*> CategoryMap;
123 typedef CategoryMap::iterator CategoryMapIter;
124 
125 typedef sigc::slot<bool, const CategoryMap::iterator&> SlotForeachCategoryIter;
126 typedef sigc::slot<bool, ModelCategory&> SlotForeachCategory;
127 
129 
130 
135 class KitModel {
136  protected:
138  bool m_dirty;
158  enum item_filter_types m_item_filter;
159  public:
160  KitModel();
161  ~KitModel();
162  void foreach_item_iter(const SlotForeachModelItemIter& slot);
163  void foreach_item(const SlotForeachModelItem& slot);
164  void foreach_category_iter(const SlotForeachCategoryIter& slot);
165  void foreach_category(const SlotForeachCategory& slot);
166  virtual ModelCategory* find_category(long id);
167  virtual ModelItem* find_item(long id);
168  virtual CategoryContainer* get_categories();
169  virtual ItemContainer* get_all_items();
170  virtual ItemContainer* get_all_items(ItemFunctor& functor);
171  virtual void add_category(ModelCategory* category);
172  virtual void add_item(ModelItem* item);
173  virtual void add_item(ModelItem* item, long cat_id);
174  virtual void copy_items(const ModelItemContainer& items, long cat_id = -1);
175  virtual bool filter(bool checked);
176  virtual void set_dirty(bool dirty = true) { m_dirty=dirty; }
177  virtual bool is_dirty() { return m_dirty; }
179  virtual void show_all() { m_item_filter = ALL; }
181  virtual void show_checked_only() { m_item_filter = CHECKED; }
183  virtual void show_unchecked_only() { m_item_filter = UNCHECKED; }
184  virtual void reset();
185  virtual void purge();
186 };
187 
188 
189 #endif // KIT_MODEL_H
long get_id()
Definition: item.hpp:46
ModelItemContainer::iterator ModelItemIter
Definition: kitmodel.hpp:82
Class encapsulating state of an object in the data model.
Definition: kitmodel.hpp:38
Comparator for comparing items by their unique ID.
Definition: kitmodel.hpp:72
sigc::slot< bool, ModelItem & > SlotForeachModelItem
Definition: kitmodel.hpp:88
Represents a Category combined with GuiState attributes.
Definition: kitmodel.hpp:94
ItemMap * m_removed_children
List of items removed from the Category.
Definition: kitmodel.hpp:97
CategoryMap * m_category_map
Map allowing categories to be located by ID.
Definition: kitmodel.hpp:145
bool m_deleted
Definition: kitmodel.hpp:41
virtual void set_dirty(bool dirty=true)
Definition: kitmodel.hpp:176
sigc::slot< bool, ModelCategory & > SlotForeachCategory
Definition: kitmodel.hpp:126
void set_dirty(bool dirty=true)
Definition: kitmodel.hpp:46
void set_new_flag(bool flag)
Definition: kitmodel.hpp:49
ItemMap::iterator ItemMapIter
Definition: kitmodel.hpp:85
Comparator used for comparing Items by id.
Definition: item.hpp:72
bool is_dirty()
Definition: kitmodel.hpp:45
std::vector< Item * > ItemContainer
Definition: item.hpp:91
virtual void set_checked(bool checked)
Definition: item.hpp:49
virtual void show_unchecked_only()
Sets the filter to show only unchecked items.
Definition: kitmodel.hpp:183
virtual void show_checked_only()
Sets the filter to show only checked items.
Definition: kitmodel.hpp:181
bool m_dirty
Definition: kitmodel.hpp:40
sigc::slot< bool, const ItemMap::iterator & > SlotForeachModelItemIter
Definition: kitmodel.hpp:87
bool is_new()
Definition: kitmodel.hpp:50
virtual void show_all()
Removes filter. All items are shown.
Definition: kitmodel.hpp:179
std::vector< ModelItem * > ModelItemContainer
Definition: kitmodel.hpp:81
void set_deleted(bool deleted)
Definition: kitmodel.hpp:48
bool m_new
Definition: kitmodel.hpp:42
ItemMap * m_item_map
Map allowing items to be located by ID.
Definition: kitmodel.hpp:152
item_filter_types
Definition: kitmodel.hpp:128
Represents an Item.
Definition: item.hpp:37
virtual void reset()
resets the state of each flag to it&#39;s default.
Definition: kitmodel.cpp:36
CategoryMap::iterator CategoryMapIter
Definition: kitmodel.hpp:123
bool is_deleted()
Definition: kitmodel.hpp:47
Represents a Category.
Definition: category.hpp:37
Represents an Item combined with GuiState attributes.
Definition: kitmodel.hpp:60
std::vector< ModelCategory * > ModelCategoryContainer
Definition: kitmodel.hpp:119
Holds a rich graph of objects representing the application&#39;s data model.
Definition: kitmodel.hpp:135
virtual ItemMap * get_removed_children()
Returns a list of items that have been removed from the Category.
Definition: kitmodel.hpp:107
ItemMap * m_added_children
List of items added to the Category.
Definition: kitmodel.hpp:99
bool m_dirty
Indicates whether the model needs saving. I.e it&#39;s state has changed.
Definition: kitmodel.hpp:138
virtual void set_checked(bool checked)
Definition: kitmodel.hpp:64
virtual bool is_dirty()
Definition: kitmodel.hpp:177
ModelCategoryContainer::iterator ModelCategoryIter
Definition: kitmodel.hpp:120
std::vector< Category * > CategoryContainer
Definition: category.hpp:84
int operator()(ModelItem *i1, ModelItem *i2)
Definition: kitmodel.hpp:75
GuiState()
Definition: kitmodel.hpp:44
std::map< long, ModelCategory * > CategoryMap
Definition: kitmodel.hpp:122
Functor for processing items.
Definition: item.hpp:85
std::map< long, ModelItem * > ItemMap
Definition: kitmodel.hpp:84
sigc::slot< bool, const CategoryMap::iterator & > SlotForeachCategoryIter
Definition: kitmodel.hpp:125
virtual ItemMap * get_added_children()
Returns a list of items that have been added to the Category.
Definition: kitmodel.hpp:109

Copyright 2008-2021 Frank Dean