summaryrefslogtreecommitdiff
path: root/src/study/SpacedRepetitionWindow.cpp
blob: 977b1ecd06fa8902901475425876dd23fee783ec (plain)
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
#include "SpacedRepetitionWindow.h"
#include "CardsStatusBar.h"
#include "NumberFrame.h"
#include "WarningPanel.h"
#include "../dictionary/Dictionary.h"
#include "../dictionary/Card.h"
#include "../dictionary/CardPack.h"

SpacedRepetitionWindow::SpacedRepetitionWindow( SpacedRepetitionModel* aModel, QWidget* aParent ):
    IStudyWindow( aModel, tr("Spaced repetition"), aParent ),
    exactAnswerLabel(NULL), exactAnswerEdit(NULL)
{
    usesExactAnswer = m_model->getCardPack()->getUsesExactAnswer();
    studyType = AppModel::SpacedRepetition;
    if(usesExactAnswer)
        createExactAnswerWidget();
    createUI(); // Can't call from parent constructor
    setWindowIcon( QIcon(":/images/spaced-rep.png") );
    dayCardsLimitShown = false;
    showNextCard();
}

SpacedRepetitionWindow::~SpacedRepetitionWindow()
{
    WriteSettings();
}

void SpacedRepetitionWindow::createExactAnswerWidget()
{
    exactAnswerEdit = new QLineEdit;

    exactAnswerLabel = new CardSideView;
    exactAnswerLabel->setMaximumHeight(40);
    exactAnswerLabel->setShowMode(CardSideView::AnsMode);
    exactAnswerLabel->setPack(m_model->getCardPack());
}

QVBoxLayout* SpacedRepetitionWindow::createLowerPanel()
{
    warningPanel = new WarningPanel;

    controlLt = new QVBoxLayout;
    controlLt->addWidget(warningPanel);
    controlLt->addLayout(createProgressLayout());
    controlLt->addLayout(new QVBoxLayout);  // Grades layout

    createGradeButtons();

    return controlLt;
}

QBoxLayout* SpacedRepetitionWindow::createProgressLayout()
{
    QVBoxLayout* lt = new QVBoxLayout;
    lt->setSpacing(3);
    lt->addLayout(createStatsLayout());
    lt->addLayout(createProgressBarLayout());
    return lt;
}

QBoxLayout* SpacedRepetitionWindow::createStatsLayout()
{
    todayNewLabel = new NumberFrame;
    todayNewLabel->setToolTip(tr("Today learned new cards"));
    todayNewLabel->setColor("#c0d6ff");

    learningCardsLabel = new NumberFrame;
    learningCardsLabel->setToolTip(tr("Scheduled learning reviews:\n"
            "new cards must be repeated today to learn"));
    learningCardsLabel->setColor(CardsStatusBar::Colors[1]);

    timeToNextLearningLabel = new QLabel;
    timeToNextLearningLabel->setToolTip(tr("Time left to the next learning review"));

    scheduledCardsLabel = new NumberFrame;
    scheduledCardsLabel->setToolTip(tr("Scheduled cards for today"));
    scheduledCardsLabel->setColor(CardsStatusBar::Colors[2]);

    scheduledNewLabel = new NumberFrame;
    scheduledNewLabel->setToolTip(tr("New scheduled cards for today:\n"
            "new cards that will be shown between the scheduled ones"));
    scheduledNewLabel->setColor("#bd8d71");

    QHBoxLayout* lt = new QHBoxLayout;
    lt->setSpacing(5);
    lt->addWidget(todayNewLabel);
    lt->addStretch();
    lt->addWidget(learningCardsLabel);
    lt->addWidget(timeToNextLearningLabel);
    lt->addWidget(scheduledCardsLabel);
    lt->addWidget(scheduledNewLabel);
    return lt;
}

QBoxLayout* SpacedRepetitionWindow::createProgressBarLayout()
{
    progressLabel = new QLabel;
    progressLabel->setToolTip(getProgressBarTooltip());

    coloredProgressBar = new CardsStatusBar;
    coloredProgressBar->setToolTip(progressLabel->toolTip());

    QHBoxLayout* lt = new QHBoxLayout;
    lt->setSpacing(5);
    lt->addWidget(progressLabel);
    lt->addWidget(coloredProgressBar);
    return lt;
}

QString SpacedRepetitionWindow::getProgressBarTooltip()
{
    QString boxSpace;
    for(int i = 0; i < 6; i++)
        boxSpace += "&nbsp;";
    QString colorBoxPattern = "<p><span style=\"background-color: %1;\">" +
            boxSpace + "</span> &nbsp;";
    QString pEnd = "</p>";

    QStringList legendLines = {
        tr("Reviewed cards"),
        tr("Learning reviews"),
        tr("Scheduled cards"),
        tr("New cards for today")};
    QString legend;
    for(int i = 0; i < 4; i++)
        legend += colorBoxPattern.arg(CardsStatusBar::Colors[i]) + legendLines[i] + pEnd;
    return tr("Progress of reviews scheduled for today:") + legend;
}

void SpacedRepetitionWindow::createGradeButtons()
{
    cardGradedSM = new QSignalMapper(this);
    connect( cardGradedSM, SIGNAL(mapped(int)),
        qobject_cast<SpacedRepetitionModel*>(m_model), SLOT(scheduleCard(int)) );

    gradeBtns[0] = createGradeButton(0, "question.png", tr("Unknown"),
            tr("Completely forgotten card, couldn't recall the answer."));
    gradeBtns[1] = createGradeButton(1, "red-stop.png", tr("Incorrect"),
            tr("The answer is incorrect."));
    gradeBtns[2] = createGradeButton(2, "blue-triangle-down.png", tr("Difficult"),
            tr("It's difficult to recall the answer. The last interval was too long."));
    gradeBtns[3] = createGradeButton(3, "green-tick.png", tr("Good"),
            tr("The answer is recalled in couple of seconds. The last interval was good enough."));
    gradeBtns[4] = createGradeButton(4, "green-triangle-up.png", tr("Easy"),
            tr("The card is too easy, and recalled without any effort. The last interval was too short."));
    goodBtn = gradeBtns[StudyRecord::Good - 1];
}

QPushButton* SpacedRepetitionWindow::createGradeButton(int i, const QString& iconName,
        const QString& label, const QString& toolTip)
{
    QPushButton* btn = new QPushButton;
    btn->setIcon(QIcon(":/images/" + iconName));
    btn->setText(QString("%1 %2").arg(i + 1).arg(label));
    btn->setToolTip("<p>" + toolTip);
    btn->setShortcut( QString::number(i + 1) );
    btn->setMinimumWidth(GradeButtonMinWidth);

#if defined(Q_OS_WIN)
    QFont font = btn->font();
    font.setPointSize(12);
    font.setFamily("Calibri");
    btn->setFont(font);
#endif

    cardGradedSM->setMapping(btn, i + 1);
    connect(btn, SIGNAL(clicked()), cardGradedSM, SLOT(map()));
    return btn;
}

void SpacedRepetitionWindow::processState()
{
    setEnabledGradeButtons();
    switch(state)
        {
        case StateAnswerHidden:
            displayQuestion();
            break;
        case StateAnswerVisible:
            displayAnswer();
            break;
        case StateNoCards:        
            updateCardsProgress();
            displayNoRemainedCards();
            break;
        }
}

void SpacedRepetitionWindow::setEnabledGradeButtons()
{
    for(int i = 0; i < StudyRecord::GradesNum; i++)
        gradeBtns[i]->setEnabled(state == StateAnswerVisible);
}

void SpacedRepetitionWindow::displayQuestion()
{
    questionLabel->setQuestion( m_model->getCurCard()->getQuestion() );
    answerStackedLt->setCurrentIndex( AnsButtonPage );
    updateCardsProgress();
    layoutGradeButtons();
    focusAnswerWidget();
    m_answerTime.start();
    processIsNewCard();
}

void SpacedRepetitionWindow::layoutGradeButtons()
{
    QHBoxLayout* higherLt = new QHBoxLayout;
    higherLt->addStretch();
    putGradeButtonsIntoLayouts(higherLt);
    higherLt->addStretch();
    replaceGradesLayout(higherLt);
    setGoodButtonText();
}

void SpacedRepetitionWindow::putGradeButtonsIntoLayouts(QBoxLayout* higherLt)
{
    QList<int> visibleGrades = static_cast<SpacedRepetitionModel*>(m_model)
            ->getAvailableGrades();
    for(int i = 0; i < StudyRecord::GradesNum; i++)
    {
        if(!visibleGrades.contains(i + 1))
        {
            gradeBtns[i]->setParent(NULL);
            continue;
        }
        higherLt->addWidget(gradeBtns[i]);
    }
}

void SpacedRepetitionWindow::replaceGradesLayout(QBoxLayout* higherLt)
{
    const int GradesLtIndex = 2;
    delete controlLt->takeAt(GradesLtIndex);
    controlLt->insertLayout(GradesLtIndex, higherLt);
}

void SpacedRepetitionWindow::setGoodButtonText()
{
    QList<int> visibleGrades = static_cast<SpacedRepetitionModel*>(m_model)
            ->getAvailableGrades();
    QString goodLabel = visibleGrades.size() == StudyRecord::GradesNum?
                tr("Good") : tr("OK");
    goodBtn->setText(QString("%1 %2").arg(StudyRecord::Good).arg(goodLabel));
    goodBtn->setShortcut(QString::number(StudyRecord::Good));
}

void SpacedRepetitionWindow::focusAnswerWidget()
{
    if(usesExactAnswer)
    {
        exactAnswerEdit->clear();
        exactAnswerEdit->setFocus();
    }
    else
        answerBtn->setFocus();
}

void SpacedRepetitionWindow::processIsNewCard()
{
    questionLabel->showNewIcon(static_cast<SpacedRepetitionModel*>(m_model)->isNew());
}

void SpacedRepetitionWindow::displayAnswer()
{
    static_cast<SpacedRepetitionModel*>(m_model)->setRecallTime(m_answerTime.elapsed());
    Card* card = m_model->getCurCard();
    QStringList correctAnswers = card->getAnswers();
    answerStackedLt->setCurrentIndex(AnsLabelPage);
    if(usesExactAnswer)
        showExactAnswer(correctAnswers);
    answerLabel->setQstAnsr(card->getQuestion(), correctAnswers);
    goodBtn->setFocus();
}

void SpacedRepetitionWindow::showExactAnswer(const QStringList& correctAnswers)
{
    if(static_cast<SpacedRepetitionModel*>(m_model)->isNew())
    {
        exactAnswerLabel->hide();
        return;
    }
    else
        exactAnswerLabel->show();
    bool isCorrect = correctAnswers.first() == exactAnswerEdit->text().trimmed();
    QString beginning("<span style=\"");
    beginning += QString("color:%1").arg(isCorrect ? "green" : "red");
    beginning += "\">";
    QString answer = beginning + exactAnswerEdit->text() + "</span>";
    exactAnswerLabel->setQstAnsr("", {answer});
}

void SpacedRepetitionWindow::updateCardsProgress()
    {
    CardPack* pack = m_model->getCardPack();
    int todayReviewed = pack->getTodayReviewedCardsNum();
    int todayNewReviewed = pack->getTodayNewCardsNum();
    int activeRepeating = pack->getActiveRepeatingCardsNum();
    int learningReviews = pack->getLearningReviewsNum();
    int timeToNextLearning = pack->getTimeToNextLearning();
    int scheduledNew = static_cast<SpacedRepetitionModel*>(m_model)->
            estimatedNewReviewedCardsToday();
    int allTodaysCards = todayReviewed + learningReviews +
            activeRepeating + scheduledNew;

    todayNewLabel->setVisible(todayNewReviewed > 0);
    todayNewLabel->setValue(todayNewReviewed);
    learningCardsLabel->setValue(learningReviews);

    int minsToNextLearning = timeToNextLearning / 60;
    timeToNextLearningLabel->setVisible(minsToNextLearning > 0);
    timeToNextLearningLabel->setText(tr("(%1 min)").
            arg(minsToNextLearning));

    scheduledCardsLabel->setValue(activeRepeating);
    scheduledNewLabel->setValue(scheduledNew);

    progressLabel->setText(QString("%1/%2").
            arg(todayReviewed).arg(allTodaysCards));
    coloredProgressBar->setValues({todayReviewed, learningReviews,
            activeRepeating, scheduledNew});

    showLimitWarnings();
}

void SpacedRepetitionWindow::showLimitWarnings()
{
    if(warningPanel->isVisible())
        return;
    CardPack* pack = m_model->getCardPack();
    if(dayLimitReached(pack->dictionary()->countTodaysAllCards()))
    {
        warningPanel->setText(tr("Day cards limit is reached: %1 cards.\n"
            "It is recommended to stop studying this dictionary.")
            .arg(StudySettings::inst()->cardsDayLimit));
        warningPanel->show();
        dayCardsLimitShown = true;
    }
}

bool SpacedRepetitionWindow::dayLimitReached(int todayReviewed)
{
    return todayReviewed >= StudySettings::inst()->cardsDayLimit &&
        !dayCardsLimitShown;
}

void SpacedRepetitionWindow::displayNoRemainedCards()
{
    centralStackedLt->setCurrentIndex(MessagePage);
    messageLabel->setText(
        QString("<div align=\"center\" style=\"font-size: 22pt; font-weight: bold\">") +
        tr("All cards are reviewed") +
        "</div>" + "<p align=\"center\" style=\"font-size: 11pt\">" +
        tr("You can go to the next pack or dictionary, or open the Word drill.") +
        "</p>");
}

void SpacedRepetitionWindow::ReadSettings()
{
    QSettings settings;
    move( settings.value("spacedrep-pos", QPoint(PosX, PosY)).toPoint() );
    resize( settings.value("spacedrep-size", QSize(Width, Height)).toSize() );
}

void SpacedRepetitionWindow::WriteSettings()
{
    QSettings settings;
    settings.setValue("spacedrep-pos", pos());
    settings.setValue("spacedrep-size", size());
}

QWidget* SpacedRepetitionWindow::getAnswerEdit()
{
    return exactAnswerEdit;
}

QWidget* SpacedRepetitionWindow::getUserAnswerLabel()
{
    return exactAnswerLabel;
}