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
33 std::basic_ostream
<char>& operator<<(std::basic_ostream
<char>& os
, const
38 // some forward declarations...
44 // These are Shopper typedefs
45 // FIXME: Is this the C++ way?
46 typedef enum _List_action
{
49 CATEGORY_LIST_CHANGED
,
56 LIST_ACTIVE_CAT_CHANGED
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
,
67 ////////////////////////////////////////////////////////////////
68 class Category
: public QObject
73 typedef enum sparseCategory_
{SPARSE
} sparseCategory
; // Only friends know about
74 Category(sparseCategory dummy
); // This constructor is for friends to instantiate objects
77 Category(const Category
&src
);
78 Category(QString name
);
86 void set_name(QString
);
89 friend bool operator< (const Category
&a
, const Category
&b
);
90 friend bool cat_cmp (const Category
*a
, const Category
*b
);
93 typedef list
<Item
*>::const_iterator pItemIter
;
97 static const Category EVERYTHING
; /* Used for filter */
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 */
108 virtual void changed();
109 virtual void deleted();
111 friend class ListParser
;
112 friend class XMLWriter
;
116 ////////////////////////////////////////////////////////////////
117 class Item
: public QObject
122 typedef enum sparseItem_
{SPARSE
} sparseItem
; // Only friends know about
123 Item(sparseItem dummy
); // This constructor is for friends to instantiate objects
127 Item(Category
&c
, QString desc
, QString note
, bool wanted
, bool bought
);
136 void set_desc(QString
);
138 void set_note(QString
);
140 void set_wanted(bool);
142 void set_bought(bool);
144 void set_category(Category
*c
);
145 Category
* get_category();
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 */
157 virtual void deleted();
158 virtual void changed();
161 friend class ListParser
;
162 friend class XMLWriter
;
164 ////////////////////////////////////////////////////////////////
165 class List
: public QObject
169 // declare some friends used to load/save Lists
170 friend class ListParser
;
171 friend class XMLWriter
;
175 List(QString dump
); // initiate from a dump
177 static List
* from_file(QString filename
);
178 void to_file(QString filename
);
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
192 // Helper functions used in managing category order
195 void swap_categories(int, int);
197 void swap_categories(Category
*a
, Category
*b
);
202 void set_name(QString
);
204 void set_state(sState
);
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();
216 void boughtNothing(); // marks all items as not bought
217 void newList(); // marks all items as not wanted
219 static QString
modeText(sState s
);
221 // collection iterators
222 typedef list
<Item
*>::const_iterator pItemIter
;
223 typedef list
<Category
*>::const_iterator pCategoryIter
;
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();
233 void state_changed();
234 void active_category_changed();
238 void cycle_active_category(bool f
);
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
;
247 list
<Category
*> categories
;
252 ////////////////////////////////////////////////////////////////
254 class ListParser
: public QXmlDefaultHandler
256 friend class List
; // Given protected constructor, only Lists can make ListParsers
258 ListParser(List
*l
); // Expected to be passed 'this'
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
);
278 map
<int, Category
*> cat_by_id
;
281 ////////////////////////////////////////////////////////////////
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
);
294 Q_DECLARE_METATYPE ( Shopper::Category
* )
296 #endif //SHOPPERLIST_H
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
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.