Added a Notify class
[shopper.git] / src / ui / CategoryDialog.cc
blob67693d6dffd1c7d64598a43b46ba35f48b90c926
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
20 #define DEBUG_SHOPPER 1
21 #include "shopper.h" // automake, i8n, gettext
23 #include "CategoryDialog.h"
24 #include "notify.h"
26 namespace Shopper
28 ////////////////////////////////////////////////////////////////
29 // Public
30 CategoryManage::CategoryManage(QWidget * parent, Shopper::List* l) :
31 CategoryDialog(parent, l, NULL)
34 ////////////////////////////////////////////////////////////////
35 // Private class CategoryDialog : public QDialog
36 CategoryDialog::CategoryDialog(QWidget * parent, Shopper::List* l, Shopper::Category* cat) :
37 QDialog(parent),
38 mylist(l),
39 mycat(cat),
40 area(NULL)
42 _ENTER;
43 // Buttons
44 bbox = new QDialogButtonBox(Qt::Horizontal, this);
45 bbox->addButton(tr("Done"), QDialogButtonBox::AcceptRole);
46 connect(bbox, SIGNAL(accepted()), this, SLOT(accept()));
48 connect(bbox->addButton(tr("Add Category"), QDialogButtonBox::ActionRole),
49 SIGNAL(clicked()),
50 this, SLOT(add_category()));
52 // Scroll area with category views
53 areaS = new QScrollArea(this);
54 areaS->setWidgetResizable(true);
56 // moved from update
57 area = new QWidget();
58 areaL = new QVBoxLayout(area);
59 area->setLayout(areaL); // set the layout and take parentage of all children
60 areaS->setWidget(area); // add area to the scroller
62 // Build them up
63 vbox = new QVBoxLayout(this);
64 vbox->addWidget(areaS);
65 vbox->addWidget(bbox);
67 // add the categories to the area.
68 update_area();
70 // Now connect the List's category_list_changed() signal to this object's updater
71 connect (l, SIGNAL(category_list_changed()),
72 this, SLOT(update_area()));
74 // geometric aesthetics
75 // int h=get_screen()->get_height();
76 // int w=get_screen()->get_width();
77 // resize((w*6)/10,h);
80 void CategoryDialog::update_area()
82 _ENTER;
83 // Clean up our old
84 QObject *child;
85 foreach (child, area->children()){
86 if (child->isWidgetType()) {
87 DEBUG("Kill a child " << child->metaObject()->className() );
88 child->deleteLater();
92 // lots of these
93 CatView *cv;
94 int pos;
95 Shopper::List::pCategoryIter begin =mylist->categoriesI(),
96 end = mylist->categoriesEnd();
97 Shopper::List::pCategoryIter catI;
98 for(catI = begin, pos=0; catI != end; ++catI,++pos)
100 cv = new CatView(**catI, *mylist);
101 areaL->addWidget(cv); // add CatView to the layout
102 connect(cv, SIGNAL(move_up()),
103 this, SLOT(move_up()));
104 connect(cv, SIGNAL(move_down()),
105 this, SLOT(move_down()));
106 if (catI == begin) cv->set_pos(pos,-1); // size of -1 won't be reached
108 cv->set_pos(pos-1,pos-1); // Now we know the end
110 area->show();
113 void CategoryDialog::add_category()
115 // Create a new category
116 mycat = new Shopper::Category("New Category");
117 mylist->add(*mycat);
118 // FIXME: set the sw->scroll and category edit focus here
120 #define DOWN_SCREEN 1
121 #define UP_SCREEN -1
122 void CategoryDialog::move_up()
124 _ENTER;
125 CatView *src = static_cast<CatView*>(this->sender());
126 move(src, UP_SCREEN);
128 void CategoryDialog::move_down()
130 _ENTER;
131 CatView *src = static_cast<CatView*>(this->sender());
132 move(src, DOWN_SCREEN);
134 void CategoryDialog::move(CatView* src, int direction)
136 _ENTER;
137 int pos = areaL->indexOf(src);
138 int max = areaL->count()-1;
139 DEBUG("src at pos=" << pos << "/"<<max<<" direction=" << direction);
141 if (direction == DOWN_SCREEN and pos == max) return; // moving 'down' the screen
142 if (direction == UP_SCREEN and pos == 0) return;
144 QLayoutItem *li = areaL->itemAt(pos+direction);
145 if (li == 0) {
146 DEBUG("No item found at pos=" << pos << " direction=" << direction);
147 return;
149 CatView *that = static_cast<CatView*>(areaL->itemAt(pos+direction)->widget());
150 Q_ASSERT(that != 0);
152 areaL->removeWidget(src);
153 areaL->insertWidget(pos+direction, src);
154 mylist->swap_categories(src->get_cat(), that->get_cat());
155 src->set_pos(pos+direction,max);
156 that->set_pos(pos,max);
157 area->show();
160 ////////////////////////////////////////////////////////////////
161 ////////////////////////////////////////////////////////////////
162 ////////////////////////////////////////////////////////////////
164 // The widget shows the state of a category
165 // it can move up or down and edits the relevant category.
166 CatView::CatView(Category &cat, List &lst, QWidget *parent) :
167 QWidget(parent),
168 mycat(&cat),
169 mylist(&lst)
171 _ENTER;
172 // UI
173 name = new LabelEntry(cat.get_name(), this);
174 up_b = new QPushButton(style()->standardIcon(QStyle::SP_ArrowUp), "", this);
175 down_b = new QPushButton(style()->standardIcon(QStyle::SP_ArrowDown), "", this);
176 QPushButton *del_b = new QPushButton(style()->standardIcon(QStyle::SP_TrashIcon), "", this);
177 QHBoxLayout *box = new QHBoxLayout(this);
179 box->addWidget(del_b);
180 box->addWidget(name);
181 box->addWidget(up_b);
182 box->addWidget(down_b);
183 setLayout(box);
185 // Respond to changing the label
186 connect(name, SIGNAL(changed()),
187 this, SLOT(name_changed()));
188 // Respond to del
189 connect(del_b, SIGNAL(clicked()),
190 this, SLOT(delete_category()));
191 // Respond to up
192 connect(up_b, SIGNAL(clicked()),
193 this, SIGNAL(move_up()));
194 // Respond to down
195 connect(down_b, SIGNAL(clicked()),
196 this, SIGNAL(move_down()));
198 // Make the container visible according to visibility logic
199 DEBUG("CatView created for" <<cat.get_name());
202 void CatView::delete_category()
204 DEBUG("Delete Category");
205 if (mylist->is_category_active(*mycat)) {
206 notify->showMessage("Shopper: Info", "Can't delete current category");
207 return;
210 // Message
211 QString l;
212 int s = mycat->get_size();
213 if (s > 0) {
214 l = "Category '" + mycat->get_name()
215 + "' has " + QString(s)
216 + " items. Delete them all? ";
217 } else {
218 l = "Delete empty category '" + mycat->get_name() + "'?";
221 QDialog dia(this);
222 QVBoxLayout vbox(&dia);
223 QDialogButtonBox bbox(QDialogButtonBox::Yes | QDialogButtonBox::No, Qt::Horizontal, &dia);
224 connect(&bbox, SIGNAL(accepted()), &dia, SLOT(accept()));
225 connect(&bbox, SIGNAL(rejected()), &dia, SLOT(reject()));
226 vbox.addWidget(new QLabel("Are you sure?", &dia));
227 vbox.addWidget(new QLabel(l, &dia));
228 vbox.addWidget(&bbox);
229 dia.setLayout(&vbox);
231 if (dia.exec() == QDialog::Accepted)
233 //FIX Hildon::Banner::show_information(*this, "Deleted category " + mycat->get_name());
234 mylist->rm(*mycat);
238 void CatView::name_changed()
240 mycat->set_name(name->getText());
242 void CatView::set_pos(int pos, int size)
244 _ENTER;
245 if (pos == 0) up_b->hide(); else up_b->show();
246 if (pos == size) down_b->hide(); else down_b->show();
247 show();
250 Category * CatView::get_cat()
252 return mycat;