- better size calculation
[buliscores] / src / matchdaymodel.cpp
1 #include <QDebug>
2 #include <QColor>
3 #include <QFontMetrics>
4 #include <QFont>
5 #include <QIcon>
6 #include <QSettings>
7
8 #include "matchdaymodel.h"
9 #include "match.h"
10
11 MatchDayModel::MatchDayModel(QObject *parent) :
12     QAbstractTableModel(parent),
13     m_lastRowCount(0),
14     m_settings("David Solbach", "BuliScores")
15 {
16
17     m_backend = new BackendKicker(this);
18
19     connect(m_backend, SIGNAL(matchListChanged()),
20             this, SLOT(onMatchListChanged()));
21
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::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 Seperator:
60             i = QIcon(":/Icons/Application/football.png").pixmap(20,20);
61             break;
62         }
63         return i;
64         break;
65
66     case Qt::DisplayRole:
67         switch (index.column()) {
68         case AwayIcon:
69             return match->awayEmblem();
70             break;
71         case AwayTeam:
72             return match->awayTeam();
73             break;
74         case AwayScore:
75             if (match->state() == Match::NotStarted) {
76                 return "-";
77             } else {
78                 return match->awayScore();
79             }
80             break;
81         case HomeIcon:
82             return match->homeEmblem();
83             break;
84         case HomeTeam:
85             return match->homeTeam();
86             break;
87         case HomeScore:
88             if (match->state() == Match::NotStarted) {
89                 return "-";
90             } else {
91                 return match->homeScore();
92             }
93             break;
94         case Date:
95             return match->date().toString("ddd hh mm");
96             break;
97         default:
98             return QVariant(QVariant::Invalid);
99             break;
100         }
101         break;
102
103     case Qt::SizeHintRole:
104         s.setHeight(25);
105         switch (index.column()) {
106         case AwayIcon:
107             s.setWidth(29);
108             break;
109         case AwayTeam:
110             s.setWidth(120);
111             break;
112         case AwayScore:
113             s.setWidth(4);
114             break;
115         case HomeIcon:
116             s.setWidth(29);
117             break;
118         case HomeTeam:
119             s.setWidth(120);
120             break;
121         case HomeScore:
122             s.setWidth(4);
123             break;
124         case Seperator:
125             s.setWidth(3);
126             break;
127         case Date:
128             s.setWidth(75);
129             break;
130         default:
131             return QVariant(QVariant::Invalid);
132             break;
133         }
134         return s;
135         break;
136
137     case Qt::BackgroundRole:
138         return QColor(0, 0, 0, 120);
139         break;
140
141     case Qt::TextAlignmentRole:
142         if (index.column() < 3) {
143             return 0x0002 | 0x0080;
144         } else if (index.column() > 3) {
145             return 0x0001 | 0x0080;
146         } else {
147             return Qt::AlignCenter;
148         }
149         break;
150     case Qt::FontRole:
151
152
153         return f;
154
155     default:
156         return QVariant(QVariant::Invalid);
157     }
158
159     return QVariant(QVariant::Invalid);
160 }
161
162
163 // only adds for now
164 void MatchDayModel::onMatchListChanged(void)
165 {
166     //remove all rows
167     qDebug() << "beginRemoveRows: " << 0 << ", " << rowCount(QModelIndex()) - 1;
168     beginRemoveRows(QModelIndex(),
169                     0,
170                     m_lastRowCount);
171     endRemoveRows();
172
173     //add rows
174     qDebug() << "beginInsertRows: " << 0 << ", " << m_backend->matchList().count() - 1;
175     beginInsertRows(QModelIndex(),
176                     0,
177                     m_backend->matchList().count() - 1);
178     endInsertRows();
179
180     m_lastRowCount = m_backend->matchList().count() - 1;
181
182     // invalidate complete data
183
184     qDebug() << "rowCount @ emit dataChanged: " << rowCount(QModelIndex());
185     emit dataChanged(index(0, 0),
186                      index(rowCount(QModelIndex()) - 1, columnCount(QModelIndex()) - 1));
187
188 }
189
190 void MatchDayModel::update(void)
191 {
192     this->m_backend->setLeague(m_settings.value("League", "1. Bundesliga").toString());
193     this->m_backend->update();
194 }