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

kanagram

  • sources
  • kde-4.12
  • kdeedu
  • kanagram
  • src
  • engine
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_originalWord = translation->text().toLower();
206  m_picHintUrl = translation->imageUrl();
207 
208  m_answeredWords.append(m_originalWord);
209  createAnagram();
210  m_hint = translation->comment();
211 
212  if (m_hint.isEmpty())
213  {
214  m_hint = i18n("No hint");
215  }
216  }
217  else
218  {
219  // this file has no entries
220  m_originalWord = "";
221  m_hint = "";
222  m_picHintUrl = "";
223  // TODO: add some error reporting here
224  }
225 }
226 
227 QString KanagramGame::filename() const
228 {
229  return m_fileList.empty() ? m_filename : m_fileList.at(m_index);
230 }
231 
232 QString KanagramGame::anagram() const
233 {
234  return m_anagram;
235 }
236 
237 QString KanagramGame::hint() const
238 {
239  return m_hint;
240 }
241 
242 QString KanagramGame::word() const
243 {
244  return m_originalWord;
245 }
246 
247 void KanagramGame::restoreWord()
248 {
249  m_anagram = m_originalWord;
250 }
251 
252 void KanagramGame::createAnagram()
253 {
254  QString anagram;
255  QString letters;
256  int randomIndex;
257 
258  do {
259  letters = m_originalWord;
260  while (!letters.isEmpty())
261  {
262  randomIndex = m_random.getLong(letters.count());
263  anagram.append(letters.at(randomIndex));
264  letters.remove(randomIndex, 1);
265  }
266  } while (anagram == m_originalWord);
267 
268  m_anagram = anagram;
269 }
270 
271 QString KanagramGame::documentTitle() const
272 {
273  if (m_document)
274  {
275  return m_document->title();
276  }
277 
278  return QString();
279 }
280 
281 QStringList KanagramGame::languageNames()
282 {
283  QStringList languageCodes = SharedKvtmlFiles::languages();
284  if (languageCodes.isEmpty()) {
285  return QStringList();
286  }
287 
288  QStringList languageNames;
289 
290  // Get the language names from the language codes
291 
292  KConfig entry(KStandardDirs::locate("locale", "all_languages"));
293 
294  foreach (const QString& languageCode, languageCodes)
295  {
296  KConfigGroup group = entry.group(languageCode);
297 
298  QString languageName = group.readEntry("Name");
299  if (languageName.isEmpty())
300  {
301  languageName = i18nc("@item:inlistbox no language for that locale", "None");
302  }
303 
304  languageNames.append(languageName);
305  m_languageCodeNameHash.insert(languageCode, languageName);
306  }
307 
308  qSort(languageNames);
309  return languageNames;
310 }
311 
312 QString KanagramGame::dataLanguage() const
313 {
314  return KGlobal::locale()->languageCodeToName(sanitizedDataLanguage());
315 }
316 
317 void KanagramGame::setDataLanguage(const QString& dataLanguage)
318 {
319  KanagramSettings::setDataLanguage(m_languageCodeNameHash.key(dataLanguage));
320  emit dataLanguageChanged();
321 }
322 
323 int KanagramGame::dataLanguageSelectedIndex() const
324 {
325  QStringList languageNames = m_languageCodeNameHash.values();
326  qSort(languageNames);
327 
328  return languageNames.indexOf(KanagramSettings::dataLanguage());
329 }
330 
331 int KanagramGame::currentCategory() const
332 {
333  return KanagramSettings::currentCategory();
334 }
335 
336 void KanagramGame::setCurrentCategory(int index)
337 {
338  if (index > 0 && index < m_fileList.count()) {
339  KanagramSettings::setCurrentCategory(index);
340  } else {
341  KanagramSettings::setCurrentCategory(0);
342  }
343 }
344 
346 KUrl KanagramGame::picHint()
347 {
348  return m_picHintUrl;
349 }
350 
351 
352 #include "kanagramgame.moc"
kanagramgame.h
KanagramGame::picHint
KUrl picHint()
Get this anagram's picture hint URL.
Definition: kanagramgame.cpp:346
KanagramGame::dataLanguageSelectedIndex
Q_INVOKABLE int dataLanguageSelectedIndex() const
Get the selected index of the data language in the language list.
Definition: kanagramgame.cpp:323
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:281
KanagramGame::KanagramGame
KanagramGame()
Default constructor.
Definition: kanagramgame.cpp:42
KanagramGame::anagram
QString anagram() const
Get the anagram to show.
Definition: kanagramgame.cpp:232
KanagramGame::refreshVocabularyList
bool refreshVocabularyList()
Refresh the list of vocabulary files available from what we find on disk.
Definition: kanagramgame.cpp:105
KanagramGame::setDataLanguage
Q_INVOKABLE void setDataLanguage(const QString &dataLanguage)
Set the data language for the kvtml and other strings.
Definition: kanagramgame.cpp:317
KanagramGame::hint
Q_INVOKABLE QString hint() const
Get this anagram's hint.
Definition: kanagramgame.cpp:237
KanagramGame::word
QString word() const
Get this anagram's answer.
Definition: kanagramgame.cpp:242
KanagramGame::setCurrentCategory
void setCurrentCategory(int index)
Set the index of the current category in the list as the current active one.
Definition: kanagramgame.cpp:336
KanagramSettings::setCurrentCategory
static void setCurrentCategory(int v)
Set This settings stores the currently selected Category.
Definition: kanagramsettings.h:22
KanagramGame::sanitizedDataLanguage
QString sanitizedDataLanguage() const
Get the sanitized data language used.
Definition: kanagramgame.cpp:68
kanagramsettings.h
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
KanagramSettings::currentCategory
static int currentCategory()
Get This settings 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:117
engineCatalogLoader
static const KCatalogLoader engineCatalogLoader("kanagram-engine")
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
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.
KanagramGame::currentCategory
Q_INVOKABLE int currentCategory() const
Get the index of the current category in the list.
Definition: kanagramgame.cpp:331
KanagramSettings::dataLanguage
static QString dataLanguage()
Get Set the default translation.
Definition: kanagramsettings.h:127
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:247
KanagramGame::documentTitle
QString documentTitle() const
Get the current vocabulary file's title.
Definition: kanagramgame.cpp:271
KanagramGame::filename
QString filename() const
Get the current vocabulary file's filename.
Definition: kanagramgame.cpp:227
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:108
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:35:35 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
  • 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