-
Notifications
You must be signed in to change notification settings - Fork 0
/
existinguserdialog.cpp
159 lines (125 loc) · 4.92 KB
/
existinguserdialog.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
#include "existinguserdialog.h"
#include "ui_existinguserdialog.h"
#include <QDebug>
#include <vector>
#include <algorithm>
QString FileName = "/Users/Parth/Documents/QT/RvZ/rvz_players.csv";
ExistingUserDialog::ExistingUserDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::ExistingUserDialog)
{
ui->setupUi(this);
readFromPlayerFile(); //reads the existing users on file
inputUser = ""; //default username
}
ExistingUserDialog::~ExistingUserDialog()
{
delete ui;
}
QString ExistingUserDialog::getUsername() const
{
return inputUser;
}
QString ExistingUserDialog::getLevel() const
{
return userLevel;
}
void ExistingUserDialog::on_deleteButton_clicked()
{
QFile username_file(FileName);
//Gets the username being deleted
QString user_delete = ui->playerListBox->currentText();
//Displays warning if file is not writable
if(!username_file.open(QIODevice::ReadWrite|QIODevice::Text))
QMessageBox::warning(this,tr("Error!"),tr("Error with rvz_players.csv! No new user created"),
QMessageBox::Ok);
QTextStream write_users(&username_file);
QString final_text; //final text to be saved on csv file
for(int i = 0; i < playerList.length(); i++)
{
//Seperates the name line in name + extra components
QStringList temp_list;
temp_list = playerList.at(i).split(':');
//checks if the name component matches the one being deleted
if(temp_list.at(1) != user_delete)
final_text.append(playerList.at(i) + "\n");
}
//deletes all user info on the file
ui->deleteAllButton->clicked(true);
//Writes the updated user info on file
write_users << final_text;
//closes file
username_file.close();
readFromPlayerFile(); //Reads new version of file to update combo box
}
void ExistingUserDialog::on_selectButton_clicked()
{
//Saves the currently active text in the combo box as the seletected user
inputUser = ui->playerListBox->currentText();
//Getting the level information for selected user
QFile username_file(FileName);
playerList.clear(); //clears playerList from previous read calls
//Displays warning if file is not readable
if(!username_file.open(QIODevice::ReadOnly|QIODevice::Text))
QMessageBox::warning(this,tr("Error!"),tr("Error reading rvz_players.csv!"),
QMessageBox::Ok);
QTextStream read_users(&username_file);
//Makes a list of the players in the file to remember when updating
while(!read_users.atEnd())
playerList.append(read_users.readLine());
for(int i = 0; i < playerList.length(); i++)
{
//Creates temp list to hold pieces of each line
QStringList temp_list;
temp_list = playerList.at(i).split(':');
//Gets the level for the selected user based on index in temp_list
if(inputUser == temp_list.at(1))
userLevel = temp_list.at(2); //get level information for selected user
}
username_file.close(); //closes file
close(); //closes dialog
}
void ExistingUserDialog::readFromPlayerFile()
{
QFile username_file(FileName);
playerList.clear(); //clears playerList from previous read calls
ui->playerListBox->clear(); //clears the combo box
//Displays warning if file is not readable
if(!username_file.open(QIODevice::ReadOnly|QIODevice::Text))
QMessageBox::warning(this,tr("Error!"),tr("Error reading rvz_players.csv!"),
QMessageBox::Ok);
QTextStream read_users(&username_file);
//Makes a list of the players in the file to remember when updating
while(!read_users.atEnd())
playerList.append(read_users.readLine());
//Loop start at the end of playerList so latest timestamps are displayed first
for(int i = playerList.size()-1; i >= 0; i--)
{
//Creates a temporary list to hold pieces of each line from player file
QStringList temp_list;
temp_list = playerList.at(i).split(':');
ui->playerListBox->addItem(temp_list.at(1)); //Adds the name to combo box
}
//closes file
username_file.close();
}
void ExistingUserDialog::on_deleteAllButton_clicked()
{
QFile username_file(FileName);
//Displays warning if file is not writable
if(!username_file.open(QIODevice::WriteOnly|QIODevice::Text))
QMessageBox::warning(this,tr("Error!"),tr("Error writing to rvz_players.csv! No new user selected"),
QMessageBox::Ok);
//Overwrites data on file
QTextStream clear_file(&username_file);
clear_file << "";
username_file.close(); //closes file
ui->playerListBox->clear(); //clears combo box
}
void ExistingUserDialog::closeEvent(QCloseEvent *event)
{
//Signals to activate mainwindow buttons and delete dialog pointer in mainwindow.cpp
changeButtonState(1);
deleteExistingUserDialog();
event->accept(); //closes dialog
}