-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainwindow.cpp
537 lines (463 loc) · 18.2 KB
/
mainwindow.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
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
#include <QString>
#include <QFileDialog>
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "svfileops.h"
#include "cardpack.h"
#include "flashcardwindow.h"
#include "popupwindow.h"
#include "databasewindow.h"
#include "statswindow.h"
#include <QCloseEvent>
#include <QSqlDatabase>
#include <QSqlError>
#include <QSqlQuery>
#include <QDomDocument>
#include <QTextStream>
#include "fmlhandler.h"
#include <QSettings>
#include "preferenceswindow.h"
#include "flashkardtrayicon.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow),
testingFlag(false),
trayIcon(this)
{
ui->setupUi(this);
connect(ui->actionRecent1,SIGNAL(triggered()),this,SLOT(loadRecent()));
connect(ui->actionRecent2,SIGNAL(triggered()),this,SLOT(loadRecent()));
connect(ui->actionRecent3,SIGNAL(triggered()),this,SLOT(loadRecent()));
connect(ui->actionRecent4,SIGNAL(triggered()),this,SLOT(loadRecent()));
connect(ui->actionRecent5,SIGNAL(triggered()),this,SLOT(loadRecent()));
trayIcon.setIcon(style()->standardIcon(QStyle::SP_FileIcon));
enableAndDisableButtons();
updateRecentFiles();
applyPreferences();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::closeEvent(QCloseEvent * event)
{
//are we actually quitting, or just hiding the window in popup mode?
QSettings settings;
if (settings.value("Testing/popupMode",false).toBool())
{
hide();
popup.info(0,tr("Popup Mode activated.\n\nClick the tray icon to restore FlashKard."));
event->ignore();
return;
}
//we're actually quitting...
//if no changes have been made or the pack is empty, the program can be safely closed
if ( !mainPack.hasUnsavedChanges() ||
mainPack.isEmpty() )
event->accept();
//otherwise ask whether user wants to save changes
else if (popup.dealWithChanges(this))
event->accept();
else
event->ignore();
}
void MainWindow::on_exitButton_clicked()
{
close(); //calls closeEvent()
}
void MainWindow::on_loadButton_clicked()
{
if (mainPack.hasUnsavedChanges() &&
popup.getYesOrNo(this,tr("Save current cards before opening a different database?")))
{
on_actionSave_FlashKard_Database_As_triggered();
}
mainPack.empty();
//get filename to open
QString fileName = QFileDialog::getOpenFileName(this,
tr("Load Flashcards"),
"~/Documents", tr("All FlashKard Files (*.~sv *.csv *.fml *.fdb);;FlashKard Markup Languge (*.fml);;Tilde~Separated Values (*.~sv);;FlashKard Database (*.fdb)"));
if (fileName.isEmpty())
return;
loadFile(fileName);
}
void MainWindow::loadRecent()
{
if (mainPack.hasUnsavedChanges() &&
popup.getYesOrNo(this,tr("Save current cards before opening a different database?")))
{
on_actionSave_FlashKard_Database_As_triggered();
}
mainPack.empty();
QAction *loadRecentAction = qobject_cast<QAction *>(sender());
loadFile(loadRecentAction->data().toString());
}
void MainWindow::loadFile(QString fileName)
{
currentlyLoadedFilename = fileName;
updateRecentFiles();
QFileInfo fileToLoad(currentlyLoadedFilename);
//based on file extension, call appropriate loader
if (fileToLoad.suffix() == "fml")
loadFML();
else if (fileToLoad.suffix() == "fdb")
loadFDB();
else if (fileToLoad.suffix() == "~sv" ||
fileToLoad.suffix() == "csv")
importdatabase(currentlyLoadedFilename);
mainPack.setUnchanged();
enableAndDisableButtons();
}
void MainWindow::on_testButton_clicked()
{
if (mainPack.isEmpty())
on_loadButton_clicked();
while (mainPack.isEmpty())
{
if(popup.importantQuestion(this,tr("Nothing to test!\n\nLoad flashcards?")))
on_loadButton_clicked();
else
return;
}
mainPack.setChanged();
setTesting(true);
flashCardWindow tester;
tester.exec();
setTesting(false);
}
void MainWindow::on_databaseButton_clicked()
{
mainPack.setChanged();
databaseWindow database;
database.exec();
enableAndDisableButtons();
}
bool MainWindow::on_saveButton_clicked()
{
if (mainPack.isEmpty()) return false; //don't save empty file
if (currentlyLoadedFilename.isEmpty())
currentlyLoadedFilename = QFileDialog::getSaveFileName(this, tr("Save Flashcard Database"),
currentlyLoadedFilename.toLatin1().data(),
tr("FlashKard Markup Languge (*.fml);;Tilde~Separated Values (*.~sv);;FlashKard Database (*.fdb)"));
if (currentlyLoadedFilename.isEmpty())
{
popup.info(this,tr("File not saved."));
return false;
}
updateRecentFiles();
QFileInfo fileToSave(currentlyLoadedFilename);
//based on file extension, call appropriate saving routine
if (fileToSave.suffix() == "fml")
saveFML();
else if (fileToSave.suffix() == "fdb")
saveFDB();
else if (fileToSave.suffix() == "~sv")
mainPack.exportdatabase(currentlyLoadedFilename);
mainPack.setUnchanged();
return true;
}
void MainWindow::on_actionSave_FlashKard_Database_As_triggered()
{
QString filename = QFileDialog::getSaveFileName(this, tr("Save Flashcard Database"),
currentlyLoadedFilename.toLatin1().data(),
tr("FlashKard Markup Languge (*.fml);;Tilde~Separated Values (*.~sv);;FlashKard Database (*.fdb)"));
if (! filename.isEmpty())
{
currentlyLoadedFilename = filename;
on_saveButton_clicked();
}
}
void MainWindow::on_actionMerge_from_FlashKard_Database_triggered()
{
//get filename to open
QString fileName = QFileDialog::getOpenFileName(this,
tr("Merge Flashcards"),
"~/Documents", tr("All FlashKard Files (*.~sv *.csv *.fml *.fdb);;FlashKard Markup Languge (*.fml);;Tilde~Separated Values (*.~sv);;FlashKard Database (*.fdb)"));
if (fileName.isEmpty())
return;
loadFile(fileName);
currentlyLoadedFilename = QString();
mainPack.setChanged();
}
void MainWindow::on_actionClear_Pack_triggered()
{
if (popup.importantQuestion(this,tr("Are you sure you want to clear the pack, removing ALL cards?")))
{
mainPack.empty();
currentlyLoadedFilename = QString();
enableAndDisableButtons();
}
}
void MainWindow::on_actionRemove_Duplicates_triggered()
{
mainPack.removeDuplicates();
enableAndDisableButtons();
}
void MainWindow::on_actionConfigure_FlashKard_triggered()
{
preferencesWindow prefs;
prefs.exec();
applyPreferences();
}
void MainWindow::enableAndDisableButtons()
{
if (mainPack.isEmpty())
{
ui->actionSave_FlashKard_Database->setDisabled(true);
ui->actionSave_FlashKard_Database_As->setDisabled(true);
ui->actionMerge_from_FlashKard_Database->setDisabled(true);
ui->actionClear_Pack->setDisabled(true);
ui->actionStatistics->setDisabled(true);
ui->saveButton->setDisabled(true);
ui->statsButton->setDisabled(true);
}
else
{
ui->actionSave_FlashKard_Database->setEnabled(true);
ui->actionSave_FlashKard_Database_As->setEnabled(true);
ui->actionMerge_from_FlashKard_Database->setEnabled(true);
ui->actionClear_Pack->setEnabled(true);
ui->actionStatistics->setEnabled(true);
ui->saveButton->setEnabled(true);
ui->statsButton->setEnabled(true);
}
if (mainPack.cardsInPack() > 1)
ui->actionRemove_Duplicates->setEnabled(true);
else
ui->actionRemove_Duplicates->setDisabled(true);
}
void MainWindow::updateRecentFiles()
{
//Lovingly adapted from the Qt examples...
//open/create settings file for storing recent files. This could be done by setting the app author and name if the setting file is required elsewhere
QSettings settings;
QStringList recentFiles = settings.value("recentFiles").toStringList();
recentFiles.removeAll(currentlyLoadedFilename);
if (!currentlyLoadedFilename.isEmpty())
recentFiles.prepend(currentlyLoadedFilename);
while (recentFiles.size() > 5)
recentFiles.removeLast();
settings.setValue("recentFiles", recentFiles);
//the following kludge is necessary only to allow for the creation of the menu entries through Qt designer
//it is high-maintenance and not really scalable. A more ideal solution would be an array as in the Qt examples.
if (recentFiles.size() > 0)
{
ui->menuRecentFiles->menuAction()->setVisible(true);
ui->actionRecent1->setVisible(true);
ui->actionRecent1->setText(QFileInfo(recentFiles[0]).fileName());
ui->actionRecent1->setData(recentFiles[0]);
}
else
{
ui->actionRecent1->setVisible(false);
ui->menuRecentFiles->menuAction()->setVisible(false);
}
if (recentFiles.size() > 1)
{
ui->actionRecent2->setVisible(true);
ui->actionRecent2->setText(QFileInfo(recentFiles[1]).fileName());
ui->actionRecent2->setData(recentFiles[1]);
}
else
ui->actionRecent2->setVisible(false);
if (recentFiles.size() > 2)
{
ui->actionRecent3->setVisible(true);
ui->actionRecent3->setText(QFileInfo(recentFiles[2]).fileName());
ui->actionRecent3->setData(recentFiles[2]);
}
else
ui->actionRecent3->setVisible(false);
if (recentFiles.size() > 3)
{
ui->actionRecent4->setVisible(true);
ui->actionRecent4->setText(QFileInfo(recentFiles[3]).fileName());
ui->actionRecent4->setData(recentFiles[3]);
}
else
ui->actionRecent4->setVisible(false);
if (recentFiles.size() > 4)
{
ui->actionRecent5->setVisible(true);
ui->actionRecent5->setText(QFileInfo(recentFiles[4]).fileName());
ui->actionRecent5->setData(recentFiles[4]);
}
else
ui->actionRecent5->setVisible(false);
}
void MainWindow::applyPreferences()
{
QSettings settings;
//testing section
settings.beginGroup("Testing");
bool popup = settings.value("popupMode",false).toBool();
trayIcon.setPopupMode(popup);
settings.endGroup();
//scoring section
settings.beginGroup("Scoring");
flashCard::bonusForCorrectAnswer = settings.value("bonusForCorrectAnswer",BONUS_FOR_CORRECT_ANSWER).toInt();
flashCard::bonusPerCorrectStreak = settings.value("bonusPerCorrectStreak",BONUS_PER_CORRECT_STREAK).toInt();
flashCard::maxBonusStreakLength = settings.value("maxBonusStreakLength",MAX_BONUS_STREAK_LENGTH).toInt();
flashCard::bonusPerKnownLevel = settings.value("bonusPerKnownLevel",BONUS_PER_KNOWN_LEVEL).toInt();
settings.endGroup();
//learning section
settings.beginGroup("Learning");
flashCard::n2lToNorm = settings.value("n2lToNorm",N2LTONORM).toInt();
flashCard::normToN2l = settings.value("normToN2l",NORMTON2L).toInt();
flashCard::normToKnown = settings.value("normToKnown",NORMTOKNOWN).toInt();
flashCard::knownToNorm = settings.value("knownToNorm",KNOWNTONORM).toInt();
flashCard::knownToOld = settings.value("knownToOld",KNOWNTOOLD).toInt();
flashCard::oldToNorm = settings.value("oldToNorm",OLDTONORM).toInt();
settings.endGroup();
}
void MainWindow::on_statsButton_clicked()
{
StatsWindow statsWindow;
statsWindow.exec();
}
void MainWindow::saveFDB()
{
//Create connection to database file with current filename
QSqlDatabase flashcardDatabase = QSqlDatabase::addDatabase("QSQLITE");
flashcardDatabase.setDatabaseName(currentlyLoadedFilename);
flashcardDatabase.open();
//create table
QSqlQuery query;
query.exec("DROP TABLE Flashcards");
query.exec("CREATE TABLE Flashcards (Question TEXT, Answer TEXT, Info TEXT, Hint TEXT, KnownLevel TINYINT, LastCorrect TINYINT, CurrentStreak TINYINT, LevelUp TINYINT, AnswerTime INT)");
//add all cards to database
flashCard * currentCard = mainPack.getFirstCard();
query.exec("BEGIN TRANSACTION");
do
{
QString knownLevel, lastCorrect, currentStreak, levelUp, answerTime;
QString queryString;
queryString = "INSERT INTO Flashcards (Question, Answer, Info, Hint, KnownLevel, LastCorrect, CurrentStreak, LevelUp, AnswerTime) VALUES ('" +
currentCard->getQuestion().replace(QString("'"),QString("''")) + "', '" +
currentCard->getAnswer().replace(QString("'"),QString("''")) + "', '" +
currentCard->getInfo().replace(QString("'"),QString("''")) + "', '" +
currentCard->getHint().replace(QString("'"),QString("''")) + "', " +
knownLevel.setNum((int)currentCard->getKnownLevel()) + ", " +
lastCorrect.setNum(currentCard->wasCorrectLastTime() ? 1 : 0) + ", " +
currentStreak.setNum(currentCard->getCurrentStreak()) + ", " +
levelUp.setNum(currentCard->getLevelUp()) + ", " +
answerTime.setNum(currentCard->answerTime) + ")";
if(!query.exec(queryString.toUtf8().data()))
{
popup.error(this,query.lastQuery());
popup.error(this,query.lastError().text());
}
}
while ((currentCard = mainPack.getNextCard(currentCard)) != NULL);
query.exec("END TRANSACTION");
flashcardDatabase.close();
QSqlDatabase::removeDatabase(QSqlDatabase::database().connectionName());
}
void MainWindow::loadFDB()
{
//Create connection to database file with current filename
QSqlDatabase flashcardDatabase = QSqlDatabase::addDatabase("QSQLITE");
flashcardDatabase.setDatabaseName(currentlyLoadedFilename);
flashcardDatabase.open();
QSqlQuery query;
//read in all cards
query.exec("SELECT Question,Answer,Info,Hint,KnownLevel,LastCorrect,CurrentStreak,LevelUp,AnswerTime FROM Flashcards");
QString qu;
QString an;
QString in;
QString hi;
knownLevel_t kl;
bool lc;
int cs;
int lu;
int at;
while (query.next())
{
qu = query.value(0).toString();
an = query.value(1).toString();
in = query.value(2).toString();
hi = query.value(3).toString();
kl = (knownLevel_t)query.value(4).toInt();
lc = query.value(5).toInt() ? 1 : 0;
cs = query.value(6).toInt();
lu = query.value(7).toInt();
at = query.value(8).toInt();
flashCard * fc = new flashCard(qu,an,in,hi,kl,lc,cs,lu,at);
mainPack.addCard(*fc,kl);
}
//close connection
flashcardDatabase.close();
QSqlDatabase::removeDatabase(QSqlDatabase::database().connectionName());
}
void MainWindow::saveFML()
{
//create XML file in memory
QDomDocument flashKardMLDoc("FlashKardML");
QDomElement flashcards = flashKardMLDoc.createElement("flashcards");
//add each flashcard node to the flashcards node
flashCard * currentCard = mainPack.getFirstCard();
do
{
QDomElement flashcard = flashKardMLDoc.createElement("flashcard");
//strings:
QDomElement question = flashKardMLDoc.createElement("Question");
QDomText questionText = flashKardMLDoc.createTextNode(currentCard->getQuestion());
question.appendChild(questionText);
flashcard.appendChild(question);
QDomElement answer = flashKardMLDoc.createElement("Answer");
QDomText answerText = flashKardMLDoc.createTextNode(currentCard->getAnswer());
answer.appendChild(answerText);
flashcard.appendChild(answer);
QDomElement info = flashKardMLDoc.createElement("Info");
QDomText infoText = flashKardMLDoc.createTextNode(currentCard->getInfo());
info.appendChild(infoText);
flashcard.appendChild(info);
QDomElement hint = flashKardMLDoc.createElement("Hint");
QDomText hintText = flashKardMLDoc.createTextNode(currentCard->getHint());
hint.appendChild(hintText);
flashcard.appendChild(hint);
//others:
QString kl, lc, cs, lu, at;
QDomElement knownLevel = flashKardMLDoc.createElement("KnownLevel");
QDomText knownLevelText = flashKardMLDoc.createTextNode(kl.setNum((int)currentCard->getKnownLevel()));
knownLevel.appendChild(knownLevelText);
flashcard.appendChild(knownLevel);
QDomElement lastCorrect = flashKardMLDoc.createElement("LastCorrect");
QDomText lastCorrectText = flashKardMLDoc.createTextNode(lc.setNum(currentCard->wasCorrectLastTime() ? 1 : 0));
lastCorrect.appendChild(lastCorrectText);
flashcard.appendChild(lastCorrect);
QDomElement currentStreak = flashKardMLDoc.createElement("CurrentStreak");
QDomText currentStreakText = flashKardMLDoc.createTextNode(cs.setNum(currentCard->getCurrentStreak()));
currentStreak.appendChild(currentStreakText);
flashcard.appendChild(currentStreak);
QDomElement levelUp = flashKardMLDoc.createElement("LevelUp");
QDomText levelUpText = flashKardMLDoc.createTextNode(lu.setNum(currentCard->getLevelUp()));
levelUp.appendChild(levelUpText);
flashcard.appendChild(levelUp);
QDomElement answerTime = flashKardMLDoc.createElement("AnswerTime");
QDomText answerTimeText = flashKardMLDoc.createTextNode(at.setNum(currentCard->answerTime));
answerTime.appendChild(answerTimeText);
flashcard.appendChild(answerTime);
flashcards.appendChild(flashcard);
}
while ((currentCard = mainPack.getNextCard(currentCard)) != NULL);
//add the whole flashcards node to the file in memory
flashKardMLDoc.appendChild(flashcards);
//write the file to disk
QFile outputFile (currentlyLoadedFilename);
if(!outputFile.open(QIODevice::WriteOnly))
popup.error(this,tr("Could not open file for writing!"));
QTextStream outputStream(&outputFile);
outputStream << flashKardMLDoc.toString();
outputFile.close();
}
void MainWindow::loadFML()
{
//use FMLHandler to parse the FML file element by element
FMLHandler handler;
QFile inputFile (currentlyLoadedFilename);
QXmlInputSource fmlSource (&inputFile);
QXmlSimpleReader reader;
reader.setContentHandler(&handler);
reader.parse(fmlSource);
}