- tested sound ouput via phonon, added two sounds (still unused)
[buliscores] / 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(matchListChanged()),
21             this, SLOT(onMatchListChanged()));
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 10;
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     case Qt::DecorationRole:
53         switch (index.column()) {
54         case AwayIcon:
55             i = match->awayEmblem().pixmap(25,25);
56             break;
57         case HomeIcon:
58             i = match->homeEmblem().pixmap(25,25);
59             break;
60         case MatchState:
61             switch(match->state()) {
62             case Match::NotStarted:
63                 return QIcon(":/bullet-grey").pixmap(15,15);
64                 break;
65             case Match::FirstHalf:
66             case Match::SecondHalf:
67                 return QIcon(":/bullet-green").pixmap(15,15);
68                 break;
69             case Match::HalfTime:
70                 return QIcon(":/bullet-yellow").pixmap(15,15);
71                 break;
72             case Match::Finished:
73                 return QIcon(":/bullet-red").pixmap(15,15);
74                 break;
75             default:
76                 return QVariant(QVariant::Invalid);
77             }
78
79             break;
80         }
81         return i;
82         break;
83
84     case Qt::DisplayRole:
85         switch (index.column()) {
86         case AwayTeam:
87             return match->awayTeam();
88             break;
89         case AwayScore:
90             if (match->state() == Match::NotStarted) {
91                 return "-";
92             } else {
93                 return match->awayScore();
94             }
95             break;
96         case HomeTeam:
97             return match->homeTeam();
98             break;
99         case HomeScore:
100             if (match->state() == Match::NotStarted) {
101                 return "-";
102             } else {
103                 return match->homeScore();
104             }
105             break;
106         case Seperator:
107             return ":";
108             break;
109         case Date:
110             return match->date().toString("ddd hh:mm");
111             break;
112
113         default:
114             return QVariant(QVariant::Invalid);
115             break;
116         }
117         break;
118
119     case Qt::SizeHintRole:
120         s.setHeight(25);
121         switch (index.column()) {
122         case Spacer:
123             s.setWidth(3);
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(4);
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(4);
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::FontRole:
170         f.setPixelSize(14);
171
172         return f;
173
174     default:
175         return QVariant(QVariant::Invalid);
176     }
177
178     return QVariant(QVariant::Invalid);
179 }
180
181
182 // only adds for now
183 void MatchDayModel::onMatchListChanged(void)
184 {
185     //remove all rows
186     //qDebug() << "beginRemoveRows: " << 0 << ", " << rowCount(QModelIndex()) - 1;
187     beginRemoveRows(QModelIndex(),
188                     0,
189                     m_lastRowCount);
190     endRemoveRows();
191
192     //add rows
193     //qDebug() << "beginInsertRows: " << 0 << ", " << m_backend->matchList().count() - 1;
194     beginInsertRows(QModelIndex(),
195                     0,
196                     m_backend->matchList().count() - 1);
197     endInsertRows();
198
199     m_lastRowCount = m_backend->matchList().count() - 1;
200
201     // invalidate complete data
202     //qDebug() << "rowCount @ emit dataChanged: " << rowCount(QModelIndex());
203     emit dataChanged(index(0, 0),
204                      index(rowCount(QModelIndex()) - 1, columnCount(QModelIndex()) - 1));
205
206 }