1 /*
2     SPDX-FileCopyrightText: 2003-2010 Peter Hedlund <peter.hedlund@kdemail.net>
3     SPDX-License-Identifier: GPL-2.0-or-later
4 */
5 
6 #include "flashview.h"
7 
8 #include <QTimer>
9 
10 #include <KLocalizedString>
11 #include <KNotification>
12 
13 #include "kwqquizmodel.h"
14 #include "kwqscorewidget.h"
15 #include "prefs.h"
16 
FlashView(QWidget * parent,KActionCollection * actionCollection)17 FlashView::FlashView(QWidget *parent, KActionCollection *actionCollection) : KWQQuizView(parent, actionCollection)
18 {
19   setupUi(this);
20   m_timer = new QTimer(this);
21   connect(m_timer, &QTimer::timeout, this, &FlashView::slotTimer);
22 }
23 
init()24 void FlashView::init()
25 {
26   score->clear();
27   score->setQuestionCount(m_quiz->questionCount());
28   score->setAsPercent(Prefs::percent());
29 
30   m_actionCollection->action(QStringLiteral("quiz_check"))->setEnabled(true);
31   m_actionCollection->action(QStringLiteral("flash_know"))->setEnabled(true);
32   m_actionCollection->action(QStringLiteral("flash_dont_know"))->setEnabled(true);
33   m_actionCollection->action(QStringLiteral("quiz_repeat_errors"))->setEnabled(false);
34   m_actionCollection->action(QStringLiteral("quiz_export_errors"))->setEnabled(false);
35   m_actionCollection->action(QStringLiteral("quiz_audio_play"))->setEnabled(false);
36 
37   // reset last file
38   audioPlayFile(QUrl(), true);
39 
40   connect(flashcard, SIGNAL(cardClicked()), this, SLOT(slotCheck()), Qt::UniqueConnection);
41 
42   m_showFirst = true;
43   slotCheck();
44 }
45 
keepDiscardCard(bool keep)46 void FlashView::keepDiscardCard(bool keep)
47 {
48   if (!keep) {
49     score->countIncrement(KWQScoreWidget::cdCorrect);
50     KNotification::event(QStringLiteral("QuizCorrect"), i18n("Your answer was correct!"));
51   }
52   else {
53     m_quiz->checkAnswer(QLatin1String(""));
54     score->countIncrement(KWQScoreWidget::cdError);
55     KNotification::event(QStringLiteral("QuizError"), i18n("Your answer was incorrect."));
56   }
57 
58   m_showFirst = true;
59 
60   m_quiz->toNext();
61   if (!m_quiz->atEnd()) {
62     slotCheck();
63   }
64   else {
65     m_quiz->finish();
66     m_actionCollection->action(QStringLiteral("quiz_check"))->setEnabled(false);
67     m_actionCollection->action(QStringLiteral("flash_know"))->setEnabled(false);
68     m_actionCollection->action(QStringLiteral("flash_dont_know"))->setEnabled(false);
69     m_actionCollection->action(QStringLiteral("quiz_repeat_errors"))->setEnabled(m_quiz->hasErrors());
70     m_actionCollection->action(QStringLiteral("quiz_export_errors"))->setEnabled(m_quiz->hasErrors());
71     m_actionCollection->action(QStringLiteral("quiz_audio_play"))->setEnabled(false);
72     disconnect(flashcard, SIGNAL(cardClicked()), 0, 0);
73   }
74 }
75 
slotCheck()76 void FlashView::slotCheck()
77 {
78   if (m_showFirst) {
79     flashcard->setCardColor(Prefs::frontCardColor());
80     flashcard->setTextColor(Prefs::frontTextColor());
81     flashcard->setFrameColor(Prefs::frontFrameColor());
82     flashcard->setTextFont(Prefs::frontFont());
83     flashcard->setIdentifier(m_quiz ->langQuestion());
84     flashcard->setImage(m_quiz->imageQuestion());
85     flashcard->setText(m_quiz->question());
86     audioPlayQuestion();
87     m_showFirst = false;
88   }
89   else {
90     flashcard->setCardColor(Prefs::backCardColor());
91     flashcard->setTextColor(Prefs::backTextColor());
92     flashcard->setFrameColor(Prefs::backFrameColor());
93     flashcard->setTextFont(Prefs::backFont());
94     flashcard->setIdentifier(m_quiz->langAnswer());
95     flashcard->setImage(m_quiz->imageAnswer());
96     flashcard->setText(m_quiz->answer());
97     audioPlayAnswer();
98     m_showFirst = true;
99   }
100 
101   if (Prefs::autoFlip()) {
102     m_timer->setSingleShot(true);
103     m_timer->start(Prefs::flipDelay() * 1000);
104   }
105   else
106     m_timer->stop();
107 }
108 
slotKnow()109 void FlashView::slotKnow()
110 {
111   keepDiscardCard(false);
112 }
113 
slotDontKnow()114 void FlashView::slotDontKnow()
115 {
116   keepDiscardCard(true);
117 }
118 
slotTimer()119 void FlashView::slotTimer( )
120 {
121   if (!m_showFirst)
122     slotCheck();
123   else
124     if (Prefs::keepDiscard())
125       slotDontKnow();
126     else
127       slotKnow();
128 }
129 
slotApplySettings()130 void FlashView::slotApplySettings( )
131 {
132   if (!m_showFirst) {
133     flashcard->setCardColor(Prefs::frontCardColor());
134     flashcard->setTextColor(Prefs::frontTextColor());
135     flashcard->setFrameColor(Prefs::frontFrameColor());
136     flashcard->setTextFont(Prefs::frontFont());
137   }
138   else {
139     flashcard->setCardColor(Prefs::backCardColor());
140     flashcard->setTextColor(Prefs::backTextColor());
141     flashcard->setFrameColor(Prefs::backFrameColor());
142     flashcard->setTextFont(Prefs::backFont());
143   }
144 
145   if (Prefs::autoFlip()) {
146     m_timer->setSingleShot(true);
147     m_timer->start(Prefs::flipDelay() * 1000);
148   }
149   else
150     m_timer->stop();
151 
152   score ->setAsPercent(Prefs::percent());
153 }
154