-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
133 lines (124 loc) · 4.78 KB
/
mainwindow.cpp
File metadata and controls
133 lines (124 loc) · 4.78 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
#include "mainwindow.h"
MainWindow::MainWindow(Data& data, QWidget *parent)
: QWidget(parent), isRemoveState(false), isThemeState(false),
data(data) {
layout = new QVBoxLayout(this);
hLayout = new QHBoxLayout(layout->widget());
exit = new QPushButton("Exit", this);
add = new QPushButton("Add", this);
remove = new QPushButton("Remove", this);
hLayout->addWidget(exit);
hLayout->addWidget(add);
hLayout->addWidget(remove);
layout->addLayout(hLayout);
connect(remove, &QPushButton::clicked, [this](){
if(isRemoveState) {
remove->setText("Remove");
} else {
remove->setText("Back");
}
isRemoveState = !isRemoveState;
});
connect(&createThemeScreen, &CreateThemeScreen::createTheme, this, &MainWindow::addTheme);
connect(&createNoteScreen, &CreateNoteScreen::createNote, this, &MainWindow::addNote);
connect(¬eEditor, &NoteEditor::changeNote, [=](string text) {
this->data.getTheme(themeName).getNote(noteName).setText(text);
cout << text << endl;
});
}
void MainWindow::loginAndShow(string password) {
this->password = password;
toMainWindowState();
show();
}
void MainWindow::buttonPushed() {
auto sender = this->sender();
for(auto button = buttons.begin(); button != buttons.end(); ++button) {
if(*button == sender) {
if(isRemoveState) {
auto remBntName = (*button)->text().toStdString();
if(isThemeState) {
data.getTheme(themeName).removeNote(remBntName);
} else {
data.removeTheme(remBntName);
}
isRemoveState = false;
remove->setText("Remove");
recreateButtons((isThemeState)? data.getTheme(themeName).getNotesNames(password)
: data.getThemesList(password));
return;
} else {
if(!isThemeState) {
toThemeState((*button)->text().toStdString());
return;
} else {
this->noteName = (*button)->text().toStdString();
noteEditor.openNote(data.getTheme(themeName).getNote(noteName), password);
}
}
}
}
}
void MainWindow::toThemeState(string themeName) {
this->themeName = themeName;
exit->setText("Back");
disconnect(exit, &QPushButton::clicked, this, &QWidget::close);
disconnect(add, &QPushButton::clicked, &createThemeScreen, &QWidget::show);
connect(exit, &QPushButton::clicked, this, &MainWindow::toMainWindowState);
connect(add, &QPushButton::clicked, &createNoteScreen, &QWidget::show);
isThemeState = true;
recreateButtons(data.getTheme(themeName).getNotesNames(password));
}
void MainWindow::toMainWindowState() {
if(isThemeState) {
disconnect(exit, &QPushButton::clicked, this, &MainWindow::toMainWindowState);
disconnect(add, &QPushButton::clicked, &createNoteScreen, &QWidget::show);
isThemeState = false;
}
connect(exit, &QPushButton::clicked, this, &QWidget::close);
connect(add, &QPushButton::clicked, &createThemeScreen, &QWidget::show);
exit->setText("Exit");
recreateButtons(data.getThemesList(password));
}
void MainWindow::recreateButtons(vector<string> buttonsNames) {
for(auto& button : buttons) {
layout->removeWidget(button);
button->deleteLater();
}
buttons.clear();
for(auto& themeName : buttonsNames) {
buttons.push_back(new QPushButton(QString(themeName.data()), this));
}
for(auto& button : buttons) {
layout->addWidget(button);
connect(button, &QPushButton::clicked, this, &MainWindow::buttonPushed);
}
}
MainWindow::~MainWindow() {
}
void MainWindow::addTheme(Theme theme) {
auto themeName = theme.getName(password);
for(auto& name : data.getThemesList(password)) { //All themes have unique name
if(name == themeName) {
cerr << "It's not unique name for theme" << endl;
return;
}
}
data.addTheme(theme);
recreateButtons(data.getThemesList(password));
createThemeScreen.close();
//Save to file...
}
void MainWindow::addNote(Note note) {
auto noteName = note.getName(password);
for(auto& name : data.getTheme(themeName).getNotesNames(password)) { //All themes have unique name
if(name == noteName) {
cerr << "It's not unique name for note" << endl;
return;
}
}
data.getTheme(themeName).addNote(note);
recreateButtons(data.getTheme(themeName).getNotesNames(password));
createNoteScreen.close();
//Save to file
}