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
35 // some forward declarations...
41 // These are Shopper typedefs
42 // FIXME: Is this the C++ way?
43 typedef enum _List_action
{
46 CATEGORY_LIST_CHANGED
,
53 LIST_ACTIVE_CAT_CHANGED
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
,
64 ////////////////////////////////////////////////////////////////
65 class Category
: public QObject
70 typedef enum sparseCategory_
{SPARSE
} sparseCategory
; // Only friends know about
71 Category(sparseCategory dummy
); // This constructor is for friends to instantiate objects
74 Category(const Category
&src
);
75 Category(QString name
);
83 void set_name(QString
);
85 void setWanted(bool b
);
87 void setBought(bool b
);
90 friend bool operator< (const Category
&a
, const Category
&b
);
91 friend bool cat_cmp (const Category
*a
, const Category
*b
);
94 typedef list
<Item
*>::const_iterator pItemIter
;
99 static const Category EVERYTHING
; /* Used for filter */
101 int id
; /* The id of the item */
102 // int pos; /* Allow user defined sorting */
103 QString name
; /* The item name */
106 static int id_master
; /* Used for uniqueness */
112 virtual void changed();
113 virtual void deleted();
115 friend class ListParser
;
116 friend class XMLWriter
;
120 ////////////////////////////////////////////////////////////////
121 class Item
: public QObject
126 typedef enum sparseItem_
{SPARSE
} sparseItem
; // Only friends know about
127 Item(sparseItem dummy
); // This constructor is for friends to instantiate objects
131 Item(Category
&c
, QString desc
, QString note
, bool wanted
, bool bought
);
140 void set_desc(QString
);
142 void set_note(QString
);
144 void set_wanted(bool);
146 void set_bought(bool);
148 void set_category(Category
*c
);
149 Category
* get_category();
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 */
161 virtual void deleted();
162 virtual void changed();
165 friend class ListParser
;
166 friend class XMLWriter
;
168 ////////////////////////////////////////////////////////////////
169 class List
: public QObject
173 // declare some friends used to load/save Lists
174 friend class ListParser
;
175 friend class XMLWriter
;
179 List(QString dump
); // initiate from a dump
181 static List
* from_file(QString filename
);
182 void to_file(QString filename
);
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
196 // Helper functions used in managing category order
199 void swap_categories(int, int);
201 void swap_categories(Category
*a
, Category
*b
);
206 void set_name(QString
);
208 void set_state(sState
);
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();
220 void boughtNothing(); // marks all items as not bought
221 void newList(); // marks all items as not wanted
223 static QString
modeText(sState s
);
225 // collection iterators
226 typedef list
<Item
*>::const_iterator pItemIter
;
227 typedef list
<Category
*>::const_iterator pCategoryIter
;
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();
237 void state_changed();
238 void active_category_changed();
242 void cycle_active_category(bool f
);
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
;
251 list
<Category
*> categories
;
256 ////////////////////////////////////////////////////////////////
258 class ListParser
: public QXmlDefaultHandler
260 friend class List
; // Given protected constructor, only Lists can make ListParsers
262 ListParser(List
*l
); // Expected to be passed 'this'
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
);
282 map
<int, Category
*> cat_by_id
;
285 ////////////////////////////////////////////////////////////////
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
);
298 Q_DECLARE_METATYPE ( Shopper::Category
* )
300 #endif //SHOPPERLIST_H
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
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.