This repository has been archived by the owner on Nov 22, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MemorizeEdit.cpp
177 lines (138 loc) · 4.86 KB
/
MemorizeEdit.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
// SPDX-FileCopyrightText: 2020 Loren Burkholder <computersemiexpert@outlook.com
// SPDX-License-Identifier: BSD-2-Clause
#include "MemorizeEdit.h"
#include <string>
#include <QSettings>
#include <QRandomGenerator>
MemorizeEdit::MemorizeEdit(QString &memorizeContent, Difficulty difficulty, QWidget *parent)
: QTextEdit{ parent },
m_difficulty{ difficulty }
{
QSettings settings;
settings.beginGroup("MemorizeEdit");
// the static_cast is necessary here
m_errorAction = static_cast<ErrorAction>(settings.value("errorAction", ErrorAction::Redo).toInt());
setAcceptRichText(true);
setTabChangesFocus(true);
setReadOnly(true);
// add a space after each newline so that words break appropriately at newlines (e.g. "word1\n" "word2"
// instead of "word1\nword2") and also change the "\n" to a "<br>" to play right with the HTML text
// std::string is the easiest way to do this
auto temp = memorizeContent.toStdString();
for (std::string::size_type pos = temp.find("\n", 0); pos != std::string::npos; pos = temp.find("\n", pos))
temp.replace(pos, 1, "<br> ");
QString newMemContent{ temp.c_str() };
m_words = newMemContent.split(" ", QString::SplitBehavior::SkipEmptyParts);
m_numWords = m_words.size();
setHtml(formattedEndString(m_difficulty));
settings.endGroup(); // MemorizeEdit
}
void MemorizeEdit::keyPressEvent(QKeyEvent *event)
{
// here we take care of keys that are expected to do something (like Ctrl-A: select all)
if (event->key() == Qt::Key::Key_A && event->modifiers() == Qt::ControlModifier)
this->selectAll();
// provide a hint
else if (event->key() == Qt::Key::Key_Question)
{
// Make sure that we don't put a space after a newline.
m_richText += "<span style=\"color:red;\">" + m_words[0];
if (m_richText.at(m_richText.length() - 1) != "\n")
m_richText += " ";
m_richText += "</span>";
// Delete this word now so that calculations don't get messed up in formattedEndString()
m_words.pop_front();
setHtml(m_richText + formattedEndString(m_difficulty));
emit messageToUser("Hint provided.");
}
// skip all keys but the letters and numbers
else if (!((event->key() >= 0x30 && event->key() <= 0x39) || (event->key() >= 0x41 && event->key() <= 0x5a)))
return;
// make sure there's still something to take care of
else if (m_words.length() > 0)
{
// Get the first letter of the word. This is designed to prevent one from
// having to input punctuation (e.g. for the string "...but" type "b"
// instead of ".")
QChar firstChar;
for (int i = 0; i < m_words[0].length(); ++i)
{
if (m_words[0][i].isLetterOrNumber())
{
firstChar = m_words[0][i];
break;
}
}
// Was the correct letter typed?
if (event->key() == firstChar.toUpper().unicode())
{
m_richText += m_words[0];
if (m_richText.at(m_richText.length() - 1) != "\n")
m_richText += " ";
// Delete this word now so that calculations don't get messed up in formattedEndString()
m_words.pop_front();
setHtml(m_richText + formattedEndString(m_difficulty));
emit messageToUser("");
}
else
{
switch (m_errorAction)
{
case ErrorAction::Redo:
emit messageToUser(tr("Oops, you messed up! Try again."));
break;
case ErrorAction::KeepGoing:
m_richText += "<span style=\"color:red;\">" + m_words[0];
if (m_richText.at(m_richText.length() - 1) != "\n")
m_richText += " ";
m_richText += "</span>";
m_words.pop_front();
setHtml(m_richText + formattedEndString(m_difficulty));
emit messageToUser(tr("Oops, you messed up!"));
break;
default:
break;
}
}
}
// we're done with the memorization, clean up and shut down
if (m_words.length() < 1)
{
// remove the "\n" at the end so that the box doesn't scroll down and hide some text
if (m_richText.endsWith("<br> "))
this->setHtml(m_richText.remove(m_richText.length() - 5, 5));
else if (m_richText.endsWith("<br> </span>"))
this->setHtml(m_richText.remove(m_richText.length() - 12, 12));
emit messageToUser(tr("Done!"));
emit done();
}
}
QString MemorizeEdit::formattedEndString(Difficulty difficulty)
{
QString formatted;
int numWordsUsed = 0;
// static because we don't want to switch which words are displayed halfway through memorization
static int mediumDifficultyOffset = QRandomGenerator::global()->generate() % 2;
switch (difficulty)
{
case Difficulty::Easy:
formatted = "<span style=\"color:#8b8b8b;\">" + m_words.join(" ") + "</span>";
break;
case Difficulty::Medium:
numWordsUsed = m_numWords - m_words.length();
for (int i = numWordsUsed; i < m_numWords; ++i)
{
if (i % 2 == mediumDifficultyOffset)
formatted += "<span style=\"color:#8b8b8b;\">" + m_words[i - numWordsUsed] + " </span>";
else
formatted += "<span style=\"color:#00000000;\">" + m_words[i - numWordsUsed] + " </span>";
}
break;
case Difficulty::Hard:
formatted = "";
break;
default:
break;
}
return formatted;
}