-
Notifications
You must be signed in to change notification settings - Fork 0
/
reader.cpp
277 lines (226 loc) · 8.45 KB
/
reader.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
#include "reader.h"
#include "gotoflashcarddialog.h"
#include "quickeditor.h"
QString Reader::getStackName(QString stackPath) {
QStringList path = stackPath.split('/');
QString name = path.last();
name.chop(5);
return name;
}
void Reader::makeUI(QWidget* parent, QApplication* app, QString stackPath) {
// UI
this->setWindowIcon(parent->windowIcon());
QString stackName = getStackName(stackPath);
this->setWindowTitle(stackName);
QFont font;
font.setPointSize(11);
this->setFont(font);
this->setStyleSheet(parent->styleSheet());
qDebug() << parent->styleSheet();
QVBoxLayout* mainLayout = new QVBoxLayout(this);
QLabel* stackNameLabel = new QLabel(stackName);
stackNameLabel->setObjectName("stackName");
mainLayout->addWidget(stackNameLabel);
QHBoxLayout* flashcardLayout = new QHBoxLayout();
mBackButton = new QPushButton();
mBackButton->setIcon(QIcon(":/icons/go-previous.png"));
mBackButton->setIconSize(QSize(32, 32));
mBackButton->setMinimumWidth(40);
mBackButton->setMaximumWidth(40);
mBackButton->setEnabled(false);
flashcardLayout->addWidget(mBackButton);
mFlashcardView = new QTextEdit();
mFlashcardView->setObjectName("flashcard");
mFlashcardView->setFontFamily(mCfgMan->mFontFamily);
mFlashcardView->setReadOnly(true);
mFlashcardView->setMinimumSize(437, 310); // A6 minimum pixel size
mFlashcardView->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
flashcardLayout->addWidget(mFlashcardView);
mForwardButton = new QPushButton();
mForwardButton->setIcon(QIcon(":/icons/go-next.png"));
mForwardButton->setIconSize(QSize(32, 32));
mForwardButton->setMinimumWidth(40);
mForwardButton->setMaximumWidth(40);
mForwardButton->setEnabled(false);
flashcardLayout->addWidget(mForwardButton);
mainLayout->addLayout(flashcardLayout);
QHBoxLayout* toolbarLayout = new QHBoxLayout();
mTurnOverButton = new QPushButton("Turn over");
toolbarLayout->addWidget(mTurnOverButton);
mQuickEditButton = new QPushButton("Quick edit");
toolbarLayout->addWidget(mQuickEditButton);
toolbarLayout->addSpacerItem(new QSpacerItem(0, 0,
QSizePolicy::MinimumExpanding,
QSizePolicy::Ignored));
mProgressBar = new QProgressBar();
mProgressBar->setFormat("");
toolbarLayout->addWidget(mProgressBar);
mProgressButton = new QPushButton("0/0");
toolbarLayout->addWidget(mProgressButton);
mainLayout->addLayout(toolbarLayout);
// Geometry
int width = 800, height = 600;
QScreen* screen = app->primaryScreen();
QRect geometry = screen->geometry();
int x = (geometry.width() - width) / 2;
int y = (geometry.height() - height) / 2;
this->setGeometry(x, y, width, height);
// Make connections
connect(mBackButton, &QPushButton::clicked, this, [this]() {
previous();
});
connect(mForwardButton, &QPushButton::clicked, this, [this]() {
next();
});
connect(mTurnOverButton, &QPushButton::clicked, this, [this]() {
turnOver();
});
connect(mQuickEditButton, &QPushButton::clicked, this, [this, stackPath]() {
QuickEditor* quickEditor = new QuickEditor(this,
mCfgMan,
stackPath,
&mStackObj,
mCurrentIndex);
quickEditor->show();
});
connect(mProgressButton, &QPushButton::clicked, this, [this]() {
auto goToFlashcardDialog = new GoToFlashcardDialog(this, mStackObj["count"].toInt());
goToFlashcardDialog->show();
});
}
void Reader::readStack(QString stackPath) {
QFile stackFile(stackPath);
stackFile.open(QIODevice::ReadOnly | QIODevice::Text);
QString data = stackFile.readAll();
stackFile.close();
mStackObj = QJsonDocument::fromJson(data.toUtf8()).object();
}
void Reader::loadFlashcard(int i, bool turnOver) {
QJsonObject flashcard = mStackObj[QString::number(i)].toObject();
QString text;
if (!turnOver) {
text = flashcard["f"].toString();
}
else {
text = flashcard["b"].toString();
}
text = text.replace("font-size:13pt;", "");
mText = QTextDocumentFragment::fromHtml(text).toPlainText();
//mText = mText.replace("\n", "<br>");
int ptSize = getMaximumFontSize(mText, mFlashcardView);
mFlashcardView->setText(text);
mFlashcardView->setFont(QFont(mCfgMan->mFontFamily, ptSize));
int margin = (mFlashcardView->height() - getTextHeight(mText, ptSize)) / 2;
QTextFrame* frame = mFlashcardView->document()->rootFrame();
QTextFrameFormat format = frame->frameFormat();
format.setTopMargin(margin);
format.setBottomMargin(margin);
frame->setFrameFormat(format);
mCurrentIndex = i;
setProgress();
if (i == 1) {
mBackButton->setEnabled(false);
}
else {
mBackButton->setEnabled(true);
}
if (i == mStackObj["count"].toInt()) {
mForwardButton->setEnabled(false);
}
else {
mForwardButton->setEnabled(true);
}
}
int Reader::getMaximumFontSize(QString text, QWidget* flashcardView) {
//text = text.replace("<br>", "\n");
//text = QTextDocumentFragment::fromHtml(text).toPlainText();
//text = text.replace("\n", "<br>");
//qDebug() << text;
int fontSize = 36;
QFont font;
font.setFamily(mCfgMan->mFontFamily);
font.setPointSize(48);
QFontMetrics fontMetrics(font);
// Find the longest line
QString maxLine;
foreach (QString line, text.split("\n")) {
if (fontMetrics.horizontalAdvance(line) >
fontMetrics.horizontalAdvance(maxLine)) {
maxLine = line;
}
}
// Getting maximum possible font size for current size of QTextEdit
while ((fontMetrics.horizontalAdvance(maxLine) >= mFlashcardView->width() - 50) ||
(getTextHeight(text, fontSize) >= mFlashcardView->height() - 50)) {
--fontSize;
font.setPointSize(fontSize);
fontMetrics = QFontMetrics(font);
}
return fontSize;
}
int Reader::getTextHeight(QString text, int ptSize) {
QFontMetrics fontMetrics = QFontMetrics(QFont(mCfgMan->mFontFamily, ptSize));
int lineHeight = fontMetrics.height();
int countLines = text.count("\n") + 1;
return (lineHeight * countLines);
}
void Reader::setProgress() {
int percentage = mCurrentIndex * 100 / mStackObj["count"].toInt();
QString progress = QString("%1/%2").arg(QString::number(mCurrentIndex),
QString::number(mStackObj["count"].toInt()));
mProgressBar->setValue(percentage);
mProgressButton->setText(progress);
}
void Reader::previous() {
loadFlashcard(mCurrentIndex - 1, false);
mTurnedOver = false;
}
void Reader::next() {
loadFlashcard(mCurrentIndex + 1, false);
mTurnedOver = false;
}
void Reader::turnOver() {
loadFlashcard(mCurrentIndex, !mTurnedOver);
mTurnedOver = !mTurnedOver;
}
void Reader::resizeEvent(QResizeEvent* event) {
int ptSize = getMaximumFontSize(mText, mFlashcardView);
mFlashcardView->setFont(QFont(mCfgMan->mFontFamily, ptSize));
int margin = (mFlashcardView->height() - getTextHeight(mText, ptSize)) / 2;
QTextFrame* frame = mFlashcardView->document()->rootFrame();
QTextFrameFormat format = frame->frameFormat();
format.setTopMargin(margin);
format.setBottomMargin(margin);
frame->setFrameFormat(format);
event->accept();
}
Reader::Reader(QWidget* parent,
QApplication* app,
QString stackPath,
ConfigMan* cfgMan,
QString cfgDir) : QWidget(nullptr) {
this->setObjectName("reader");
mParent = parent;
mCfgMan = cfgMan;
mCfgDir = cfgDir;
makeUI(parent, app, stackPath);
readStack(stackPath);
if (mStackObj["count"].toInt() != 0) {
mProgressButton->setEnabled(true);
mTurnOverButton->setEnabled(true);
mQuickEditButton->setEnabled(true);
loadFlashcard(1, false);
}
else {
mFlashcardView->setHtml("<p style=\"text-align: center\"><b>Empty.</b></p>");
mProgressButton->setEnabled(false);
mTurnOverButton->setEnabled(false);
mQuickEditButton->setEnabled(false);
}
}
void Reader::closeEvent(QCloseEvent* event) {
mParent->show();
event->accept();
}
Reader::~Reader() {
}