- Refactor: score and course UI widget management into common files
[scorecard] / src / list-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 <QColor>
10 #include <QBrush>
11 #include <QFont>
12 #include "list-model.h"
13 #include "score-common.h"
14
15 void ScoreListModel::update(QList<Score *> &list)
16 {
17     scoreList = list;
18     reset();
19 }
20
21 int ScoreListModel::rowCount(const QModelIndex &) const
22 {
23     return scoreList.count();
24 }
25
26 QVariant ScoreListModel::data(const QModelIndex &index, int role) const
27 {
28   if (!index.isValid())
29     return QVariant();
30
31   int row = index.row();
32
33   Score * score = scoreList.at(row);
34   //Course * course = findCourse(score->getClubName(), score->getCourseName());
35
36   //
37   // ALIGNMENT
38   //
39 #if 0
40   if (role == Qt::TextAlignmentRole ) {
41     return Qt::AlignCenter;
42   }
43 #endif
44   if (index.column() > 10)
45     return QVariant();
46
47   //
48   // Data
49   //
50   if (role == Qt::DisplayRole) {
51       int n = score->getTotal(Total).toInt();
52       QString s = QString("%1, %2").arg(score->getClubName()).arg(score->getCourseName());
53       QString tabs;
54
55       int len = s.length();
56       if (len >= 40)
57           tabs = "\t";
58       else if (len >= 32)
59           tabs = "\t\t";
60       else if (len >= 24)
61           tabs = "\t\t\t";
62       else if (len >= 16)
63           tabs = "\t\t\t\t";
64       else
65           tabs = "\t\t\t\t\t";
66
67       QString str = QString("%1 %2 %3\n%4").arg(s, -40).arg(tabs).arg(n).arg(score->getDate());
68       return str;
69   }
70   return QVariant();
71 }
72
73 CourseListModel::CourseListModel(QList<Club *> &list, QObject *parent) : QAbstractListModel(parent), clubList(list)
74 {
75     updateList();
76 }
77
78 int CourseListModel::rowCount(const QModelIndex &) const
79 {
80     return clubAndCourseList.count();
81 }
82
83 QVariant CourseListModel::data(const QModelIndex &index, int role) const
84 {
85     if (!index.isValid())
86         return QVariant();
87
88     int row = index.row();
89
90     if (role == Qt::DisplayRole) {
91         QString *s = clubAndCourseList.at(row);
92         return *s;
93     }
94     return QVariant();
95 }
96
97
98 void CourseListModel::updateList()
99 {
100     QListIterator<Club *> i(clubList);
101     
102     while (!clubAndCourseList.isEmpty())
103         delete clubAndCourseList.takeFirst();
104
105     while (i.hasNext()) {
106         Club *club = i.next();
107         QList<Course *> courseList = club->getCourseList();
108
109         QListIterator<Course *> j(courseList);
110         while (j.hasNext()) {
111             Course *course = j.next();
112             QString *str = new QString(club->getName() + ", " + course->getName());
113             clubAndCourseList << str;
114         }
115     }
116 }
117
118 void CourseListModel::update(QList<Club *> &list)
119 {
120     clubList = list;
121     updateList();
122     reset();
123 }