/
qflashcards.cpp
263 lines (230 loc) · 7.05 KB
/
qflashcards.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
#include "qflashcards.h"
#include "ui_qflashcards.h"
#include "cardgroup.h"
#include "cardmanager.h"
#include "cardentry.h"
#include "centrallabel.h"
#include <QLabel>
#include <QSizePolicy>
#include <QFileDialog>
#include <QTextBrowser>
#include <QDesktopServices>
#include <QUrl>
#include <QFileInfo>
#include <QDebug>
#include "filehandler.h"
const QString GPL("https://www.gnu.org/licenses/gpl-3.0.en.html");
const QUrl GPLURL(GPL);
QFlashCards::QFlashCards(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::QFlashCards)
{
ui->setupUi(this);
show();
}
QFlashCards::~QFlashCards()
{
delete ui;
}
void QFlashCards::setWorkingDir(QString dir) {
this->dir = dir;
}
void QFlashCards::showSpecialText(MessageType msg, CardType caller) {
CentralLabel* label = new CentralLabel(this);
label->setMessage(msg);
label->setCardType(caller);
connect(label, &CentralLabel::signalTryAgain, this, &QFlashCards::gotoCard);
connect(label, &CentralLabel::signalNextCard, this, &QFlashCards::gotoNextCard);
connect(label, &CentralLabel::signalShowAnswer, this, &QFlashCards::gotoShowAnswer);
setCentralWidget(label);
}
/**
* @brief QFlashCards::on_actionExit_triggered
*
* Well, exit the application when the exit menu is selected
*/
void QFlashCards::on_actionExit_triggered()
{
exit(0);
}
void QFlashCards::on_actionReview_triggered()
{
reviewCard = new ReviewCard(this);
CardManager::getCardManager()->shuffle();
CardManager::getCardManager()->reset();
reviewCard->setData(CardManager::getCardManager()->getCard());
setCentralWidget(reviewCard);
centralWidget()->showNormal();
}
void QFlashCards::on_actionFree_Responce_triggered()
{
freeResponceCard = new InputCard(this);
CardManager::getCardManager()->shuffle();
CardManager::getCardManager()->reset();
freeResponceCard->setData(CardManager::getCardManager()->getCard());
setCentralWidget(freeResponceCard);
connect(freeResponceCard, &InputCard::writeToStatus,
this, &QFlashCards::showSpecialText);
centralWidget()->showNormal();
}
void QFlashCards::on_actionNewCards_triggered()
{
createNewCards = new CreateCard(this);
CardManager::getCardManager()->sort();
CardManager::getCardManager()->reset();
createNewCards->setData(CardManager::getCardManager()->getNewCard());
statusBar()->showMessage(tr("New Deck Started"), 2000);
setWindowTitle(tr("Flash Cards"));
setCentralWidget(createNewCards);
centralWidget()->showNormal();
}
void QFlashCards::on_actionEdit_Cards_triggered()
{
editCards = new EditingCard(this);
CardManager::getCardManager()->sort();
CardManager::getCardManager()->preset();
CardEntry* aCard = CardManager::getCardManager()->getCard();
editCards->setData(aCard);
setCentralWidget(editCards);
CardManager::getCardManager()->sort();
centralWidget()->showNormal();
}
void QFlashCards::gotoCard(CardType card) {
switch(card) {
case CardType::INPUT:
gotoFreeResponce();
break;
default:
break;
}
}
void QFlashCards::gotoNextCard(CardType card) {
switch(card) {
case CardType::INPUT:
freeResponceCard = new InputCard(this);
freeResponceCard->setData(CardManager::getCardManager()->getCard());
setCentralWidget(freeResponceCard);
connect(freeResponceCard, &InputCard::writeToStatus,
this, &QFlashCards::showSpecialText);
centralWidget()->showNormal();
break;
default:
break;
}
}
void QFlashCards::gotoShowAnswer(CardType card) {
switch(card) {
case CardType::INPUT:
freeResponceCard = new InputCard(this);
freeResponceCard->setData(CardManager::getCardManager()->getCurrentCard());
setCentralWidget(freeResponceCard);
connect(freeResponceCard, &InputCard::writeToStatus,
this, &QFlashCards::showSpecialText);
freeResponceCard->showAnswer();
centralWidget()->showNormal();
break;
default:
break;
}
}
void QFlashCards::gotoFreeResponce()
{
freeResponceCard = new InputCard(this);
freeResponceCard->setData(CardManager::getCardManager()->getCurrentCard());
setCentralWidget(freeResponceCard);
connect(freeResponceCard, &InputCard::writeToStatus,
this, &QFlashCards::showSpecialText);
centralWidget()->showNormal();
}
void QFlashCards::on_actionLoad_triggered()
{
QString fileName
= QFileDialog::getOpenFileName(
this, tr("Open File"),
dir,
tr("Flashcards (*.qfcml)"));
if(fileName.isEmpty()) {
return;
}
statusBar()->showMessage(tr("Loaded File"), 2000);
FileHandler handler;
handler.readFile(fileName);
upDateTileWName(fileName);
on_actionReview_triggered();
}
void QFlashCards::on_actionSave_triggered()
{ QString* fileName = CardManager::getCardManager()->getFileName();
if(fileName->isEmpty()) {
qDebug() << "Card was empty";
on_actionSave_As_triggered();
} else {
FileHandler handler;
QFile file(*fileName);
QFileInfo info(file);
// This should never really occur now
if(info.isDir()) {
fileName->append("untitled.qfcml");
}
statusBar()->showMessage(tr("Saved File"), 2000);
handler.saveFile(*fileName);
upDateTileWName(*fileName);
}
}
void QFlashCards::on_actionSave_As_triggered()
{
QString fileName
= QFileDialog::getSaveFileName(
this, tr("Save Flashcards"),
dir,
tr("Flashcards (*.qfcml)"));
if(fileName.isEmpty()) {
return;
}
FileHandler handler;
QFile file(fileName);
QFileInfo info(file);
// This should never really occur now
if(info.isDir()) {
fileName.append("untitled.qfcml");
}
statusBar()->showMessage(tr("Saved File"), 2000);
handler.saveFile(fileName);
upDateTileWName(fileName);
}
void QFlashCards::on_actionNew_triggered()
{
CardManager::getCardManager()->newCardGroup();
on_actionNewCards_triggered();
}
void QFlashCards::on_actionHelp_triggered()
{
QTextBrowser* helpview = new QTextBrowser;
helpview->setEnabled(true);
helpview->setSource(QString("qrc:///html/help.html"));
setCentralWidget(helpview);
}
void QFlashCards::on_actionAbout_triggered()
{
QTextBrowser* helpview = new QTextBrowser;
helpview->setEnabled(true);
helpview->setSource(QString("qrc:///html/about.html"));
setCentralWidget(helpview);
}
void QFlashCards::on_actionLicense_triggered()
{
QDesktopServices::openUrl(GPLURL);
}
void QFlashCards::upDateTileWName(const QString& fileName) {
QFile file(fileName);
QFileInfo info(file);
QString name = info.fileName();
int dot = name.lastIndexOf(".");
int len = name.length() - dot;
if(dot > - 1) {
name.remove(dot, len);
}
setWindowTitle(tr("Flash Cards")
.append(" - ")
.append(name));
setWorkingDir(info.canonicalPath());
}