Item dialog shows correct category in dropdown
[shopper.git] / src / CatListModel.cc
blob7f1a64ee1949745bd08671281d975d5bf28c3d7c
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
21 //#define DEBUG_SHOPPER 1
22 #include "CatListModel.h"
23 #include "shopper.h" // automake, i8n, gettext
25 namespace Shopper
26 { // class CatListModel : public QAbstractTableModel
27 CatListModel::CatListModel(Shopper::List &l, bool show_everything1) :
28 mylist(&l),
29 show_everything(show_everything1)
31 _ENTER;
32 connect(mylist, SIGNAL(category_list_changed()),
33 this, SLOT(listChanged()));
34 connect(mylist, SIGNAL(active_category_changed()),
35 this, SLOT(activeCategoryChanged()));
38 ////////////////////////////////////////////////////////////////
39 // Interface Methods
40 int CatListModel::currentIndex(){
41 return indexOfCurrentCat();
43 ////////////////////////////////////////////////////////////////
44 // Interface Slots
45 void CatListModel::listChanged ()
47 emit layoutAboutToBeChanged();
48 emit layoutChanged();
50 void CatListModel::activeCategoryChanged()
52 int i= indexOfCurrentCat();
53 DEBUG("emit currentChanged(" <<i <<")");
54 emit currentChanged(i);
57 ////////////////////////////////////////////////////////////////
58 // Utility
59 int CatListModel::indexOfCurrentCat()
61 Shopper::Category *ac = mylist->get_active_category();
62 if (ac == 0) return 0;
63 return indexOfCat(ac);
66 int CatListModel::indexOfCat(Shopper::Category *cat)
68 int i = (show_everything?1:0);
69 Shopper::List::pCategoryIter c;
70 Shopper::List::pCategoryIter c_end;
71 for (c = mylist->categoriesI(); *c!=cat and c!=c_end; i++, c++)
73 Q_ASSERT(c!=c_end);
74 return i;
77 ////////////////////////////////////////////////////////////////
78 // Override virtuals
79 int CatListModel::rowCount(const QModelIndex & parent) const
81 Q_UNUSED(parent)
82 DEBUG("rowCount is " << mylist->categories.size()+(show_everything?1:0));
83 return mylist->categories.size()+(show_everything?1:0);
85 int CatListModel::columnCount(const QModelIndex & parent) const
87 Q_UNUSED(parent)
88 return 2;
90 QVariant CatListModel::headerData ( int section, Qt::Orientation orientation, int role ) const
92 Q_UNUSED(orientation)
93 Q_UNUSED(role)
94 DEBUG("Role " << role);
95 switch (section) {
96 case 0: return QVariant("Ptr");
97 case 1: return QVariant("Data");
98 default: return QVariant();
101 QVariant CatListModel::data ( const QModelIndex & index, int role ) const
103 // _ENTER;
104 if (role != Qt::UserRole and
105 role != Qt::DisplayRole and
106 role != Qt::DecorationRole
107 ) { // FIX: This should be done properly
108 // DEBUG("Unsupported Role " << role);
109 return QVariant();
111 // int col = index.column();
112 int i = index.row() + (show_everything?0:1); // This works.
113 // DEBUG("Asked for (translated) " << i <<":"<<col << " Role:" << role << (show_everything?" show:yes":" show:no"));
114 if (i == 0) { // 0 = Everything
115 if (role == Qt::UserRole) {
116 return QVariant();
117 } else {
118 // DEBUG("Giving string Everything");
119 return QVariant(tr("Everything"));
122 Shopper::Category *cat;
123 Shopper::List::pCategoryIter c;
124 Shopper::List::pCategoryIter end=mylist->categoriesEnd();
125 // Ugh... ??? unless we reimpliment Shopper::List
127 c = mylist->categoriesI();
128 do {
129 Q_ASSERT(c != end);
130 Q_CHECK_PTR(*c);
131 } while (--i and ++c!=end);
133 cat = *c;
134 Q_CHECK_PTR(cat);
135 QString name = cat->get_name();
137 Q_CHECK_PTR(cat);
138 if (role == Qt::UserRole) {
139 // DEBUG("Giving pointer to " << cat->get_name());
140 return QVariant::fromValue(cat);
141 } else {
142 // DEBUG("Giving string " << cat->get_name());
143 return QVariant(cat->get_name());
147 // Use like this:
148 // Category *c;
149 // if (v.canConvert<Category*>())
150 // c = v.value<Category*>();