• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdeedu API Reference
  • KDE Home
  • Contact Us
 

parley

  • sources
  • kde-4.14
  • kdeedu
  • parley
  • src
  • practice
practicestatemachine.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  Copyright 2009-2010 Frederik Gladhorn <gladhorn@kde.org>
3  Copyright 2009 Daniel Laidig <d.laidig@gmx.de>
4  ***************************************************************************/
5 
6 /***************************************************************************
7  * *
8  * This program is free software; you can redistribute it and/or modify *
9  * it under the terms of the GNU General Public License as published by *
10  * the Free Software Foundation; either version 2 of the License, or *
11  * (at your option) any later version. *
12  * *
13  ***************************************************************************/
14 
15 #include "practicestatemachine.h"
16 
17 #include "parleydocument.h"
18 
19 #include "prefs.h"
20 #include "abstractbackendmode.h"
21 #include "comparisonbackendmode.h"
22 #include "conjugationbackendmode.h"
23 #include "examplesentencebackendmode.h"
24 #include "flashcardbackendmode.h"
25 #include "genderbackendmode.h"
26 #include "multiplechoicebackendmode.h"
27 #include "writtenbackendmode.h"
28 
29 using namespace Practice;
30 
31 PracticeStateMachine::PracticeStateMachine(AbstractFrontend* frontend, ParleyDocument* doc,
32  SessionManagerBase* sessionManager, QObject* parent)
33  : QObject(parent)
34  , m_frontend(frontend)
35  , m_document(doc)
36  , m_current(0)
37  , m_sessionManager(sessionManager)
38 {
39  createPracticeMode();
40 
41  // To allow to skip an an entry
42  connect(m_frontend, SIGNAL(skipAction()), this, SLOT(nextEntry()));
43  connect(m_frontend, SIGNAL(stopPractice()), this, SLOT(slotPracticeFinished()));
44  connect(m_frontend, SIGNAL(hintAction()), m_mode, SLOT(hintAction()));
45 
46  connect(m_frontend, SIGNAL(continueAction()), this, SLOT(continueAction()));
47 
48  connect(m_mode, SIGNAL(answerRight()), this, SLOT(answerRight()));
49  connect(m_mode, SIGNAL(answerWrongRetry()), this, SLOT(answerWrongRetry()));
50  connect(m_mode, SIGNAL(answerWrongShowSolution()), this, SLOT(answerWrongShowSolution()));
51  connect(m_mode, SIGNAL(showSolution()), this, SLOT(showSolution()));
52 }
53 
54 void PracticeStateMachine::createPracticeMode()
55 {
56  switch (Prefs::practiceMode()) {
57  case Prefs::EnumPracticeMode::FlashCardsPractice:
58  kDebug() << "Create Flash Card Practice backend";
59  m_frontend->setMode(AbstractFrontend::FlashCard);
60  m_mode = new FlashCardBackendMode(m_frontend, this);
61  break;
62  case Prefs::EnumPracticeMode::MultipleChoicePractice:
63  kDebug() << "Create MultipleChoice Practice backend";
64  m_frontend->setMode(AbstractFrontend::MultipleChoice);
65  m_mode = new MultipleChoiceBackendMode(m_frontend, this, m_sessionManager);
66  break;
67  case Prefs::EnumPracticeMode::MixedLettersPractice:
68  kDebug() << "Create Mixed Letters Practice backend";
69  m_frontend->setMode(AbstractFrontend::MixedLetters);
70  m_mode = new WrittenBackendMode(m_frontend, this, m_sessionManager, m_document->document());
71  break;
72  case Prefs::EnumPracticeMode::WrittenPractice:
73  kDebug() << "Create Written Practice backend";
74  m_frontend->setMode(AbstractFrontend::Written);
75  m_mode = new WrittenBackendMode(m_frontend, this, m_sessionManager, m_document->document());
76  break;
77  case Prefs::EnumPracticeMode::ExampleSentencesPractice:
78  kDebug() << "Create Written Practice backend";
79  m_frontend->setMode(AbstractFrontend::ExampleSentence);
80  m_mode = new ExampleSentenceBackendMode(m_frontend, this, m_sessionManager, m_document->document());
81  break;
82  case Prefs::EnumPracticeMode::GenderPractice:
83  m_frontend->setMode(AbstractFrontend::MultipleChoice);
84  m_mode = new GenderBackendMode(m_frontend, this, m_sessionManager, m_document->document());
85  break;
86  case Prefs::EnumPracticeMode::ConjugationPractice:
87  m_frontend->setMode(AbstractFrontend::Conjugation);
88  m_mode = new ConjugationBackendMode(m_frontend, this, m_sessionManager, m_document->document());
89  break;
90  case Prefs::EnumPracticeMode::ComparisonPractice:
91  m_frontend->setMode(AbstractFrontend::Comparison);
92  m_mode = new ComparisonBackendMode(m_frontend, this, m_sessionManager, m_document->document());
93  break;
94 
95  default:
96  Q_ASSERT("Implement selected practice mode" == 0);
97  }
98 }
99 
100 void Practice::PracticeStateMachine::start()
101 {
102  kDebug() << "Start practice";
103  m_sessionManager->practiceStarted();
104  nextEntry();
105 }
106 
107 void PracticeStateMachine::nextEntry()
108 {
109  m_state = NotAnswered;
110  m_current = m_sessionManager->nextTrainingEntry();
111 
112  //kDebug() << "GETTING ENTRY - " << m_current;
113 
114  //after going through all words, or at the start of practice
115  if (m_current == 0) {
116  slotPracticeFinished();
117  return;
118  }
119  if (!m_mode->setTestEntry(m_current)) {
120  // this is just a fall back, if an invalid entry slipped through
121  currentEntryFinished();
122  nextEntry();
123  }
124  updateFrontend();
125 }
126 
127 void PracticeStateMachine::slotPracticeFinished()
128 {
129  kDebug() << "Stop practice";
130  m_sessionManager->practiceFinished();
131  emit practiceFinished();
132 }
133 
134 void PracticeStateMachine::currentEntryFinished()
135 {
136  m_sessionManager->removeCurrentEntryFromPractice();
137 }
138 
139 void PracticeStateMachine::continueAction()
140 {
141  //kDebug() << "continue" << m_state;
142  switch (m_state) {
143  // on continue, we check the answer, if in NotAnsweredState or AnswerWasWrongState
144  case NotAnswered:
145  case AnswerWasWrong:
146  m_mode->checkAnswer();
147  break;
148 
149  case SolutionShown:
150  gradeEntryAndContinue();
151  break;
152  }
153 }
154 
155 void PracticeStateMachine::answerRight()
156 {
157  //kDebug() << "ans right";
158 
159  m_frontend->setFeedbackState(AbstractFrontend::AnswerCorrect);
160  if (m_state == NotAnswered) {
161  m_frontend->setResultState(AbstractFrontend::AnswerCorrect);
162  } else {
163  m_frontend->setResultState(AbstractFrontend::AnswerWrong);
164  }
165 
166  m_state = SolutionShown;
167  m_frontend->showSolution();
168 }
169 
170 void PracticeStateMachine::answerWrongRetry()
171 {
172  //kDebug() << "wrong retr";
173  m_frontend->setFeedbackState(AbstractFrontend::AnswerWrong);
174  m_state = AnswerWasWrong;
175 }
176 
177 void PracticeStateMachine::answerWrongShowSolution()
178 {
179  //kDebug() << "wrong sol";
180  m_frontend->setFeedbackState(AbstractFrontend::AnswerWrong);
181  //User gave an empty answer or the same answer for a second time so we want to drop out.
182  m_frontend->setResultState(AbstractFrontend::AnswerWrong);
183  m_state = SolutionShown;
184  m_frontend->showSolution();
185 }
186 
187 void PracticeStateMachine::showSolution()
188 {
189  //kDebug() << "show solution";
190  m_state = SolutionShown;
191  m_frontend->showSolution();
192 }
193 
194 void PracticeStateMachine::updateFrontend()
195 {
196  m_frontend->setFeedbackState(AbstractFrontend::QuestionState);
197  m_frontend->setResultState(AbstractFrontend::QuestionState);
198  m_frontend->setLessonName(m_current->entry()->lesson()->name());
199  m_frontend->showGrade(m_current->entry()->translation(m_current->languageTo())->preGrade(),
200  m_current->entry()->translation(m_current->languageTo())->grade());
201 
202  // show the word that is currently practiced in the progress bar
203  m_frontend->setFinishedWordsTotalWords(
204  m_sessionManager->allEntryCount() - m_sessionManager->activeEntryCount(),
205  m_sessionManager->allEntryCount());
206 
207  // Set fonts
208  m_frontend->setQuestionFont((m_current->languageFrom() == Prefs::learningLanguage())
209  ? m_frontend->learningLangFont()
210  : m_frontend->knownLangFont());
211  m_frontend->setSolutionFont((m_current->languageTo() == Prefs::learningLanguage())
212  ? m_frontend->learningLangFont()
213  : m_frontend->knownLangFont());
214 
215  grade_t grade = m_mode->currentGradeForEntry();
216  grade_t goodGrade = qMax(grade, grade_t(KV_LEV1_GRADE)); // if the word hasn't been practiced yet, use grade 1 as a base
217 
218  // Normal mode: check if current word was not answered wrong -> if yes, mark next grade as achievable
219  // Alternative mode (3 consecutive answers): check if word was not answered wrong and two consecutive times correct -> if yes, mark next grade as achievable
220  if ((!(Prefs::altLearn()) && m_current->statisticBadCount() == 0) ||
221  (m_current->statisticBadCount() == 0 && m_current->answeredCorrectInSequence() == 2 && Prefs::altLearn())) {
222  goodGrade = qMax(KV_LEV2_GRADE, qMin(grade + 1, KV_MAX_GRADE));
223  }
224 
225  m_frontend->setBoxes(grade, goodGrade, KV_LEV1_GRADE);
226 
227  QString imgFrom = m_current->entry()->translation(m_current->languageFrom())->imageUrl().url();
228  QString imgTo = m_current->entry()->translation(m_current->languageTo())->imageUrl().url();
229  if (imgFrom.isEmpty()) {
230  imgFrom = imgTo;
231  }
232  if (imgTo.isEmpty()) {
233  imgTo = imgFrom;
234  }
235  if (Prefs::flashcardsFrontImage()) {
236  m_frontend->setQuestionImage(imgFrom);
237  } else {
238  m_frontend->setQuestionImage(QString());
239  }
240  if (Prefs::flashcardsBackImage()) {
241  m_frontend->setSolutionImage(imgTo);
242  } else {
243  m_frontend->setSolutionImage(QString());
244  }
245  m_frontend->showQuestion();
246 }
247 
248 void PracticeStateMachine::gradeEntryAndContinue()
249 {
250  grade_t currentPreGrade = m_mode->currentPreGradeForEntry();
251  grade_t currentGrade = m_mode->currentGradeForEntry();
252 
253  if (m_frontend->resultState() == AbstractFrontend::AnswerCorrect) {
254  m_current->updateStatisticsRightAnswer(currentPreGrade, currentGrade);
255  } else {
256  m_current->updateStatisticsWrongAnswer(currentPreGrade, currentGrade);
257  }
258 
259  if (m_current->shouldChangeGrades()) {
260  m_mode->updateGrades();
261  if (m_frontend->resultState() == AbstractFrontend::AnswerCorrect) {
262  currentEntryFinished();
263  }
264  }
265  emit nextEntry();
266 }
267 
268 #include "practicestatemachine.moc"
Prefs::altLearn
static bool altLearn()
Get Use the Leitner learning method.
Definition: prefs.h:336
Practice::AbstractFrontend::setResultState
virtual void setResultState(ResultState resultState)=0
The result state indicated whether a word is counted as correct (and grades are raised) and can be ch...
TestEntry::entry
KEduVocExpression * entry() const
Definition: testentry.cpp:149
Prefs::EnumPracticeMode::MultipleChoicePractice
Definition: prefs.h:26
Practice::PracticeStateMachine::practiceFinished
void practiceFinished()
TestEntry::answeredCorrectInSequence
int answeredCorrectInSequence()
Definition: testentry.cpp:44
Practice::SessionManagerBase::nextTrainingEntry
virtual TestEntry * nextTrainingEntry()
Get the next entry to show to the user.
Definition: sessionmanagerbase.cpp:124
Prefs::EnumPracticeMode::ComparisonPractice
Definition: prefs.h:26
Prefs::EnumPracticeMode::ConjugationPractice
Definition: prefs.h:26
Practice::AbstractFrontend::showQuestion
virtual void showQuestion()=0
enter question mode - the user is asked to provide the solution
Practice::AbstractFrontend::setBoxes
virtual void setBoxes(grade_t currentBox, grade_t newBoxIfCorrect, grade_t newBoxIfWrong)=0
Prefs::practiceMode
static EnumPracticeMode::type practiceMode()
Get The practice method that is currently selected.
Definition: prefs.h:1324
Practice::WrittenBackendMode
Definition: writtenbackendmode.h:25
TestEntry::updateStatisticsRightAnswer
void updateStatisticsRightAnswer(grade_t currentPreGrade, grade_t currentGrade)
update the internal statistics for this practice with a right result
Definition: testentry.cpp:64
Prefs::flashcardsFrontImage
static bool flashcardsFrontImage()
Get Show images on the front of the flashcard.
Definition: prefs.h:260
Practice::AbstractFrontend::showGrade
virtual void showGrade(int preGrade, int grade)=0
Practice::AbstractBackendMode::checkAnswer
virtual void checkAnswer()=0
Check if the current answer is right.
ParleyDocument::document
KEduVocDocument * document()
Definition: parleydocument.cpp:97
Practice::AbstractFrontend::setQuestionImage
virtual void setQuestionImage(const KUrl &img)=0
prefs.h
Practice::AbstractFrontend::QuestionState
Definition: abstractfrontend.h:42
genderbackendmode.h
Practice::SessionManagerBase::removeCurrentEntryFromPractice
virtual void removeCurrentEntryFromPractice()
Finish the currently active entry.
Definition: sessionmanagerbase.cpp:160
multiplechoicebackendmode.h
Practice::AbstractFrontend::setQuestionFont
virtual void setQuestionFont(const QFont &font)=0
Practice::AbstractFrontend::setLessonName
virtual void setLessonName(const QString &lesson)=0
parleydocument.h
Practice::AbstractFrontend::learningLangFont
virtual QFont learningLangFont() const =0
ParleyDocument
Definition: parleydocument.h:29
Practice::SessionManagerBase::allEntryCount
int allEntryCount() const
The number of entries available for the practice session.
Definition: sessionmanagerbase.cpp:172
Prefs::EnumPracticeMode::GenderPractice
Definition: prefs.h:26
Practice::AbstractFrontend::AnswerWrong
Definition: abstractfrontend.h:45
Practice::AbstractFrontend::MixedLetters
Definition: abstractfrontend.h:33
Practice::AbstractFrontend::knownLangFont
virtual QFont knownLangFont() const =0
fonts for learning and known languages.
QObject
abstractbackendmode.h
Practice::ConjugationBackendMode
Definition: conjugationbackendmode.h:24
Practice::ComparisonBackendMode
Definition: comparisonbackendmode.h:24
QString::isEmpty
bool isEmpty() const
Practice::MultipleChoiceBackendMode
Definition: multiplechoicebackendmode.h:24
Practice::AbstractFrontend::setMode
virtual void setMode(Mode mode)=0
switch between different modes such as written, flash card, etc
Practice::SessionManagerBase::activeEntryCount
int activeEntryCount()
The number of entries that are still to be practiced.
Definition: sessionmanagerbase.cpp:177
Practice::PracticeStateMachine::slotPracticeFinished
void slotPracticeFinished()
Definition: practicestatemachine.cpp:127
Practice::AbstractBackendMode::updateGrades
virtual void updateGrades()
Change the grades for the current entry.
Definition: abstractbackendmode.cpp:63
TestEntry::updateStatisticsWrongAnswer
void updateStatisticsWrongAnswer(grade_t currentPreGrade, grade_t currentGrade)
update the internal statistics for this practice with a wrong result
Definition: testentry.cpp:96
QString
Practice::SessionManagerBase::practiceFinished
virtual void practiceFinished()
Definition: sessionmanagerbase.cpp:112
Practice::AbstractFrontend::Comparison
Definition: abstractfrontend.h:37
Practice::FlashCardBackendMode
Definition: flashcardbackendmode.h:23
conjugationbackendmode.h
Practice::AbstractBackendMode::setTestEntry
virtual bool setTestEntry(TestEntry *current)
start practicing a new word.
Definition: abstractbackendmode.cpp:28
Practice::AbstractFrontend::setFinishedWordsTotalWords
virtual void setFinishedWordsTotalWords(int finished, int total)=0
The status such as lesson or number of words has changed.
Practice::AbstractFrontend::FlashCard
Definition: abstractfrontend.h:32
examplesentencebackendmode.h
Practice::AbstractFrontend::AnswerCorrect
Definition: abstractfrontend.h:43
writtenbackendmode.h
TestEntry::statisticBadCount
int statisticBadCount()
Definition: testentry.cpp:54
Practice::AbstractFrontend::Conjugation
Definition: abstractfrontend.h:36
Practice::ExampleSentenceBackendMode
Definition: examplesentencebackendmode.h:23
Practice::AbstractFrontend::showSolution
virtual void showSolution()=0
enter show solution mode - the solution is shown
TestEntry::languageFrom
int languageFrom() const
Definition: testentry.cpp:109
Prefs::EnumPracticeMode::ExampleSentencesPractice
Definition: prefs.h:26
Practice::AbstractFrontend::setFeedbackState
virtual void setFeedbackState(ResultState feedbackState)=0
The feedback state tells the user if the currently entered word is correct (independent of whether th...
Practice::AbstractBackendMode::currentGradeForEntry
virtual grade_t currentGradeForEntry() const
The grade of the current entry - this has a default implementation to return the grade for the curren...
Definition: abstractbackendmode.cpp:58
Practice::AbstractFrontend::setSolutionFont
virtual void setSolutionFont(const QFont &font)=0
Practice::AbstractFrontend
Definition: abstractfrontend.h:26
Prefs::EnumPracticeMode::MixedLettersPractice
Definition: prefs.h:26
Prefs::learningLanguage
static int learningLanguage()
Get The language that you are learning.
Definition: prefs.h:1248
Practice::PracticeStateMachine::PracticeStateMachine
PracticeStateMachine(AbstractFrontend *frontend, ParleyDocument *doc, SessionManagerBase *sessionManager, QObject *parent=0)
Definition: practicestatemachine.cpp:31
TestEntry::languageTo
int languageTo() const
Definition: testentry.cpp:114
Practice::SessionManagerBase
Definition: sessionmanagerbase.h:40
Practice::PracticeStateMachine::start
void start()
Definition: practicestatemachine.cpp:100
Practice::AbstractFrontend::setSolutionImage
virtual void setSolutionImage(const KUrl &img)=0
QObject::connect
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
practicestatemachine.h
comparisonbackendmode.h
Practice::GenderBackendMode
Definition: genderbackendmode.h:24
Practice::PracticeStateMachine::stopPractice
void stopPractice()
TestEntry::shouldChangeGrades
bool shouldChangeGrades()
check if the entry was finished and the practice backend may update the grades that will be saved to ...
Definition: testentry.cpp:91
Practice::AbstractFrontend::Written
Definition: abstractfrontend.h:35
Practice::AbstractFrontend::ExampleSentence
Definition: abstractfrontend.h:38
Prefs::EnumPracticeMode::FlashCardsPractice
Definition: prefs.h:26
Prefs::flashcardsBackImage
static bool flashcardsBackImage()
Get Show images on the back of the flashcard.
Definition: prefs.h:279
flashcardbackendmode.h
Practice::AbstractFrontend::MultipleChoice
Definition: abstractfrontend.h:34
Practice::AbstractFrontend::resultState
virtual ResultState resultState()=0
Practice::AbstractBackendMode::currentPreGradeForEntry
virtual grade_t currentPreGradeForEntry() const
The pregrade of the current entry - this has a default implementation to return the pregrade for the ...
Definition: abstractbackendmode.cpp:53
Prefs::EnumPracticeMode::WrittenPractice
Definition: prefs.h:26
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:15:56 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

parley

Skip menu "parley"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdeedu API Reference

Skip menu "kdeedu API Reference"
  • Analitza
  •     lib
  • kalgebra
  • kalzium
  •   libscience
  • kanagram
  • kig
  •   lib
  • klettres
  • marble
  • parley
  • rocs
  •   App
  •   RocsCore
  •   VisualEditor
  •   stepcore

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal