File indexing completed on 2024-02-25 03:56:14

0001 /*
0002     SPDX-FileCopyrightText: 2009-2010 Frederik Gladhorn <gladhorn@kde.org>
0003     SPDX-FileCopyrightText: 2009-2010 Daniel Laidig <laidig@kde.org>
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "writtenpracticewidget.h"
0008 #include "ui_practice_widget_written.h"
0009 
0010 #include "guifrontend.h"
0011 #include "latexrenderer.h"
0012 
0013 #include "prefs.h"
0014 
0015 #include <KColorScheme>
0016 #include <KLocalizedString>
0017 
0018 #include <QDebug>
0019 #include <QTimer>
0020 
0021 using namespace Practice;
0022 
0023 WrittenPracticeWidget::WrittenPracticeWidget(GuiFrontend *frontend, QWidget *parent, bool isExampleSentenceMode)
0024     : AbstractModeWidget(frontend, parent)
0025 {
0026     m_ui = new Ui::WrittenPracticeWidget();
0027     m_ui->setupUi(this);
0028     m_ui->mixedSolutionLabel->setVisible(false);
0029     this->isExampleSentenceMode = isExampleSentenceMode;
0030     connect(m_ui->answerEdit, &QLineEdit::returnPressed, this, &WrittenPracticeWidget::continueClicked);
0031     connect(frontend, &AbstractFrontend::continueAction, this, &AbstractModeWidget::stopAudio);
0032     connect(frontend, &AbstractFrontend::skipAction, this, &AbstractModeWidget::stopAudio);
0033 }
0034 
0035 void WrittenPracticeWidget::setQuestionFont(const QFont &font)
0036 {
0037     m_ui->questionLabel->setFont(font);
0038 }
0039 
0040 void WrittenPracticeWidget::setSolutionFont(const QFont &font)
0041 {
0042     m_ui->answerEdit->setFont(font);
0043     m_ui->solutionLabel->setFont(font);
0044     m_ui->mixedSolutionLabel->setFont(font);
0045 }
0046 
0047 void WrittenPracticeWidget::continueClicked()
0048 {
0049     Q_EMIT continueAction();
0050 }
0051 
0052 QVariant WrittenPracticeWidget::userInput()
0053 {
0054     return QVariant(m_ui->answerEdit->text());
0055 }
0056 
0057 void WrittenPracticeWidget::setQuestion(const QVariant &question)
0058 {
0059     m_ui->questionLabel->setMinimumSize(QSize(0, 0));
0060     if (LatexRenderer::isLatex(question.toString())) {
0061         if (!m_latexRenderer) {
0062             m_latexRenderer = new LatexRenderer(this);
0063             m_latexRenderer->setResultLabel(m_ui->questionLabel);
0064         }
0065         m_latexRenderer->renderLatex(question.toString());
0066     } else {
0067         m_ui->questionLabel->setText(question.toString());
0068     }
0069 }
0070 
0071 void WrittenPracticeWidget::showQuestion()
0072 {
0073     m_ui->answerEdit->setEnabled(true);
0074     m_ui->answerEdit->clear();
0075     QTimer::singleShot(0, m_ui->answerEdit, SLOT(setFocus()));
0076     m_ui->answerEdit->setPalette(palette());
0077     m_ui->solutionLabel->setText(QString());
0078     m_ui->helpLabel->clear();
0079 
0080     for (QWidget *child : qAsConst(synonymWidgets)) {
0081         m_ui->synonymList->removeWidget(child);
0082         delete child;
0083     }
0084     synonymWidgets.clear();
0085 
0086     if (isExampleSentenceMode == false) {
0087         m_ui->questionPronunciationLabel->setVisible(m_ui->questionPronunciationLabel->isEnabled());
0088         m_ui->questionSoundButton->setVisible(m_ui->questionSoundButton->isEnabled());
0089     } else {
0090         m_ui->questionPronunciationLabel->setVisible(false);
0091         m_ui->questionSoundButton->setVisible(false);
0092     }
0093     m_ui->solutionPronunciationLabel->setVisible(false);
0094     m_ui->solutionSoundButton->setVisible(false);
0095 }
0096 
0097 void WrittenPracticeWidget::setSolution(const QVariant &solution)
0098 {
0099     m_solution = solution.toString();
0100 }
0101 
0102 void WrittenPracticeWidget::showSolution()
0103 {
0104     m_ui->solutionLabel->setText(m_solution);
0105     m_ui->answerEdit->setEnabled(false);
0106     if (m_feedbackState == AbstractFrontend::AnswerCorrect) {
0107         m_ui->answerEdit->setPalette(m_correctPalette);
0108     } else {
0109         m_ui->answerEdit->setPalette(m_wrongPalette);
0110     }
0111     m_ui->solutionLabel->setPalette(m_correctPalette);
0112 
0113     m_ui->solutionPronunciationLabel->setVisible(m_ui->solutionPronunciationLabel->isEnabled());
0114     m_ui->solutionSoundButton->setVisible(m_ui->solutionSoundButton->isEnabled());
0115 }
0116 
0117 void WrittenPracticeWidget::setSynonym(const QString &synonym)
0118 {
0119     m_synonym = synonym;
0120 }
0121 
0122 void WrittenPracticeWidget::showSynonym()
0123 {
0124     QLabel *synonym = new QLabel();
0125     QFont font;
0126     // TODO: use slightly smaller font for synonyms
0127     // font.setPointSize(9);
0128     synonym->setAlignment(Qt::AlignCenter);
0129     synonym->setFont(font);
0130     synonym->setText(i18n("Synonym: ") + m_synonym);
0131     synonymWidgets.append(synonym);
0132     m_ui->synonymList->addWidget(synonym);
0133     m_ui->answerEdit->clear();
0134 }
0135 
0136 void WrittenPracticeWidget::setHint(const QVariant &hint)
0137 {
0138     m_ui->helpLabel->setText(hint.toString());
0139 }
0140 
0141 void WrittenPracticeWidget::setFeedback(const QVariant &feedback)
0142 {
0143     m_ui->helpLabel->setText(feedback.toString());
0144 }
0145 
0146 void WrittenPracticeWidget::setFeedbackState(AbstractFrontend::ResultState feedbackState)
0147 {
0148     m_feedbackState = feedbackState;
0149 }
0150 
0151 void WrittenPracticeWidget::setResultState(AbstractFrontend::ResultState resultState)
0152 {
0153     m_resultState = resultState;
0154 }
0155 
0156 void WrittenPracticeWidget::setQuestionSound(const QUrl &soundUrl)
0157 {
0158     m_ui->questionSoundButton->setSoundFile(soundUrl);
0159 }
0160 
0161 void WrittenPracticeWidget::setSolutionSound(const QUrl &soundUrl)
0162 {
0163     m_ui->solutionSoundButton->setSoundFile(soundUrl);
0164 }
0165 
0166 void WrittenPracticeWidget::setSolutionPronunciation(const QString &pronunciationText)
0167 {
0168     m_ui->solutionPronunciationLabel->setText('[' + pronunciationText + ']');
0169     m_ui->solutionPronunciationLabel->setEnabled(!pronunciationText.isNull());
0170 }
0171 
0172 void WrittenPracticeWidget::setQuestionPronunciation(const QString &pronunciationText)
0173 {
0174     m_ui->questionPronunciationLabel->setText('[' + pronunciationText + ']');
0175     m_ui->questionPronunciationLabel->setEnabled(!pronunciationText.isNull());
0176 }
0177 
0178 #include "moc_writtenpracticewidget.cpp"