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
21 //#define DEBUG_SHOPPER 1
22 #include "CatListModel.h"
23 #include "shopper.h" // automake, i8n, gettext
26 { // class CatListModel : public QAbstractTableModel
27 CatListModel::CatListModel(Shopper::List
&l
, bool show_everything1
) :
29 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
== 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
++)
77 ////////////////////////////////////////////////////////////////
79 int CatListModel::rowCount(const QModelIndex
& parent
) const
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
90 QVariant
CatListModel::headerData ( int section
, Qt::Orientation orientation
, int role
) const
94 DEBUG("Role " << role
);
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
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);
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
) {
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();
131 } while (--i
and ++c
!=end
);
135 QString name
= cat
->get_name();
138 if (role
== Qt::UserRole
) {
139 // DEBUG("Giving pointer to " << cat->get_name());
140 return QVariant::fromValue(cat
);
142 // DEBUG("Giving string " << cat->get_name());
143 return QVariant(cat
->get_name());
149 // if (v.canConvert<Category*>())
150 // c = v.value<Category*>();