Repository navigation
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
428 lines (353 loc) · 15.3 KB
/
Copy pathmainwindow.cpp
File metadata and controls
428 lines (353 loc) · 15.3 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
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
#include "mainwindow.h"
#include "privatechatwidget.h"
#include "usersystem.h"
#include <QInputDialog>
#include <QStyleFactory>
#include <QPalette>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent), dbHandler(this)
{
// Apply dark theme
applyDarkTheme();
// Set up the main window
setWindowTitle("CryptexChat");
setWindowIcon(QIcon(":/images/CryptexChat_Logo.png"));
resize(800, 600);
// Create stacked widget to manage different pages
stackedWidget = new QStackedWidget();
setCentralWidget(stackedWidget);
// Set up each page
setupWelcomePage();
setupLoginPage();
setupRegisterPage();
// Start with welcome page
stackedWidget->setCurrentWidget(welcomePage);
// Initialize the database
if (!dbHandler.initialize()) {
QMessageBox::critical(this, "Database Error", "Failed to initialize the database connection.");
}
}
void MainWindow::applyDarkTheme()
{
// Set style to Fusion which works well with custom palettes
qApp->setStyle(QStyleFactory::create("Fusion"));
// Create dark palette
QPalette darkPalette;
// Dark background colors
darkPalette.setColor(QPalette::Window, QColor(45, 45, 45));
darkPalette.setColor(QPalette::WindowText, Qt::white);
darkPalette.setColor(QPalette::Base, QColor(25, 25, 25));
darkPalette.setColor(QPalette::AlternateBase, QColor(53, 53, 53));
// Text colors
darkPalette.setColor(QPalette::Text, Qt::white);
darkPalette.setColor(QPalette::Button, QColor(53, 53, 53));
darkPalette.setColor(QPalette::ButtonText, Qt::white);
// Selection colors
darkPalette.setColor(QPalette::Highlight, QColor(42, 130, 218));
darkPalette.setColor(QPalette::HighlightedText, Qt::black);
// Tooltip colors
darkPalette.setColor(QPalette::ToolTipBase, QColor(42, 130, 218));
darkPalette.setColor(QPalette::ToolTipText, Qt::white);
// Link color
darkPalette.setColor(QPalette::Link, QColor(42, 130, 218));
// Apply the palette
qApp->setPalette(darkPalette);
// Set stylesheet for additional customization
qApp->setStyleSheet("QToolTip { color: #ffffff; background-color: #2a82da; border: 1px solid white; }"
"QLineEdit, QTextEdit { background-color: #303030; border: 1px solid #5c5c5c; border-radius: 3px; padding: 2px; }"
"QPushButton { background-color: #5c5c5c; border-radius: 5px; border: none; color: white; padding: 8px; margin: 2px; max-width: 250px; }"
"QPushButton:hover { background-color: #6e6e6e; }"
"QPushButton:pressed { background-color: #2a82da; }"
"QLabel { color: #ffffff; }"
"QMessageBox { background-color: #353535; }");
}
MainWindow::~MainWindow()
{
}
void MainWindow::setupWelcomePage()
{
welcomePage = new QWidget();
// Create layout
QVBoxLayout *layout = new QVBoxLayout(welcomePage);
// Add logo/image
QLabel *logoLabel = new QLabel();
QPixmap logoPixmap(":/images/CryptexChat_Logo.png");
logoLabel->setPixmap(logoPixmap.scaled(128, 128, Qt::KeepAspectRatio, Qt::SmoothTransformation));
logoLabel->setAlignment(Qt::AlignCenter);
// Add title
QLabel *titleLabel = new QLabel("Welcome to CryptexChat");
QFont titleFont = titleLabel->font();
titleFont.setPointSize(24);
titleFont.setBold(true);
titleLabel->setFont(titleFont);
titleLabel->setAlignment(Qt::AlignCenter);
// Add subtitle
QLabel *subtitleLabel = new QLabel("Connect with friends securely");
subtitleLabel->setAlignment(Qt::AlignCenter);
QFont subtitleFont = subtitleLabel->font();
subtitleFont.setPointSize(12);
subtitleLabel->setFont(subtitleFont);
// Create container for buttons
QWidget *buttonContainer = new QWidget();
QVBoxLayout *buttonLayout = new QVBoxLayout(buttonContainer);
// Add buttons
loginButton = new QPushButton("Login");
registerButton = new QPushButton("Register");
// Style buttons
loginButton->setMinimumHeight(40);
registerButton->setMinimumHeight(40);
// Give the buttons a fixed width
loginButton->setFixedWidth(200);
registerButton->setFixedWidth(200);
// Connect buttons
connect(loginButton, &QPushButton::clicked, this, &MainWindow::showLoginForm);
connect(registerButton, &QPushButton::clicked, this, &MainWindow::showRegisterForm);
// Add buttons to button layout
buttonLayout->addWidget(loginButton, 0, Qt::AlignCenter);
buttonLayout->addWidget(registerButton, 0, Qt::AlignCenter);
buttonLayout->setSpacing(10);
// Add widgets to main layout
layout->addStretch();
layout->addWidget(logoLabel);
layout->addWidget(titleLabel);
layout->addWidget(subtitleLabel);
layout->addSpacing(40);
layout->addWidget(buttonContainer);
layout->addStretch();
// Add page to stacked widget
stackedWidget->addWidget(welcomePage);
}
void MainWindow::setupLoginPage()
{
loginPage = new QWidget();
// Create layout
QVBoxLayout *layout = new QVBoxLayout(loginPage);
// Add title
QLabel *titleLabel = new QLabel("Login to Your Account");
QFont titleFont = titleLabel->font();
titleFont.setPointSize(20);
titleFont.setBold(true);
titleLabel->setFont(titleFont);
titleLabel->setAlignment(Qt::AlignCenter);
// Create form container with max width
QWidget *formContainer = new QWidget();
formContainer->setFixedWidth(320);
QVBoxLayout *containerLayout = new QVBoxLayout(formContainer);
// Create vertical form layout instead of QFormLayout
QVBoxLayout *formLayout = new QVBoxLayout();
formLayout->setSpacing(10);
// Username label and field
QLabel *usernameLabel = new QLabel("Username:");
usernameLabel->setAlignment(Qt::AlignLeft);
loginUsernameField = new QLineEdit();
loginUsernameField->setMinimumHeight(30);
loginUsernameField->setPlaceholderText("Enter your Username");
loginUsernameField->setStyleSheet("QLineEdit { color: white; } QLineEdit::placeholder { color: white; }");
// Password label and field
QLabel *passwordLabel = new QLabel("Password:");
passwordLabel->setAlignment(Qt::AlignLeft);
loginPasswordField = new QLineEdit();
loginPasswordField->setMinimumHeight(30);
loginPasswordField->setEchoMode(QLineEdit::Password);
loginPasswordField->setPlaceholderText("Enter your password");
loginPasswordField->setStyleSheet("QLineEdit { color: white; } QLineEdit::placeholder { color: white; }");
// Add widgets to form layout
formLayout->addWidget(usernameLabel);
formLayout->addWidget(loginUsernameField);
formLayout->addWidget(passwordLabel);
formLayout->addWidget(loginPasswordField);
// Create button container
QWidget *buttonContainer = new QWidget();
QVBoxLayout *buttonLayout = new QVBoxLayout(buttonContainer);
// Create buttons
loginSubmitButton = new QPushButton("Login");
loginBackButton = new QPushButton("Back");
// Style buttons
loginSubmitButton->setMinimumHeight(40);
loginBackButton->setMinimumHeight(40);
loginSubmitButton->setFixedWidth(200);
loginBackButton->setFixedWidth(200);
// Connect buttons
connect(loginSubmitButton, &QPushButton::clicked, this, &MainWindow::performLogin);
connect(loginBackButton, &QPushButton::clicked, this, [this]() {
stackedWidget->setCurrentWidget(welcomePage);
});
// Add buttons to button layout
buttonLayout->addWidget(loginSubmitButton, 0, Qt::AlignCenter);
buttonLayout->addWidget(loginBackButton, 0, Qt::AlignCenter);
buttonLayout->setSpacing(10);
// Add layouts to container
containerLayout->addLayout(formLayout);
containerLayout->addSpacing(20);
// Add widgets to main layout
layout->addSpacing(30);
layout->addWidget(titleLabel);
layout->addSpacing(40);
layout->addWidget(formContainer, 0, Qt::AlignHCenter);
layout->addSpacing(20);
layout->addWidget(buttonContainer);
layout->addStretch();
// Add page to stacked widget
stackedWidget->addWidget(loginPage);
}
void MainWindow::setupRegisterPage()
{
registerPage = new QWidget();
// Create layout
QVBoxLayout *layout = new QVBoxLayout(registerPage);
// Add title
QLabel *titleLabel = new QLabel("Create a New Account");
QFont titleFont = titleLabel->font();
titleFont.setPointSize(20);
titleFont.setBold(true);
titleLabel->setFont(titleFont);
titleLabel->setAlignment(Qt::AlignCenter);
// Create form container with max width
QWidget *formContainer = new QWidget();
formContainer->setFixedWidth(320);
QVBoxLayout *containerLayout = new QVBoxLayout(formContainer);
// Create vertical form layout instead of QFormLayout
QVBoxLayout *formLayout = new QVBoxLayout();
formLayout->setSpacing(10);
// Username label and field
QLabel *usernameLabel = new QLabel("Username:");
usernameLabel->setAlignment(Qt::AlignLeft);
registerUsernameField = new QLineEdit();
registerUsernameField->setMinimumHeight(30);
registerUsernameField->setPlaceholderText("Choose a username");
registerUsernameField->setStyleSheet("QLineEdit { color: white; } QLineEdit::placeholder { color: white; }");
// Email label and field
QLabel *emailLabel = new QLabel("Email:");
emailLabel->setAlignment(Qt::AlignLeft);
registerEmailField = new QLineEdit();
registerEmailField->setMinimumHeight(30);
registerEmailField->setPlaceholderText("Enter your email");
registerEmailField->setStyleSheet("QLineEdit { color: white; } QLineEdit::placeholder { color: white; }");
// Password label and field
QLabel *passwordLabel = new QLabel("Password:");
passwordLabel->setAlignment(Qt::AlignLeft);
registerPasswordField = new QLineEdit();
registerPasswordField->setMinimumHeight(30);
registerPasswordField->setEchoMode(QLineEdit::Password);
registerPasswordField->setPlaceholderText("Choose a password");
registerPasswordField->setStyleSheet("QLineEdit { color: white; } QLineEdit::placeholder { color: white; }");
// Add widgets to form layout
formLayout->addWidget(usernameLabel);
formLayout->addWidget(registerUsernameField);
formLayout->addWidget(emailLabel);
formLayout->addWidget(registerEmailField);
formLayout->addWidget(passwordLabel);
formLayout->addWidget(registerPasswordField);
// Create button container
QWidget *buttonContainer = new QWidget();
QVBoxLayout *buttonLayout = new QVBoxLayout(buttonContainer);
// Create buttons
registerSubmitButton = new QPushButton("Register");
registerBackButton = new QPushButton("Back");
// Style buttons
registerSubmitButton->setMinimumHeight(40);
registerBackButton->setMinimumHeight(40);
registerSubmitButton->setFixedWidth(200);
registerBackButton->setFixedWidth(200);
// Connect buttons
connect(registerSubmitButton, &QPushButton::clicked, this, &MainWindow::performRegistration);
connect(registerBackButton, &QPushButton::clicked, this, [this]() {
stackedWidget->setCurrentWidget(welcomePage);
});
// Add buttons to button layout
buttonLayout->addWidget(registerSubmitButton, 0, Qt::AlignCenter);
buttonLayout->addWidget(registerBackButton, 0, Qt::AlignCenter);
buttonLayout->setSpacing(10);
// Add layouts to container
containerLayout->addLayout(formLayout);
containerLayout->addSpacing(20);
// Add widgets to main layout
layout->addSpacing(30);
layout->addWidget(titleLabel);
layout->addSpacing(40);
layout->addWidget(formContainer, 0, Qt::AlignHCenter);
layout->addSpacing(20);
layout->addWidget(buttonContainer);
layout->addStretch();
// Add page to stacked widget
stackedWidget->addWidget(registerPage);
}
void MainWindow::showLoginForm()
{
loginUsernameField->clear();
loginPasswordField->clear();
stackedWidget->setCurrentWidget(loginPage);
}
void MainWindow::showRegisterForm()
{
registerUsernameField->clear();
registerEmailField->clear();
registerPasswordField->clear();
stackedWidget->setCurrentWidget(registerPage);
}
void MainWindow::performLogin()
{
QString usename = loginUsernameField->text();
QString password = loginPasswordField->text();
if (usename.isEmpty() || password.isEmpty()) {
QMessageBox::warning(this, "Login Error", "Please enter both email and password.");
return;
}
UserSystem us(&dbHandler);
if (us.loginUser(usename, password)) {
currentUser = qMakePair(usename, usename); // Store the current user
QMessageBox::information(this, "Login Successful",
"Welcome back, " + currentUser.first + "!");
startPrivateChat();
} else {
QMessageBox::warning(this, "Login Error",
"Invalid Username or Password. Please try again.");
}
}
void MainWindow::startPrivateChat()
{
bool ok;
QString userName = QInputDialog::getText(this, "Start Private Chat",
"Enter user's username:",
QLineEdit::Normal, "", &ok);
if (ok && !userName.isEmpty()) {
QString username = dbHandler.userExists(userName);
if (username != "") {
// Pass the database handler and user system reference
PrivateChatWidget* privateChatWidget = new PrivateChatWidget(currentUser.second, userName, userName, dbHandler, userSystem, this);
// Connect the back button signal
connect(privateChatWidget, &PrivateChatWidget::backToMenuRequested, this, [this, privateChatWidget]() {
// Switch back to welcome page
stackedWidget->setCurrentWidget(welcomePage);
privateChatWidget->deleteLater();
});
stackedWidget->addWidget(privateChatWidget);
stackedWidget->setCurrentWidget(privateChatWidget);
} else {
QMessageBox::warning(this, "User Not Found",
"No user with this Username was found.");
}
}
}
void MainWindow::performRegistration()
{
QString username = registerUsernameField->text();
QString email = registerEmailField->text();
QString password = registerPasswordField->text();
if (username.isEmpty() || email.isEmpty() || password.isEmpty()) {
QMessageBox::warning(this, "Registration Error",
"All fields are required.");
return;
}
// Register user via UserSystem (unified auth)
UserSystem us(&dbHandler);
if (us.registerUser(username, email, password)) {
QMessageBox::information(this, "Registration Successful",
"Account created successfully! You can now login.");
// Switch to login page
stackedWidget->setCurrentWidget(loginPage);
} else {
QMessageBox::warning(this, "Registration Error",
"Username already exists or database error occurred.");
}
}