- added settingsdialog
[buliscores] / src / matchdaymodel.cpp
1 #include <QColor>
2 #include <QFontMetrics>
3 #include <QFont>
4 #include <QIcon>
5 #include <QSettings>
6
7 #include "matchdaymodel.h"
8 #include "match.h"
9
10 MatchDayModel::MatchDayModel(QObject *parent) :
11     QAbstractTableModel(parent)
12 {
13     m_backend = new BackendKicker(this);
14
15     connect(m_backend, SIGNAL(matchListChanged()),
16             this, SLOT(onMatchListChanged()));
17
18 }
19
20 int MatchDayModel::rowCount(const QModelIndex&) const
21 {
22     int count = m_backend->matchList().count();
23
24     return count;
25 }
26
27 int MatchDayModel::columnCount(const QModelIndex&) const
28 {
29     return 8;
30 }
31
32 QVariant MatchDayModel::data(const QModelIndex& index, int role) const
33 {
34     Match*       match;
35     QFont        f;
36     QSize        s;
37     QIcon        i;
38
39     f.setPixelSize(14);
40
41     if ((match = m_backend->matchList().at(index.row())) == NULL) {
42         return QVariant(QVariant::Invalid);
43     }
44
45     // DisplayRole
46     switch (role) {
47     case Qt::DecorationRole:
48         switch (index.column()) {
49         case AwayIcon:
50             i = match->awayEmblem().pixmap(25,25);
51             break;
52         case HomeIcon:
53             i = match->homeEmblem().pixmap(25,25);
54             break;
55         case Seperator:
56             i = QIcon(":/Icons/Application/football.png").pixmap(20,20);
57             break;
58         }
59         return i;
60         break;
61
62     case Qt::DisplayRole:
63         switch (index.column()) {
64         case AwayIcon:
65             return match->awayEmblem();
66             break;
67         case AwayTeam:
68             return match->awayTeam();
69             break;
70         case AwayScore:
71             if (match->state() == Match::NotStarted) {
72                 return "-";
73             } else {
74                 return match->awayScore();
75             }
76             break;
77         case HomeIcon:
78             return match->homeEmblem();
79             break;
80         case HomeTeam:
81             return match->homeTeam();
82             break;
83         case HomeScore:
84             if (match->state() == Match::NotStarted) {
85                 return "-";
86             } else {
87                 return match->homeScore();
88             }
89             break;
90         case Date:
91             return match->date().toString("ddd hh mm");
92             break;
93         default:
94             return QVariant(QVariant::Invalid);
95             break;
96         }
97         break;
98
99     case Qt::SizeHintRole:
100         s.setHeight(25);
101         switch (index.column()) {
102         case AwayIcon:
103             s.setWidth(29);
104             break;
105         case AwayTeam:
106             s.setWidth(120);
107             break;
108         case AwayScore:
109             s.setWidth(4);
110             break;
111         case HomeIcon:
112             s.setWidth(29);
113             break;
114         case HomeTeam:
115             s.setWidth(120);
116             break;
117         case HomeScore:
118             s.setWidth(4);
119             break;
120         case Seperator:
121             s.setWidth(3);
122             break;
123         case Date:
124             s.setWidth(110);
125             break;
126         default:
127             return QVariant(QVariant::Invalid);
128             break;
129         }
130         return s;
131         break;
132
133     case Qt::BackgroundRole:
134         return QColor(0, 0, 0, 120);
135         break;
136
137     case Qt::TextAlignmentRole:
138         if (index.column() < 3) {
139             return 0x0002 | 0x0080;
140         } else if (index.column() > 3) {
141             return 0x0001 | 0x0080;
142         } else {
143             return Qt::AlignCenter;
144         }
145         break;
146     case Qt::FontRole:
147
148
149         return f;
150
151     default:
152         return QVariant(QVariant::Invalid);
153     }
154
155     return QVariant(QVariant::Invalid);
156 }
157
158
159 // only adds for now
160 void MatchDayModel::onMatchListChanged(void)
161 {
162     beginInsertRows(QModelIndex(),
163                     rowCount(QModelIndex()),
164                     rowCount(QModelIndex()));
165     endInsertRows();
166
167     // invalidate complete data
168     emit dataChanged(index(0, 0),
169                      index(rowCount(QModelIndex()) - 1, columnCount(QModelIndex()) - 1));
170
171 }
172
173 void MatchDayModel::update(void)
174 {
175     QSettings settings("David Solbach", "BuliScores");
176
177     this->m_backend->setLeague(settings.value("League", "1. Bundesliga").toString());
178     this->m_backend->update();
179 }