File indexing completed on 2024-04-21 03:51:03

0001 /*
0002     SPDX-FileCopyrightText: 2009 Frederik Gladhorn <gladhorn@kde.org>
0003     SPDX-License-Identifier: GPL-2.0-or-later
0004 */
0005 
0006 #ifndef ABSTRACTBACKENDMODE_H
0007 #define ABSTRACTBACKENDMODE_H
0008 
0009 #include <QVariant>
0010 
0011 #include "abstractfrontend.h"
0012 #include "testentry.h"
0013 
0014 namespace Practice
0015 {
0016 class AbstractBackendMode : public QObject
0017 {
0018     Q_OBJECT
0019 
0020 public:
0021     AbstractBackendMode(AbstractFrontend *frontend, QObject *parent);
0022     ~AbstractBackendMode() override = default;
0023 
0024     /** start practicing a new word. sets some default that can be overwritten by the modes.
0025      * m_frontend->showQuestion() should be called after the initialization. */
0026     virtual bool setTestEntry(TestEntry *current);
0027 
0028     /** add a new synonym to the list of shown/answered synonyms depending on which mode we
0029      * are in. */
0030     void addSynonym(const QString &entry)
0031     {
0032         if (!entry.isEmpty())
0033             m_synonyms.append(entry);
0034     }
0035 
0036     /**
0037      * The pregrade of the current entry - this has a default
0038      * implementation to return the pregrade for the current translation.
0039      *
0040      * @see currentGradeForEntry() for a comment about reimplementation.
0041      *
0042      * @return the pregrade
0043      */
0044     virtual grade_t currentPreGradeForEntry() const;
0045     /**
0046      * The grade of the current entry - this has a default
0047      * implementation to return the grade for the current translation.
0048      *
0049      * This is used in the frontend to visualize the confidence
0050      * level. If the practice is not on the translation itself but for
0051      * example conjugations, the mode needs to re-implement this
0052      * function in order to change the grade of the correct parts
0053      * of the translation.
0054      *
0055      * For modes that work on several words, this should return the
0056      * worst grade of them.
0057      *
0058      * @return the grade
0059      */
0060     virtual grade_t currentGradeForEntry() const;
0061 
0062     /**
0063      * Change the grades for the current entry.
0064      * The default implementation changes the grade of the current translation.
0065      * Modes working on other parts of the translations need to re-implement this.
0066      */
0067     virtual void updateGrades();
0068 
0069 public Q_SLOTS:
0070     /** the frontend requested a hint */
0071     virtual void hintAction() = 0;
0072 
0073     /**
0074      * Check if the current answer is right.
0075      * This function should Q_EMIT one of these Q_SIGNALS: answerRight, answerWrongRetry, answerWrongShowSolution or showSolution
0076      */
0077     virtual void checkAnswer() = 0;
0078 
0079 Q_SIGNALS:
0080     void removeCurrentEntryFromPractice();
0081 
0082     /** ask for the next word to be practiced */
0083     void nextEntry();
0084 
0085     void answerRight();
0086     void answerWrongRetry();
0087     void answerWrongShowSolution();
0088     void showSolution();
0089 
0090 protected:
0091     // Support functions for derived classes
0092 
0093     /** Update the grade for the current entry.
0094      *
0095      * If the answer from the user was correct, using isCorrectAnswer
0096      * as the indicator, then the grade of the current entry is
0097      * increased one step if there was no previous wrong answer;
0098      * otherwise it's set to pregrade 1, grade 0. The exception is if
0099      * the word is new (pregrade 0, grade 0). In that case if it's
0100      * correct it's presumed that the user knows the word reasonably
0101      * well and the grade is set to a higher level. The exact value of
0102      * this level is arbitrarily chosen now and may become
0103      * configurable in the future.
0104      *
0105      * @param text the translation or similar in the kedudocument that should be changed
0106      * @param isCorrectAnswer true if the current answer is correct
0107      * @param hasNoPreviousBadAnswers true if there never was any wrong answer for this word in the current session
0108      */
0109     void updateGrade(KEduVocText &text, bool isCorrectAnswer, bool hasNoPreviousBadAnswers);
0110 
0111 protected:
0112     AbstractFrontend *m_frontend{nullptr};
0113     TestEntry *m_current{nullptr};
0114     QStringList m_synonyms{nullptr};
0115 };
0116 
0117 }
0118 
0119 #endif // ABSTRACTBACKENDMODE_H