-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmultithreaded_main.cpp
More file actions
217 lines (179 loc) · 7.69 KB
/
multithreaded_main.cpp
File metadata and controls
217 lines (179 loc) · 7.69 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
#include <QApplication>
#include <QWidget>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QTextEdit>
#include <QCheckBox>
#include <QMenuBar>
#include <QThread>
#include <random>
#include <string>
#include <thread>
#include <future>
#include <vector>
#include <QDialog>
#include <QComboBox>
#include <QColorDialog>
#include <QPalette>
#include <QPushButton>
// Function to generate a random password
std::string generatePassword(int length, bool includeUpper, bool includeLower, bool includeDigits, bool includeSpecial) {
const std::string upper_case = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const std::string lower_case = "abcdefghijklmnopqrstuvwxyz";
const std::string digits = "0123456789";
const std::string special_chars = "!@#$%^&*()-_=+[]{}|;:',.<>?/`~";
std::string all_chars;
if (includeUpper) all_chars += upper_case;
if (includeLower) all_chars += lower_case;
if (includeDigits) all_chars += digits;
if (includeSpecial) all_chars += special_chars;
if (all_chars.empty()) {
throw std::invalid_argument("No character sets selected.");
}
std::random_device rd;
std::mt19937 generator(rd());
std::uniform_int_distribution<> distribution(0, all_chars.size() - 1);
std::string password;
for (int i = 0; i < length; ++i) {
password += all_chars[distribution(generator)];
}
return password;
}
// Function to be run in a separate thread
void generatePasswordThread(int length, bool includeUpper, bool includeLower, bool includeDigits, bool includeSpecial, std::promise<std::string>&& passwordPromise) {
try {
std::string password = generatePassword(length, includeUpper, includeLower, includeDigits, includeSpecial);
passwordPromise.set_value(password);
} catch (const std::exception& e) {
passwordPromise.set_exception(std::current_exception());
}
}
// Settings dialog class for theme and color selection
class SettingsDialog : public QDialog {
Q_OBJECT
public:
SettingsDialog(QWidget *parent = nullptr) : QDialog(parent) {
setWindowTitle("Settings");
QVBoxLayout *layout = new QVBoxLayout(this);
QLabel *themeLabel = new QLabel("Select Theme:");
layout->addWidget(themeLabel);
// Dropdown for light/dark theme selection
QComboBox *themeComboBox = new QComboBox();
themeComboBox->addItem("Light");
themeComboBox->addItem("Dark");
layout->addWidget(themeComboBox);
QLabel *accentColorLabel = new QLabel("Select Accent Color:");
layout->addWidget(accentColorLabel);
// Button to open color picker dialog
QPushButton *colorButton = new QPushButton("Choose Color");
layout->addWidget(colorButton);
// Apply and close buttons
QHBoxLayout *buttonLayout = new QHBoxLayout();
QPushButton *applyButton = new QPushButton("Apply");
QPushButton *closeButton = new QPushButton("Close");
buttonLayout->addWidget(applyButton);
buttonLayout->addWidget(closeButton);
layout->addLayout(buttonLayout);
// Signals and slots for theme and color selection
connect(themeComboBox, &QComboBox::currentTextChanged, this, [this, themeComboBox]() {
if (themeComboBox->currentText() == "Dark") {
applyDarkTheme();
} else {
applyLightTheme();
}
});
connect(colorButton, &QPushButton::clicked, this, [this]() {
QColor chosenColor = QColorDialog::getColor(Qt::white, this, "Select Accent Color");
if (chosenColor.isValid()) {
applyAccentColor(chosenColor);
}
});
connect(applyButton, &QPushButton::clicked, this, &QDialog::accept);
connect(closeButton, &QPushButton::clicked, this, &QDialog::reject);
}
private:
void applyDarkTheme() {
QPalette darkPalette;
darkPalette.setColor(QPalette::Window, QColor(53, 53, 53));
darkPalette.setColor(QPalette::WindowText, Qt::white);
darkPalette.setColor(QPalette::Base, QColor(42, 42, 42));
darkPalette.setColor(QPalette::Text, Qt::white);
darkPalette.setColor(QPalette::Button, QColor(53, 53, 53));
darkPalette.setColor(QPalette::ButtonText, Qt::white);
QApplication::setPalette(darkPalette);
}
void applyLightTheme() {
QApplication::setPalette(QApplication::style()->standardPalette());
}
void applyAccentColor(const QColor &color) {
QPalette palette = QApplication::palette();
palette.setColor(QPalette::Highlight, color);
QApplication::setPalette(palette);
}
};
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
QWidget window;
window.setWindowTitle("Password Generator");
QVBoxLayout *layout = new QVBoxLayout(&window);
// Menu bar for settings
QMenuBar *menuBar = new QMenuBar(&window);
QMenu *settingsMenu = menuBar->addMenu("☰ Settings");
QAction *themeAction = new QAction("Customize Appearance", &window);
settingsMenu->addAction(themeAction);
layout->setMenuBar(menuBar);
QLabel *label = new QLabel("Enter the desired password length:");
layout->addWidget(label);
QLineEdit *lengthInput = new QLineEdit();
layout->addWidget(lengthInput);
// Checkbox options for password criteria
QCheckBox *upperCaseCheckBox = new QCheckBox("Include Upper-case Letters");
QCheckBox *lowerCaseCheckBox = new QCheckBox("Include Lower-case Letters");
QCheckBox *digitsCheckBox = new QCheckBox("Include Digits");
QCheckBox *specialCharsCheckBox = new QCheckBox("Include Special Characters");
layout->addWidget(upperCaseCheckBox);
layout->addWidget(lowerCaseCheckBox);
layout->addWidget(digitsCheckBox);
layout->addWidget(specialCharsCheckBox);
// Button to generate password
QPushButton *generateButton = new QPushButton("Generate Password");
layout->addWidget(generateButton);
// Text area to display the generated password
QTextEdit *passwordDisplay = new QTextEdit();
layout->addWidget(passwordDisplay);
// Event to open the settings dialog
QObject::connect(themeAction, &QAction::triggered, [&]() {
SettingsDialog settingsDialog(&window);
settingsDialog.exec();
});
QObject::connect(generateButton, &QPushButton::clicked, [&]() {
bool validInput = true;
int length = lengthInput->text().toInt(&validInput);
if (!validInput || length <= 0) {
passwordDisplay->setText("Error: Invalid password length.");
return;
}
bool includeUpper = upperCaseCheckBox->isChecked();
bool includeLower = lowerCaseCheckBox->isChecked();
bool includeDigits = digitsCheckBox->isChecked();
bool includeSpecial = specialCharsCheckBox->isChecked();
// Create a promise and future to handle multithreading
std::promise<std::string> passwordPromise;
std::future<std::string> passwordFuture = passwordPromise.get_future();
// Run password generation in a separate thread
std::thread passwordThread(generatePasswordThread, length, includeUpper, includeLower, includeDigits, includeSpecial, std::move(passwordPromise));
// Wait for the result from the future and display it
try {
passwordThread.join(); // Join the thread to ensure it completes
passwordDisplay->setText(QString::fromStdString(passwordFuture.get()));
} catch (const std::exception& e) {
passwordDisplay->setText(QString::fromStdString(e.what()));
}
});
window.show();
return app.exec();
}
#include "moc_multithreaded_main.cpp" // Include the MOC generated file