Item dialog shows correct category in dropdown
[shopper.git] / src / Category.cc
blob4776aa5dc2072c80f5548d47c9aff50085107672
1 /* Shopper
2 * Copyright (C) 2008 David Greaves <david@dgreaves.com>
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
20 //#define DEBUG_SHOPPER 1
22 #include "shopper.h"
24 #include <iostream>
25 #include <sstream>
26 #include <string>
27 #include <fstream>
28 #include <list>
29 #include <set>
30 #include <algorithm>
31 #include <signal.h>
32 #include "shopperList.h"
34 using namespace std;
36 namespace Shopper
38 ////////////////////////////////////////////////////////////////
39 // Category
40 int Category::id_master = 0;
41 Category::Category(sparseCategory dummy) : // SPARSE constructor for ListParser
42 id(0),
43 name(),
44 _wanted(0),
45 _bought(0)
46 { Q_UNUSED(dummy) }
47 Category::Category() :
48 id (Category::id_master++),
49 _wanted(0),
50 _bought(0)
53 Category::Category(QString name) :
54 id (Category::id_master++),
55 name(name),
56 _wanted(0),
57 _bought(0)
59 Category::~Category()
61 Item* it;
62 while (!items.empty()) // Can't use an iterator as rm() modifies the list... <sigh>
64 it = items.back();
65 rm(*it);
67 emit deleted();
70 void Category::dbg() {
71 DEBUG("\nCat name : " << name <<
72 "\n id : " << id <<
73 "\n wanted : " << _wanted <<
74 "\n bought : " << _bought <<
75 "\n");
78 // Operators
79 bool operator< (const Category &a, const Category &b){
80 return (a.id < b.id);
82 bool cat_cmp (const Category *a, const Category *b) {
83 return (a->id < b->id);
85 // cmp (user defined)
86 // bool user_cmp (const Category &a, const Category &b)
87 // {
88 // return (a.pos < b.pos);
89 // }
91 // accessors
92 int Category::get_id() const { return id; }
93 void Category::set_id(int i) {
94 if (id != i) {
95 id = i;
96 emit changed();
99 void Category::set_name(QString n) {
100 if (name != n) {
101 name = n;
102 emit changed();
105 QString Category::get_name() const { return name; }
106 int Category::get_size() const { return items.size(); }
108 void Category::setWanted(bool b) { b?_wanted++:_wanted--; emit changed(); }
109 void Category::setBought(bool b) { b?_bought++:_bought--; emit changed(); }
110 int Category::wanted() const { return _wanted; }
111 int Category::bought() const { return _bought; }
114 void Category::add(Item &it)
116 DEBUG("Category add item " << it.get_desc() << "\n");
117 items.push_back(&it);
118 items.sort(cmpItem);
119 emit item_added(&it);
121 void Category::rm(Item &it, bool deleteItem)
123 DEBUG("Category remove item " << it.get_desc() << "\n");
124 items.remove(&it);
125 emit item_removed(&it);
126 if (deleteItem) delete &it;
129 Category::pItemIter Category::itemsI() {return items.begin();}
130 Category::pItemIter Category::itemsEnd(){return items.end();}
131 int Category::size() const {return items.size();}