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