Category dialog draws, moves, deletes - still needs popups to handle deleting items
[shopper.git] / src / shopperList.h
blobe1285c9d70f04baccd3f115c9a4bacd35e0ea37d
1 /* Shopper
2 * Copyright (C) 2008 David Greaves
4 * This software is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public License
6 * as published by the Free Software Foundation; either version 2.1 of
7 * the License, or (at your option) any later version.
9 * This software is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this software; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
17 * 02110-1301 USA
21 #ifndef SHOPPERLIST_H
22 #define SHOPPERLIST_H
24 #include <QObject>
25 #include <QtXml>
27 #include <iostream>
28 #include <list>
29 #include <set>
30 #include <map>
31 using namespace std;
33 std::basic_ostream<char>& operator<<(std::basic_ostream<char>& os, const
34 QString& str);
36 namespace Shopper
38 // some forward declarations...
39 class List;
40 class Category;
41 class Item;
44 // These are Shopper typedefs
45 // FIXME: Is this the C++ way?
46 typedef enum _List_action {
47 CATEGORY_CHANGED,
48 CATEGORY_DELETED,
49 CATEGORY_LIST_CHANGED,
50 ITEM_CHANGED,
51 ITEM_DELETED,
52 ITEM_LIST_CHANGED,
53 LIST_CHANGED,
54 LIST_STATE_CHANGED,
55 LIST_CLEARED,
56 LIST_ACTIVE_CAT_CHANGED
57 } List_action;
59 // a signal used between data and view
60 // typedef sigc::signal<void, List_action, List*, Category*, Item*> signal_changed_t;
62 // This is really application state but it needs saving to XML
63 typedef enum sState_ {MAKING_LIST,
64 OUT_SHOPPING,
65 WHATS_LEFT} sState;
67 ////////////////////////////////////////////////////////////////
68 class Category : public QObject
70 Q_OBJECT;
72 private:
73 typedef enum sparseCategory_ {SPARSE} sparseCategory; // Only friends know about
74 Category(sparseCategory dummy); // This constructor is for friends to instantiate objects
75 public:
76 Category();
77 Category(const Category &src);
78 Category(QString name);
79 ~Category();
81 void dbg();
82 // accessors
83 QString print_id();
84 int get_id();
85 void set_id(int);
86 void set_name(QString);
87 int get_size();
88 QString get_name();
89 friend bool operator< (const Category &a, const Category &b);
90 friend bool cat_cmp (const Category *a, const Category *b);
91 void add(Item &it);
92 void rm(Item &it);
93 typedef list<Item*>::const_iterator pItemIter;
94 pItemIter itemsI();
95 pItemIter itemsEnd();
97 static const Category EVERYTHING; /* Used for filter */
98 private:
99 int id; /* The id of the item */
100 // int pos; /* Allow user defined sorting */
101 QString name; /* The item name */
102 static int id_master; /* Used for uniqueness */
104 // Items in list
105 list<Item*> items;
107 signals:
108 virtual void changed();
109 virtual void deleted();
111 friend class ListParser;
112 friend class XMLWriter;
113 friend class List;
116 ////////////////////////////////////////////////////////////////
117 class Item : public QObject
119 Q_OBJECT;
121 private:
122 typedef enum sparseItem_ {SPARSE} sparseItem; // Only friends know about
123 Item(sparseItem dummy); // This constructor is for friends to instantiate objects
125 public:
126 Item();
127 Item(Category &c, QString desc, QString note, bool wanted, bool bought);
128 ~Item();
130 void dbg();
132 // accessors
133 QString print_id();
134 int get_id();
135 void set_id(int);
136 void set_desc(QString);
137 QString get_desc();
138 void set_note(QString);
139 QString get_note();
140 void set_wanted(bool);
141 bool get_wanted();
142 void set_bought(bool);
143 bool get_bought();
144 void set_category(Category *c);
145 Category* get_category();
147 private:
148 int id; /* The id of the item (why?) */
149 static int id_master; /* Used for uniqueness */
150 Category *category; /* The id of the category the item is in */
151 QString desc; /* The item desc */
152 QString note; /* User note (flavour, quantity)*/
153 bool wanted; /* do we want one of these? */
154 bool bought; /* when out shopping, have we bought one of these */
155 public:
156 signals:
157 virtual void deleted();
158 virtual void changed();
161 friend class ListParser;
162 friend class XMLWriter;
164 ////////////////////////////////////////////////////////////////
165 class List : public QObject
167 Q_OBJECT;
169 // declare some friends used to load/save Lists
170 friend class ListParser;
171 friend class XMLWriter;
172 public:
173 // Constructors
174 List();
175 List(QString dump); // initiate from a dump
176 ~List();
177 static List* from_file(QString filename);
178 void to_file(QString filename);
180 void dbg();
182 //List rawList(); // err dunno
183 operator QString(); // ustring cast gives XML
184 // ostream (>>) gives XML
185 friend std::ostream& operator<< (std::ostream &out, const List &l);
187 void add(Item &it); // List will manage Item
188 void rm(Item &it); // Delete item
189 void add(Category &cat); // List will manage Category
190 void rm(Category &cat); // Delete Category and all items
191 private:
192 // Helper functions used in managing category order
193 void resequence();
194 void reorder();
195 void swap_categories(int, int);
196 public:
197 void swap_categories(Category *a, Category *b);
199 public:
200 // accessors
201 QString get_name();
202 void set_name(QString);
203 sState get_state();
204 void set_state(sState);
206 // Filtering
207 Category* get_active_category();
208 bool is_category_active(Category &c);
209 void no_category_active();
210 void make_category_active(Category &c);
211 void make_category_inactive(Category &c);
212 void make_next_category_active();
213 void make_prev_category_active();
215 // Mode handling
216 void boughtNothing(); // marks all items as not bought
217 void newList(); // marks all items as not wanted
218 QString modeText();
219 static QString modeText(sState s);
221 // collection iterators
222 typedef list<Item*>::const_iterator pItemIter;
223 typedef list<Category*>::const_iterator pCategoryIter;
224 pItemIter itemsI();
225 pItemIter itemsEnd();
226 pCategoryIter categoriesI();
227 pCategoryIter categoriesEnd();
229 signals: // Not sure we need this detail!!
230 void item_list_changed();
231 void category_list_changed();
232 void changed();
233 void state_changed();
234 void active_category_changed();
235 void deleted();
237 private:
238 void cycle_active_category(bool f);
239 // members:
240 QString name;
241 sState state; // making, out:all, out:left
242 Category* active_category ;
243 // If using a set for multiple active categories
244 // set<Category> active_categories ;
245 map<int, Category*> cat_by_id;
246 public:
247 list<Category*> categories;
248 private:
249 list<Item*> items;
252 ////////////////////////////////////////////////////////////////
254 class ListParser : public QXmlDefaultHandler
256 friend class List; // Given protected constructor, only Lists can make ListParsers
257 protected:
258 ListParser(List *l); // Expected to be passed 'this'
259 // ~ListParser();
260 void from_string(QString);
261 void start_list_el(const QXmlAttributes & atts);
262 void start_cat_el(const QXmlAttributes & atts);
263 void start_item_el(const QXmlAttributes & atts);
265 // QXmlDefaultHandler Interface
266 bool startElement (const QString & namespaceURI,
267 const QString & localName,
268 const QString & qName,
269 const QXmlAttributes & atts);
270 bool endElement (const QString & namespaceURI,
271 const QString & localName,
272 const QString & qName);
273 bool fatalError ( const QXmlParseException & exception );
275 List* l;
276 Category* c;
277 Item* i;
278 map<int, Category*> cat_by_id;
279 int active_cat_id;
281 ////////////////////////////////////////////////////////////////
282 class XMLWriter
284 private:
285 XMLWriter(const List *l); // Expected to be passed 'this'
286 std::ostream& write(std::ostream &o);
287 friend class List; // Given a private constructor, only Lists can make XMLWriters
288 friend std::ostream& operator<< (std::ostream &out, const List &l);
289 const List *l;
294 Q_DECLARE_METATYPE ( Shopper::Category* )
296 #endif //SHOPPERLIST_H
300 // Ordering
302 // This needs 2 functions: re-sequence and re-order
304 // re-sequence iterates the list in the current list order and sets
305 // 'id' incrementally
307 // re-order causes the list to examine all the member id's and
308 // re-order itself
310 // Each category has a sort position - 'id'.
311 // On load this is used to insert into the <list>
312 // post-load the list is re-sequenced
314 // On 'add' the category is inserted according to a probably duplicate id
315 // Then the list is re-sequenced.
317 // To re-order the list, the id's are swapped and the list is re-ordered.