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
21 #ifndef SHOPPER_LIST_H
22 #define SHOPPER_LIST_H
35 // some forward declarations...
41 typedef enum _clear_list
{CLEAR_WANTED
,CLEAR_BOUGHT
} clear_list_mode
;
43 // This is really application state but it needs saving to XML
44 typedef enum sState_
{MAKING_LIST
,
48 class List
: public QObject
52 // declare some friends used to load/save Lists
53 friend class ListParser
;
54 friend class XMLWriter
;
58 List(QString dump
); // initiate from a dump
60 static List
* from_file(QString filename
);
61 void to_file(QString filename
);
65 //List rawList(); // err dunno
66 operator QString(); // ustring cast gives XML
67 // ostream (>>) gives XML
68 friend std::ostream
& operator<< (std::ostream
&out
, const List
&l
);
70 void add(Item
&it
); // List will manage Item
71 void rm(Item
&it
); // Delete item
72 void add(Category
&cat
); // List will manage Category
73 void rm(Category
&cat
); // Delete Category and all items
75 // Helper functions used in managing category order
78 void swap_categories(int, int);
80 void swap_categories(Category
*a
, Category
*b
);
84 QString
get_name() const;
85 void set_name(QString
);
86 sState
get_state() const;
87 void set_state(sState
);
88 void clear(clear_list_mode m
);
91 Category
* get_active_category();
92 bool is_category_active(Category
&c
) const;
93 void no_category_active();
94 void make_category_active(Category
&c
);
95 void make_category_inactive(Category
&c
);
96 void make_next_category_active();
97 void make_prev_category_active();
100 void boughtNothing(); // marks all items as not bought
101 void newList(); // marks all items as not wanted
103 static QString
modeText(sState s
);
105 // collection iterators
106 // typedef list<Item*>::const_iterator pItemIter;
107 typedef list
<Category
*>::const_iterator pCategoryIter
;
108 // pItemIter itemsI();
109 // pItemIter itemsEnd();
110 pCategoryIter
categoriesI();
111 pCategoryIter
categoriesEnd();
114 // void item_list_changed();
115 void item_added(Item
*);
116 void item_removed(Item
*);
118 void category_list_changed();
119 void category_list_reordered();
120 void category_added(Category
*);
121 void category_removed(Category
*);
124 void state_changed();
125 void active_category_changed();
129 void cycle_active_category(bool f
);
132 sState state
; // making, out:all, out:left
133 Category
* active_category
;
134 // If using a set for multiple active categories
135 // set<Category> active_categories ;
136 map
<int, Category
*> cat_by_id
;
138 list
<Category
*> categories
;
143 ////////////////////////////////////////////////////////////////
145 class ListParser
: public QXmlDefaultHandler
147 friend class List
; // Given protected constructor, only Lists can make ListParsers
149 ListParser(List
*l
); // Expected to be passed 'this'
151 void from_string(QString
);
152 void start_list_el(const QXmlAttributes
& atts
);
153 void start_cat_el(const QXmlAttributes
& atts
);
154 void start_item_el(const QXmlAttributes
& atts
);
156 // QXmlDefaultHandler Interface
157 bool startElement (const QString
& namespaceURI
,
158 const QString
& localName
,
159 const QString
& qName
,
160 const QXmlAttributes
& atts
);
161 bool endElement (const QString
& namespaceURI
,
162 const QString
& localName
,
163 const QString
& qName
);
164 bool fatalError ( const QXmlParseException
& exception
);
169 map
<int, Category
*> cat_by_id
;
172 ////////////////////////////////////////////////////////////////
176 XMLWriter(const List
*l
); // Expected to be passed 'this'
177 std::ostream
& write(std::ostream
&o
);
178 friend class List
; // Given a private constructor, only Lists can make XMLWriters
179 friend std::ostream
& operator<< (std::ostream
&out
, const List
&l
);
185 #endif //SHOPPERLIST_H
191 // This needs 2 functions: re-sequence and re-order
193 // re-sequence iterates the list in the current list order and sets
194 // 'id' incrementally
196 // re-order causes the list to examine all the member id's and
199 // Each category has a sort position - 'id'.
200 // On load this is used to insert into the <list>
201 // post-load the list is re-sequenced
203 // On 'add' the category is inserted according to a probably duplicate id
204 // Then the list is re-sequenced.
206 // To re-order the list, the id's are swapped and the list is re-ordered.