901a5d298a7b94ebe2d7e74f572d0d3c00b3465b
[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 8;
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     f.setPixelSize(14);
44
45     if ((match = m_backend->matchList().at(index.row())) == NULL) {
46         return QVariant(QVariant::Invalid);
47     }
48
49     // DisplayRole
50     switch (role) {
51     case Qt::BackgroundRole:
52         return QBrush(QColor(0, 0, 0, 100));
53         break;
54     case Qt::DecorationRole:
55         switch (index.column()) {
56         case AwayIcon:
57             i = match->awayEmblem().pixmap(25,25);
58             break;
59         case HomeIcon:
60             i = match->homeEmblem().pixmap(25,25);
61             break;
62         case Seperator:
63             i = QIcon(":/Icons/Application/football.png").pixmap(20,20);
64             break;
65         }
66         return i;
67         break;
68
69     case Qt::DisplayRole:
70         switch (index.column()) {
71         case AwayIcon:
72             return match->awayEmblem();
73             break;
74         case AwayTeam:
75             return match->awayTeam();
76             break;
77         case AwayScore:
78             if (match->state() == Match::NotStarted) {
79                 return "-";
80             } else {
81                 return match->awayScore();
82             }
83             break;
84         case HomeIcon:
85             return match->homeEmblem();
86             break;
87         case HomeTeam:
88             return match->homeTeam();
89             break;
90         case HomeScore:
91             if (match->state() == Match::NotStarted) {
92                 return "-";
93             } else {
94                 return match->homeScore();
95             }
96             break;
97         case Date:
98             return match->date().toString("ddd hh mm");
99             break;
100         default:
101             return QVariant(QVariant::Invalid);
102             break;
103         }
104         break;
105
106     case Qt::SizeHintRole:
107         s.setHeight(25);
108         switch (index.column()) {
109         case AwayIcon:
110             s.setWidth(29);
111             break;
112         case AwayTeam:
113             s.setWidth(120);
114             break;
115         case AwayScore:
116             s.setWidth(4);
117             break;
118         case HomeIcon:
119             s.setWidth(29);
120             break;
121         case HomeTeam:
122             s.setWidth(120);
123             break;
124         case HomeScore:
125             s.setWidth(4);
126             break;
127         case Seperator:
128             s.setWidth(3);
129             break;
130         case Date:
131             s.setWidth(75);
132             break;
133         default:
134             return QVariant(QVariant::Invalid);
135             break;
136         }
137         return s;
138         break;
139
140     case Qt::TextAlignmentRole:
141         if (index.column() < 3) {
142             return 0x0002 | 0x0080;
143         } else if (index.column() > 3) {
144             return 0x0001 | 0x0080;
145         } else {
146             return Qt::AlignCenter;
147         }
148         break;
149     case Qt::FontRole:
150
151
152         return f;
153
154     default:
155         return QVariant(QVariant::Invalid);
156     }
157
158     return QVariant(QVariant::Invalid);
159 }
160
161
162 // only adds for now
163 void MatchDayModel::onMatchListChanged(void)
164 {
165     //remove all rows
166     qDebug() << "beginRemoveRows: " << 0 << ", " << rowCount(QModelIndex()) - 1;
167     beginRemoveRows(QModelIndex(),
168                     0,
169                     m_lastRowCount);
170     endRemoveRows();
171
172     //add rows
173     qDebug() << "beginInsertRows: " << 0 << ", " << m_backend->matchList().count() - 1;
174     beginInsertRows(QModelIndex(),
175                     0,
176                     m_backend->matchList().count() - 1);
177     endInsertRows();
178
179     m_lastRowCount = m_backend->matchList().count() - 1;
180
181     // invalidate complete data
182     qDebug() << "rowCount @ emit dataChanged: " << rowCount(QModelIndex());
183     emit dataChanged(index(0, 0),
184                      index(rowCount(QModelIndex()) - 1, columnCount(QModelIndex()) - 1));
185
186 }