-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrankingtablemodel.cpp
171 lines (126 loc) · 3.41 KB
/
rankingtablemodel.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#include "rankingtablemodel.h"
#include <Qtimer>
#include <QPixmap>
#include <QImage>
rankingTableModel::rankingTableModel(QObject *parent)
{
db = &(Connect4DAO::getInstance());
Connect4& DB_Instance = Connect4::getInstance();
Player* p1 = DB_Instance.getPlayer(Labels::player_1);
users=db->getRanking();
for(int i=0; i<users.size(); i++)
{
beginInsertRows(QModelIndex(), rowCount(), rowCount());
endInsertRows();
}
}
void rankingTableModel::update_users()
{
users=db->getRanking();
for(int i=0;i<users.size(); i++)
{
if(users.at(i)->getNickName() == "CPU")
{
users.removeAt(i);
break;
}
}
QString text = searchLine->text();
beginRemoveRows(QModelIndex(), 0, rowCount() - 1);
endRemoveRows();
for(int i=0; i<users.size(); i++)
{
beginInsertRows(QModelIndex(), rowCount(), rowCount());
endInsertRows();
}
for(int i=0; i<users.size(); i++)
{
QString name = users.at(i)->getNickName();
if(!name.contains(text))
{
beginRemoveRows(QModelIndex(), i, i);
users.removeAt(i);
endRemoveRows();
i--;
}
}
}
int rankingTableModel::rowCount(const QModelIndex &)const
{
return static_cast<int>(users.size());
}
int rankingTableModel::columnCount(const QModelIndex &)const
{
return COLCOUNT;
}
QVariant rankingTableModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant();
if (index.row() < 0 || index.row() >= static_cast<int>(users.size()))
return QVariant();
Player* player = users.at(index.row());
if (role == Qt::DisplayRole) {
switch (index.column()) {
case NAME:
return player->getNickName();
case POINTS:
return QString::number(player->getPoints());
default:
return QVariant();
}
}
if(role==Qt::DecorationRole)
{
if(index.column() == IMG)
{
QImage foto = player->getAvatar();
QPixmap pixmap = QPixmap::fromImage(foto);
return pixmap.scaled(32, 32, Qt::KeepAspectRatio, Qt::SmoothTransformation);
}
}
return QVariant();
}
QVariant rankingTableModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if (role != Qt::DisplayRole)
return QVariant();
if (orientation == Qt::Horizontal) {
switch (section) {
case IMG:
return QString("");
case NAME:
return QString("ID");
case POINTS:
return QString(Labels::points);
default:
return QVariant();
}
}
return QVariant();
}
Qt::ItemFlags rankingTableModel::flags(const QModelIndex &index) const
{
if (!index.isValid())
return Qt::NoItemFlags;
Qt::ItemFlags flags = Qt::ItemIsSelectable | Qt::ItemIsEnabled;
return flags;
}
void rankingTableModel::addUser(const Player* user)
{
beginInsertRows(QModelIndex(), rowCount(), rowCount());
db->addPlayer(*user);
endInsertRows();
}
void rankingTableModel::set_lineEdit(QLineEdit* searchLine)
{
this->searchLine = searchLine;
}
Player* rankingTableModel::getPlayer(int row)
{
return users.at(row);
}
QList<Round*> rankingTableModel::getPlayerRounds(QString NickName)
{
return db->getRoundsForPlayer(NickName);
}