File indexing completed on 2024-04-14 03:49:09

0001 /*
0002     SPDX-FileCopyrightText: 2010 Frederik Gladhorn <gladhorn@kde.org>
0003     SPDX-License-Identifier: GPL-2.0-or-later
0004 */
0005 
0006 #include "conjugationmodewidget.h"
0007 #include "conjugationdata.h"
0008 
0009 #include "ui_practice_widget_conjugation.h"
0010 
0011 #include <KColorScheme>
0012 #include <KLocalizedString>
0013 #include <QDebug>
0014 #include <QRadioButton>
0015 #include <QTimer>
0016 
0017 namespace Practice
0018 {
0019 struct PersonConjugationSolutionWidgets {
0020     QLabel *person;
0021     QLineEdit *input;
0022     QLabel *solution;
0023 
0024     PersonConjugationSolutionWidgets(QGridLayout *layout, QWidget *parent = nullptr)
0025     {
0026         person = new QLabel(parent);
0027         person->setAlignment(Qt::AlignRight);
0028         input = new QLineEdit(parent);
0029         solution = new QLabel(parent);
0030         int row = layout->rowCount();
0031         layout->addWidget(person, row, 0);
0032         layout->addWidget(input, row, 1);
0033         layout->addWidget(solution, row, 2);
0034     }
0035 
0036     void setVisible(bool visible)
0037     {
0038         person->setVisible(visible);
0039         input->setVisible(visible);
0040         solution->setVisible(visible);
0041     }
0042 };
0043 }
0044 
0045 using namespace Practice;
0046 
0047 ConjugationModeWidget::ConjugationModeWidget(GuiFrontend *frontend, QWidget *parent)
0048     : AbstractModeWidget(frontend, parent)
0049 {
0050     m_ui = new Ui::ConjugationPracticeWidget();
0051     m_ui->setupUi(this);
0052 }
0053 
0054 ConjugationModeWidget::~ConjugationModeWidget()
0055 {
0056     qDeleteAll(m_personWidgets);
0057 }
0058 
0059 void ConjugationModeWidget::setQuestionFont(const QFont &font)
0060 {
0061     m_ui->questionLabel->setFont(font);
0062 }
0063 
0064 void ConjugationModeWidget::setSolutionFont(const QFont &font)
0065 {
0066     m_ui->infinitiveEdit->setFont(font);
0067     m_solutionFont = font;
0068 }
0069 
0070 void ConjugationModeWidget::setQuestion(const QVariant &question)
0071 {
0072     if (!question.canConvert<ConjugationData>()) {
0073         qWarning() << "expected ConjugationData";
0074         return;
0075     }
0076     ConjugationData data = question.value<ConjugationData>();
0077 
0078     // It's annoying to have to search for information, so put everything into one place if
0079     // there`s only one pronoun
0080     if (data.personalPronouns.size() > 1) {
0081         m_ui->tenseLabel->setText(data.tense);
0082     } else {
0083         m_ui->tenseLabel->clear();
0084     }
0085 
0086     m_ui->questionLabel->setText(data.questionInfinitive);
0087     m_ui->infinitiveEdit->setText(data.solutionInfinitive);
0088 
0089     setNumberOfConjugationWidgets(data.personalPronouns.size());
0090     int i = 0;
0091     for (QString pp : qAsConst(data.personalPronouns)) {
0092         if (data.personalPronouns.size() == 1) {
0093             pp += " (" + data.tense + ')';
0094         }
0095         m_personWidgets.at(i)->person->setText(pp);
0096         m_personWidgets.at(i)->person->setFont(m_solutionFont);
0097         m_personWidgets.at(i)->input->clear();
0098         m_personWidgets.at(i)->input->setPalette(QApplication::palette());
0099         m_personWidgets.at(i)->input->setFont(m_solutionFont);
0100         m_personWidgets.at(i)->solution->clear();
0101         m_personWidgets.at(i)->solution->setFont(m_solutionFont);
0102         connect(m_personWidgets.at(i)->input, &QLineEdit::returnPressed, this, &ConjugationModeWidget::nextConjugationForm);
0103         ++i;
0104     }
0105 }
0106 
0107 void ConjugationModeWidget::showQuestion()
0108 {
0109     Q_ASSERT(!m_personWidgets.isEmpty());
0110     QTimer::singleShot(0, m_personWidgets.at(0)->input, SLOT(setFocus()));
0111     m_ui->feedbackLabel->setText(i18n("Enter all conjugation forms."));
0112 }
0113 
0114 void ConjugationModeWidget::setSolution(const QVariant &solution)
0115 {
0116     m_solution = solution.toStringList();
0117 }
0118 
0119 void ConjugationModeWidget::setFeedback(const QVariant &feedback)
0120 {
0121     m_ui->feedbackLabel->setText(feedback.toString());
0122 }
0123 
0124 void ConjugationModeWidget::showSolution()
0125 {
0126     for (int i = 0; i < m_solution.size() && i < m_personWidgets.size(); ++i) {
0127         if (m_personWidgets.at(i)->input->text() == m_solution.at(i)) {
0128             m_personWidgets.at(i)->input->setPalette(m_correctPalette);
0129         } else {
0130             m_personWidgets.at(i)->input->setPalette(m_wrongPalette);
0131             m_personWidgets.at(i)->solution->setPalette(m_correctPalette);
0132             m_personWidgets.at(i)->solution->setText(m_solution.at(i));
0133         }
0134     }
0135 }
0136 
0137 QVariant ConjugationModeWidget::userInput()
0138 {
0139     QStringList answers;
0140     for (int i = 0; i < m_personWidgets.size(); ++i) {
0141         answers.append(m_personWidgets.at(i)->input->text());
0142     }
0143     return answers;
0144 }
0145 
0146 void ConjugationModeWidget::setHint(const QVariant & /*hint*/)
0147 {
0148 }
0149 
0150 void ConjugationModeWidget::setNumberOfConjugationWidgets(const int numberOfForms)
0151 {
0152     while (m_personWidgets.size() < numberOfForms) {
0153         PersonConjugationSolutionWidgets *w = new PersonConjugationSolutionWidgets(m_ui->conjugationsLayout, this);
0154         m_personWidgets.append(w);
0155     }
0156     for (int i = 0; i < m_personWidgets.size(); ++i) {
0157         m_personWidgets[i]->setVisible(i < numberOfForms);
0158     }
0159 }
0160 
0161 void ConjugationModeWidget::nextConjugationForm()
0162 {
0163     for (int i = 0; i < m_personWidgets.count(); ++i) {
0164         if (sender() == m_personWidgets.at(i)->input) {
0165             if ((i + 1 < m_personWidgets.count()) && (m_personWidgets.at(i + 1)->input->isVisible())) {
0166                 // move to the next input widget
0167                 m_personWidgets.at(i + 1)->input->setFocus();
0168             } else {
0169                 Q_EMIT continueAction();
0170             }
0171             return;
0172         }
0173     }
0174 }
0175 
0176 void ConjugationModeWidget::setQuestionPronunciation(const QString & /*pronunciationText*/)
0177 {
0178 }
0179 
0180 void ConjugationModeWidget::setSolutionPronunciation(const QString & /*pronunciationText*/)
0181 {
0182 }
0183 
0184 void ConjugationModeWidget::setQuestionSound(const QUrl & /*soundUrl*/)
0185 {
0186 }
0187 void ConjugationModeWidget::setSolutionSound(const QUrl & /*soundUrl*/)
0188 {
0189 }
0190 
0191 #include "moc_conjugationmodewidget.cpp"