use mp3s instead of wav which seems to work...
[buliscores] / src / src / matchdaymodel.cpp
1 #include <QDebug>
2 #include <QBrush>
3 #include <QColor>
4 #include <QFontMetrics>
5 #include <QFont>
6 #include <QIcon>
7 #include <QSettings>
8 #include <QApplication>
9
10 #include "matchdaymodel.h"
11 #include "match.h"
12
13 MatchDayModel::MatchDayModel(QObject *parent, MatchDayBackend *backend) :
14     QAbstractTableModel(parent),
15     m_lastRowCount(0),
16     m_settings(qApp->organizationName(), qApp->applicationName())
17 {
18     m_backend = backend;
19
20     connect(m_backend, SIGNAL(updateFinished(int)),
21             this, SLOT(onUpdateFinished(int)));
22 }
23
24 int MatchDayModel::rowCount(const QModelIndex&) const
25 {
26     int count = m_backend->matchList().count();
27
28     return count;
29 }
30
31 int MatchDayModel::columnCount(const QModelIndex&) const
32 {
33     return 11;
34 }
35
36 QVariant MatchDayModel::data(const QModelIndex& index, int role) const
37 {
38     Match*       match;
39     QFont        f;
40     QSize        s;
41     QIcon        i;
42
43     if ((match = m_backend->matchList().at(index.row())) == NULL) {
44         return QVariant(QVariant::Invalid);
45     }
46
47     // DisplayRole
48     switch (role) {
49     case Qt::BackgroundRole:
50         return QBrush(QColor(20, 20, 20, 100));
51         break;
52
53     case Qt::DecorationRole:
54         switch (index.column()) {
55         case AwayIcon:
56             i = match->awayEmblem().pixmap(25,25);
57             break;
58         case HomeIcon:
59             i = match->homeEmblem().pixmap(25,25);
60             break;
61         case MatchState:
62             switch(match->state()) {
63             case Match::NotStarted:
64                 return QIcon(":/bullet-grey").pixmap(20,20);
65                 break;
66             case Match::FirstHalf:
67             case Match::SecondHalf:
68                 return QIcon(":/football").pixmap(20,20);
69                 break;
70             case Match::HalfTime:
71                 return QIcon(":/bullet-yellow").pixmap(20,20);
72                 break;
73             case Match::Finished:
74                 return QIcon(":/bullet-red").pixmap(20,20);
75                 break;
76             default:
77                 return QVariant(QVariant::Invalid);
78             }
79
80             break;
81         }
82         return i;
83         break;
84
85     case Qt::DisplayRole:
86         switch (index.column()) {
87         case AwayTeam:
88             return match->awayTeam();
89             break;
90         case AwayScore:
91             if (match->state() == Match::NotStarted) {
92                 return "-";
93             } else {
94                 return match->awayScore();
95             }
96             break;
97         case HomeTeam:
98             return match->homeTeam();
99             break;
100         case HomeScore:
101             if (match->state() == Match::NotStarted) {
102                 return "-";
103             } else {
104                 return match->homeScore();
105             }
106             break;
107         case Seperator:
108             return ":";
109             break;
110         case Date:
111             return match->date().toString("ddd hh:mm");
112             break;
113
114         default:
115             return QVariant(QVariant::Invalid);
116             break;
117         }
118         break;
119
120     case Qt::SizeHintRole:
121         s.setHeight(25);
122         switch (index.column()) {
123         case Spacer:
124         case Spacer2:
125             s.setWidth(2);
126             break;            
127         case MatchState:
128             s.setWidth(15);
129             break;
130         case AwayIcon:
131             s.setWidth(29);
132             break;
133         case AwayTeam:
134             s.setWidth(120);
135             break;
136         case AwayScore:
137             s.setWidth(25);
138             break;
139         case HomeIcon:
140             s.setWidth(29);
141             break;
142         case HomeTeam:
143             s.setWidth(120);
144             break;
145         case HomeScore:
146             s.setWidth(25);
147             break;
148         case Seperator:
149             s.setWidth(5);
150             break;
151         case Date:
152             s.setWidth(75);
153             break;
154         default:
155             return QVariant(QVariant::Invalid);
156             break;
157         }
158         return s;
159         break;
160
161     case Qt::TextAlignmentRole:
162         if (index.column() < Seperator) {
163             return 0x0002 | 0x0080;
164         } else if (index.column() > Seperator) {
165             return 0x0001 | 0x0080;
166         } else {
167             return Qt::AlignCenter;
168         }
169         break;
170
171     case Qt::ForegroundRole:
172         if ((index.column() == HomeScore &&
173              match->lastHomeGoal().secsTo((QDateTime::currentDateTime())) < 300) ||
174             (index.column() == AwayScore &&
175              match->lastAwayGoal().secsTo((QDateTime::currentDateTime())) < 300)) {
176             return QColor(255, 0, 51);
177         }
178         break;
179
180     case Qt::FontRole:
181         if ((index.column() == HomeScore ||
182              index.column() == AwayScore)) {
183             f.setBold(true);
184             f.setPixelSize(20);
185         } else {
186             f.setBold(false);
187             f.setPixelSize(14);
188         }
189
190         return f;
191
192     default:
193         return QVariant(QVariant::Invalid);
194     }
195
196     return QVariant(QVariant::Invalid);
197 }
198
199
200 void MatchDayModel::onUpdateFinished(int)
201 {
202     //remove all rows
203     beginRemoveRows(QModelIndex(),
204                     0,
205                     m_lastRowCount);
206     endRemoveRows();
207
208     //add rows
209     beginInsertRows(QModelIndex(),
210                     0,
211                     m_backend->matchList().count() - 1);
212     endInsertRows();
213
214     m_lastRowCount = m_backend->matchList().count() - 1;
215
216     // invalidate complete data
217     qDebug() << "MatchDayModel::emit dataChanged: " << rowCount(QModelIndex());
218     emit dataChanged(index(0, 0),
219                      index(rowCount(QModelIndex()) - 1, columnCount(QModelIndex()) - 1));
220
221 }