-
Notifications
You must be signed in to change notification settings - Fork 0
/
quickeditor.cpp
207 lines (165 loc) · 7.07 KB
/
quickeditor.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
#include "quickeditor.h"
#include "reader.h"
#include "editor.h"
void QuickEditor::makeUI(QWidget* parent,
ConfigMan* cfgMan,
QString stackPath,
QJsonObject* stackObj,
int i) {
this->setWindowTitle("Go to flashcard");
this->setWindowIcon(parent->windowIcon());
this->setStyleSheet(parent->styleSheet());
// UI
QVBoxLayout* mainLayout = new QVBoxLayout(this);
QHBoxLayout* toolbarLayout = new QHBoxLayout();
QPushButton* boldButton = new QPushButton("B");
boldButton->setObjectName("boldButton");
boldButton->setFixedWidth(24);
toolbarLayout->addWidget(boldButton);
QPushButton* italicButton = new QPushButton("I");
italicButton->setObjectName("italicButton");
italicButton->setFixedWidth(24);
toolbarLayout->addWidget(italicButton);
QPushButton* underlineButton = new QPushButton("U");
underlineButton->setObjectName("underlineButton");
underlineButton->setFixedWidth(24);
toolbarLayout->addWidget(underlineButton);
QPushButton* setTextColorButton = new QPushButton();
setTextColorButton->setIcon(QIcon(":/icons/color-wheel.png"));
toolbarLayout->addWidget(setTextColorButton);
QPushButton* clearStyleButton = new QPushButton();
clearStyleButton->setIcon(QIcon(":/icons/edit-clear.png"));
toolbarLayout->addWidget(clearStyleButton);
toolbarLayout->addSpacerItem(new QSpacerItem(0, 0,
QSizePolicy::MinimumExpanding,
QSizePolicy::Ignored));
mainLayout->addLayout(toolbarLayout);
QHBoxLayout* flashcardLayout = new QHBoxLayout();
QTextEdit* frontTextEdit = new QTextEdit();
QFont font(cfgMan->mFontFamily, 13);
frontTextEdit->setFont(font);
flashcardLayout->addWidget(frontTextEdit);
QTextEdit* backTextEdit = new QTextEdit();
backTextEdit->setFont(font);
flashcardLayout->addWidget(backTextEdit);
mainLayout->addLayout(flashcardLayout);
QHBoxLayout* buttonsLayout = new QHBoxLayout();
buttonsLayout->addSpacerItem(new QSpacerItem(0, 0,
QSizePolicy::MinimumExpanding,
QSizePolicy::Ignored));
QPushButton* cancelButton = new QPushButton("Cancel");
buttonsLayout->addWidget(cancelButton);
QPushButton* saveButton = new QPushButton("Save");
buttonsLayout->addWidget(saveButton);
mainLayout->addLayout(buttonsLayout);
// Set contents
QString front = stackObj->operator[](QString::number(i)).toObject()["f"].toString();
frontTextEdit->setHtml(front);
QString back = stackObj->operator[](QString::number(i)).toObject()["b"].toString();
backTextEdit->setHtml(back);
// Making connections
connect(boldButton, &QPushButton::clicked, this, [frontTextEdit, backTextEdit]() {
Editor::bold(frontTextEdit);
Editor::bold(backTextEdit);
});
connect(italicButton, &QPushButton::clicked, this, [frontTextEdit, backTextEdit]() {
Editor::italic(frontTextEdit);
Editor::italic(backTextEdit);
});
connect(underlineButton, &QPushButton::clicked, this, [frontTextEdit, backTextEdit]() {
Editor::underline(frontTextEdit);
Editor::underline(backTextEdit);
});
connect(setTextColorButton, &QPushButton::clicked, this, [frontTextEdit,
backTextEdit,
parent]() {
Editor::setColor(frontTextEdit,
static_cast<Reader*>(parent)->mCfgMan,
static_cast<Reader*>(parent)->mCfgDir);
Editor::setColor(backTextEdit,
static_cast<Reader*>(parent)->mCfgMan,
static_cast<Reader*>(parent)->mCfgDir);
});
connect(clearStyleButton, &QPushButton::clicked, this, [frontTextEdit, backTextEdit]() {
Editor::clearStyle(frontTextEdit);
Editor::clearStyle(backTextEdit);
});
connect(frontTextEdit, &QTextEdit::textChanged, this, [this]() {
mChanged = true;
});
connect(backTextEdit, &QTextEdit::textChanged, this, [this]() {
mChanged = true;
});
connect(cancelButton, &QPushButton::clicked, this, [this]() {
if (mChanged) {
QMessageBox msgBox(this);
msgBox.setWindowTitle("There are unsaved changes");
msgBox.setText("Do you want to revert changes you made to this flashcard?");
msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel);
msgBox.setIcon(QMessageBox::Question);
if (msgBox.exec() == QMessageBox::Yes) {
this->hide();
delete this;
}
}
else {
this->hide();
delete this;
}
});
connect(saveButton, &QPushButton::clicked, this, [this, stackObj, i,
frontTextEdit, backTextEdit,
stackPath, parent]() {
QJsonObject flashcardObj;
QString frontHtml = frontTextEdit->toHtml();
flashcardObj["f"] = QJsonValue(frontHtml);
QString backHtml = backTextEdit->toHtml();
flashcardObj["b"] = QJsonValue(backHtml);
stackObj->operator[](QString::number(i)) = QJsonValue(flashcardObj);
QJsonDocument doc(*stackObj);
QFile file(stackPath);
file.open(QFile::WriteOnly);
file.write(doc.toJson(QJsonDocument::Indented));
file.close();
bool isTurnedOver = static_cast<Reader*>(parent)->mTurnedOver;
static_cast<Reader*>(parent)->loadFlashcard(i, isTurnedOver);
this->hide();
delete this;
});
}
void QuickEditor::closeEvent(QCloseEvent* event) {
if (mChanged) {
QMessageBox msgBox(this);
msgBox.setWindowTitle("There are unsaved changes");
msgBox.setText("Do you want to revert changes you made to this flashcard?");
msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel);
msgBox.setIcon(QMessageBox::Question);
if (msgBox.exec() == QMessageBox::Yes) {
event->accept();
}
else {
event->ignore();
}
}
else {
event->accept();
}
}
void QuickEditor::setWindowGeometry() {
int width = 800, height = 330;
QScreen* screen = qApp->primaryScreen();
QRect screenGeometry = screen->geometry();
int x = (screenGeometry.width() - width) / 2;
int y = (screenGeometry.height() - height) / 2;
this->setGeometry(x, y, width, height);
this->setFixedSize(width, height);
}
QuickEditor::QuickEditor(QWidget* parent,
ConfigMan* cfgMan,
QString stackPath,
QJsonObject* stackObj,
int i) {
this->setObjectName("quickEditor");
makeUI(parent, cfgMan, stackPath, stackObj, i);
setWindowGeometry();
}