Move from SLabel to ActiveLabel
[shopper.git] / src / ui / ListView.cc
blob706bf9ebdb673e0dcbd10cd33541383303577591
1 /* Shopper
2 * Copyright (C) 2008 David Greaves <david@dgreaves.com>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
20 //#define DEBUG_SHOPPER 1
21 #include "ListView.h"
22 #include "shopper.h" // automake, i8n, gettext
23 #include "CategoryView.h"
24 #include "ItemDialog.h"
25 #include "LabelEntry.h"
26 #include <QHash>
27 #include <QFrame>
28 #include <QVBoxLayout>
29 #include <QSizePolicy>
30 #include <QSettings>
31 #include <QScrollBar>
32 #include <QApplication>
33 #include <iostream>
34 #include "GestureWatcher.h"
36 using namespace std;
38 namespace Shopper
40 // This class displays and manages a list.
42 // It creates ItemViews dynamically
43 // When the mode changes it updates the display
44 // When the active category changes, it updates the display
46 struct ListView::Private {
47 QHash<Category*, CategoryView*> catMap;
48 Shopper::List *mylist;
50 QFrame *contents; // This widget has a layout and contains all the items
51 QVBoxLayout *lvBox; // and is scrolled by this widget
52 int zoom;
56 // class ListView: public QScrollArea
57 ListView::ListView(List &l, QWidget *parent) :
58 QFingerScrollArea(parent)
60 p = new Private;
61 p->mylist =&l;
62 p->contents = 0;
63 p->lvBox = 0;
64 QSettings settings;
65 p->zoom = settings.value("ui/ZoomLevel", 4).toInt();
66 viewport()->setAutoFillBackground(false);
68 // setBackgroundRole(QPalette::Light);
69 setWidgetResizable(true);
70 QSizePolicy sizePolicy(QSizePolicy::Ignored, QSizePolicy::Preferred);
71 sizePolicy.setHorizontalStretch(1);
72 setSizePolicy(sizePolicy);
73 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
74 setup();
75 updateVisibility();
77 // Handle changes to the data structure
78 connect(&l, SIGNAL(category_added(Category*)),
79 this, SLOT(add_cat(Category*)));
80 connect(&l, SIGNAL(category_removed(Category*)),
81 this, SLOT(del_cat(Category*)));
82 connect(&l, SIGNAL(category_list_reordered()),
83 this, SLOT(list_reordered()));
85 // Now connect the List's changed signals
86 // When the mode changes
87 connect(&l, SIGNAL(state_changed()),
88 this, SLOT(list_changed()));
90 // When the active category changes
91 connect(&l, SIGNAL(active_category_changed()),
92 this, SLOT(active_cat_changed()));
94 // Issued on name change
95 connect(&l, SIGNAL(changed()),
96 this, SLOT(list_changed()));
99 ListView::~ListView()
101 delete p;
104 void ListView::filter_by(Category &c) // Only view certain categories
106 p->mylist->make_category_active(c);
109 void ListView::list_reordered()
111 setup();
113 void ListView::active_cat_changed()
115 updateVisibility();
116 QScrollBar *vs = verticalScrollBar();
117 if (vs) vs->setValue(vs->minimum());
119 void ListView::list_changed()
121 _ENTER;
122 updateVisibility();
125 // FIX: This should specify position
126 void ListView::add_cat(Shopper::Category *c)
128 // CategoryView *cw;
129 // cw = new CategoryView(*c, *(p->mylist), p->contents);
130 // p->lvBox->addWidget(cw, 1);
131 // p->catMap[c] = cw;
132 Q_UNUSED(c)
133 setup(); // FIX: change Shopper::List to be based on QList and do this properly
136 void ListView::del_cat(Shopper::Category *c)
138 QWidget *w = p->catMap[c];
139 p->lvBox->removeWidget(w);
140 p->catMap.remove(c);
141 delete w;
142 updateVisibility(); // FIX: change Shopper::List to be based on QList and do this properly
145 // Creates the structure of view widgets
146 void ListView::setup()
148 QVBoxLayout* oldBox = p->lvBox;
150 p->contents = new QFrame; // don't worry, the old value is destroyed by setWidget() in a bit...
151 p->lvBox = new QVBoxLayout;
152 p->contents->setLayout(p->lvBox);
153 p->contents->setLineWidth(3);
155 // Scans all the categories in the Shopper::List and creates views
156 for(Shopper::List::pCategoryIter cI = p->mylist->categoriesI();
157 cI != p->mylist->categoriesEnd(); ++cI)
159 Shopper::Category *c = *cI;
161 CategoryView *cw;
162 if (p->catMap.contains(c)) {
163 cw = p->catMap[c];
164 cw->setParent(p->contents);
165 cw->show();
166 DEBUG("Existing CategoryView for " << c->get_name());
167 } else {
168 cw = new CategoryView(*c, *(p->mylist), p->contents);
169 p->catMap[c] = cw;
170 cw->setZoom(p->zoom);
171 DEBUG("New CategoryView for " << c->get_name());
173 if (oldBox) oldBox->removeWidget(cw);
174 p->lvBox->addWidget(cw, 1);
178 p->lvBox->addStretch(1);
179 QSizePolicy sizePolicy(QSizePolicy::Ignored, QSizePolicy::Preferred);
180 sizePolicy.setHorizontalStretch(1);
181 p->contents->setSizePolicy(sizePolicy);
182 setWidget(p->contents); // deletes the old widget and anything left in it...
183 qApp->sendPostedEvents();
184 qApp->processEvents();
185 qApp->processEvents();
188 // iterate through all widgets and hide/show them
189 void ListView::updateVisibility()
191 _ENTER;
192 CategoryView *cw;
193 p->contents->hide();
194 for(Shopper::List::pCategoryIter cI = p->mylist->categoriesI();
195 cI != p->mylist->categoriesEnd(); ++cI)
197 Shopper::Category *c = *cI;
198 cw = p->catMap[c];
199 bool isActive = p->mylist->is_category_active(*c);
200 if (isActive) {
201 cw->updateVisibility();
202 cw->show();
203 } else {
204 cw->hide();
207 p->contents->show();
210 #define MAXZOOM 6
211 void ListView::setZoom(int z)
213 if (z < 0) return;
214 if (z > MAXZOOM) return;
215 p->zoom=z;
216 QSettings settings;
217 settings.setValue("ui/ZoomLevel", z);
218 int max1 = verticalScrollBar()->maximum();
219 int s = verticalScrollBar()->value();
220 DEBUG("Scrolled to " << s << "/" <<max1);
221 for(Shopper::List::pCategoryIter cI = p->mylist->categoriesI();
222 cI != p->mylist->categoriesEnd(); ++cI)
224 p->catMap[*cI]->setZoom(p->zoom);
226 qApp->sendPostedEvents();
227 qApp->processEvents();
228 qApp->processEvents();
229 int max2 = verticalScrollBar()->maximum();
230 // DEBUG("given " << max2 << " going to " << s*max2/max1);
231 if (max1) verticalScrollBar()->setValue(s*max2/max1);
233 void ListView::zoomOut() { setZoom(p->zoom-1); }
234 void ListView::zoomIn() { setZoom(p->zoom+1); }