-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwordprocessor.cpp
More file actions
348 lines (301 loc) · 7.21 KB
/
wordprocessor.cpp
File metadata and controls
348 lines (301 loc) · 7.21 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
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
#include "wordprocessor.h"
#include <QMessageBox>
#include <QFile>
#include <QTextStream>
#include <QDebug>
#include <set>
#include <stdlib.h> /* srand, rand */
#include <time.h> /* time */
wordProcessor::wordProcessor()
{
_separate.insert(TABULATOR, "\t");
_separate.insert(OLDWAY, " - ");
_separate.insert(COLON, ":");
_separate.insert(EQUAL, "=");
QString fileName = "words.txt";
openFile(fileName);
init();
_switched = false;
}
bool wordProcessor::checkAnswer(QString string)
{
bool ret = false;
int indexX, indexY;
// first word will be asked (english)
if(!_switched)
{
indexX = _askedIndex;
indexY = 1; // hungarian
}
else
{
indexX = _askedIndex;
indexY = 0; // english
// second word will be asked (hungarian)
}
_prevAnswer.clear();
QStringList fields = _words[indexX][indexY].split(",");
string.replace(" ","");
qDebug() << string;
for(int i = 0; i < fields.size(); ++i)
{
QString str = fields[i];
_prevAnswer << str;
qDebug() << str;
str.replace(" ", "");
ret = (QString::compare(string,str, Qt::CaseInsensitive) == 0);
if(ret)
{
break;
}
}
qDebug() << ((ret == true)?"true":"false");
qDebug() << "--------------";
if(ret && _actualIndex <= _words.size())
{
++_goodAnswer;
}
else if(_actualIndex <= _words.size())
{
++_badAnswer;
_wrongWords.push_back(_words[indexX]);
}
return ret;
}
int wordProcessor::goodAnswer() const
{
return _goodAnswer;
}
void wordProcessor::setGoodAnswer(int goodAnswer)
{
_goodAnswer = goodAnswer;
}
int wordProcessor::badAnswer() const
{
return _badAnswer;
}
void wordProcessor::setBadAnswer(int badAnswer)
{
_badAnswer = badAnswer;
}
QString wordProcessor::getNewWord()
{
QString word = "no words";
int indexX, indexY;
// first word will be asked (english)
if(!_switched)
{
indexX = _actualIndex;
indexY = 0;
}
else
{
// second word will be asked (hungarian)
indexX = _actualIndex;
indexY = 1;
}
// english
if(_actualIndex < _words.size())
{
_askedIndex = _actualIndex;
++_actualIndex;
word = _words[indexX][indexY];
}
else if(0 >= _words.size())
{
QMessageBox mb(QMessageBox::Critical,
"ERROR",
"There is no word!");
mb.setStandardButtons(QMessageBox::Ok);
mb.setIcon(QMessageBox::Critical);
mb.exec();
}
return word;
}
int wordProcessor::getAskedIndex() const
{
return _actualIndex;
/*
if(_askedIndex <= 0)
{
return 0;
}
else if(_askedIndex >= _words.size())
{
return _words.size();
}
return _askedIndex;*/
}
void wordProcessor::init()
{
_actualIndex = 0;
_badAnswer = 0;
_goodAnswer = 0;
_allWords = _words.size();
_askedIndex = 0;
std::srand(time(0));
std::random_shuffle(_words.begin(),_words.end());
_wrongWords.clear();
}
int wordProcessor::getAllWords() const
{
return _allWords;
}
QStringList wordProcessor::getPrevAnswer() const
{
return _prevAnswer;
}
QStringList wordProcessor::getAnswer(int arrayIndex) const
{
QStringList words;
words << "no answer";
int indexX, indexY;
if(!(arrayIndex >= _words.size()) && (arrayIndex >= 0))
{
words.clear();
// answer (hungarian)
if(!_switched)
{
indexX = arrayIndex;
indexY = 1;
}
else
{
// if hungarian asked then the answer in english
indexX = arrayIndex;
indexY = 0;
}
words = _words[indexX][indexY].split(",");
}
else
{
QMessageBox mb(QMessageBox::Critical,
"ERROR",
"There is no word!");
mb.setStandardButtons(QMessageBox::Ok);
mb.setIcon(QMessageBox::Critical);
mb.exec();
}
return words;
}
bool wordProcessor::openFile(QString fileName)
{
bool ret = false;
_words.clear();
/*QString path = QCoreApplication::applicationDirPath(); Qt::current
path.append("/words.txt");*/
QFile file(fileName);
if(!file.open(QIODevice::ReadOnly)) {
QMessageBox::information(0, "no file?", file.errorString());
QString line = "no words\tnincs szó";
QStringList fields = line.split("\t");
//model->appendRow(fields);
_words.push_back(fields);
ret = false;
}
QTextStream in(&file);
in.setCodec("UTF-8");
while(!in.atEnd()) {
QString line = in.readLine();
QStringList fields = line.split(_separate[EQUAL]);
if(2 <= fields.size())
{
_words.push_back(fields);
}
else
{
qDebug() << "Hibás szó:";
qDebug() << fields;
}
//model->appendRow(fields);
}
qDebug() << "words size: " << _words.size();
if(_words.size() > 0)
{
ret = true;
}
file.close();
return ret;
}
bool wordProcessor::saveFile(QString fileName, bool append)
{
bool ret = false;
QFile file(fileName);
if(append)
{
file.open(QIODevice::WriteOnly | QIODevice::Append | QIODevice::Text);
}
else
{
file.open(QIODevice::WriteOnly | QIODevice::Text);
}
if(!file.isOpen()){
qDebug() << "- Error, unable to open" << "outputFilename" << "for output";
return false;
}
//QDataStream outStream(&file);
QTextStream outStream(&file);
/*
for(QStringList sl : _wrongWords)
{
for(QString s : sl)
{
outStream << s;
}
}*/
for(int i = 0; i < _wrongWords.size(); ++i)
{
qDebug() << "export line: " << i;
outStream << _wrongWords[i][0];
outStream << _separate[TABULATOR];
outStream << _wrongWords[i][1];
outStream << "\n";
}
file.close();
return true;
}
bool wordProcessor::getSwitch()
{
return _switched;
}
bool wordProcessor::getWrongAnswersOnly(bool append)
{
bool ret = false;
qDebug() << "wrong words size: " << _wrongWords.size();
if(0 >= _wrongWords.size())
{
QMessageBox mb(QMessageBox::Critical,
"No word",
"There is no wrong word!\nNoice!");
mb.setStandardButtons(QMessageBox::Ok);
mb.setIcon(QMessageBox::Information);
mb.exec();
}
else
{
ret = true;
if(!append)
{
_words.clear();
}
for(QStringList s : _wrongWords)
{
_words.push_back(s);
}
/*std::set<QStringList> s( _words.begin(), _words.end() );
_words.assign( s.begin(), s.end() );*/
/*
std::set<QStringList> s;
unsigned size = _words.size();
for( unsigned i = 0; i < size; ++i ) s.insert( _words[i] );
_words.assign( s.begin(), s.end() );*/
}
return ret;
}
void wordProcessor::switchLanguage()
{
_switched = !_switched;
qDebug() << "--------------";
qDebug() << ((_switched == true)?"true":"false");
qDebug() << "--------------";
}