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

klettres

  • sources
  • kde-4.12
  • kdeedu
  • klettres
  • src
soundfactory.cpp
Go to the documentation of this file.
1 /* -------------------------------------------------------------
2  From KDE Tuberling
3  mailto:e.bischoff@noos.fr
4  ------------------------------------------------------------- */
5 /*
6  * Copyright (C) 2001 Eric Bischoff
7  2004-2007 Anne-Marie Mahfouf <annma@kde.org>
8 
9  This program is free software; you can redistribute it and/or
10  modify it under the terms of the GNU General Public
11  License as published by the Free Software Foundation; either
12  version 2 of the License, or (at your option) any later version.
13 
14  This program is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  GNU General Public License for more details.
18 
19  You should have received a copy of the GNU General Public License
20  along with this program; if not, write to the Free Software
21  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22  */
23 
24 #include "soundfactory.h"
25 
26 #include <KMessageBox>
27 #include <KLocale>
28 #include <KDebug>
29 #include <KRandomSequence>
30 #include <KStandardDirs>
31 #include <Phonon/MediaObject>
32 
33 #include "klettres.h"
34 #include "prefs.h"
35 
36 SoundFactory::SoundFactory(KLettres *parent, const char *)
37  : QObject(parent), m_player(0)
38 {
39  klettres = parent;
40 
41  namesList.clear();
42  filesList.clear();
43  sounds = 0;
44 
45  bool ok = klettres->loadLayout(m_layoutsDocument);
46  if (ok) {
47  change(Prefs::language());
48  }
49  if (!ok) {
50  loadFailure();
51  } else {
52  setSoundSequence();
53  }
54 }
55 
56 SoundFactory::~SoundFactory()
57 {
58 }
59 
60 void SoundFactory::change(const QString &currentLanguage)
61 {
62  //go load the sounds for the current language
63  bool ok = loadLanguage(m_layoutsDocument, currentLanguage);
64  //tell the user if there are no sounds or get the random sounds
65  if (!ok) {
66  loadFailure();
67  } else {
68  setSoundSequence();
69  }
70 }
71 
72 void SoundFactory::playSound(int mySound)
73 {
74  QString soundFile;
75 
76  if ((uint) mySound >= sounds) {
77  return;
78  }
79 
80  soundFile = KStandardDirs::locate("data", "klettres/" + filesList[mySound]);
81  kDebug() << "soundFile " << soundFile;
82 
83  if (soundFile.isEmpty()) {
84  return;
85  }
86 
87  if (!m_player) {
88  m_player = Phonon::createPlayer(Phonon::GameCategory, soundFile);
89  m_player->setParent(this);
90  } else {
91  m_player->setCurrentSource(soundFile);
92  }
93  m_player->play();
94 }
95 
96 void SoundFactory::loadFailure()
97 {
98  KMessageBox::error(klettres, i18n("Error while loading the sound names."));
99  klettres->slotChangeLevel(Prefs::level()-1);
100  bool ok = loadLanguage(m_layoutsDocument, Prefs::language());
101  if (ok) {
102  change(Prefs::language());
103  }
104 }
105 
106 bool SoundFactory::loadLanguage(QDomDocument &layoutDocument, const QString &currentLanguage)
107 {
108  QDomNodeList languagesList,
109  alphabetList,
110  syllablesList,
111  soundNamesList;
112  QDomElement languageElement,
113  alphabetElement,
114  syllableElement,
115  soundNameElement;
116  QDomAttr nameAttribute, fileAttribute;
117 
118  languagesList = layoutDocument.elementsByTagName("language");
119  QDomAttr codeAttribute;
120  //check if the sound files match current language
121  languageElement = (const QDomElement &) languagesList.item(0).toElement();
122  codeAttribute = languageElement.attributeNode("code");
123 
124  if (currentLanguage != codeAttribute.value()) {
125  kDebug() << "Fail reading language !!! ";
126  return false;
127  } else {
128  kDebug() << "current language " << currentLanguage;
129  }
130  //check here if alphabet and syllables both exist
131  alphabetList = languageElement.elementsByTagName("alphabet");
132  syllablesList = languageElement.elementsByTagName("syllables");
133 
134  //load the sounds for level 1 and 2 (alphabet)
135  if ((Prefs::level() == 1) || (Prefs::level() == 2)) {
136  if (alphabetList.count() != 1) {
137  return false;
138  }
139  alphabetElement = (const QDomElement &) alphabetList.item(0).toElement();
140  soundNamesList = alphabetElement.elementsByTagName("sound");
141  }
142 
143  //load the sounds for level 3 and 4 (syllables)
144  if ((Prefs::level() == 3) || (Prefs::level() == 4)) {
145  if (syllablesList.count() != 1) {
146  Prefs::setLevel(1);
147  Prefs::self()->writeConfig();
148  return false;
149  }
150 
151  syllableElement = (const QDomElement &) syllablesList.item(0).toElement();
152 
153  soundNamesList = syllableElement.elementsByTagName("sound");
154  }
155  //Counts the number of sounds
156  sounds = soundNamesList.count();
157  kDebug() << "number of sounds" << sounds << endl;
158  if (sounds < 1) {
159  return false;
160  }
161  namesList.clear();
162  filesList.clear();
163 
164  for (uint sound = 0; sound < sounds; sound++) {
165  soundNameElement = (const QDomElement &) soundNamesList.item(sound).toElement();
166  nameAttribute = soundNameElement.attributeNode("name");
167  //namesList helds the names of the letter or syllable to display
168  namesList.append(nameAttribute.value());
169  fileAttribute = soundNameElement.attributeNode("file");
170  //filesList helds the names of the sound files (i.e the location of the sounds like fr/alpha/a-0.mp3)
171  filesList.append(fileAttribute.value());
172  }
173  if (namesList.isEmpty()) {
174  return false;
175  }
176  if (filesList.isEmpty()) {
177  return false;
178  }
179  return true;
180 }
181 
182 void SoundFactory::setSoundSequence()
183 {
184  // Seed the random number generator
185  KRandomSequence randomSequence;
186  randomList.clear();
187  //get the number of sounds then shuffle it: each number will be taken once then the sequence will come back
188  for (uint j = 0; j < sounds; j++)
189  randomList.append(j);
190 
191  randomSequence.randomize(randomList);
192 }
193 
194 #include "soundfactory.moc"
SoundFactory::filesList
QStringList filesList
List of sound files associated with each sound name.
Definition: soundfactory.h:67
klettres.h
Prefs::language
static QString language()
Get Language.
Definition: prefs.h:43
prefs.h
Prefs::level
static int level()
Get Difficulty level.
Definition: prefs.h:131
QObject
KLettres::loadLayout
bool loadLayout(QDomDocument &layoutDocument)
Load the xml file.
Definition: klettres.cpp:101
SoundFactory::namesList
QStringList namesList
List of sound names.
Definition: soundfactory.h:65
KLettres
Application Main Window.
Definition: klettres.h:41
Prefs::self
static Prefs * self()
Definition: prefs.cpp:17
SoundFactory::SoundFactory
SoundFactory(KLettres *parent, const char *name)
Definition: soundfactory.cpp:36
KLettres::slotChangeLevel
void slotChangeLevel(int)
Set the new level.
Definition: klettres.cpp:296
SoundFactory::~SoundFactory
~SoundFactory()
Definition: soundfactory.cpp:56
SoundFactory::sounds
uint sounds
Number of sounds corresponding to the current language and level (alphabet or syllables) ...
Definition: soundfactory.h:63
SoundFactory::loadLanguage
bool loadLanguage(QDomDocument &layoutDocument, const QString &currentLanguage)
Load the sounds of one given language Call that when you read the language from Config and when the l...
Definition: soundfactory.cpp:106
SoundFactory::randomList
QList< int > randomList
The random sequence of integers.
Definition: soundfactory.h:77
SoundFactory::change
void change(const QString &currentLanguage)
Change the language when the user changes the language in the Languages menu.
Definition: soundfactory.cpp:60
SoundFactory::klettres
KLettres * klettres
Call the main instance of the program.
Definition: soundfactory.h:57
Prefs::setLevel
static void setLevel(int v)
Set Difficulty level.
Definition: prefs.h:109
SoundFactory::playSound
void playSound(int)
Play the sound associated to int soundRef.
Definition: soundfactory.cpp:72
soundfactory.h
SoundFactory::m_layoutsDocument
QDomDocument m_layoutsDocument
The language document.
Definition: soundfactory.h:75
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:36:13 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

klettres

Skip menu "klettres"
  • Main Page
  • Namespace List
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

kdeedu API Reference

Skip menu "kdeedu API Reference"
  • Analitza
  •     lib
  • kalgebra
  • kalzium
  •   libscience
  • kanagram
  • kig
  •   lib
  • klettres
  • kstars
  • libkdeedu
  •   keduvocdocument
  • 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