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

klettres

  • sources
  • kde-4.14
  • 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 <KRandomSequence>
29 #include <KStandardDirs>
30 #include <Phonon/MediaObject>
31 
32 #include "klettres.h"
33 #include "prefs.h"
34 
35 SoundFactory::SoundFactory(KLettres *parent, const char *)
36  : QObject(parent), m_player(0)
37 {
38  klettres = parent;
39 
40  namesList.clear();
41  filesList.clear();
42  sounds = 0;
43 
44  bool ok = klettres->loadLayout(m_layoutsDocument);
45  if (ok) {
46  change(Prefs::language());
47  }
48  if (!ok) {
49  loadFailure();
50  } else {
51  setSoundSequence();
52  }
53 }
54 
55 SoundFactory::~SoundFactory()
56 {
57 }
58 
59 void SoundFactory::change(const QString &currentLanguage)
60 {
61  //go load the sounds for the current language
62  bool ok = loadLanguage(m_layoutsDocument, currentLanguage);
63  //tell the user if there are no sounds or get the random sounds
64  if (!ok) {
65  loadFailure();
66  } else {
67  setSoundSequence();
68  }
69 }
70 
71 void SoundFactory::playSound(int mySound)
72 {
73  QString soundFile;
74 
75  if ((uint) mySound >= sounds) {
76  return;
77  }
78 
79  soundFile = KStandardDirs::locate("data", "klettres/" + filesList[mySound]);
80  kDebug() << "soundFile " << soundFile;
81 
82  if (soundFile.isEmpty()) {
83  return;
84  }
85 
86  if (!m_player) {
87  m_player = Phonon::createPlayer(Phonon::GameCategory, soundFile);
88  m_player->setParent(this);
89  } else {
90  m_player->setCurrentSource(soundFile);
91  }
92  m_player->play();
93 }
94 
95 void SoundFactory::loadFailure()
96 {
97  KMessageBox::error(klettres, i18n("Error while loading the sound names."));
98  klettres->slotChangeLevel(Prefs::level()-1);
99  bool ok = loadLanguage(m_layoutsDocument, Prefs::language());
100  if (ok) {
101  change(Prefs::language());
102  }
103 }
104 
105 bool SoundFactory::loadLanguage(QDomDocument &layoutDocument, const QString &currentLanguage)
106 {
107  QDomNodeList languagesList,
108  alphabetList,
109  syllablesList,
110  soundNamesList;
111  QDomElement languageElement,
112  alphabetElement,
113  syllableElement,
114  soundNameElement;
115  QDomAttr nameAttribute, fileAttribute;
116 
117  languagesList = layoutDocument.elementsByTagName("language");
118  QDomAttr codeAttribute;
119  //check if the sound files match current language
120  languageElement = (const QDomElement &) languagesList.item(0).toElement();
121  codeAttribute = languageElement.attributeNode("code");
122 
123  if (currentLanguage != codeAttribute.value()) {
124  kDebug() << "Fail reading language !!! ";
125  return false;
126  } else {
127  kDebug() << "current language " << currentLanguage;
128  }
129  //check here if alphabet and syllables both exist
130  alphabetList = languageElement.elementsByTagName("alphabet");
131  syllablesList = languageElement.elementsByTagName("syllables");
132 
133  //load the sounds for level 1 and 2 (alphabet)
134  if ((Prefs::level() == 1) || (Prefs::level() == 2)) {
135  if (alphabetList.count() != 1) {
136  return false;
137  }
138  alphabetElement = (const QDomElement &) alphabetList.item(0).toElement();
139  soundNamesList = alphabetElement.elementsByTagName("sound");
140  }
141 
142  //load the sounds for level 3 and 4 (syllables)
143  if ((Prefs::level() == 3) || (Prefs::level() == 4)) {
144  if (syllablesList.count() != 1) {
145  Prefs::setLevel(1);
146  Prefs::self()->writeConfig();
147  return false;
148  }
149 
150  syllableElement = (const QDomElement &) syllablesList.item(0).toElement();
151 
152  soundNamesList = syllableElement.elementsByTagName("sound");
153  }
154  //Counts the number of sounds
155  sounds = soundNamesList.count();
156  kDebug() << "number of sounds" << sounds << endl;
157  if (sounds < 1) {
158  return false;
159  }
160  namesList.clear();
161  filesList.clear();
162 
163  for (uint sound = 0; sound < sounds; sound++) {
164  soundNameElement = (const QDomElement &) soundNamesList.item(sound).toElement();
165  nameAttribute = soundNameElement.attributeNode("name");
166  //namesList helds the names of the letter or syllable to display
167  namesList.append(nameAttribute.value());
168  fileAttribute = soundNameElement.attributeNode("file");
169  //filesList helds the names of the sound files (i.e the location of the sounds like fr/alpha/a-0.mp3)
170  filesList.append(fileAttribute.value());
171  }
172  if (namesList.isEmpty()) {
173  return false;
174  }
175  if (filesList.isEmpty()) {
176  return false;
177  }
178  return true;
179 }
180 
181 void SoundFactory::setSoundSequence()
182 {
183  // Seed the random number generator
184  KRandomSequence randomSequence;
185  randomList.clear();
186  //get the number of sounds then shuffle it: each number will be taken once then the sequence will come back
187  for (uint j = 0; j < sounds; j++)
188  randomList.append(j);
189 
190  randomSequence.randomize(randomList);
191 }
192 
193 #include "soundfactory.moc"
SoundFactory::filesList
QStringList filesList
List of sound files associated with each sound name.
Definition: soundfactory.h:67
QList::clear
void clear()
QDomElement::elementsByTagName
QDomNodeList elementsByTagName(const QString &tagname) const
QDomNodeList::item
QDomNode item(int index) const
Phonon::MediaObject::play
void play()
QDomNodeList
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
Phonon::createPlayer
MediaObject * createPlayer(Phonon::Category category, const MediaSource &source)
KLettres::loadLayout
bool loadLayout(QDomDocument &layoutDocument)
Load the xml file.
Definition: klettres.cpp:100
SoundFactory::namesList
QStringList namesList
List of sound names.
Definition: soundfactory.h:65
KLettres
Application Main Window.
Definition: klettres.h:41
QDomNode::toElement
QDomElement toElement() const
Prefs::self
static Prefs * self()
Definition: prefs.cpp:17
SoundFactory::SoundFactory
SoundFactory(KLettres *parent, const char *name)
Definition: soundfactory.cpp:35
QDomNodeList::count
int count() const
QList::append
void append(const T &value)
QDomAttr
KLettres::slotChangeLevel
void slotChangeLevel(int)
Set the new level.
Definition: klettres.cpp:295
QObject
SoundFactory::~SoundFactory
~SoundFactory()
Definition: soundfactory.cpp:55
QList::isEmpty
bool isEmpty() const
QDomDocument::elementsByTagName
QDomNodeList elementsByTagName(const QString &tagname) const
QString::isEmpty
bool isEmpty() const
SoundFactory::sounds
uint sounds
Number of sounds corresponding to the current language and level (alphabet or syllables) ...
Definition: soundfactory.h:63
QString
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:105
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:59
SoundFactory::klettres
KLettres * klettres
Call the main instance of the program.
Definition: soundfactory.h:57
QObject::setParent
void setParent(QObject *parent)
Phonon::MediaObject::setCurrentSource
void setCurrentSource(const MediaSource &source)
QDomDocument
QDomAttr::value
QString value() const
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:71
soundfactory.h
QObject::parent
QObject * parent() const
QDomElement
QDomElement::attributeNode
QDomAttr attributeNode(const QString &name)
SoundFactory::m_layoutsDocument
QDomDocument m_layoutsDocument
The language document.
Definition: soundfactory.h:75
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:12:27 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
  • 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
  • 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