bin dir cleanup
[scorecard] / src / tree-model.cpp
1 #include "tree-model.h"
2 #include <QDebug>
3
4 ScoreTreeModel::ScoreTreeModel(QList <Score *> &scoreList, QObject *parent) : QAbstractItemModel(parent) 
5 {
6   rootItem = new TreeItem();
7   setupModelData(scoreList, rootItem);
8 }
9
10 void ScoreTreeModel::setupModelData(QList<Score *> &scoreList, TreeItem *parent)
11 {
12   QListIterator<Score *> iScore(scoreList);
13   QMultiMap<QString, Score *> yearMap;
14   QStringList yearList;
15   QSet<QString> yearSet;
16
17   // Create multi map with years as keys, scores as values
18   // Create list of years
19   while (iScore.hasNext()) {
20     Score *score = iScore.next();
21     QString year = score->getDate().split("-").at(0);
22     yearMap.insert(year, score);
23     yearList << year;
24   }
25
26   // Create uniq list of years
27   yearSet = QSet<QString>::fromList(yearList);
28   QList<QString> years = yearMap.keys();
29   QSetIterator<QString> iYear(yearSet);
30
31   // For each year, add scores as childs
32   while (iYear.hasNext()) {
33     QString year = iYear.next();
34
35     qDebug() << "---" << year;
36
37     TreeItem *newItem = new TreeItem(year);
38     parent->appendChild(newItem);
39
40     QList<Score *> scoresPerYear = yearMap.values(year);
41     QListIterator<Score *> iScoresPerYear(scoresPerYear);
42
43     // for each year, add score
44     while (iScoresPerYear.hasNext()) {
45       Score *s = iScoresPerYear.next();
46       TreeItem *nextItem = new TreeItem(s);
47       newItem->appendChild(nextItem);    
48     }
49   }
50 }
51
52 int ScoreTreeModel::rowCount(const QModelIndex & parent) const
53 {    
54   TreeItem *parentItem;
55   if (parent.column() > 0)
56     return 0;
57
58   if (!parent.isValid())
59     parentItem = rootItem;
60   else
61     parentItem = static_cast<TreeItem*>(parent.internalPointer());
62   
63   return parentItem->childCount();
64 }
65  
66 int ScoreTreeModel::columnCount(const QModelIndex & parent) const
67 {
68   if (parent.isValid())
69     return static_cast<TreeItem*>(parent.internalPointer())->columnCount();
70   else
71     return rootItem->columnCount();
72 }
73
74 TreeItem *ScoreTreeModel::itemFromIndex(const QModelIndex &index) const
75 {
76   if (index.isValid())
77     return static_cast<TreeItem*>(index.internalPointer());
78   else
79     return rootItem;
80 }
81
82 //Given a model index for a parent item, this function allows views
83 //and delegates to access children of that item
84 QModelIndex ScoreTreeModel::index(int row, int column, const QModelIndex &parent) const
85 {
86   QModelIndex index;
87
88   if (!hasIndex(row, column, parent))
89     return QModelIndex();
90
91   TreeItem *parentItem = itemFromIndex(parent);
92   TreeItem *childItem = parentItem->children.value(row);
93
94   if (childItem)
95     index = createIndex(row, column, childItem);
96   else
97     index = QModelIndex();
98
99   //qDebug() << "index: " << row << ":" << column << "parent" << parent << "index" << index;
100   return index;
101 }
102
103 QModelIndex ScoreTreeModel::parent(const QModelIndex & index) const
104 {
105   if (!index.isValid())
106     return QModelIndex();
107
108   TreeItem *item = itemFromIndex(index);
109   if (!item)
110     return QModelIndex();
111
112   TreeItem *parent = item->parent;
113   if (!parent)
114     return QModelIndex();
115
116   TreeItem *grandParent = parent->parent;
117   if (!grandParent)
118     return QModelIndex();
119   
120   int row = grandParent->children.indexOf(parent);
121   return createIndex(row, 0, parent);
122 }
123
124 QVariant ScoreTreeModel::data(const QModelIndex &index, int role) const
125 {
126   if (!index.isValid())
127     return QVariant();
128
129   if (role != Qt::DisplayRole && role < Qt::UserRole)
130     return QVariant();
131
132   TreeItem *item = itemFromIndex(index);
133   if (!item)
134     return QVariant();
135
136   switch(role) {
137   case Qt::DisplayRole:
138     return item->str;
139
140     // User defined roles
141   case Type:
142     return item->type;
143
144   case Total:
145     if (item->score)
146       return item->score->getTotal(Total);
147     break;
148
149   case TotalOut:
150     if (item->score)
151       return item->score->getTotal(TotalOut);
152     break;
153
154   case TotalIn:
155     if (item->score)
156       return item->score->getTotal(TotalIn);
157     break;
158
159   default:
160     qDebug() << "data: unknow role: " << role;
161   }
162   return QVariant();
163 }
164
165 QVariant ScoreTreeModel::headerData(int section, Qt::Orientation orientation, int role) const
166 {
167
168     if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
169         return rootItem->data(section);
170
171     return QVariant();
172
173 }
174