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