BuildmLearn Toolkit  2.0.4
BuildmLearn Toolkit is an easy-to-use program that helps users make mobile apps without any knowledge of application development.
 All Classes Functions Enumerations Groups Pages
quizitem.cpp
1 /*
2  Copyright (c) 2012, BuildmLearn Contributors listed at http://buildmlearn.org/people/
3  All rights reserved.
4 
5  Redistribution and use in source and binary forms, with or without
6  modification, are permitted provided that the following conditions are met:
7 
8  * Redistributions of source code must retain the above copyright notice, this
9  list of conditions and the following disclaimer.
10 
11  * Redistributions in binary form must reproduce the above copyright notice,
12  this list of conditions and the following disclaimer in the documentation
13  and/or other materials provided with the distribution.
14 
15  * Neither the name of the BuildmLearn nor the names of its
16  contributors may be used to endorse or promote products derived from
17  this software without specific prior written permission.
18 
19  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30 
31 #include "templates/quiz/quizitem.h"
32 
33 #include "definitions/definitions.h"
34 
35 
36 QuizItem::QuizItem(QWidget *parent) : QWidget(parent), m_state(Unanswered), m_ui(new Ui::QuizItem) {
37  m_ui->setupUi(this);
38 
39  QFont caption_font = m_ui->m_lblQuestionNumber->font();
40  caption_font.setPointSize(caption_font.pointSize() + SIMULATOR_HEADER_SIZE_INCREASE);
41  m_ui->m_lblQuestionNumber->setFont(caption_font);
42 
43  setupButtons();
44  createConnections();
45 }
46 
47 QuizItem::~QuizItem() {
48  delete m_ui;
49 }
50 
51 void QuizItem::setupButtons() {
52  m_answerButtons = QList<QRadioButton*>();
53  m_answerButtons.append(m_ui->m_rbAnswerOne);
54  m_answerButtons.append(m_ui->m_rbAnswerTwo);
55  m_answerButtons.append(m_ui->m_rbAnswerThree);
56  m_answerButtons.append(m_ui->m_rbAnswerFour);
57 }
58 
59 void QuizItem::createConnections() {
60  connect(m_ui->m_btnNext, SIGNAL(clicked()), this, SLOT(onNextClicked()));
61  connect(m_ui->m_btnConfirm, SIGNAL(clicked()), this, SLOT(onSubmitClicked()));
62  connect(m_ui->m_rbAnswerOne, SIGNAL(toggled(bool)), m_ui->m_lblWarning, SLOT(hide()));
63  connect(m_ui->m_rbAnswerTwo, SIGNAL(toggled(bool)), m_ui->m_lblWarning, SLOT(hide()));
64  connect(m_ui->m_rbAnswerThree, SIGNAL(toggled(bool)), m_ui->m_lblWarning, SLOT(hide()));
65  connect(m_ui->m_rbAnswerFour, SIGNAL(toggled(bool)), m_ui->m_lblWarning, SLOT(hide()));
66 }
67 
68 void QuizItem::clearStylesheets() {
69  foreach (QRadioButton *answer_button, m_answerButtons) {
70  answer_button->setStyleSheet(QString());
71  }
72 }
73 
74 void QuizItem::setQuestion(const QuizQuestion &question, int question_number, int total_questions) {
75  m_question = question;
76 
77  m_ui->m_rbAnswerOne->setText(question.answerOne());
78  m_ui->m_rbAnswerTwo->setText(question.answerTwo());
79  m_ui->m_rbAnswerThree->setText(question.answerThree());
80  m_ui->m_rbAnswerFour->setText(question.answerFour());
81 
82  m_ui->m_rbAnswerOne->setVisible(question.correctAnswer() == 0 || !question.answerOne().simplified().isEmpty());
83  m_ui->m_rbAnswerTwo->setVisible(question.correctAnswer() == 1 || !question.answerTwo().simplified().isEmpty());
84  m_ui->m_rbAnswerThree->setVisible(question.correctAnswer() == 2 || !question.answerThree().simplified().isEmpty());
85  m_ui->m_rbAnswerFour->setVisible(question.correctAnswer() == 3 || !question.answerFour().simplified().isEmpty());
86 
87  m_ui->m_lblQuestionNumber->setText(tr("Question number %1 of %2").arg(QString::number(question_number),
88  QString::number(total_questions)));
89  m_ui->m_lblQuestionText->setText(question.question());
90 }
91 
93  return m_state;
94 }
95 
97  clearStylesheets();
98 
99  foreach (QRadioButton *answer_button, m_answerButtons) {
100  // Hacky way to really force all radio buttons to be unchecked.
101  answer_button->setEnabled(false);
102  answer_button->setCheckable(false);
103  answer_button->setChecked(false);
104  answer_button->setEnabled(true);
105  answer_button->setCheckable(true);
106  }
107 
108  m_ui->m_lblWarning->setVisible(false);
109  m_ui->m_btnConfirm->setEnabled(true);
110  m_state = Unanswered;
111 }
112 
113 void QuizItem::onNextClicked() {
114  // Just signal that user is done with this question.
115  emit questionSubmitted();
116 }
117 
118 void QuizItem::onSubmitClicked() {
119  // Check if user selected any answer, if he did not, then remind him it.
120  if (!m_ui->m_rbAnswerOne->isChecked() && !m_ui->m_rbAnswerTwo->isChecked() &&
121  !m_ui->m_rbAnswerThree->isChecked() && !m_ui->m_rbAnswerFour->isChecked()) {
122  // No answer seems to be selected.
123  m_ui->m_lblWarning->setText("Select some answer, please.");
124  m_ui->m_lblWarning->setVisible(true);
125  }
126  else {
127  int selected_answer = 0;
128 
129  // User selected some answer, highlight correct and incorrect answer.
130  for (int i = 0; i < m_answerButtons.size(); i++) {
131  if (m_answerButtons.at(i)->isChecked()) {
132  selected_answer = i;
133  }
134  }
135 
136  if (selected_answer == m_question.correctAnswer()) {
137  m_ui->m_lblWarning->setText("That is correct answer.");
138  m_answerButtons.at(selected_answer)->setStyleSheet("background-color: green;");
139  m_state = AnsweredCorrectly;
140  }
141  else {
142  m_ui->m_lblWarning->setText("That is wrong answer.");
143  m_answerButtons.at(selected_answer)->setStyleSheet("background-color: red;");
144  m_answerButtons.at(m_question.correctAnswer())->setStyleSheet("background-color: green;");
145  m_state = AnsweredWrongly;
146  }
147 
148  foreach (QRadioButton *button, m_answerButtons) {
149  button->setEnabled(false);
150  }
151 
152  m_ui->m_btnConfirm->setEnabled(false);
153  m_ui->m_lblWarning->setVisible(true);
154  }
155 }
void reset()
Resets widget for the question to its original/default state.
Definition: quizitem.cpp:96
Widget which represents single question in Quiz.
Definition: quizitem.h:47
State state() const
Access to state of quiz question widget.
Definition: quizitem.cpp:92
void setQuestion(const QuizQuestion &question, int question_number, int total_questions)
Sets new question for this widget.
Definition: quizitem.cpp:74
State
Enum containing possible states for widget for the question.
Definition: quizitem.h:52
int correctAnswer() const
Access to inde of correct answer.
void questionSubmitted()
Emitted if users clicks "Next" or "Submit".
Container for one question.
Definition: quizquestion.h:41