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
22 #include "shopper.h" // automake, i8n, gettext
23 #include "CategoryView.h"
24 #include "ItemDialog.h"
25 #include "LabelEntry.h"
28 #include <QVBoxLayout>
29 #include <QSizePolicy>
32 #include <QApplication>
34 #include "GestureWatcher.h"
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
56 // class ListView: public QScrollArea
57 ListView::ListView(List
&l
, QWidget
*parent
) :
58 QFingerScrollArea(parent
)
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
);
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()));
104 void ListView::filter_by(Category
&c
) // Only view certain categories
106 p
->mylist
->make_category_active(c
);
109 void ListView::list_reordered()
113 void ListView::active_cat_changed()
116 QScrollBar
*vs
= verticalScrollBar();
117 if (vs
) vs
->setValue(vs
->minimum());
119 void ListView::list_changed()
125 // FIX: This should specify position
126 void ListView::add_cat(Shopper::Category
*c
)
129 // cw = new CategoryView(*c, *(p->mylist), p->contents);
130 // p->lvBox->addWidget(cw, 1);
131 // p->catMap[c] = cw;
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
);
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
;
162 if (p
->catMap
.contains(c
)) {
164 cw
->setParent(p
->contents
);
166 DEBUG("Existing CategoryView for " << c
->get_name());
168 cw
= new CategoryView(*c
, *(p
->mylist
), p
->contents
);
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()
194 for(Shopper::List::pCategoryIter cI
= p
->mylist
->categoriesI();
195 cI
!= p
->mylist
->categoriesEnd(); ++cI
)
197 Shopper::Category
*c
= *cI
;
199 bool isActive
= p
->mylist
->is_category_active(*c
);
201 cw
->updateVisibility();
211 void ListView::setZoom(int z
)
214 if (z
> MAXZOOM
) return;
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); }