The class declaration should be the first header in the cc file for
[shopper.git] / src / List.h
blob84c856a038939d63734e52159eaddd7ff5e3d44e
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 SHOPPER_LIST_H
22 #define SHOPPER_LIST_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;
40 // types
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,
45 OUT_SHOPPING,
46 WHATS_LEFT} sState;
48 class List : public QObject
50 Q_OBJECT;
52 // declare some friends used to load/save Lists
53 friend class ListParser;
54 friend class XMLWriter;
55 public:
56 // Constructors
57 List();
58 List(QString dump); // initiate from a dump
59 ~List();
60 static List* from_file(QString filename);
61 void to_file(QString filename);
63 void dbg();
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
74 private:
75 // Helper functions used in managing category order
76 void resequence();
77 void reorder();
78 void swap_categories(int, int);
79 public:
80 void swap_categories(Category *a, Category *b);
82 public:
83 // accessors
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);
90 // Filtering
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();
99 // Mode handling
100 void boughtNothing(); // marks all items as not bought
101 void newList(); // marks all items as not wanted
102 QString modeText();
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();
113 signals:
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*);
123 void changed();
124 void state_changed();
125 void active_category_changed();
126 void deleted();
128 private:
129 void cycle_active_category(bool f);
130 // members:
131 QString name;
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;
137 public:
138 list<Category*> categories;
139 private:
140 list<Item*> items;
143 ////////////////////////////////////////////////////////////////
145 class ListParser : public QXmlDefaultHandler
147 friend class List; // Given protected constructor, only Lists can make ListParsers
148 protected:
149 ListParser(List *l); // Expected to be passed 'this'
150 // ~ListParser();
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 );
166 List* l;
167 Category* c;
168 Item* i;
169 map<int, Category*> cat_by_id;
170 int active_cat_id;
172 ////////////////////////////////////////////////////////////////
173 class XMLWriter
175 private:
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);
180 const List *l;
185 #endif //SHOPPERLIST_H
189 // Ordering
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
197 // re-order itself
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.