31 #include "templates/learnspellings/learnspellingssimulator.h"
33 #include "definitions/definitions.h"
34 #include "core/templatecore.h"
35 #include "core/templatefactory.h"
36 #include "gui/custommessagebox.h"
37 #include "network-web/networkfactory.h"
38 #include "miscellaneous/iofactory.h"
39 #include "miscellaneous/application.h"
40 #include "templates/learnspellings/learnspellingseditor.h"
42 #include <QInputDialog>
44 #include <QTextStream>
45 #include <QDataStream>
50 LearnSpellingsSimulator::LearnSpellingsSimulator(
TemplateCore *core, QWidget *parent)
54 QFont caption_font = m_ui->m_lblQuestionNumber->font();
55 caption_font.setPointSize(caption_font.pointSize() + SIMULATOR_HEADER_SIZE_INCREASE);
56 m_ui->m_lblQuestionNumber->setFont(caption_font);
58 caption_font = m_ui->m_lblHeading->font();
59 caption_font.setPointSize(caption_font.pointSize() + SIMULATOR_HEADING_SIZE_INCREASE);
60 m_ui->m_lblHeading->setFont(caption_font);
62 connect(m_ui->m_btnPlayWord, SIGNAL(clicked()),
this, SLOT(playWord()));
63 connect(m_ui->m_btnStart, SIGNAL(clicked()),
this, SLOT(start()));
64 connect(m_ui->m_btnRestart, SIGNAL(clicked()),
this, SLOT(restart()));
65 connect(m_ui->m_btnExit, SIGNAL(clicked()),
this, SLOT(exit()));
66 connect(m_ui->m_btnSkip, SIGNAL(clicked()),
this, SLOT(skipThisWord()));
67 connect(m_ui->m_btnSpellIt, SIGNAL(clicked()),
this, SLOT(spellThisWord()));
68 connect(m_ui->m_btnGoToNextWord, SIGNAL(clicked()),
this, SLOT(loadNextWord()));
71 LearnSpellingsSimulator::~LearnSpellingsSimulator() {
75 bool LearnSpellingsSimulator::startSimulation() {
85 m_words = editor->activeWords();
87 if (m_words.isEmpty()) {
91 m_ui->m_btnStart->setEnabled(
true);
92 m_ui->m_lblAuthor->setText(editor->m_ui->m_txtAuthor->lineEdit()->text());
93 m_ui->m_lblHeading->setText(editor->m_ui->m_txtName->lineEdit()->text());
94 m_ui->m_phoneWidget->setCurrentIndex(1);
99 bool LearnSpellingsSimulator::stopSimulation() {
100 m_ui->m_phoneWidget->setCurrentIndex(0);
107 bool LearnSpellingsSimulator::goBack() {
111 void LearnSpellingsSimulator::start() {
113 m_resultCorrect = m_resultIncorrect = m_resultSkipped = 0;
117 void LearnSpellingsSimulator::restart() {
118 m_ui->m_phoneWidget->setCurrentIndex(1);
121 void LearnSpellingsSimulator::exit() {
126 void LearnSpellingsSimulator::playWord() {
127 #if defined(Q_OS_OS2)
129 qApp->trayIcon()->showMessage(tr(
"Cannot play sound"), tr(
"Sound cannot play on this platform."),
130 QSystemTrayIcon::Warning);
133 CustomMessageBox::show(
this, QMessageBox::Warning, tr(
"Cannot play sound"), tr(
"Sound cannot play on this platform."));
141 if (current_item.audioFilePath().isEmpty() || !QFile::exists(current_item.audioFilePath())) {
143 QString word = m_words.at(m_activeWord).word().replace(
' ',
'+');
145 QString url = QString(TTS_SERVICE_URL).arg(word);
148 if (result_of_download != QNetworkReply::NoError) {
151 qApp->trayIcon()->showMessage(tr(
"Cannot play sound"), tr(
"Sound cannot play on this platform because sound file was not downloaded."),
152 QSystemTrayIcon::Warning);
155 CustomMessageBox::show(
this, QMessageBox::Warning, tr(
"Cannot play sound"), tr(
"Sound cannot play on this platform because sound file was not downloaded."));
162 QString sound_file_name = qApp->templateManager()->tempDirectory() + QDir::separator() +
163 "sound_" + QDateTime::currentDateTime().toString(
"yyyy-MM-dd-hhmmss") +
".wav";
164 QFile sound_file(sound_file_name);
165 sound_file.open(QIODevice::WriteOnly | QIODevice::Truncate | QIODevice::Unbuffered);
166 sound_file.write(output);
172 m_words[m_activeWord].setAudioFilePath(sound_file_name);
178 m_ui->m_btnSkip->setEnabled(
true);
179 m_ui->m_btnSpellIt->setEnabled(
true);
182 void LearnSpellingsSimulator::skipThisWord() {
187 void LearnSpellingsSimulator::spellThisWord() {
191 QString guessed_word = QInputDialog::getText(
this, tr(
"Enter spelling"), tr(
"Enter spelling")).simplified();
194 if (!guessed_word.isEmpty()) {
195 if (QString::compare(guessed_word, current_word.word(), Qt::CaseInsensitive) == 0) {
198 m_ui->m_lblResultCaption->setText(tr(
"This is correct spelling"));
203 m_ui->m_lblResultCaption->setText(tr(
"This is not the correct spelling"));
207 m_ui->m_lblEnteredWord->setText(tr(
"You entered %1").arg(guessed_word));
208 m_ui->m_lblCorrectWordDescription->setText(tr(
"<p style=\" font-size: 18pt;\">%1</p><p>%2</p>").arg(current_word.word(), current_word.meaning()));
209 m_ui->m_listener->setCurrentIndex(1);
212 CustomMessageBox::show(
this, QMessageBox::Warning, tr(
"Enter some word"), tr(
"You must enter some word"));
216 void LearnSpellingsSimulator::loadNextWord() {
218 m_ui->m_lblQuestionNumber->setText(tr(
"Word #%1 of %2").arg(QString::number(m_activeWord + 1),
219 QString::number(m_words.size())));
221 if (m_activeWord < m_words.size()) {
223 m_ui->m_btnSkip->setEnabled(
false);
224 m_ui->m_btnSpellIt->setEnabled(
false);
225 m_ui->m_listener->setCurrentIndex(0);
226 m_ui->m_phoneWidget->setCurrentIndex(2);
230 m_ui->m_lblTotalCorrect->setText(tr(
"Correct spelled %1").arg(QString::number(m_resultCorrect)));
231 m_ui->m_lblTotalUnanswered->setText(tr(
"Unanswered %1").arg(QString::number(m_resultSkipped)));
232 m_ui->m_lblTotalWrong->setText(tr(
"Wrond spelled %1").arg(QString::number(m_resultIncorrect)));
233 m_ui->m_phoneWidget->setCurrentIndex(3);
virtual TemplateEditor * editor() const
Access to editor widget of the template.
Editor widget of Learn Spellings template.
void canGoBackChanged(bool can_go_back)
Emitted if "can go back" status of simulator changes.
bool canGenerateApplications()
Specifies if template can generate applications or not.
Simulator widget for Learn Spellings template.
static void playWaveFile(const QString &file_path)
Plays selected file with default audio output.
TemplateCore * core() const
Access to associated template core.
static bool isSystemTrayAvailable()
Indicates whether tray icon is supported.
static QMessageBox::StandardButton show(QWidget *parent, QMessageBox::Icon icon, const QString &title, const QString &text, const QString &informative_text=QString(), const QString &detailed_text=QString(), QMessageBox::StandardButtons buttons=QMessageBox::Ok, QMessageBox::StandardButton default_button=QMessageBox::Ok)
Displays custom message box.
static QNetworkReply::NetworkError downloadFile(const QString &url, int timeout, QByteArray &output, bool protected_contents=false, const QString &username=QString(), const QString &password=QString())
Performs SYNCHRONOUS download of file with given URL and given timeout.
void simulationStopRequested()
Emitted if simulation is stopped from template itself.
The core class container for single template.
Class which represents single word for Learn Spellings template.
Base widget which represents simulator of the template.