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
flashcardsimulator.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/flashcard/flashcardsimulator.h"
32 
33 #include "core/templatecore.h"
34 #include "templates/flashcard/flashcardeditor.h"
35 #include "templates/flashcard/flashcarditem.h"
36 #include "definitions/definitions.h"
37 #include "miscellaneous/application.h"
38 #include "miscellaneous/skinfactory.h"
39 
40 
41 FlashCardSimulator::FlashCardSimulator(TemplateCore *core, QWidget *parent)
42  : TemplateSimulator(core, parent),
43  m_ui(new Ui::FlashCardSimulator) {
44  m_ui->setupUi(this);
45 
46  QFont caption_font = m_ui->m_lblHeading->font();
47  caption_font.setPointSize(caption_font.pointSize() + SIMULATOR_HEADING_SIZE_INCREASE);
48  m_ui->m_lblHeading->setFont(caption_font);
49 
50  connect(m_ui->m_btnStart, SIGNAL(clicked()), this, SLOT(start()));
51  connect(m_ui->m_btnRestart, SIGNAL(clicked()), this, SLOT(restart()));
52 }
53 
54 FlashCardSimulator::~FlashCardSimulator() {
55  qDebug("Destroying FlashCardSimulator instance.");
56 
57  delete m_ui;
58 }
59 
60 bool FlashCardSimulator::startSimulation() {
61  FlashCardEditor *editor = static_cast<FlashCardEditor*>(core()->editor());
62 
63  if (!editor->canGenerateApplications()) {
64  // There are no active questions or quiz does not
65  // containt its name or author name.
66  return false;
67  }
68 
69  // Remove existing flash cards.
70  while (m_ui->m_phoneWidget->count() > 3) {
71  QWidget *question_widget = m_ui->m_phoneWidget->widget(2);
72 
73  m_ui->m_phoneWidget->removeWidget(question_widget);
74  question_widget->deleteLater();
75  }
76 
77  // Load the questions, setup the quiz and start it.
78  m_ui->m_btnStart->setEnabled(true);
79  m_ui->m_lblAuthor->setText(editor->m_ui->m_txtAuthor->lineEdit()->text());
80  m_ui->m_lblHeading->setText(editor->m_ui->m_txtName->lineEdit()->text());
81 
82  int question_number = 1;
83  QList<FlashCardQuestion> questions = editor->activeQuestions();
84 
85  foreach (const FlashCardQuestion &question, questions) {
86  FlashCardItem *item = new FlashCardItem(m_ui->m_phoneWidget);
87 
88  connect(item, SIGNAL(nextCardRequested()), this, SLOT(moveToNextCard()));
89  connect(item, SIGNAL(previousCardRequested()), this, SLOT(moveToPreviousCard()));
90 
91  item->setQuestion(question, question_number++, questions.size());
92  m_ui->m_phoneWidget->insertWidget(m_ui->m_phoneWidget->count() - 1, item);
93  }
94 
95  // Go to "start" page and begin.
96  m_ui->m_phoneWidget->setCurrentIndex(1);
97  return true;
98 }
99 
100 bool FlashCardSimulator::stopSimulation() {
101  m_ui->m_phoneWidget->setCurrentIndex(0);
102 
103  emit canGoBackChanged(false);
104 
105  return true;
106 }
107 
108 bool FlashCardSimulator::goBack() {
109  return false;
110 }
111 
112 void FlashCardSimulator::start() {
113  moveToNextCard();
114 }
115 
116 void FlashCardSimulator::restart() {
117  m_ui->m_phoneWidget->setCurrentIndex(1);
118 }
119 
120 void FlashCardSimulator::moveToNextCard() {
121  int current_index = m_ui->m_phoneWidget->currentIndex();
122 
123  if (current_index < m_ui->m_phoneWidget->count() - 2) {
124  // We are not on the last flash card.
125  static_cast<FlashCardItem*>(m_ui->m_phoneWidget->widget(current_index + 1))->reset();
126  }
127 
128  m_ui->m_phoneWidget->setCurrentIndex(current_index + 1);
129 }
130 
131 void FlashCardSimulator::moveToPreviousCard() {
132  int current_index = m_ui->m_phoneWidget->currentIndex();
133 
134  if (current_index > 1) {
135  // We are not on the first flash card.
136  static_cast<FlashCardItem*>(m_ui->m_phoneWidget->widget(current_index - 1))->reset();
137  }
138 
139  m_ui->m_phoneWidget->setCurrentIndex(current_index - 1);
140 }
virtual TemplateEditor * editor() const
Access to editor widget of the template.
Definition: templatecore.h:84
void setQuestion(const FlashCardQuestion &question, int question_number, int total_questions)
Sets new question for this widget.
void canGoBackChanged(bool can_go_back)
Emitted if "can go back" status of simulator changes.
TemplateCore * core() const
Access to associated template core.
The core class container for single template.
Definition: templatecore.h:43
bool canGenerateApplications()
Specifies if template can generate applications or not.
Base widget which represents simulator of the template.