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();
00106 virtual ItemMap* get_removed_children() { return m_removed_children; }
00108 virtual ItemMap* get_added_children() { return m_added_children; }
00109 virtual void add_item(Item*);
00110 virtual void remove_item(Item* item);
00111 virtual void remove_items(ModelItemContainer* items);
00112 virtual void reset();
00113 virtual void purge();
00114 friend class KitModel;
00115 };
00116
00117
00118 typedef std::vector<ModelCategory*> ModelCategoryContainer;
00119 typedef ModelCategoryContainer::iterator ModelCategoryIter;
00120
00121 typedef std::map<long, ModelCategory*> CategoryMap;
00122 typedef CategoryMap::iterator CategoryMapIter;
00123
00124 typedef sigc::slot<bool, const CategoryMap::iterator&> SlotForeachCategoryIter;
00125 typedef sigc::slot<bool, ModelCategory&> SlotForeachCategory;
00126
00127 enum item_filter_types {ALL, CHECKED, UNCHECKED};
00128
00129
00134 class KitModel {
00135 protected:
00137 bool m_dirty;
00144 CategoryMap* m_category_map;
00151 ItemMap* m_item_map;
00157 enum item_filter_types m_item_filter;
00158 public:
00159 KitModel();
00160 ~KitModel();
00161 void foreach_item_iter(const SlotForeachModelItemIter& slot);
00162 void foreach_item(const SlotForeachModelItem& slot);
00163 void foreach_category_iter(const SlotForeachCategoryIter& slot);
00164 void foreach_category(const SlotForeachCategory& slot);
00165 virtual ModelCategory* find_category(long id);
00166 virtual ModelItem* find_item(long id);
00167 virtual CategoryContainer* get_categories();
00168 virtual ItemContainer* get_all_items();
00169 virtual void add_category(ModelCategory* category);
00170 virtual void add_item(ModelItem* item);
00171 virtual void add_item(ModelItem* item, long cat_id);
00172 virtual void copy_items(const ModelItemContainer& items, long cat_id = -1);
00173 virtual bool filter(bool checked);
00174 virtual void set_dirty(bool dirty = true) { m_dirty=dirty; }
00175 virtual bool is_dirty() { return m_dirty; }
00177 virtual void show_all() { m_item_filter = ALL; }
00179 virtual void show_checked_only() { m_item_filter = CHECKED; }
00181 virtual void show_unchecked_only() { m_item_filter = UNCHECKED; }
00182 virtual void reset();
00183 virtual void purge();
00184 };
00185
00186
00187 #endif // KIT_MODEL_H