2 * Copyright (C) 2009 Sakari Poussa
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.
9 #include "tree-model.h"
12 ScoreTreeModel::ScoreTreeModel(QList <Score *> &scoreList, QObject *parent) : QAbstractItemModel(parent)
14 rootItem = new TreeItem();
15 setupModelData(scoreList, rootItem);
18 void ScoreTreeModel::setupModelData(QList<Score *> &scoreList, TreeItem *parent)
20 QListIterator<Score *> iScore(scoreList);
21 QMultiMap<QString, Score *> yearMap;
23 QSet<QString> yearSet;
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);
34 // Create uniq list of years
35 yearSet = QSet<QString>::fromList(yearList);
36 QList<QString> years = yearMap.keys();
37 QSetIterator<QString> iYear(yearSet);
39 // For each year, add scores as childs
40 while (iYear.hasNext()) {
41 QString year = iYear.next();
43 qDebug() << "---" << year;
45 TreeItem *newItem = new TreeItem(year);
46 parent->appendChild(newItem);
48 QList<Score *> scoresPerYear = yearMap.values(year);
49 QListIterator<Score *> iScoresPerYear(scoresPerYear);
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);
60 int ScoreTreeModel::rowCount(const QModelIndex & parent) const
63 if (parent.column() > 0)
66 if (!parent.isValid())
67 parentItem = rootItem;
69 parentItem = static_cast<TreeItem*>(parent.internalPointer());
71 return parentItem->childCount();
74 int ScoreTreeModel::columnCount(const QModelIndex & parent) const
77 return static_cast<TreeItem*>(parent.internalPointer())->columnCount();
79 return rootItem->columnCount();
82 TreeItem *ScoreTreeModel::itemFromIndex(const QModelIndex &index) const
85 return static_cast<TreeItem*>(index.internalPointer());
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
96 if (!hasIndex(row, column, parent))
99 TreeItem *parentItem = itemFromIndex(parent);
100 TreeItem *childItem = parentItem->children.value(row);
103 index = createIndex(row, column, childItem);
105 index = QModelIndex();
107 //qDebug() << "index: " << row << ":" << column << "parent" << parent << "index" << index;
111 QModelIndex ScoreTreeModel::parent(const QModelIndex & index) const
113 if (!index.isValid())
114 return QModelIndex();
116 TreeItem *item = itemFromIndex(index);
118 return QModelIndex();
120 TreeItem *parent = item->parent;
122 return QModelIndex();
124 TreeItem *grandParent = parent->parent;
126 return QModelIndex();
128 int row = grandParent->children.indexOf(parent);
129 return createIndex(row, 0, parent);
132 QVariant ScoreTreeModel::data(const QModelIndex &index, int role) const
134 if (!index.isValid())
137 if (role != Qt::DisplayRole && role < Qt::UserRole)
140 TreeItem *item = itemFromIndex(index);
145 case Qt::DisplayRole:
148 // User defined roles
154 return item->score->getTotal(Total);
159 return item->score->getTotal(TotalOut);
164 return item->score->getTotal(TotalIn);
168 qDebug() << "data: unknow role: " << role;
173 QVariant ScoreTreeModel::headerData(int section, Qt::Orientation orientation, int role) const
176 if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
177 return rootItem->data(section);