-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsignupdialog.cpp
More file actions
89 lines (73 loc) · 3.14 KB
/
Copy pathsignupdialog.cpp
File metadata and controls
89 lines (73 loc) · 3.14 KB
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
#include "signupdialog.h"
#include "ui_signupdialog.h"
SignupDialog::SignupDialog(QWidget *parent) : QDialog(parent), ui(new Ui::SignupDialog) {
ui->setupUi(this);
// Dynamically set initial ID based on role dropdown
QString initialRole = ui->roleDropdown->currentText();
QString initialID = generateUserID(initialRole);
ui->idDisplayLabel->setText("Your ID: " + initialID);
// Update ID dynamically when the user selects a role
connect(ui->roleDropdown, &QComboBox::currentTextChanged, this, [=]() {
QString newID = generateUserID(ui->roleDropdown->currentText());
ui->idDisplayLabel->setText("Your ID: " + newID);
qDebug() << "Role changed to: " << ui->roleDropdown->currentText() << ", New ID: " << newID; // Debugging
});
}
SignupDialog::~SignupDialog() {
delete ui;
}
// Generates unique ID for Student/Manager
QString SignupDialog::generateUserID(QString role) {
QString filename = (role == "Manager") ? "managers.txt" : "students.txt";
int lastID = (role == "Manager") ? 5000 : 1000;
QFile file(filename);
if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
QTextStream in(&file);
while (!in.atEnd()) {
QString line = in.readLine();
QStringList parts = line.split(" | ");
if (parts.size() > 0 && parts[0].startsWith(role == "Manager" ? "MNG" : "STD")) {
lastID = parts[0].right(4).toInt();
}
}
file.close();
}
lastID++;
return (role == "Manager" ? "MNG" : "STD") + QString::number(lastID);
}
// Handles Signup Button Click
void SignupDialog::on_signupConfirmButton_clicked() {
QString name = ui->nameField->text().trimmed();
QString password = ui->passwordField->text().trimmed();
QString role = ui->roleDropdown->currentText();
QString id = ui->idDisplayLabel->text().split(": ")[1]; // Extract ID from the label
// Ensure valid inputs
if (name.isEmpty() || password.isEmpty()) {
QMessageBox::warning(this, "Error", "Please fill all fields!");
return;
}
if (!name.contains(QRegularExpression("^[A-Za-z]+$"))) {
QMessageBox::warning(this, "Error", "Name can only contain letters!");
return;
}
if (password.startsWith("-")) {
QMessageBox::warning(this, "Error", "Password cannot haveha negative sign!");
return;
}
if (!password.contains(QRegularExpression("^[A-Za-z0-9_]+$"))) {
QMessageBox::warning(this, "Error", "Password must contain only letters or numbers!");
return;
}
// Choose correct file based on role
QString filename = (role == "Manager") ? "managers.txt" : "students.txt";
QFile file(filename);
if (!file.open(QIODevice::Append | QIODevice::Text)) {
QMessageBox::warning(this, "Error", "Could not save user details!");
return;
}
QTextStream out(&file);
out << id << " | " << name << " | " << password << " | " << role << " | NONE\n"; // Default no registered program
file.close();
QMessageBox::information(this, "Success", "Signup Complete! **Your ID:** " + id + "\nSave this ID for login.");
this->close();
}