-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainwindow.cpp
610 lines (468 loc) · 18.5 KB
/
mainwindow.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
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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
#include "mainwindow.h"
#include "elementbutton.h"
#include "elementinfowidget.h"
#include "userpasswordinput.h"
#include "usernameinput.h"
#include "usernoteinput.h"
#include <QLabel>
#include <QPushButton>
#include <QScrollArea>
#include <QVBoxLayout>
#include <QStackedWidget>
#include <QGridLayout>
#include <QDebug>
#include <QMessageBox>
ePasswordCore MainWindow::core;
int MainWindow::element_buttons_height = 50;
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
qDebug() << "constructor";
p_central_widget = new QLabel;
p_grid_layout = new QGridLayout;
p_add_button = new QPushButton;
p_wipe_button = new QPushButton;
p_stacked_info_widget = new QStackedWidget;
p_scroll_area = new QScrollArea;
p_scroll_area_widget = new QWidget;
p_scroll_area_widget_layout = new QVBoxLayout;
p_author_button = new QPushButton;
p_null_widget = new QLabel;
p_stacked_info_widget->addWidget(p_null_widget);
p_author_widget = new QWidget;
scroll_widget_height = 0;
setCentralWidget(p_central_widget);
p_central_widget->setLayout(p_grid_layout);
p_scroll_area->setWidget(p_scroll_area_widget);
p_scroll_area_widget->setLayout(p_scroll_area_widget_layout);
createObjectNames();
createWidgetsPlacement();
createButtonsSettings();
createScrollAreaSettings();
createBackGround();
createTakeNameInputWidget();
createTakePasswordInputWidget();
createTakeNoteInputWidget();
createAuthorWidgetSettings();
if(!core.elements.empty())
processReadElementsFromFile();
clearElementInfoWidget();
}
MainWindow::~MainWindow()
{
qDebug() << "destructor";
}
void MainWindow::createWidgetsPlacement()
{
qDebug() << "create widgets placement";
QHBoxLayout *p_add_wipe_layout = new QHBoxLayout;
p_add_wipe_layout->setSpacing(0);
p_add_wipe_layout->setMargin(10);
p_add_wipe_layout->setAlignment(Qt::AlignCenter);
p_add_wipe_layout->addWidget(p_add_button);
p_add_wipe_layout->addWidget(p_wipe_button);
p_grid_layout->addLayout(p_add_wipe_layout, 0, 0, 1, 2);
p_grid_layout->addWidget(p_author_button, 0, 2, 1, 2);
p_grid_layout->addWidget(p_scroll_area, 1, 0, 1, 2);
p_grid_layout->addWidget(p_stacked_info_widget, 1, 2, 1, 2);
}
void MainWindow::createButtonsSettings()
{
qDebug() << "buttons settings";
//add button
QPixmap add_pix(":/images/add.png");
p_add_button->setIcon(add_pix);
p_add_button->setIconSize(add_pix.size() / 2.5);
p_add_button->setFixedSize(add_pix.size() / 2.5);
p_add_button->setFlat(true);
p_add_button->setToolTip("Add new data");
connect(p_add_button, SIGNAL(clicked(bool)), SLOT(processTakeName()));
// connect(p_add_button, SIGNAL(clicked(bool)), SLOT(lockOtherWidgets()));
//wipe button
QPixmap wipe_pix(":/images/removeall.png");
p_wipe_button->setIcon(wipe_pix);
p_wipe_button->setIconSize(wipe_pix.size() / 2.5);
p_wipe_button->setFixedSize(wipe_pix.size() / 2.5);
p_wipe_button->setFlat(true);
p_wipe_button->setToolTip("Remove all saves forever");
connect(p_wipe_button, SIGNAL(clicked(bool)), SLOT(processWipeData()));
//author button
p_author_button->setText("York's product");
p_author_button->setFont(QFont("phosphate", 35));
p_author_button->setFlat(true);
p_author_button->setFixedWidth(450);
p_author_button->setStyleSheet("color : #591a57;");
connect(p_author_button, SIGNAL(clicked(bool)), SLOT(showAuthorWidget()));
p_add_button->setCursor(Qt::OpenHandCursor);
p_wipe_button->setCursor(Qt::OpenHandCursor);
p_author_button->setCursor(Qt::OpenHandCursor);
p_author_button->setToolTip("Information about author");
}
void MainWindow::createObjectNames()
{
qDebug() << "create object names";
p_central_widget->setObjectName("central widget");
p_grid_layout->setObjectName("grid");
p_add_button->setObjectName("add button");
p_wipe_button->setObjectName("wipe_button");
p_stacked_info_widget->setObjectName("stacked widget");
p_scroll_area->setObjectName("scroll area");
p_scroll_area_widget->setObjectName("scroll widget");
p_scroll_area_widget_layout->setObjectName("scroll layout");
p_author_button->setObjectName("author button");
p_author_widget->setObjectName("author widget");
}
void MainWindow::createScrollAreaSettings()
{
qDebug() << "create scroll area settings";
p_scroll_area_widget_layout->setMargin(0);
p_scroll_area_widget_layout->setSpacing(0);
p_scroll_area->setFixedWidth(230);
p_scroll_area->setStyleSheet("background-color: rgba(255, 255, 255, 0);");
p_scroll_area->setFrameStyle(QFrame::Panel);
p_scroll_area->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
p_scroll_area->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
}
void MainWindow::createBackGround()
{
qDebug() << "background create";
p_central_widget->setPixmap(QPixmap(":/images/bg2.jpg"));
}
void MainWindow::createTakeNameInputWidget()
{
qDebug() << "create take name widget";
p_name_input = new UserNameInput;
p_name_input->setNameMaximumLength(20);
p_name_input->setNameMinimumLength(1);
p_name_input->setText("name settings");
p_stacked_info_widget->addWidget(p_name_input);
connect(p_name_input, SIGNAL(sendName(QString)), SLOT(copyToNameBuffer(QString)));
connect(p_name_input, SIGNAL(sendName(QString)), SLOT(unlockOtherWidgets()));
}
void MainWindow::createTakePasswordInputWidget()
{
qDebug() << "create take password widget";
p_password_input = new UserPasswordInput;
p_password_input->setPasswordMaximumLength(20);
p_password_input->setPasswordMinimumLength(5);
p_password_input->setText("password settings");
p_stacked_info_widget->addWidget(p_password_input);
connect(p_password_input, SIGNAL(sendPassword(QString)), SLOT(copyToPasswordBuffer(QString)));
connect(p_password_input, SIGNAL(sendPassword(QString)), SLOT(unlockOtherWidgets()));
}
void MainWindow::createTakeNoteInputWidget()
{
qDebug() << "create take note widget";
p_note_input = new UserNoteInput;
p_note_input->setText("note settings");
p_stacked_info_widget->addWidget(p_note_input);
connect(p_note_input, SIGNAL(sendNote(QString)), SLOT(copyToNoteBuffer(QString)));
connect(p_note_input, SIGNAL(sendNote(QString)), SLOT(unlockOtherWidgets()));
}
void MainWindow::clearInput()
{
qDebug() << "clear input";
p_name_input->clearInput();
p_password_input->clearInput();
p_note_input->clearInput();
}
void MainWindow::createAuthorWidgetSettings()
{
qDebug() << "create author widget settings";
QTextEdit *p_about_text = new QTextEdit;
p_about_text->setText("Hi! My name is York.\n"
"I'm from Saint-Petersburg (Russia).\n\n"
"I really hope you will use my\npassword-"
"manager with fun.\n\n"
"If you wanna connect with me,\n"
"use this e-mail : [email protected]\n\n"
"This is an open-source application,\n"
"so I can send you all the sources.\n\n"
"Thanks for using my application,\n"
"good luck!"
);
p_about_text->setAlignment(Qt::AlignCenter);
p_about_text->setFont(QFont("Courier", 17));
p_about_text->setReadOnly(true);
p_about_text->setStyleSheet("background-color : rgba(255, 255, 255, 0)");
p_stacked_info_widget->addWidget(p_author_widget);
QPushButton *p_close_button = new QPushButton("close");
QVBoxLayout *p_about_layout = new QVBoxLayout;
p_about_layout->addSpacing(20);
p_about_layout->addWidget(p_about_text);
p_about_layout->addWidget(p_close_button);
p_author_widget->setLayout(p_about_layout);
connect(p_close_button, SIGNAL(clicked(bool)), p_author_widget, SLOT(close()));
connect(p_close_button, SIGNAL(clicked(bool)), SLOT(clearElementInfoWidget()));
p_close_button->setCursor(Qt::OpenHandCursor);
}
//slots
void MainWindow::processRefreshScrollArea()
{
qDebug() << "refresh scroll area";
std::sort(element_buttons_list.begin(), element_buttons_list.end(), element_buttons_compare);
scroll_widget_height = 0;
delete p_scroll_area_widget_layout;
p_scroll_area_widget_layout = new QVBoxLayout;
p_scroll_area_widget->setLayout(p_scroll_area_widget_layout);
for(QList<ElementButton*>::iterator iter = element_buttons_list.begin(); iter != element_buttons_list.end(); ++iter)
{
p_scroll_area_widget->resize(p_scroll_area->width() - 2, scroll_widget_height += element_buttons_height + 15);
p_scroll_area_widget_layout->addWidget((*iter));
}
}
void MainWindow::processCreateNewElement(QString name, QString password, QString note)
{
qDebug() << "creating new element";
if(name == 0 && password == 0 && note == 0)
{
name = name_buffer;
password = password_buffer;
note = note_buffer;
core.addElement(name, password, note);
core.writeFile();
setElementInfoWidget(createNewInfoLabel(name, password, note));
processRefreshScrollArea();
}
else
createNewInfoLabel(name, password, note);
}
void MainWindow::processRemoveElement()
{
qDebug() << "process remove element";
removeElement((ElementInfoWidget*) sender());
}
void MainWindow::removeElement(ElementInfoWidget *p_widget)
{
qDebug() << "remove element";
for(QList<ElementButton*>::iterator iter = element_buttons_list.begin(); iter != element_buttons_list.end(); ++iter)
if(*iter == p_widget->p_pair_button)
{
element_buttons_list.erase(iter);
delete p_widget->p_pair_button;
break;
}
core.removeElement(sender()->objectName());
core.writeFile();
delete sender();
processRefreshScrollArea();
clearElementInfoWidget();
}
void MainWindow::processWipeData()
{
qDebug() << "process wipe data";
QMessageBox *p_wipe_alert = new QMessageBox;
p_wipe_alert->setText("All data will be deleted.\nAre you sure?");
p_wipe_alert->setIcon(QMessageBox::Information);
p_wipe_alert->setInformativeText("Wipe all data?");
p_wipe_alert->setStandardButtons(QMessageBox::Yes | QMessageBox::No);
p_wipe_alert->setDefaultButton(QMessageBox::Yes);
short result = p_wipe_alert->exec();
if(result == QMessageBox::Yes)
{
ElementButton *p_temp_button;
for(QList<ElementButton*>::iterator iter = element_buttons_list.begin(); iter != element_buttons_list.end(); ++iter)
{
p_temp_button = *iter;
element_buttons_list.erase(iter);
core.removeElement(p_temp_button->p_pair_widget->objectName());
delete p_temp_button->p_pair_widget;
delete p_temp_button;
}
core.writeFile();
clearElementInfoWidget();
processRefreshScrollArea();
}
else;
delete p_wipe_alert;
}
void MainWindow::processTakeName()
{
qDebug() << "process take name";
clearInput();
p_name_input->setVisible(true);
p_stacked_info_widget->setCurrentWidget(p_name_input);
p_name_input->p_line_edit->setFocus();
if(sender() == p_add_button)
{
connect(p_name_input, SIGNAL(sendName(QString)), SLOT(processTakePassword()), Qt::UniqueConnection);
}
else
{
disconnect(p_name_input, SIGNAL(sendName(QString)), this, SLOT(processTakePassword()));
}
}
void MainWindow::processTakePassword()
{
qDebug() << "process take password";
clearInput();
p_password_input->setVisible(true);
p_stacked_info_widget->setCurrentWidget(p_password_input);
p_password_input->p_generate_button->setFocus();
if(sender() == p_name_input)
{
connect(p_password_input, SIGNAL(sendPassword(QString)), SLOT(processTakeNote()), Qt::UniqueConnection);
}
else
{
disconnect(p_password_input, SIGNAL(sendPassword(QString)), this, SLOT(processTakeNote()));
}
}
void MainWindow::processTakeNote()
{
qDebug() << "process take note";
clearInput();
p_note_input->setVisible(true);
p_stacked_info_widget->setCurrentWidget(p_note_input);
p_note_input->p_text_edit->setFocus();
if(sender() == p_password_input)
{
connect(p_note_input, SIGNAL(sendNote(QString)), SLOT(processCreateNewElement()), Qt::UniqueConnection);
}
else
disconnect(p_note_input, SIGNAL(sendNote(QString)), this, SLOT(processCreateNewElement()));
}
void MainWindow::createEditConnections()
{
qDebug() << "create edit connections";
ElementInfoWidget *p_sender = (ElementInfoWidget*) sender();
//name
connect(p_name_input, SIGNAL(sendName(QString)), sender(), SLOT(setName(QString)), Qt::UniqueConnection);
connect(p_name_input, SIGNAL(sendName(QString)), SLOT(processRefreshScrollArea()), Qt::UniqueConnection);
//password
connect(p_password_input, SIGNAL(sendPassword(QString)), sender(), SLOT(setPassword(QString)), Qt::UniqueConnection);
//note
connect(p_note_input, SIGNAL(sendNote(QString)), sender(), SLOT(setNote(QString)), Qt::UniqueConnection);
connect(p_name_input, SIGNAL(sendName(QString)), SLOT(destroyEditConnections()), Qt::UniqueConnection);
connect(p_password_input, SIGNAL(sendPassword(QString)), SLOT(destroyEditConnections()), Qt::UniqueConnection);
connect(p_note_input, SIGNAL(sendNote(QString)), SLOT(destroyEditConnections()), Qt::UniqueConnection);
p_name_input->p_line_edit->setText(p_sender->name);
p_name_input->checkText();
p_password_input->p_line_edit->setText(p_sender->password);
p_note_input->p_text_edit->setText(p_sender->note);
p_name_input->p_next_button->setText("Ok");
p_password_input->p_next_button->setText("Ok");
p_note_input->p_next_button->setText("Ok");
}
void MainWindow::destroyEditConnections()
{
qDebug() << "destroy edit connections";
ElementInfoWidget *p_sender = (ElementInfoWidget*) sender();
//name
disconnect(p_name_input, SIGNAL(sendName(QString)), sender(), SLOT(setName(QString)));
//password
disconnect(p_password_input, SIGNAL(sendPassword(QString)), sender(), SLOT(setPassword(QString)));
//note
disconnect(p_note_input, SIGNAL(sendNote(QString)), sender(), SLOT(setNote(QString)));
disconnect(p_name_input, SIGNAL(sendName(QString)), this, SLOT(destroyEditConnections()));
disconnect(p_password_input, SIGNAL(sendPassword(QString)), this, SLOT(destroyEditConnections()));
disconnect(p_note_input, SIGNAL(sendNote(QString)), this, SLOT(destroyEditConnections()));
processElementEdited(p_sender);
clearInput();
p_name_input->p_next_button->setText("Next");
p_password_input->p_next_button->setText("Next");
p_note_input->p_next_button->setText("Next");
}
void MainWindow::processElementEdited(ElementInfoWidget *p_widget)
{
qDebug() << "element edited : " << p_widget->objectName();
QString name, password, note;
name = p_widget->name;
password = p_widget->password;
note = p_widget->note;
removeElement(p_widget);
core.addElement(name, password, note);
core.writeFile();
setElementInfoWidget(createNewInfoLabel(name, password, note));
processRefreshScrollArea();
}
void MainWindow::clearElementInfoWidget()
{
qDebug() << "show empty widget";
p_stacked_info_widget->setCurrentWidget(p_null_widget);
}
void MainWindow::setElementInfoWidget(ElementInfoWidget *p_widget)
{
qDebug() << "set current widget : " + p_widget->objectName();
p_stacked_info_widget->setCurrentWidget(p_widget);
}
void MainWindow::showAuthorWidget()
{
qDebug() << "show author widget";
p_stacked_info_widget->setCurrentWidget(p_author_widget);
}
ElementInfoWidget * MainWindow::createNewInfoLabel(QString name, QString password, QString note)
{
qDebug() << "create new info label";
ElementInfoWidget *p_new_widget = new ElementInfoWidget;
ElementButton *p_new_button = new ElementButton;
p_new_widget->setPairButton(p_new_button);
p_new_button->setPairWidget(p_new_widget);
p_new_widget->setName(name);
p_new_widget->setPassword(password);
p_new_widget->setNote(note);
p_new_widget->setObjectName(name);
p_stacked_info_widget->addWidget(p_new_widget);
element_buttons_list << p_new_button;
connect(p_new_button, SIGNAL(sendPairWidget(ElementInfoWidget*)), SLOT(setElementInfoWidget(ElementInfoWidget*)));
connect(p_new_widget, SIGNAL(nameEditButtonClicked()), SLOT(processTakeName()));
connect(p_new_widget, SIGNAL(nameEditButtonClicked()), SLOT(createEditConnections()));
connect(p_new_widget, SIGNAL(passwordEditButtonClicked()), SLOT(processTakePassword()));
connect(p_new_widget, SIGNAL(passwordEditButtonClicked()), SLOT(createEditConnections()));
connect(p_new_widget, SIGNAL(noteEditButtonClicked()), SLOT(processTakeNote()));
connect(p_new_widget, SIGNAL(noteEditButtonClicked()), SLOT(createEditConnections()));
connect(p_new_widget, SIGNAL(nameEdited()), SLOT(destroyEditConnections()));
connect(p_new_widget, SIGNAL(passwordEdited()), SLOT(destroyEditConnections()));
connect(p_new_widget, SIGNAL(noteEdited()), SLOT(destroyEditConnections()));
connect(p_new_widget, SIGNAL(removeButtonClicked()), SLOT(processRemoveElement()));
return p_new_widget;
}
void MainWindow::copyToNameBuffer(QString string)
{
qDebug() << "copy name " << string;
name_buffer = string;
}
void MainWindow::copyToPasswordBuffer(QString string)
{
qDebug() << "copy password " << string;
password_buffer = string;
}
void MainWindow::copyToNoteBuffer(QString string)
{
qDebug() << "copy note " << string;
note_buffer = string;
}
void MainWindow::setOtherWidgetsEabled(bool boolean)
{
qDebug() << "set widgets enabled " << boolean;
p_add_button->setEnabled(boolean);
p_wipe_button->setEnabled(boolean);
p_scroll_area->setEnabled(boolean);
p_author_button->setEnabled(boolean);
}
void MainWindow::lockOtherWidgets()
{
qDebug() << "lock other widgets";
setOtherWidgetsEabled(false);
}
void MainWindow::unlockOtherWidgets()
{
qDebug() << "unlock other widgets";
setOtherWidgetsEabled(true);
}
void MainWindow::processReadElementsFromFile()
{
qDebug() << "read elements from file";
QString name, password, note;
core.readFile();
for(core.iter = core.elements.begin(); core.iter != core.elements.end();)
{
name = core.iter.value();
core.iter++;
password = core.iter.value();
core.iter++;
note = core.iter.value();
core.iter++;
processCreateNewElement(name, password, note);
}
}