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
quizsimulator.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/quizsimulator.h"
32 
33 #include "core/templatecore.h"
34 #include "templates/quiz/quizcore.h"
35 #include "templates/quiz/quizeditor.h"
36 #include "templates/quiz/quizitem.h"
37 #include "definitions/definitions.h"
38 
39 #include <QMessageBox>
40 #include <QLabel>
41 #include <QPushButton>
42 #include <QRadioButton>
43 
44 
45 QuizSimulator::QuizSimulator(TemplateCore *core, QWidget *parent)
46  : TemplateSimulator(core, parent), m_ui(new Ui::QuizSimulator) {
47  m_ui->setupUi(this);
48 
49  QFont caption_font = m_ui->m_lblHeading->font();
50  caption_font.setPointSize(caption_font.pointSize() + SIMULATOR_HEADING_SIZE_INCREASE);
51  m_ui->m_lblHeading->setFont(caption_font);
52 
53  connect(m_ui->m_btnStart, SIGNAL(clicked()), this, SLOT(start()));
54  connect(m_ui->m_btnRestart, SIGNAL(clicked()), this, SLOT(restart()));
55  connect(m_ui->m_btnExit, SIGNAL(clicked()), this, SLOT(exit()));
56 }
57 
58 QuizSimulator::~QuizSimulator() {
59  qDebug("Destroying QuizSimulator instance.");
60 
61  delete m_ui;
62 }
63 
64 bool QuizSimulator::startSimulation() {
65  QuizEditor *editor = static_cast<QuizEditor*>(core()->editor());
66 
67  if (!editor->canGenerateApplications()) {
68  // There are no active questions or quiz does not
69  // containt its name or author name.
70  return false;
71  }
72 
73  // Remove existing questions.
74  while (m_ui->m_phoneWidget->count() > 3) {
75  QWidget *question_widget = m_ui->m_phoneWidget->widget(2);
76 
77  m_ui->m_phoneWidget->removeWidget(question_widget);
78  question_widget->deleteLater();
79  }
80 
81  // Load the questions, setup the quiz and start it.
82  m_ui->m_btnStart->setEnabled(true);
83  m_ui->m_lblAuthor->setText(editor->m_ui->m_txtAuthor->lineEdit()->text());
84  m_ui->m_lblHeading->setText(editor->m_ui->m_txtName->lineEdit()->text());
85 
86  int question_number = 1;
87  QList<QuizQuestion> questions = editor->activeQuestions();
88 
89  foreach (const QuizQuestion &question, questions) {
90  QuizItem *item = new QuizItem(m_ui->m_phoneWidget);
91 
92  connect(item, SIGNAL(questionSubmitted()), this, SLOT(questionSubmitted()));
93 
94  item->setQuestion(question, question_number++, questions.size());
95  m_ui->m_phoneWidget->insertWidget(m_ui->m_phoneWidget->count() - 1, item);
96  }
97 
98  m_ui->m_phoneWidget->setCurrentIndex(1);
99  return true;
100 }
101 
102 bool QuizSimulator::stopSimulation() {
103  m_ui->m_phoneWidget->setCurrentIndex(0);
104 
105  emit canGoBackChanged(false);
106 
107  return true;
108 }
109 
110 bool QuizSimulator::goBack() {
111  return false;
112 }
113 
114 void QuizSimulator::start() {
115  m_ui->m_phoneWidget->setCurrentIndex(2);
116 }
117 
118 void QuizSimulator::prepareSummary() {
119  int answered_correctly = 0;
120  int answered_wrongly = 0;
121  int unanswered = 0;
122 
123  for (int i = 2; i < m_ui->m_phoneWidget->count() - 1; i++) {
124  QuizItem *widget = static_cast<QuizItem*>(m_ui->m_phoneWidget->widget(i));
125 
126  switch (widget->state()) {
127  case QuizItem::AnsweredCorrectly:
128  answered_correctly++;
129  break;
130 
131  case QuizItem::AnsweredWrongly:
132  answered_wrongly++;
133  break;
134 
135  case QuizItem::Unanswered:
136  default:
137  unanswered++;
138  break;
139  }
140  }
141 
142  m_ui->m_lblTotalCorrect->setText(tr("Total correct %1").arg(answered_correctly));
143  m_ui->m_lblTotalWrong->setText(tr("Total wrong %1").arg(answered_wrongly));
144  m_ui->m_lblTotalUnanswered->setText(tr("Total unanswered %1").arg(unanswered));
145 }
146 
147 void QuizSimulator::questionSubmitted() {
148  int current_index = m_ui->m_phoneWidget->currentIndex();
149 
150  if (current_index == m_ui->m_phoneWidget->count() - 2) {
151  // This is the last confirmed question. Go to "summary".
152  prepareSummary();
153  }
154 
155  m_ui->m_phoneWidget->setCurrentIndex(current_index + 1);
156 }
157 
158 void QuizSimulator::restart() {
159  // Reset all the questions.
160  for (int i = 2; i < m_ui->m_phoneWidget->count() - 1; i++) {
161  static_cast<QuizItem*>(m_ui->m_phoneWidget->widget(i))->reset();
162  }
163 
164  m_ui->m_phoneWidget->setCurrentIndex(1);
165 }
166 
167 void QuizSimulator::exit() {
168  stopSimulation();
170 }
Editor for Quiz.
Definition: quizeditor.h:51
bool canGenerateApplications()
Specifies if template can generate applications or not.
Definition: quizeditor.cpp:416
virtual TemplateEditor * editor() const
Access to editor widget of the template.
Definition: templatecore.h:84
Simulator for Quiz template.
Definition: quizsimulator.h:50
Widget which represents single question in Quiz.
Definition: quizitem.h:47
void canGoBackChanged(bool can_go_back)
Emitted if "can go back" status of simulator changes.
State state() const
Access to state of quiz question widget.
Definition: quizitem.cpp:92
QList< QuizQuestion > activeQuestions() const
Access to list of added questions.
Definition: quizeditor.cpp:141
void setQuestion(const QuizQuestion &question, int question_number, int total_questions)
Sets new question for this widget.
Definition: quizitem.cpp:74
TemplateCore * core() const
Access to associated template core.
void simulationStopRequested()
Emitted if simulation is stopped from template itself.
The core class container for single template.
Definition: templatecore.h:43
Container for one question.
Definition: quizquestion.h:41
Base widget which represents simulator of the template.