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

parley

  • sources
  • kde-4.12
  • 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 "abstractbackendmode.h"
20 #include "comparisonbackendmode.h"
21 #include "conjugationbackendmode.h"
22 #include "examplesentencebackendmode.h"
23 #include "flashcardbackendmode.h"
24 #include "genderbackendmode.h"
25 #include "multiplechoicebackendmode.h"
26 #include "writtenbackendmode.h"
27 
28 using namespace Practice;
29 
30 PracticeStateMachine::PracticeStateMachine(AbstractFrontend* frontend, ParleyDocument* doc, const PracticeOptions& options, TestEntryManager* testEntryManager, QObject* parent)
31 :QObject(parent)
32 ,m_frontend(frontend)
33 ,m_document(doc)
34 ,m_options(options)
35 ,m_current(0)
36 ,m_testEntryManager(testEntryManager)
37 {
38  createPracticeMode();
39 
40  // To allow to skip an an entry
41  connect(m_frontend, SIGNAL(skipAction()), this, SLOT(nextEntry()));
42  connect(m_frontend, SIGNAL(stopPractice()), this, SLOT(slotPracticeFinished()));
43  connect(m_frontend, SIGNAL(hintAction()), m_mode, SLOT(hintAction()));
44 
45  connect(m_frontend, SIGNAL(continueAction()), this, SLOT(continueAction()));
46 
47  connect(m_mode, SIGNAL(answerRight()), this, SLOT(answerRight()));
48  connect(m_mode, SIGNAL(answerWrongRetry()), this, SLOT(answerWrongRetry()));
49  connect(m_mode, SIGNAL(answerWrongShowSolution()), this, SLOT(answerWrongShowSolution()));
50  connect(m_mode, SIGNAL(showSolution()), this, SLOT(showSolution()));
51 }
52 
53 void PracticeStateMachine::createPracticeMode()
54 {
55  switch(m_options.mode()) {
56  case Prefs::EnumPracticeMode::FlashCardsPractice:
57  kDebug() << "Create Flash Card Practice backend";
58  m_frontend->setMode(AbstractFrontend::FlashCard);
59  m_mode = new FlashCardBackendMode(m_options, m_frontend, this);
60  break;
61  case Prefs::EnumPracticeMode::MultipleChoicePractice:
62  kDebug() << "Create MultipleChoice Practice backend";
63  m_frontend->setMode(AbstractFrontend::MultipleChoice);
64  m_mode = new MultipleChoiceBackendMode(m_options, m_frontend, this, m_testEntryManager);
65  break;
66  case Prefs::EnumPracticeMode::MixedLettersPractice:
67  kDebug() << "Create Mixed Letters Practice backend";
68  m_frontend->setMode(AbstractFrontend::MixedLetters);
69  m_mode = new WrittenBackendMode(m_options, m_frontend, this, m_testEntryManager, m_document->document());
70  break;
71  case Prefs::EnumPracticeMode::WrittenPractice:
72  kDebug() << "Create Written Practice backend";
73  m_frontend->setMode(AbstractFrontend::Written);
74  m_mode = new WrittenBackendMode(m_options, m_frontend, this, m_testEntryManager, m_document->document());
75  break;
76  case Prefs::EnumPracticeMode::ExampleSentencesPractice:
77  kDebug() << "Create Written Practice backend";
78  m_frontend->setMode(AbstractFrontend::Written);
79  m_mode = new ExampleSentenceBackendMode(m_options, m_frontend, this,m_testEntryManager, m_document->document());
80  break;
81  case Prefs::EnumPracticeMode::GenderPractice:
82  m_frontend->setMode(AbstractFrontend::MultipleChoice);
83  m_mode = new GenderBackendMode(m_options, m_frontend, this, m_testEntryManager, m_document->document());
84  break;
85  case Prefs::EnumPracticeMode::ConjugationPractice:
86  m_frontend->setMode(AbstractFrontend::Conjugation);
87  m_mode = new ConjugationBackendMode(m_options, m_frontend, this, m_testEntryManager, m_document->document());
88  break;
89  case Prefs::EnumPracticeMode::ComparisonPractice:
90  m_frontend->setMode(AbstractFrontend::Comparison);
91  m_mode = new ComparisonBackendMode(m_options, m_frontend, this, m_testEntryManager, m_document->document());
92  break;
93 
94  default:
95  Q_ASSERT("Implement selected practice mode" == 0);
96  }
97 }
98 
99 void Practice::PracticeStateMachine::start()
100 {
101  kDebug() << "Start practice";
102  m_testEntryManager->practiceStarted();
103  nextEntry();
104 }
105 
106 void PracticeStateMachine::nextEntry()
107 {
108  m_state = NotAnswered;
109  m_current = m_testEntryManager->getNextEntry();
110 
111  kDebug() << "GETTING ENTRY - " << m_current;
112 
113  //after going through all words, or at the start of practice
114  if (m_current == 0) {
115  slotPracticeFinished();
116  return;
117  }
118  if (!m_mode->setTestEntry(m_current)) {
119  // this is just a fall back, if an invalid entry slipped through
120  currentEntryFinished();
121  nextEntry();
122  }
123  updateFrontend();
124 }
125 
126 void PracticeStateMachine::slotPracticeFinished()
127 {
128  kDebug() << "Stop practice";
129  m_testEntryManager->practiceFinished();
130  emit practiceFinished();
131 }
132 
133 void PracticeStateMachine::currentEntryFinished()
134 {
135  m_testEntryManager->removeCurrentEntryFromPractice();
136 }
137 
138 void PracticeStateMachine::continueAction()
139 {
140  kDebug() << "continue" << m_state;
141  switch (m_state) {
142  // on continue, we check the answer, if in NotAnsweredState or AnswerWasWrongState
143  case NotAnswered:
144  case AnswerWasWrong:
145  m_mode->checkAnswer();
146  break;
147 
148  case SolutionShown:
149  gradeEntryAndContinue();
150  break;
151  }
152 }
153 
154 void PracticeStateMachine::answerRight()
155 {
156  kDebug() << "ans right";
157 
158  m_frontend->setFeedbackState(AbstractFrontend::AnswerCorrect);
159  if (m_state == NotAnswered) {
160  m_frontend->setResultState(AbstractFrontend::AnswerCorrect);
161  } else {
162  m_frontend->setResultState(AbstractFrontend::AnswerWrong);
163  }
164 
165  m_state = SolutionShown;
166  m_frontend->showSolution();
167 }
168 
169 void PracticeStateMachine::answerWrongRetry()
170 {
171  kDebug() << "wrong retr";
172  m_frontend->setFeedbackState(AbstractFrontend::AnswerWrong);
173  m_state = AnswerWasWrong;
174 }
175 
176 void PracticeStateMachine::answerWrongShowSolution()
177 {
178  kDebug() << "wrong sol";
179  m_frontend->setFeedbackState(AbstractFrontend::AnswerWrong);
180  //User gave an empty answer or the same answer for a second time so we want to drop out.
181  m_frontend->setResultState(AbstractFrontend::AnswerWrong);
182  m_state = SolutionShown;
183  m_frontend->showSolution();
184 }
185 
186 void PracticeStateMachine::showSolution()
187 {
188  kDebug() << "show solution";
189  m_state = SolutionShown;
190  m_frontend->showSolution();
191 }
192 
193 void PracticeStateMachine::updateFrontend()
194 {
195  m_frontend->setFeedbackState(AbstractFrontend::QuestionState);
196  m_frontend->setResultState(AbstractFrontend::QuestionState);
197  m_frontend->setLessonName(m_current->entry()->lesson()->name());
198 
199  // show the word that is currently practiced in the progress bar
200  m_frontend->setFinishedWordsTotalWords(
201  m_testEntryManager->totalEntryCount() - m_testEntryManager->activeEntryCount() + 1,
202  m_testEntryManager->totalEntryCount());
203 
204  grade_t grade = m_mode->currentGradeForEntry();
205  grade_t goodGrade = qMax(grade, grade_t(KV_LEV1_GRADE)); // if the word hasn't been practiced yet, use grade 1 as a base
206  if (m_current->statisticBadCount() == 0) {
207  goodGrade = qMax(KV_LEV2_GRADE, qMin(grade+1, KV_MAX_GRADE));
208  }
209 
210  m_frontend->setBoxes(grade, goodGrade, KV_LEV1_GRADE);
211 
212  QString imgFrom = m_current->entry()->translation(m_options.languageFrom())->imageUrl().url();
213  QString imgTo = m_current->entry()->translation(m_options.languageTo())->imageUrl().url();
214  if (imgFrom.isEmpty()) {
215  imgFrom = imgTo;
216  }
217  if (imgTo.isEmpty()) {
218  imgTo = imgFrom;
219  }
220  if (Prefs::flashcardsFrontImage()) {
221  m_frontend->setQuestionImage(imgFrom);
222  } else {
223  m_frontend->setQuestionImage(QString());
224  }
225  if (Prefs::flashcardsBackImage()) {
226  m_frontend->setSolutionImage(imgTo);
227  } else {
228  m_frontend->setSolutionImage(QString());
229  }
230  m_frontend->showQuestion();
231 }
232 
233 void PracticeStateMachine::gradeEntryAndContinue()
234 {
235  if (m_frontend->resultState() == AbstractFrontend::AnswerCorrect) {
236  m_current->updateStatisticsRightAnswer();
237  } else {
238  m_current->updateStatisticsWrongAnswer();
239  }
240 
241  kDebug() << "entry finished: " << m_frontend->resultState() << " change grades? " << m_current->changeGrades();
242  if (m_current->changeGrades()) {
243  m_mode->updateGrades();
244  if (m_frontend->resultState() == AbstractFrontend::AnswerCorrect) {
245  currentEntryFinished();
246  }
247  }
248  emit nextEntry();
249 }
250 
251 #include "practicestatemachine.moc"
TestEntry::changeGrades
bool changeGrades()
check if the entry was finished and the practice backend may update the grades that will be saved to ...
Definition: testentry.cpp:86
Practice::PracticeOptions
Definition: practiceoptions.h:22
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...
Practice::TestEntryManager::activeEntryCount
int activeEntryCount()
The number of entries that are still to be practiced.
Definition: testentrymanager.cpp:139
Prefs::EnumPracticeMode::MultipleChoicePractice
Definition: prefs.h:26
Practice::PracticeStateMachine::practiceFinished
void practiceFinished()
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
Practice::PracticeStateMachine::PracticeStateMachine
PracticeStateMachine(AbstractFrontend *frontend, ParleyDocument *doc, const PracticeOptions &options, TestEntryManager *testEntryManager, QObject *parent=0)
Definition: practicestatemachine.cpp:30
Practice::WrittenBackendMode
Definition: writtenbackendmode.h:24
Practice::PracticeOptions::languageFrom
int languageFrom() const
Definition: practiceoptions.h:27
Prefs::flashcardsFrontImage
static bool flashcardsFrontImage()
Get Show images on the front of the flashcard.
Definition: prefs.h:255
Practice::AbstractBackendMode::checkAnswer
virtual void checkAnswer()=0
Check if the current answer is right.
ParleyDocument::document
KEduVocDocument * document()
Definition: parleydocument.cpp:93
Practice::AbstractFrontend::setQuestionImage
virtual void setQuestionImage(const KUrl &img)=0
Practice::AbstractFrontend::QuestionState
Definition: abstractfrontend.h:40
Practice::TestEntryManager
Definition: testentrymanager.h:31
genderbackendmode.h
QObject
multiplechoicebackendmode.h
Practice::AbstractFrontend::setLessonName
virtual void setLessonName(const QString &lesson)=0
parleydocument.h
ParleyDocument
Definition: parleydocument.h:29
Practice::TestEntryManager::getNextEntry
TestEntry * getNextEntry()
Get the next entry to show to the user.
Definition: testentrymanager.cpp:188
Prefs::EnumPracticeMode::GenderPractice
Definition: prefs.h:26
Practice::AbstractFrontend::AnswerWrong
Definition: abstractfrontend.h:43
Practice::AbstractFrontend::MixedLetters
Definition: abstractfrontend.h:32
abstractbackendmode.h
Practice::ConjugationBackendMode
Definition: conjugationbackendmode.h:23
Practice::ComparisonBackendMode
Definition: comparisonbackendmode.h:23
Practice::MultipleChoiceBackendMode
Definition: multiplechoicebackendmode.h:23
Practice::AbstractFrontend::setMode
virtual void setMode(Mode mode)=0
switch between different modes such as written, flash card, etc
Practice::PracticeStateMachine::slotPracticeFinished
void slotPracticeFinished()
Definition: practicestatemachine.cpp:126
Practice::AbstractBackendMode::updateGrades
virtual void updateGrades()
Change the grades for the current entry.
Definition: abstractbackendmode.cpp:46
Practice::TestEntryManager::totalEntryCount
int totalEntryCount()
The number of entries in the practice.
Definition: testentrymanager.cpp:134
Practice::AbstractFrontend::Comparison
Definition: abstractfrontend.h:36
TestEntry::updateStatisticsRightAnswer
void updateStatisticsRightAnswer()
update the internal statistics for this practice with a right result
Definition: testentry.cpp:64
Practice::PracticeOptions::languageTo
int languageTo() const
Definition: practiceoptions.h:28
Practice::FlashCardBackendMode
Definition: flashcardbackendmode.h:22
conjugationbackendmode.h
Practice::AbstractBackendMode::setTestEntry
virtual bool setTestEntry(TestEntry *current)
start practicing a new word.
Definition: abstractbackendmode.cpp:27
Practice::AbstractFrontend::setFinishedWordsTotalWords
virtual void setFinishedWordsTotalWords(int finished, int total)=0
The status such as lesson or number of words has changed.
TestEntry::updateStatisticsWrongAnswer
void updateStatisticsWrongAnswer()
update the internal statistics for this practice with a wrong result
Definition: testentry.cpp:91
Practice::AbstractFrontend::FlashCard
Definition: abstractfrontend.h:31
examplesentencebackendmode.h
Practice::AbstractFrontend::AnswerCorrect
Definition: abstractfrontend.h:41
writtenbackendmode.h
Practice::TestEntryManager::practiceFinished
void practiceFinished()
Definition: testentrymanager.cpp:128
TestEntry::statisticBadCount
int statisticBadCount()
Definition: testentry.cpp:54
Practice::AbstractFrontend::Conjugation
Definition: abstractfrontend.h:35
Practice::ExampleSentenceBackendMode
Definition: examplesentencebackendmode.h:22
Practice::AbstractFrontend::showSolution
virtual void showSolution()=0
enter show solution mode - the solution is shown
Prefs::EnumPracticeMode::ExampleSentencesPractice
Definition: prefs.h:26
Practice::PracticeOptions::mode
Prefs::EnumPracticeMode::type mode() const
Definition: practiceoptions.cpp:32
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::TestEntryManager::removeCurrentEntryFromPractice
void removeCurrentEntryFromPractice()
Finish the currently active entry.
Definition: testentrymanager.cpp:98
Practice::AbstractFrontend
Definition: abstractfrontend.h:25
Practice::AbstractBackendMode::currentGradeForEntry
virtual grade_t currentGradeForEntry()
The grade of the current entry - this has an default implementation to return the grade for the curre...
Definition: abstractbackendmode.cpp:41
Prefs::EnumPracticeMode::MixedLettersPractice
Definition: prefs.h:26
Practice::PracticeStateMachine::start
void start()
Definition: practicestatemachine.cpp:99
Practice::AbstractFrontend::setSolutionImage
virtual void setSolutionImage(const KUrl &img)=0
practicestatemachine.h
comparisonbackendmode.h
Practice::GenderBackendMode
Definition: genderbackendmode.h:23
Practice::PracticeStateMachine::stopPractice
void stopPractice()
Practice::AbstractFrontend::Written
Definition: abstractfrontend.h:34
Prefs::EnumPracticeMode::FlashCardsPractice
Definition: prefs.h:26
Prefs::flashcardsBackImage
static bool flashcardsBackImage()
Get Show images on the back of the flashcard.
Definition: prefs.h:274
flashcardbackendmode.h
Practice::AbstractFrontend::MultipleChoice
Definition: abstractfrontend.h:33
Practice::AbstractFrontend::resultState
virtual ResultState resultState()=0
TestEntry::entry
KEduVocExpression * entry()
Definition: testentry.cpp:134
Prefs::EnumPracticeMode::WrittenPractice
Definition: prefs.h:26
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:42:06 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
  • kstars
  • libkdeedu
  •   keduvocdocument
  • 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