This repository has been archived by the owner on Oct 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayerbase.cpp
277 lines (230 loc) · 9.15 KB
/
playerbase.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#include "playerbase.h"
#include "QDebug"
#include <algorithm>
//initializing static member
playerBase playerBase::s_Instance;
playerBase::~playerBase()
{
//freeing memory allocated for tableWidget items
for(int i = 0; i < playerCount; i++)
delete itemPlayerStorage[i];
}
void playerBase::load()
{
//clearing all data and loading unmodified data from file
for(int i = 0; i < playerCount; i++)
delete itemPlayerStorage[i];
itemPlayerStorage.clear();
playerStorage.clear();
playerCount = 0;
//opening file
QFile dataBase("TempDataBase.txt");
dataBase.open(QFile::ReadOnly | QFile::Text);
QTextStream in(&dataBase);
in.setCodec("UTF-8"); //setting codec to properly handle polish charactes
while(!in.atEnd())
{
Player tempPlayer;
//reading one line from file and spliting it into three strings
//syntax of a given line in the file: player_firstName\tplayer_lastName\tplayer_rating (fields separated by \t)
QList<QString> playerDataList = in.readLine().split("\t");
tempPlayer.setFirstName(playerDataList[0]);
tempPlayer.setLastName(playerDataList[1]);
tempPlayer.setSextoBase(playerDataList[2]);
tempPlayer.setRating(playerDataList[3].toShort());
tempPlayer.setCategories(playerDataList[4]);
tempPlayer.setCountry(playerDataList[5]);
tempPlayer.setClub(playerDataList[6]);
playerStorage.append(tempPlayer);
playerCount++;
}
dataBase.close();
this->sort();
//adding players to itemPlayerStorage vector used by tableWidget to display player data
for(int i = 0; i < playerCount; i++)
{
QTableWidgetItem* tempItem;
tempItem = new QTableWidgetItem;
(*tempItem).setData(0, playerStorage.value(i).getFirstName());
itemPlayerStorage.append(tempItem);
tempItem = new QTableWidgetItem;
(*tempItem).setData(0, playerStorage.value(i).getLastName());
itemPlayerStorage.append(tempItem);
tempItem = new QTableWidgetItem;
(*tempItem).setData(0,playerStorage.value(i).getSex());
itemPlayerStorage.append(tempItem);
tempItem = new QTableWidgetItem;
(*tempItem).setData(0, playerStorage.value(i).getRating());
itemPlayerStorage.append(tempItem);
tempItem = new QTableWidgetItem;
(*tempItem).setData(0,playerStorage.value(i).getCategories());
itemPlayerStorage.append(tempItem);
tempItem = new QTableWidgetItem;
(*tempItem).setData(0,playerStorage.value(i).getCountry());
itemPlayerStorage.append(tempItem);
tempItem = new QTableWidgetItem;
(*tempItem).setData(0,playerStorage.value(i).getClub());
itemPlayerStorage.append(tempItem);
}
}
void playerBase::save()
{
//Opening file
QFile dataBase("TempDataBase.txt");
dataBase.open(QFile::WriteOnly | QFile::Text);
//Writing data to file
for(int i = 0; i < playerCount; i++)
{
QTextStream out(&dataBase);
out.setCodec("UTF-8"); //setting codec to properly handle polish charactes
out << playerStorage.value(i).getFirstName() << "\t" << playerStorage.value(i).getLastName() << "\t" << playerStorage.value(i).getSex() << "\t" << playerStorage.value(i).getRating() << "\t" << playerStorage.value(i).getCategories() << "\t" << playerStorage.value(i).getCountry() << "\t" << playerStorage.value(i).getClub() <<"\n";
}
//Closing the file
dataBase.flush();
dataBase.close();
}
void playerBase::add(Player player)
{
playerStorage.append(player);
playerCount++;
this->sort();
//clearing previous itemPlayerStorage vector and freeing allocated memory
for(int i = 0; i < (playerCount - 1); i++)
{
delete itemPlayerStorage[i];
}
itemPlayerStorage.clear();
//generating new itemPlayerStorage vector based on new sorted data
for(int i = 0; i < playerCount; i++)
{
QTableWidgetItem* tempItem;
tempItem = new QTableWidgetItem;
(*tempItem).setData(0, playerStorage.value(i).getFirstName());
itemPlayerStorage.append(tempItem);
tempItem = new QTableWidgetItem;
(*tempItem).setData(0, playerStorage.value(i).getLastName());
itemPlayerStorage.append(tempItem);
tempItem = new QTableWidgetItem;
(*tempItem).setData(0,playerStorage.value(i).getSex());
itemPlayerStorage.append(tempItem);
tempItem = new QTableWidgetItem;
(*tempItem).setData(0, playerStorage.value(i).getRating());
itemPlayerStorage.append(tempItem);
tempItem = new QTableWidgetItem;
(*tempItem).setData(0,playerStorage.value(i).getCategories());
itemPlayerStorage.append(tempItem);
tempItem = new QTableWidgetItem;
(*tempItem).setData(0,playerStorage.value(i).getCountry());
itemPlayerStorage.append(tempItem);
tempItem = new QTableWidgetItem;
(*tempItem).setData(0,playerStorage.value(i).getClub());
itemPlayerStorage.append(tempItem);
}
}
void playerBase::remove(int row)
{
playerStorage.remove(row);
playerCount--;
this->sort();
//clearing previous itemPlayerStorage vector and freeing allocated memory
for(int i = 0; i < (playerCount + 1); i++)
{
delete itemPlayerStorage[i];
}
itemPlayerStorage.clear();
//generating new itemPlayerStorage vector based on new sorted data
for(int i = 0; i < playerCount; i++)
{
QTableWidgetItem* tempItem;
tempItem = new QTableWidgetItem;
(*tempItem).setData(0, playerStorage.value(i).getFirstName());
itemPlayerStorage.append(tempItem);
tempItem = new QTableWidgetItem;
(*tempItem).setData(0, playerStorage.value(i).getLastName());
itemPlayerStorage.append(tempItem);
tempItem = new QTableWidgetItem;
(*tempItem).setData(0,playerStorage.value(i).getSex());
itemPlayerStorage.append(tempItem);
tempItem = new QTableWidgetItem;
(*tempItem).setData(0, playerStorage.value(i).getRating());
itemPlayerStorage.append(tempItem);
tempItem = new QTableWidgetItem;
(*tempItem).setData(0,playerStorage.value(i).getCategories());
itemPlayerStorage.append(tempItem);
tempItem = new QTableWidgetItem;
(*tempItem).setData(0,playerStorage.value(i).getCountry());
itemPlayerStorage.append(tempItem);
tempItem = new QTableWidgetItem;
(*tempItem).setData(0,playerStorage.value(i).getClub());
itemPlayerStorage.append(tempItem);
}
}
//In Mariusz's opinion this does work.
//for reasons unknown this method doesn't work
void playerBase::edit(Player player, int row)
{
playerStorage[row].setFirstName(player.getFirstName());
playerStorage[row].setLastName(player.getLastName());
playerStorage[row].setSextoBase(player.getSex());
playerStorage[row].setRating(player.getRating());
playerStorage[row].setCategories(player.getCategories());
playerStorage[row].setCountry(player.getCountry());
playerStorage[row].setClub(player.getClub());
this->sort();
//clearing previous player table items
for(int i = 0; i < (playerCount); i++)
{
delete itemPlayerStorage[i];
}
itemPlayerStorage.clear();
//generating new player storage items
for(int i = 0; i < playerCount; i++)
{
QTableWidgetItem* tempItem;
tempItem = new QTableWidgetItem;
(*tempItem).setData(0, playerStorage.value(i).getFirstName());
itemPlayerStorage.append(tempItem);
tempItem = new QTableWidgetItem;
(*tempItem).setData(0, playerStorage.value(i).getLastName());
itemPlayerStorage.append(tempItem);
tempItem = new QTableWidgetItem;
(*tempItem).setData(0, playerStorage.value(i).getSex());
itemPlayerStorage.append(tempItem);
tempItem = new QTableWidgetItem;
(*tempItem).setData(0, playerStorage.value(i).getRating());
itemPlayerStorage.append(tempItem);
tempItem = new QTableWidgetItem;
(*tempItem).setData(0, playerStorage.value(i).getCategories());
itemPlayerStorage.append(tempItem);
tempItem = new QTableWidgetItem;
(*tempItem).setData(0, playerStorage.value(i).getCountry());
itemPlayerStorage.append(tempItem);
tempItem = new QTableWidgetItem;
(*tempItem).setData(0, playerStorage.value(i).getClub());
itemPlayerStorage.append(tempItem);
}
}
int playerBase::get_playerCount()
{
return playerCount;
}
Player playerBase::get_player(int row)
{
Player tempPlayer;
tempPlayer.setFirstName(playerStorage.value(row).getFirstName());
tempPlayer.setLastName(playerStorage.value(row).getLastName());
tempPlayer.setSextoBase(playerStorage.value(row).getSex());
tempPlayer.setRating(playerStorage.value(row).getRating());
tempPlayer.setCategories(playerStorage.value(row).getCategories());
tempPlayer.setCountry(playerStorage.value(row).getCountry());
tempPlayer.setClub(playerStorage.value(row).getClub());
return tempPlayer;
}
void playerBase::sort()
{
qSort(playerStorage);
for(int i = 0; i < playerCount; i++)
{
playerStorage.value(i).setRang(i+1);
}
}