Adding new category now shows and selects newly added category
[shopper.git] / src / CatListModel.cc
blob97ed0e0befe5403792eab5d0673200b19c9a2582
1 /* Shopper
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
17 * 02110-1301 USA
21 #define DEBUG_SHOPPER 1
22 #include "shopper.h" // automake, i8n, gettext
23 #include "CatListModel.h"
25 namespace Shopper
26 { // class CatListModel : public QAbstractTableModel
27 CatListModel::CatListModel(Shopper::List &l, bool show_everything1) :
28 show_everything(show_everything1),
29 mylist(&l)
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 == 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++)
69 Q_ASSERT(c!=c_end);
70 return i;
73 ////////////////////////////////////////////////////////////////
74 // Override virtuals
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
82 return 2;
84 QVariant CatListModel::headerData ( int section, Qt::Orientation orientation, int role ) const
86 // DEBUG("Role " << role);
87 switch (section) {
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
95 // _ENTER;
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);
101 return QVariant();
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) {
108 return QVariant();
109 } else {
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();
120 do {
121 Q_ASSERT(c != end);
122 Q_CHECK_PTR(*c);
123 } while (--i and ++c!=end);
125 cat = *c;
126 Q_CHECK_PTR(cat);
127 QString name = cat->get_name();
129 Q_CHECK_PTR(cat);
130 if (role == Qt::UserRole) {
131 // DEBUG("Giving pointer to " << cat->get_name());
132 return QVariant::fromValue(cat);
133 } else {
134 // DEBUG("Giving string " << cat->get_name());
135 return QVariant(cat->get_name());
139 // Use like this:
140 // Category *c;
141 // if (v.canConvert<Category*>())
142 // c = v.value<Category*>();