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
21 #define DEBUG_SHOPPER 1
22 #include "shopper.h" // automake, i8n, gettext
23 #include "CatListModel.h"
26 { // class CatListModel : public QAbstractTableModel
27 CatListModel::CatListModel(Shopper::List
&l
, bool show_everything1
) :
28 show_everything(show_everything1
),
32 connect(mylist
, SIGNAL(category_list_changed()),
33 this, SLOT(listChanged()));
34 connect(mylist
, SIGNAL(active_category_changed()),
35 this, SLOT(activeCategoryChanged()));
38 ////////////////////////////////////////////////////////////////
40 int CatListModel::currentIndex(){
41 return indexOfCurrentCat();
43 ////////////////////////////////////////////////////////////////
45 void CatListModel::listChanged ()
47 emit
layoutAboutToBeChanged();
50 void CatListModel::activeCategoryChanged()
52 int i
= indexOfCurrentCat();
53 // DEBUG("emit currentChanged(" <<i <<")");
54 emit
currentChanged(i
);
57 ////////////////////////////////////////////////////////////////
59 int CatListModel::indexOfCurrentCat()
61 Shopper::Category
*ac
= mylist
->get_active_category();
62 if (ac
== NULL
) return 0;
64 int i
= (show_everything
?1:0);
65 Shopper::List::pCategoryIter c
;
66 Shopper::List::pCategoryIter c_end
;
67 for (c
= mylist
->categoriesI(); *c
!=ac
and c
!=c_end
; i
++, c
++)
73 ////////////////////////////////////////////////////////////////
75 int CatListModel::rowCount(const QModelIndex
& parent
) const
77 // DEBUG("rowCount is " << mylist->categories.size()+(show_everything?1:0));
78 return mylist
->categories
.size()+(show_everything
?1:0);
80 int CatListModel::columnCount(const QModelIndex
& parent
) const
84 QVariant
CatListModel::headerData ( int section
, Qt::Orientation orientation
, int role
) const
86 // DEBUG("Role " << role);
88 case 0: return new QVariant("Ptr");
89 case 1: return new QVariant("Data");
90 default: return new QVariant();
93 QVariant
CatListModel::data ( const QModelIndex
& index
, int role
) const
96 if (role
!= Qt::UserRole
and
97 role
!= Qt::DisplayRole
and
98 role
!= Qt::DecorationRole
99 ) { // FIX: This should be done properly
100 // DEBUG("Unsupported Role " << role);
103 int col
= index
.column();
104 int i
= index
.row() + (show_everything
?0:1); // This works.
105 // DEBUG("Asked for (translated) " << i <<":"<<col << " Role:" << role << (show_everything?" show:yes":" show:no"));
106 if (i
== 0) { // 0 = Everything
107 if (role
== Qt::UserRole
) {
110 // DEBUG("Giving string Everything");
111 return QVariant(tr("Everything"));
114 Shopper::Category
*cat
;
115 Shopper::List::pCategoryIter c
;
116 Shopper::List::pCategoryIter end
=mylist
->categoriesEnd();
117 // Ugh... ??? unless we reimpliment Shopper::List
119 c
= mylist
->categoriesI();
123 } while (--i
and ++c
!=end
);
127 QString name
= cat
->get_name();
130 if (role
== Qt::UserRole
) {
131 // DEBUG("Giving pointer to " << cat->get_name());
132 return QVariant::fromValue(cat
);
134 // DEBUG("Giving string " << cat->get_name());
135 return QVariant(cat
->get_name());
141 // if (v.canConvert<Category*>())
142 // c = v.value<Category*>();