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

parley

  • sources
  • kde-4.14
  • kdeedu
  • parley
  • src
  • practice
multiplechoicemodewidget.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  Copyright 2009 Frederik Gladhorn <gladhorn@kde.org>
3  ***************************************************************************/
4 
5 /***************************************************************************
6  * *
7  * This program is free software; you can redistribute it and/or modify *
8  * it under the terms of the GNU General Public License as published by *
9  * the Free Software Foundation; either version 2 of the License, or *
10  * (at your option) any later version. *
11  * *
12  ***************************************************************************/
13 
14 #include "multiplechoicemodewidget.h"
15 #include "multiplechoicedata.h"
16 #include "latexrenderer.h"
17 #include "guifrontend.h"
18 
19 #include "ui_practice_widget_multiplechoice.h"
20 
21 #include <KDebug>
22 #include <kcolorscheme.h>
23 #include <QtGui/QRadioButton>
24 #include <QTimer>
25 #include <QKeyEvent>
26 #include <QVBoxLayout>
27 
28 using namespace Practice;
29 
30 
31 MultiplechoiceModeWidget::MultiplechoiceModeWidget(GuiFrontend *frontend, QWidget* parent)
32  : AbstractModeWidget(frontend, parent), m_latexRenderer(0)
33 {
34  m_ui = new Ui::MultiplechoicePracticeWidget();
35  m_ui->setupUi(this);
36 
37  for (int i = 0; i < 5; i++) {
38  QAction *action = new QAction(this);
39  action->setShortcut(Qt::Key_1 + i);
40  addAction(action);
41  m_actions.append(action);
42  }
43  connect(frontend, SIGNAL(continueAction()), this, SIGNAL(stopAudio()));
44  connect(frontend, SIGNAL(skipAction()), this, SIGNAL(stopAudio()));
45 
46 }
47 
48 void MultiplechoiceModeWidget::setQuestionFont(const QFont& font)
49 {
50  m_ui->questionLabel->setFont(font);
51 }
52 
53 void MultiplechoiceModeWidget::setSolutionFont(const QFont& font)
54 {
55  m_solutionFont = font;
56  foreach(QRadioButton * radio, m_choiceButtons) {
57  radio->setFont(m_solutionFont);
58  }
59 }
60 
61 void MultiplechoiceModeWidget::setQuestion(const QVariant& question)
62 {
63  if (!question.canConvert<MultipleChoiceData>()) {
64  kWarning() << "expected MultipleChoiceData";
65  return;
66  }
67  MultipleChoiceData data = question.value<MultipleChoiceData>();
68 
69  m_ui->questionLabel->setMinimumSize(QSize(0, 0));
70  if (LatexRenderer::isLatex(data.question)) {
71  if (!m_latexRenderer) {
72  m_latexRenderer = new LatexRenderer(this);
73  m_latexRenderer->setResultLabel(m_ui->questionLabel);
74  }
75  m_latexRenderer->renderLatex(data.question);
76  } else {
77  m_ui->questionLabel->setText(data.question);
78  }
79 
80  if (m_choiceButtons.size() != data.choices.size()) {
81  qDeleteAll(m_choiceButtons);
82  m_choiceButtons.clear();
83  setNumberOfRadioButtons(data.choices.size());
84  }
85 
86  int j = 0;
87  foreach(QRadioButton * radio, m_choiceButtons) {
88  radio->setText(data.choices[j]);
89  radio->setToolTip(data.choices[j]);
90  radio->setFont(m_solutionFont);
91  j++;
92  }
93 }
94 
95 void MultiplechoiceModeWidget::showQuestion()
96 {
97  //necessary trick to uncheck'em all
98  m_choiceButtons[0]->setChecked(true);
99  m_choiceButtons[0]->setAutoExclusive(false);
100  m_choiceButtons[0]->setChecked(false);
101  m_choiceButtons[0]->setAutoExclusive(true);
102 
103  m_ui->questionPronunciationLabel->setVisible(m_ui->questionPronunciationLabel->isEnabled());
104  m_ui->questionSoundButton->setVisible(m_ui->questionSoundButton->isEnabled());
105  m_ui->solutionPronunciationLabel->setVisible(false);
106  m_ui->solutionSoundButton->setVisible(false);
107  m_ui->feedbackLabel->clear();
108 
109  foreach(QRadioButton * radio, m_choiceButtons) {
110  radio->setPalette(palette());
111  radio->setEnabled(true);
112  }
113 
114  QTimer::singleShot(0, m_choiceButtons[0], SLOT(setFocus()));
115 }
116 
117 void MultiplechoiceModeWidget::setNumberOfRadioButtons(const int numberOfChoices)
118 {
119  QVBoxLayout *verticalLayout = new QVBoxLayout();
120  m_ui->gridLayout->addLayout(verticalLayout, 2, 0);
121 
122  for (int i = 0; i < numberOfChoices; i++) {
123  QRadioButton *radio_button = new QRadioButton(this);
124  verticalLayout->addWidget(radio_button);
125  m_choiceButtons.append(radio_button);
126  if (i < 5) {
127  connect(m_actions.at(i), SIGNAL(triggered()), radio_button, SLOT(click()));
128  }
129  connect(radio_button, SIGNAL(clicked()), this, SIGNAL(continueAction()));
130  radio_button->installEventFilter(this);
131  }
132 }
133 
134 void MultiplechoiceModeWidget::setSynonym(const QString& /*entry*/)
135 {
136  //TODO Do something here to show synonyms
137 }
138 
139 
140 void MultiplechoiceModeWidget::showSynonym()
141 {
142  //TODO Do something here to show synonyms
143 }
144 
145 bool MultiplechoiceModeWidget::eventFilter(QObject *obj, QEvent *event)
146 {
147  // make it possible to use the enter key to select multiple choice options
148  if (event->type() == QEvent::KeyPress) {
149  QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
150  if (keyEvent->key() == Qt::Key_Enter || keyEvent->key() == Qt::Key_Return) {
151  QRadioButton *radioButton = qobject_cast<QRadioButton*>(obj);
152  if (radioButton) {
153  radioButton->click();
154  return true;
155  }
156  }
157  }
158 
159  return QObject::eventFilter(obj, event);
160 }
161 
162 void MultiplechoiceModeWidget::setSolution(const QVariant& solution)
163 {
164  m_solution = solution.toInt();
165 }
166 
167 void MultiplechoiceModeWidget::setHint(const QVariant& hint)
168 {
169  m_choiceButtons.at(hint.toInt())->setEnabled(false);
170 }
171 
172 void MultiplechoiceModeWidget::setFeedback(const QVariant& feedback)
173 {
174  m_ui->feedbackLabel->setText(feedback.toString());
175 }
176 
177 void MultiplechoiceModeWidget::showSolution()
178 {
179  int input = -1;
180  if (userInput().isValid()) {
181  input = userInput().toInt();
182  }
183  m_choiceButtons[m_solution]->setPalette(m_correctPalette);
184  if (input != -1 && input != m_solution) {
185  m_choiceButtons[input]->setPalette(m_wrongPalette);
186  }
187  foreach(QRadioButton * radio, m_choiceButtons) {
188  radio->setEnabled(false);
189  }
190  m_ui->solutionPronunciationLabel->setVisible(m_ui->solutionPronunciationLabel->isEnabled());
191  m_ui->solutionSoundButton->setVisible(m_ui->solutionSoundButton->isEnabled());
192 }
193 
194 QVariant MultiplechoiceModeWidget::userInput()
195 {
196 
197  int i = 0;
198  foreach(QRadioButton * radio, m_choiceButtons) {
199  if (radio->isChecked()) return i;
200  i++;
201  }
202 
203  return QVariant();
204 }
205 
206 void MultiplechoiceModeWidget::setQuestionSound(const KUrl& soundUrl)
207 {
208  m_ui->questionSoundButton->setSoundFile(soundUrl);
209 }
210 
211 void MultiplechoiceModeWidget::setSolutionSound(const KUrl& soundUrl)
212 {
213  m_ui->solutionSoundButton->setSoundFile(soundUrl);
214 }
215 
216 void MultiplechoiceModeWidget::setSolutionPronunciation(const QString& pronunciationText)
217 {
218  m_ui->solutionPronunciationLabel->setText('[' + pronunciationText + ']');
219  m_ui->solutionPronunciationLabel->setEnabled(!pronunciationText.isNull());
220 }
221 
222 void MultiplechoiceModeWidget::setQuestionPronunciation(const QString& pronunciationText)
223 {
224  m_ui->questionPronunciationLabel->setText('[' + pronunciationText + ']');
225  m_ui->questionPronunciationLabel->setEnabled(!pronunciationText.isNull());
226 }
227 
228 #include "multiplechoicemodewidget.moc"
QVariant::canConvert
bool canConvert(Type t) const
Practice::MultiplechoiceModeWidget::setNumberOfRadioButtons
virtual void setNumberOfRadioButtons(const int numberOfChoices)
Definition: multiplechoicemodewidget.cpp:117
QList::clear
void clear()
QEvent
QWidget
QEvent::type
Type type() const
QWidget::setPalette
void setPalette(const QPalette &)
Practice::GuiFrontend
Definition: guifrontend.h:35
Practice::MultiplechoiceModeWidget::setSolution
virtual void setSolution(const QVariant &solution)
Definition: multiplechoicemodewidget.cpp:162
Practice::MultiplechoiceModeWidget::showSynonym
virtual void showSynonym()
Definition: multiplechoicemodewidget.cpp:140
Practice::MultiplechoiceModeWidget::setQuestion
virtual void setQuestion(const QVariant &question)
Definition: multiplechoicemodewidget.cpp:61
Practice::MultiplechoiceModeWidget::MultiplechoiceModeWidget
MultiplechoiceModeWidget(GuiFrontend *frontend, QWidget *parent=0)
Definition: multiplechoicemodewidget.cpp:31
QWidget::addAction
void addAction(QAction *action)
Practice::MultipleChoiceData::question
QString question
Definition: multiplechoicedata.h:26
Practice::MultiplechoiceModeWidget::eventFilter
virtual bool eventFilter(QObject *obj, QEvent *event)
Definition: multiplechoicemodewidget.cpp:145
Practice::MultiplechoiceModeWidget::userInput
virtual QVariant userInput()
Definition: multiplechoicemodewidget.cpp:194
QFont
QList::at
const T & at(int i) const
Practice::MultiplechoiceModeWidget::showQuestion
virtual void showQuestion()
Definition: multiplechoicemodewidget.cpp:95
Practice::AbstractModeWidget::continueAction
void continueAction()
QVariant::value
T value() const
Practice::LatexRenderer
Definition: latexrenderer.h:27
Practice::MultiplechoiceModeWidget::setSolutionSound
virtual void setSolutionSound(const KUrl &soundUrl)
Definition: multiplechoicemodewidget.cpp:211
Practice::MultipleChoiceData
Definition: multiplechoicedata.h:25
Practice::LatexRenderer::setResultLabel
void setResultLabel(QLabel *label)
Definition: latexrenderer.h:33
Practice::AbstractModeWidget
Definition: abstractwidget.h:27
multiplechoicedata.h
QList::size
int size() const
QString::isNull
bool isNull() const
Practice::AbstractModeWidget::skipAction
void skipAction()
Practice::MultipleChoiceData::choices
QStringList choices
Definition: multiplechoicedata.h:27
QWidget::setEnabled
void setEnabled(bool)
QBoxLayout::addWidget
void addWidget(QWidget *widget, int stretch, QFlags< Qt::AlignmentFlag > alignment)
QList::append
void append(const T &value)
QObject::installEventFilter
void installEventFilter(QObject *filterObj)
QVariant::toInt
int toInt(bool *ok) const
Practice::MultiplechoiceModeWidget::setQuestionFont
virtual void setQuestionFont(const QFont &font)
Definition: multiplechoicemodewidget.cpp:48
QObject
QWidget::setFocus
void setFocus()
Practice::AbstractModeWidget::m_correctPalette
QPalette m_correctPalette
Definition: abstractwidget.h:76
QVBoxLayout
QObject::eventFilter
virtual bool eventFilter(QObject *watched, QEvent *event)
Practice::MultiplechoiceModeWidget::setHint
virtual void setHint(const QVariant &hint)
Definition: multiplechoicemodewidget.cpp:167
Practice::MultiplechoiceModeWidget::setFeedback
virtual void setFeedback(const QVariant &feedback)
Definition: multiplechoicemodewidget.cpp:172
QString
Practice::AbstractModeWidget::m_wrongPalette
QPalette m_wrongPalette
Definition: abstractwidget.h:77
QKeyEvent::key
int key() const
multiplechoicemodewidget.h
QSize
QWidget::font
const QFont & font() const
QAction::setShortcut
void setShortcut(const QKeySequence &shortcut)
Practice::MultiplechoiceModeWidget::showSolution
virtual void showSolution()
Definition: multiplechoicemodewidget.cpp:177
Practice::MultiplechoiceModeWidget::setSynonym
virtual void setSynonym(const QString &entry)
Definition: multiplechoicemodewidget.cpp:134
QAbstractButton::isChecked
bool isChecked() const
QKeyEvent
Practice::MultiplechoiceModeWidget::setSolutionFont
virtual void setSolutionFont(const QFont &font)
Definition: multiplechoicemodewidget.cpp:53
guifrontend.h
Practice::MultiplechoiceModeWidget::setSolutionPronunciation
virtual void setSolutionPronunciation(const QString &pronunciationText)
Definition: multiplechoicemodewidget.cpp:216
QRadioButton
QAction
latexrenderer.h
QAbstractButton::setText
void setText(const QString &text)
Practice::MultiplechoiceModeWidget::setQuestionSound
virtual void setQuestionSound(const KUrl &soundUrl)
Definition: multiplechoicemodewidget.cpp:206
QWidget::setToolTip
void setToolTip(const QString &)
QObject::connect
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QVariant::toString
QString toString() const
QWidget::event
virtual bool event(QEvent *event)
Practice::LatexRenderer::renderLatex
void renderLatex(QString tex)
Definition: latexrenderer.cpp:51
Practice::AbstractModeWidget::stopAudio
void stopAudio()
Practice::LatexRenderer::isLatex
static bool isLatex(const QString &tex)
Definition: latexrenderer.cpp:96
QAbstractButton::click
void click()
Practice::MultiplechoiceModeWidget::setQuestionPronunciation
virtual void setQuestionPronunciation(const QString &pronunciationText)
Definition: multiplechoicemodewidget.cpp:222
QTimer::singleShot
singleShot
QVariant
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:15:56 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
  • 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