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

kanagram

  • sources
  • kde-4.14
  • kdeedu
  • kanagram
  • src
kanagramgame.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005 by Joshua Keel <joshuakeel@gmail.com> *
3  * (C) 2007 by Jeremy Whiting <jpwhiting@kde.org> *
4  * (C) 2012 by Laszlo Papp <lpapp@kde.org> *
5  * *
6  * Portions of this code taken from KMessedWords by Reuben Sutton *
7  * *
8  * This program is free software; you can redistribute it and/or modify *
9  * it under the terms of the GNU General Public License as published by *
10  * the Free Software Foundation; either version 2 of the License, or *
11  * (at your option) any later version. *
12  * *
13  * This program is distributed in the hope that it will be useful, *
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16  * GNU General Public License for more details. *
17  * *
18  * You should have received a copy of the GNU General Public License *
19  * along with this program; if not, write to the *
20  * Free Software Foundation, Inc., *
21  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
22  ***************************************************************************/
23 
24 #include "kanagramgame.h"
25 
26 
27 #include "kanagramsettings.h"
28 
29 #include <KDE/KUrl>
30 #include <sharedkvtmlfiles.h>
31 #include <keduvocdocument.h>
32 #include <keduvocexpression.h>
33 
34 #include <KDE/KUrl>
35 #include <KDE/KStandardDirs>
36 #include <KDE/KLocale>
37 
38 #include <QtCore/QFileInfo>
39 
40 static const KCatalogLoader engineCatalogLoader("kanagram-engine");
41 
42 KanagramGame::KanagramGame() : m_index(0), m_document(NULL)
43 {
44  // Get the list of vocabularies
45  refreshVocabularyList();
46 
47  // Load the default vocabulary
48  loadDefaultVocabulary();
49 }
50 
51 KanagramGame::~KanagramGame()
52 {
53  delete m_document;
54  m_document = NULL;
55 }
56 
57 bool KanagramGame::checkFile()
58 {
59  if (!QFile::exists(KStandardDirs::locate("data", m_filename)))
60  {
61  emit fileError(m_filename);
62  return false;
63  }
64 
65  return true;
66 }
67 
68 QString KanagramGame::sanitizedDataLanguage() const
69 {
70  QString dataLanguage = KanagramSettings::dataLanguage();
71 
72  if (dataLanguage.isEmpty()) {
73  QStringList languageCodes = SharedKvtmlFiles::languages();
74  if (languageCodes.contains(KGlobal::locale()->language())) {
75  dataLanguage = KGlobal::locale()->language();
76  } else {
77  dataLanguage = "en";
78  }
79  }
80 
81  return dataLanguage;
82 
83 }
84 
85 void KanagramGame::loadDefaultVocabulary()
86 {
87  m_filename = KanagramSettings::defaultVocabulary();
88  if (m_filename.isEmpty() || !QFileInfo(m_filename).exists())
89  {
90  refreshVocabularyList();
91  nextVocabulary();
92  }
93 
94  delete m_document;
95  m_document = new KEduVocDocument(this);
96 
98  int result = m_document->open(KUrl(KStandardDirs::locate("data", m_filename)));
99  if (result != 0) {
100  kDebug() << m_document->errorDescription(result);
101  }
102  nextAnagram();
103 }
104 
105 bool KanagramGame::refreshVocabularyList()
106 {
107  QString oldFilename = m_filename;
108  m_fileList = SharedKvtmlFiles::fileNames(sanitizedDataLanguage());
109  if ( m_document ) {
110  useVocabulary(m_document->title());
111  }
112  return oldFilename != m_filename;
113 }
114 
115 QStringList KanagramGame::vocabularyList() const
116 {
117  return SharedKvtmlFiles::titles(sanitizedDataLanguage());
118 }
119 
120 void KanagramGame::useVocabulary(const QString &vocabularyname)
121 {
122  useVocabulary(vocabularyList().indexOf(vocabularyname));
123 }
124 
125 void KanagramGame::useVocabulary(int index)
126 {
127  if (index > 0 && index < m_fileList.count())
128  {
129  m_index = index;
130  m_filename = m_fileList.at(index);
131  }
132  else
133  {
134  m_index = 0;
135  m_filename = m_fileList.first();
136  }
137 
138  checkFile();
139  delete m_document;
140  m_document = new KEduVocDocument(this);
142  m_document->open(KUrl(KStandardDirs::locate("data", m_filename)));
143  m_answeredWords.clear();
144 }
145 
146 void KanagramGame::previousVocabulary()
147 {
148  if (--m_index < 0)
149  {
150  m_index = m_fileList.size() - 1;
151  }
152 
153  m_filename = m_fileList.at(m_index);
154  checkFile();
155  delete m_document;
156  m_document = new KEduVocDocument(this);
158  m_document->open(KUrl(KStandardDirs::locate("data", m_filename)));
159  m_answeredWords.clear();
160 }
161 
162 void KanagramGame::nextVocabulary()
163 {
164  if (++m_index >= m_fileList.size())
165  {
166  m_index = 0;
167  }
168 
169  if (!m_fileList.isEmpty())
170  {
171  m_filename = m_fileList.at(m_index);
172  checkFile();
173  delete m_document;
174  m_document = new KEduVocDocument(this);
176  m_document->open(KUrl(KStandardDirs::locate("data", m_filename)));
177  m_answeredWords.clear();
178  }
179 }
180 
181 void KanagramGame::nextAnagram()
182 {
183  checkFile();
184 
185  int totalWords = m_document->lesson()->entryCount(KEduVocLesson::Recursive);
186  int randomWordIndex = m_random.getLong(totalWords);
187 
188  if (totalWords == (int)m_answeredWords.size())
189  {
190  m_answeredWords.clear();
191  }
192 
193  if (totalWords > 0)
194  {
195  KEduVocTranslation *translation = m_document->lesson()->entries(KEduVocLesson::Recursive).at(randomWordIndex)->translation(0);
196 
197  // Find the next word not used yet
198  while (m_answeredWords.contains(translation->text()))
199  {
200  randomWordIndex = m_random.getLong(totalWords);
201  translation = m_document->lesson()->entries(KEduVocLesson::Recursive).at(randomWordIndex)->translation(0);
202  }
203 
204  // lowercase the entry text so german words that start capitalized will be lowercased
205  m_uppercaseOnly=KanagramSettings::uppercaseOnly();
206  if(m_uppercaseOnly)
207  {
208  m_originalWord = translation->text().toUpper();
209  }
210  else
211  {
212  m_originalWord = translation->text().toLower();
213  }
214  m_picHintUrl = translation->imageUrl();
215  m_audioUrl = translation->soundUrl();
216 
217  m_answeredWords.append(m_originalWord);
218  createAnagram();
219  m_hint = translation->comment();
220 
221  if (m_hint.isEmpty())
222  {
223  m_hint = i18n("No hint");
224  }
225  }
226  else
227  {
228  // this file has no entries
229  m_originalWord = "";
230  m_hint = "";
231  m_picHintUrl = "";
232  m_audioUrl = "";
233  // TODO: add some error reporting here
234  }
235 }
236 
237 QString KanagramGame::filename() const
238 {
239  return m_fileList.empty() ? m_filename : m_fileList.at(m_index);
240 }
241 
242 QString KanagramGame::anagram() const
243 {
244  return m_anagram;
245 }
246 
247 QString KanagramGame::hint() const
248 {
249  return m_hint;
250 }
251 
252 QString KanagramGame::word() const
253 {
254  return m_originalWord;
255 }
256 
257 void KanagramGame::restoreWord()
258 {
259  m_anagram = m_originalWord;
260 }
261 
262 void KanagramGame::createAnagram()
263 {
264  if (m_originalWord.count(m_originalWord.at(0)) < m_originalWord.size()) {
265  QString anagram;
266  QString letters;
267  int randomIndex;
268 
269  do {
270  anagram.clear();
271  letters = m_originalWord;
272  while (!letters.isEmpty())
273  {
274  randomIndex = m_random.getLong(letters.count());
275  anagram.append(letters.at(randomIndex));
276  letters.remove(randomIndex, 1);
277  }
278  } while (anagram == m_originalWord);
279 
280  m_anagram = anagram;
281  } else {
282  m_anagram = m_originalWord;
283  }
284 }
285 
286 QString KanagramGame::documentTitle() const
287 {
288  if (m_document)
289  {
290  return m_document->title();
291  }
292 
293  return QString();
294 }
295 
296 QStringList KanagramGame::languageNames()
297 {
298  QStringList languageCodes = SharedKvtmlFiles::languages();
299  if (languageCodes.isEmpty()) {
300  return QStringList();
301  }
302 
303  QStringList languageNames;
304 
305  // Get the language names from the language codes
306 
307  KConfig entry(KStandardDirs::locate("locale", "all_languages"));
308 
309  foreach (const QString& languageCode, languageCodes)
310  {
311  KConfigGroup group = entry.group(languageCode);
312 
313  QString languageName = group.readEntry("Name");
314  if (languageName.isEmpty())
315  {
316  languageName = i18nc("@item:inlistbox no language for that locale", "None");
317  }
318 
319  languageNames.append(languageName);
320  m_languageCodeNameHash.insert(languageCode, languageName);
321  }
322 
323  qSort(languageNames);
324  return languageNames;
325 }
326 
327 QString KanagramGame::dataLanguage() const
328 {
329  return KGlobal::locale()->languageCodeToName(sanitizedDataLanguage());
330 }
331 
332 void KanagramGame::setDataLanguage(const QString& dataLanguage)
333 {
334  KanagramSettings::setDataLanguage(m_languageCodeNameHash.key(dataLanguage));
335  emit dataLanguageChanged();
336 }
337 
338 int KanagramGame::dataLanguageSelectedIndex() const
339 {
340  QStringList languageNames = m_languageCodeNameHash.values();
341  qSort(languageNames);
342 
343  return languageNames.indexOf(KanagramSettings::dataLanguage());
344 }
345 
346 int KanagramGame::currentCategory() const
347 {
348  return KanagramSettings::currentCategory();
349 }
350 
351 void KanagramGame::setCurrentCategory(int index)
352 {
353  if (index > 0 && index < m_fileList.count()) {
354  KanagramSettings::setCurrentCategory(index);
355  } else {
356  KanagramSettings::setCurrentCategory(0);
357  }
358 }
359 
361 KUrl KanagramGame::picHint()
362 {
363  return m_picHintUrl;
364 }
365 
367 KUrl KanagramGame::audioFile()
368 {
369  return m_audioUrl;
370 }
371 
372 #include "kanagramgame.moc"
KanagramSettings::uppercaseOnly
static bool uppercaseOnly()
Get Use uppercase.
Definition: kanagramsettings.h:203
kanagramgame.h
QList::clear
void clear()
KanagramGame::picHint
KUrl picHint()
Get this anagram's picture hint URL.
Definition: kanagramgame.cpp:361
QString::append
QString & append(QChar ch)
QHash::insert
iterator insert(const Key &key, const T &value)
QString::toUpper
QString toUpper() const
KanagramGame::dataLanguageSelectedIndex
Q_INVOKABLE int dataLanguageSelectedIndex() const
Get the selected index of the data language in the language list.
Definition: kanagramgame.cpp:338
QHash::key
const Key key(const T &value) const
KanagramGame::vocabularyList
Q_INVOKABLE QStringList vocabularyList() const
Get the list of vocabularies.
Definition: kanagramgame.cpp:115
KanagramGame::languageNames
Q_INVOKABLE QStringList languageNames()
Return the language names found available in the system.
Definition: kanagramgame.cpp:296
QList::at
const T & at(int i) const
KanagramGame::KanagramGame
KanagramGame()
Default constructor.
Definition: kanagramgame.cpp:42
QString::size
int size() const
QStringList::contains
bool contains(const QString &str, Qt::CaseSensitivity cs) const
KanagramGame::anagram
QString anagram() const
Get the anagram to show.
Definition: kanagramgame.cpp:242
KanagramGame::refreshVocabularyList
bool refreshVocabularyList()
Refresh the list of vocabulary files available from what we find on disk.
Definition: kanagramgame.cpp:105
QFile::exists
bool exists() const
KanagramGame::setDataLanguage
Q_INVOKABLE void setDataLanguage(const QString &dataLanguage)
Set the data language for the kvtml and other strings.
Definition: kanagramgame.cpp:332
QString::remove
QString & remove(int position, int n)
KanagramGame::hint
Q_INVOKABLE QString hint() const
Get this anagram's hint.
Definition: kanagramgame.cpp:247
KanagramGame::word
QString word() const
Get this anagram's answer.
Definition: kanagramgame.cpp:252
KanagramGame::setCurrentCategory
void setCurrentCategory(int index)
Set the index of the current category in the list as the current active one.
Definition: kanagramgame.cpp:351
KanagramSettings::setCurrentCategory
static void setCurrentCategory(int v)
Set This setting stores the currently selected Category.
Definition: kanagramsettings.h:22
QList::size
int size() const
QString::clear
void clear()
KanagramGame::sanitizedDataLanguage
QString sanitizedDataLanguage() const
Get the sanitized data language used.
Definition: kanagramgame.cpp:68
kanagramsettings.h
QList::count
int count(const T &value) const
QList::append
void append(const T &value)
KanagramGame::audioFile
KUrl audioFile()
Get this anagram's audio URL.
Definition: kanagramgame.cpp:367
QList::empty
bool empty() const
KanagramGame::loadDefaultVocabulary
void loadDefaultVocabulary()
Load the default vocabulary file.
Definition: kanagramgame.cpp:85
KanagramGame::previousVocabulary
void previousVocabulary()
Use the previous vocabulary file in the list.
Definition: kanagramgame.cpp:146
QList::isEmpty
bool isEmpty() const
QString::isEmpty
bool isEmpty() const
KanagramSettings::currentCategory
static int currentCategory()
Get This setting stores the currently selected Category.
Definition: kanagramsettings.h:32
KanagramSettings::setDataLanguage
static void setDataLanguage(const QString &v)
Set Set the default translation.
Definition: kanagramsettings.h:250
engineCatalogLoader
static const KCatalogLoader engineCatalogLoader("kanagram-engine")
QList::first
T & first()
QString
QStringList
QFileInfo
QString::toLower
QString toLower() const
QFileInfo::exists
bool exists() const
KanagramGame::~KanagramGame
~KanagramGame()
Default destructor.
Definition: kanagramgame.cpp:51
KanagramGame::dataLanguageChanged
void dataLanguageChanged()
Signal the ui that the data language has changed.
KanagramGame::nextVocabulary
void nextVocabulary()
Use the next vocabulary file in the list.
Definition: kanagramgame.cpp:162
QString::count
int count() const
QString::at
const QChar at(int position) const
KanagramGame::nextAnagram
void nextAnagram()
Set the index to the next word.
Definition: kanagramgame.cpp:181
KanagramGame::fileError
void fileError(const QString &filename)
Signal the ui that a there's a file error of some kind.
QHash::values
QList< T > values() const
KanagramGame::currentCategory
Q_INVOKABLE int currentCategory() const
Get the index of the current category in the list.
Definition: kanagramgame.cpp:346
QStringList::indexOf
int indexOf(const QRegExp &rx, int from) const
KanagramSettings::dataLanguage
static QString dataLanguage()
Get Set the default translation.
Definition: kanagramsettings.h:260
KanagramGame::dataLanguage
Q_INVOKABLE QString dataLanguage() const
Get the data language for the kvtml and other strings.
KanagramGame::restoreWord
void restoreWord()
Restore the word, set the anagram to the answer.
Definition: kanagramgame.cpp:257
KanagramGame::documentTitle
QString documentTitle() const
Get the current vocabulary file's title.
Definition: kanagramgame.cpp:286
KanagramGame::filename
QString filename() const
Get the current vocabulary file's filename.
Definition: kanagramgame.cpp:237
KanagramGame::useVocabulary
void useVocabulary(const QString &vocabularyname)
Set the vocabulary to use according to the vocabulary name.
Definition: kanagramgame.cpp:120
KanagramSettings::defaultVocabulary
static QString defaultVocabulary()
Get Set the default vocabulary.
Definition: kanagramsettings.h:241
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:12:00 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kanagram

Skip menu "kanagram"
  • Main Page
  • Namespace List
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

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