added preferences + dialog
[shopper.git] / src / shopperList.h
blob84feb5fa5229c5966dd5b03f8e1383a2e4bdad10
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 namespace Shopper
35 // some forward declarations...
36 class List;
37 class Category;
38 class Item;
41 // These are Shopper typedefs
42 // FIXME: Is this the C++ way?
43 typedef enum _List_action {
44 CATEGORY_CHANGED,
45 CATEGORY_DELETED,
46 CATEGORY_LIST_CHANGED,
47 ITEM_CHANGED,
48 ITEM_DELETED,
49 ITEM_LIST_CHANGED,
50 LIST_CHANGED,
51 LIST_STATE_CHANGED,
52 LIST_CLEARED,
53 LIST_ACTIVE_CAT_CHANGED
54 } List_action;
56 // a signal used between data and view
57 // typedef sigc::signal<void, List_action, List*, Category*, Item*> signal_changed_t;
59 // This is really application state but it needs saving to XML
60 typedef enum sState_ {MAKING_LIST,
61 OUT_SHOPPING,
62 WHATS_LEFT} sState;
64 ////////////////////////////////////////////////////////////////
65 class Category : public QObject
67 Q_OBJECT;
69 private:
70 typedef enum sparseCategory_ {SPARSE} sparseCategory; // Only friends know about
71 Category(sparseCategory dummy); // This constructor is for friends to instantiate objects
72 public:
73 Category();
74 Category(const Category &src);
75 Category(QString name);
76 ~Category();
78 void dbg();
79 // accessors
80 QString print_id();
81 int get_id();
82 void set_id(int);
83 void set_name(QString);
84 int get_size();
85 void setWanted(bool b);
86 int wanted();
87 void setBought(bool b);
88 int bought();
89 QString get_name();
90 friend bool operator< (const Category &a, const Category &b);
91 friend bool cat_cmp (const Category *a, const Category *b);
92 void add(Item &it);
93 void rm(Item &it);
94 typedef list<Item*>::const_iterator pItemIter;
95 pItemIter itemsI();
96 pItemIter itemsEnd();
97 int size();
99 static const Category EVERYTHING; /* Used for filter */
100 private:
101 int id; /* The id of the item */
102 // int pos; /* Allow user defined sorting */
103 QString name; /* The item name */
104 int _wanted;
105 int _bought;
106 static int id_master; /* Used for uniqueness */
108 // Items in list
109 list<Item*> items;
111 signals:
112 virtual void changed();
113 virtual void deleted();
115 friend class ListParser;
116 friend class XMLWriter;
117 friend class List;
120 ////////////////////////////////////////////////////////////////
121 class Item : public QObject
123 Q_OBJECT;
125 private:
126 typedef enum sparseItem_ {SPARSE} sparseItem; // Only friends know about
127 Item(sparseItem dummy); // This constructor is for friends to instantiate objects
129 public:
130 Item();
131 Item(Category &c, QString desc, QString note, bool wanted, bool bought);
132 ~Item();
134 void dbg();
136 // accessors
137 QString print_id();
138 int get_id();
139 void set_id(int);
140 void set_desc(QString);
141 QString get_desc();
142 void set_note(QString);
143 QString get_note();
144 void set_wanted(bool);
145 bool get_wanted();
146 void set_bought(bool);
147 bool get_bought();
148 void set_category(Category *c);
149 Category* get_category();
151 private:
152 int id; /* The id of the item (why?) */
153 static int id_master; /* Used for uniqueness */
154 Category *category; /* The id of the category the item is in */
155 QString desc; /* The item desc */
156 QString note; /* User note (flavour, quantity)*/
157 bool wanted; /* do we want one of these? */
158 bool bought; /* when out shopping, have we bought one of these */
159 public:
160 signals:
161 virtual void deleted();
162 virtual void changed();
165 friend class ListParser;
166 friend class XMLWriter;
168 ////////////////////////////////////////////////////////////////
169 class List : public QObject
171 Q_OBJECT;
173 // declare some friends used to load/save Lists
174 friend class ListParser;
175 friend class XMLWriter;
176 public:
177 // Constructors
178 List();
179 List(QString dump); // initiate from a dump
180 ~List();
181 static List* from_file(QString filename);
182 void to_file(QString filename);
184 void dbg();
186 //List rawList(); // err dunno
187 operator QString(); // ustring cast gives XML
188 // ostream (>>) gives XML
189 friend std::ostream& operator<< (std::ostream &out, const List &l);
191 void add(Item &it); // List will manage Item
192 void rm(Item &it); // Delete item
193 void add(Category &cat); // List will manage Category
194 void rm(Category &cat); // Delete Category and all items
195 private:
196 // Helper functions used in managing category order
197 void resequence();
198 void reorder();
199 void swap_categories(int, int);
200 public:
201 void swap_categories(Category *a, Category *b);
203 public:
204 // accessors
205 QString get_name();
206 void set_name(QString);
207 sState get_state();
208 void set_state(sState);
210 // Filtering
211 Category* get_active_category();
212 bool is_category_active(Category &c);
213 void no_category_active();
214 void make_category_active(Category &c);
215 void make_category_inactive(Category &c);
216 void make_next_category_active();
217 void make_prev_category_active();
219 // Mode handling
220 void boughtNothing(); // marks all items as not bought
221 void newList(); // marks all items as not wanted
222 QString modeText();
223 static QString modeText(sState s);
225 // collection iterators
226 typedef list<Item*>::const_iterator pItemIter;
227 typedef list<Category*>::const_iterator pCategoryIter;
228 pItemIter itemsI();
229 pItemIter itemsEnd();
230 pCategoryIter categoriesI();
231 pCategoryIter categoriesEnd();
233 signals: // Not sure we need this detail!!
234 void item_list_changed();
235 void category_list_changed();
236 void changed();
237 void state_changed();
238 void active_category_changed();
239 void deleted();
241 private:
242 void cycle_active_category(bool f);
243 // members:
244 QString name;
245 sState state; // making, out:all, out:left
246 Category* active_category ;
247 // If using a set for multiple active categories
248 // set<Category> active_categories ;
249 map<int, Category*> cat_by_id;
250 public:
251 list<Category*> categories;
252 private:
253 list<Item*> items;
256 ////////////////////////////////////////////////////////////////
258 class ListParser : public QXmlDefaultHandler
260 friend class List; // Given protected constructor, only Lists can make ListParsers
261 protected:
262 ListParser(List *l); // Expected to be passed 'this'
263 // ~ListParser();
264 void from_string(QString);
265 void start_list_el(const QXmlAttributes & atts);
266 void start_cat_el(const QXmlAttributes & atts);
267 void start_item_el(const QXmlAttributes & atts);
269 // QXmlDefaultHandler Interface
270 bool startElement (const QString & namespaceURI,
271 const QString & localName,
272 const QString & qName,
273 const QXmlAttributes & atts);
274 bool endElement (const QString & namespaceURI,
275 const QString & localName,
276 const QString & qName);
277 bool fatalError ( const QXmlParseException & exception );
279 List* l;
280 Category* c;
281 Item* i;
282 map<int, Category*> cat_by_id;
283 int active_cat_id;
285 ////////////////////////////////////////////////////////////////
286 class XMLWriter
288 private:
289 XMLWriter(const List *l); // Expected to be passed 'this'
290 std::ostream& write(std::ostream &o);
291 friend class List; // Given a private constructor, only Lists can make XMLWriters
292 friend std::ostream& operator<< (std::ostream &out, const List &l);
293 const List *l;
298 Q_DECLARE_METATYPE ( Shopper::Category* )
300 #endif //SHOPPERLIST_H
304 // Ordering
306 // This needs 2 functions: re-sequence and re-order
308 // re-sequence iterates the list in the current list order and sets
309 // 'id' incrementally
311 // re-order causes the list to examine all the member id's and
312 // re-order itself
314 // Each category has a sort position - 'id'.
315 // On load this is used to insert into the <list>
316 // post-load the list is re-sequenced
318 // On 'add' the category is inserted according to a probably duplicate id
319 // Then the list is re-sequenced.
321 // To re-order the list, the id's are swapped and the list is re-ordered.