File indexing completed on 2023-05-30 10:45:27

0001 /*
0002     SPDX-FileCopyrightText: 2003-2010 Peter Hedlund <peter.hedlund@kdemail.net>
0003     SPDX-License-Identifier: GPL-2.0-or-later
0004 */
0005 
0006 #include "multipleview.h"
0007 
0008 #include <QButtonGroup>
0009 #include <QActionGroup>
0010 
0011 #include <KLocalizedString>
0012 #include <KNotification>
0013 
0014 #include "kwqquizmodel.h"
0015 #include "kwqscorewidget.h"
0016 #include "prefs.h"
0017 
0018 MultipleView::MultipleView(QWidget *parent, KActionCollection *actionCollection) : KWQQuizView(parent, actionCollection)
0019 {
0020   setupUi(this);
0021   m_choicesButtons = new QButtonGroup(this);
0022   m_choicesButtons->addButton(opt1, 1);
0023   m_choicesButtons->addButton(opt2, 2);
0024   m_choicesButtons->addButton(opt3, 3);
0025   connect(m_choicesButtons, SIGNAL(buttonClicked(int)), this, SLOT(slotChoiceClicked(int)));
0026   m_choicesActions = new QActionGroup(this);
0027   connect(m_choicesActions, &QActionGroup::triggered, this, &MultipleView::slotChoiceActionTriggered);
0028   m_choicesActions->addAction(m_actionCollection->action(QStringLiteral("quiz_Opt1")));
0029   m_choicesActions->addAction(m_actionCollection->action(QStringLiteral("quiz_Opt2")));
0030   m_choicesActions->addAction(m_actionCollection->action(QStringLiteral("quiz_Opt3")));
0031   m_actionCollection->action(QStringLiteral("quiz_Opt1"))->setData(1);
0032   m_actionCollection->action(QStringLiteral("quiz_Opt2"))->setData(2);
0033   m_actionCollection->action(QStringLiteral("quiz_Opt3"))->setData(3);
0034 }
0035 
0036 void MultipleView::init()
0037 {
0038   m_choices.clear();
0039 
0040   score->clear();
0041   score->setQuestionCount(m_quiz->questionCount());
0042   score->setAsPercent(Prefs::percent());
0043 
0044   opt1->show();
0045   opt2->show();
0046   opt3->show();
0047 
0048   lblQuestion->setFont(Prefs::editorFont());
0049   lblPreviousQuestion->setFont(Prefs::editorFont());
0050   lblYourAnswer->setFont(Prefs::editorFont());
0051   lblCorrect->setFont(Prefs::editorFont());
0052   opt1->setFont(Prefs::editorFont());
0053   opt2->setFont(Prefs::editorFont());
0054   opt3->setFont(Prefs::editorFont());
0055 
0056   lblPreviousQuestionHeader->clear();
0057   lblPreviousQuestion->clear();
0058   lblYourAnswerHeader->clear();
0059   lblYourAnswer->clear();
0060   lblCorrectHeader->clear();
0061   lblCorrect->clear();
0062 
0063   picPrevious->clear();
0064   picYourAnswer->clear();
0065   picCorrectAnswer->clear();
0066 
0067   m_actionCollection->action(QStringLiteral("quiz_check"))->setEnabled(true);
0068   m_actionCollection->action(QStringLiteral("quiz_repeat_errors"))->setEnabled(false);
0069   m_actionCollection->action(QStringLiteral("quiz_export_errors"))->setEnabled(false);
0070   m_actionCollection->action(QStringLiteral("quiz_audio_play"))->setEnabled(false);
0071   m_actionCollection->action(QStringLiteral("quiz_Opt1"))->setEnabled(true);
0072   m_actionCollection->action(QStringLiteral("quiz_Opt2"))->setEnabled(true);
0073   m_actionCollection->action(QStringLiteral("quiz_Opt3"))->setEnabled(true);
0074 
0075   showQuestion();
0076 }
0077 
0078 void MultipleView::slotCheck()
0079 {
0080   if (m_actionCollection->action(QStringLiteral("quiz_check"))->isEnabled())
0081   {
0082     if (m_choicesButtons->checkedId() == -1)
0083         return;
0084 
0085     QString ans = m_choices[m_choicesButtons->checkedId() - 1];
0086 
0087     bool fIsCorrect = m_quiz->checkAnswer(ans);
0088 
0089     if (fIsCorrect)
0090     {
0091       picYourAnswer->setPixmap(QIcon::fromTheme(QStringLiteral("answer-correct")).pixmap(32));
0092       lblCorrectHeader->clear();
0093       picCorrectAnswer->clear();
0094       lblCorrect->clear();
0095       score->countIncrement(KWQScoreWidget::cdCorrect);
0096       KNotification::event(QStringLiteral("QuizCorrect"), i18n("Your answer was correct!"));
0097     }
0098     else
0099     {
0100       picYourAnswer->setPixmap(QIcon::fromTheme(QStringLiteral("error")).pixmap(32));
0101       lblCorrect->setText(m_quiz->answer());
0102       picCorrectAnswer->setPixmap(QIcon::fromTheme(QStringLiteral("answer-correct")).pixmap(32));
0103       lblCorrectHeader->setText(i18n("Correct Answer"));
0104       score->countIncrement(KWQScoreWidget::cdError);
0105       KNotification::event(QStringLiteral("QuizError"), i18n("Your answer was incorrect."));
0106     }
0107 
0108     lblPreviousQuestionHeader->setText(i18n("Previous Question"));
0109     lblPreviousQuestion->setText(m_quiz->question());
0110     picPrevious->setPixmap(QIcon::fromTheme(QStringLiteral("question")).pixmap(32));
0111 
0112     lblYourAnswerHeader->setText(i18n("Your Answer"));
0113     lblYourAnswer->setText(m_quiz->yourAnswer(ans));
0114 
0115     m_quiz->toNext();
0116     if (!m_quiz->atEnd())
0117     {
0118       showQuestion();
0119     }
0120     else
0121     {
0122       m_quiz->finish();
0123       m_actionCollection->action(QStringLiteral("quiz_check"))->setEnabled(false);
0124       m_actionCollection->action(QStringLiteral("quiz_Opt1"))->setEnabled(false);
0125       m_actionCollection->action(QStringLiteral("quiz_Opt2"))->setEnabled(false);
0126       m_actionCollection->action(QStringLiteral("quiz_Opt3"))->setEnabled(false);
0127       m_actionCollection->action(QStringLiteral("quiz_repeat_errors"))->setEnabled(m_quiz->hasErrors());
0128       m_actionCollection->action(QStringLiteral("quiz_export_errors"))->setEnabled(m_quiz->hasErrors());
0129       m_actionCollection->action(QStringLiteral("quiz_audio_play"))->setEnabled(false);
0130 
0131       lblQuestionLanguage->setText(i18n("Summary"));
0132       lblQuestion->clear();
0133       lblAnswerLanguage->clear();
0134       opt1->hide();
0135       opt2->hide();
0136       opt3->hide();
0137       picQuestion->setPixmap(QIcon::fromTheme(QStringLiteral("kwordquiz")).pixmap(32));
0138       picAnswer->clear();
0139     }
0140   }
0141 }
0142 
0143 void MultipleView::slotChoiceActionTriggered(QAction *choice)
0144 {
0145   slotChoiceClicked(choice->data().toInt());
0146 }
0147 
0148 void MultipleView::slotChoiceClicked(int choice)
0149 {
0150   m_choicesButtons->button(choice)->setChecked(true);
0151   if (Prefs::autoCheck())
0152     slotCheck();
0153 }
0154 
0155 /*!
0156     \fn MultipleView::showQuestion()
0157  */
0158 void MultipleView::showQuestion()
0159 {
0160   lblQuestionLanguage->setText(m_quiz ->langQuestion());
0161   lblQuestion->setText(m_quiz ->question());
0162 
0163   picQuestion->setPixmap(QIcon::fromTheme(m_quiz->quizIcon(KWQQuizModel::IconLeftCol)).pixmap(32));
0164 
0165   lblAnswerLanguage->setText(m_quiz ->langAnswer());
0166 
0167   m_choices = m_quiz->multiOptions();
0168 
0169   opt1->setText("&1 " + m_choices[0]);
0170   opt2->setText("&2 " + m_choices[1]);
0171   opt3->setText("&3 " + m_choices[2]);
0172 
0173   m_choicesButtons->setExclusive(false);
0174   opt1->setChecked(false);
0175   opt2->setChecked(false);
0176   opt3->setChecked(false);
0177   m_choicesButtons->setExclusive(true);
0178   setFocus();
0179   picAnswer->setPixmap(QIcon::fromTheme(m_quiz->quizIcon(KWQQuizModel::IconRightCol)).pixmap(32));
0180 }
0181 
0182 void MultipleView::slotApplySettings()
0183 {
0184   score->setAsPercent(Prefs::percent());
0185 }