00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #ifndef KIT_MODEL_H
00024 #define KIT_MODEL_H 1
00025
00026 #include "item.hpp"
00027 #include "category.hpp"
00028 #include <map>
00029 #include <sigc++/signal.h>
00030
00031
00038 class GuiState {
00039 protected:
00040 bool m_dirty;
00041 bool m_deleted;
00042 bool m_new;
00043 public:
00044 GuiState() : m_dirty(false), m_deleted(false), m_new(false) {}
00045 bool is_dirty() { return m_dirty; }
00046 void set_dirty(bool dirty = true) { m_dirty = dirty; }
00047 bool is_deleted() { return m_deleted; }
00048 void set_deleted(bool deleted) { m_deleted = deleted; }
00049 void set_new_flag(bool flag) { m_new = flag; }
00050 bool is_new() { return m_new; }
00051 virtual void reset();
00052 };
00053
00054 class ItemCompareId;
00055
00056
00060 class ModelItem : public Item, public GuiState {
00061 public:
00062 ModelItem() : Item(), GuiState() {}
00063 friend class ModelItemCompareId;
00064 virtual void set_checked(bool checked) { set_dirty(); Item::set_checked(checked); }
00065 };
00066
00067
00072 class ModelItemCompareId {
00073 public:
00074 ModelItemCompareId() {}
00075 int operator()(ModelItem* i1, ModelItem* i2) {
00076 return (i1->get_id() < i2->get_id());
00077 }
00078 };
00079
00080
00081 typedef std::vector<ModelItem*> ModelItemContainer;
00082 typedef ModelItemContainer::iterator ModelItemIter;
00083
00084 typedef std::map<long, ModelItem*> ItemMap;
00085 typedef ItemMap::iterator ItemMapIter;
00086
00087 typedef sigc::slot<bool, const ItemMap::iterator&> SlotForeachModelItemIter;
00088 typedef sigc::slot<bool, ModelItem&> SlotForeachModelItem;
00089
00090
00094 class ModelCategory : public Category, public GuiState {
00095 protected:
00097 ItemMap* m_removed_children;
00099 ItemMap* m_added_children;
00100 public:
00101 ModelCategory();
00102 ~ModelCategory();
00103 virtual ModelItemContainer* get_model_items();
00104 virtual ItemContainer* get_items();
00105 virtual ItemContainer* get_items(ItemFunctor& functor);
00107 virtual ItemMap* get_removed_children() { return m_removed_children; }
00109 virtual ItemMap* get_added_children() { return m_added_children; }
00110 virtual void add_item(Item*);
00111 virtual void remove_item(Item* item);
00112 virtual void remove_items(ModelItemContainer* items);
00113 virtual void reset();
00114 virtual void purge();
00115 friend class KitModel;
00116 };
00117
00118
00119 typedef std::vector<ModelCategory*> ModelCategoryContainer;
00120 typedef ModelCategoryContainer::iterator ModelCategoryIter;
00121
00122 typedef std::map<long, ModelCategory*> CategoryMap;
00123 typedef CategoryMap::iterator CategoryMapIter;
00124
00125 typedef sigc::slot<bool, const CategoryMap::iterator&> SlotForeachCategoryIter;
00126 typedef sigc::slot<bool, ModelCategory&> SlotForeachCategory;
00127
00128 enum item_filter_types {ALL, CHECKED, UNCHECKED};
00129
00130
00135 class KitModel {
00136 protected:
00138 bool m_dirty;
00145 CategoryMap* m_category_map;
00152 ItemMap* m_item_map;
00158 enum item_filter_types m_item_filter;
00159 public:
00160 KitModel();
00161 ~KitModel();
00162 void foreach_item_iter(const SlotForeachModelItemIter& slot);
00163 void foreach_item(const SlotForeachModelItem& slot);
00164 void foreach_category_iter(const SlotForeachCategoryIter& slot);
00165 void foreach_category(const SlotForeachCategory& slot);
00166 virtual ModelCategory* find_category(long id);
00167 virtual ModelItem* find_item(long id);
00168 virtual CategoryContainer* get_categories();
00169 virtual ItemContainer* get_all_items();
00170 virtual ItemContainer* get_all_items(ItemFunctor& functor);
00171 virtual void add_category(ModelCategory* category);
00172 virtual void add_item(ModelItem* item);
00173 virtual void add_item(ModelItem* item, long cat_id);
00174 virtual void copy_items(const ModelItemContainer& items, long cat_id = -1);
00175 virtual bool filter(bool checked);
00176 virtual void set_dirty(bool dirty = true) { m_dirty=dirty; }
00177 virtual bool is_dirty() { return m_dirty; }
00179 virtual void show_all() { m_item_filter = ALL; }
00181 virtual void show_checked_only() { m_item_filter = CHECKED; }
00183 virtual void show_unchecked_only() { m_item_filter = UNCHECKED; }
00184 virtual void reset();
00185 virtual void purge();
00186 };
00187
00188
00189 #endif // KIT_MODEL_H