• Skip to content
  • Skip to link menu
KDE API Documentation - Wordlist.qml Source File (GCompris-qt)
  • KDE Home
  • Contact Us
 

GCompris-qt

  • src
  • core
Wordlist.qml
1 /* GCompris - Wordlist.qml
2  *
3  * Copyright (C) 2014 Holger Kaelberer <holger.k@elberer.de>
4  *
5  * Authors:
6  * Holger Kaelberer <holger.k@elberer.de>
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 3 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, see <http://www.gnu.org/licenses/>.
20  */
21 import QtQuick 2.0
22 import GCompris 1.0
23 import "core.js" as Core
24 
52 Item {
53  id: wordlist
54 
61  property string defaultFilename: ""
62 
69  property bool useDefault: true
70 
77  property string filename: ""
78 
85  property var wordList: ({})
86 
88 
89  property var randomWordList: []
90  property int maxLevel: 0
91 
93 
98  signal error(string msg);
99 
105  function loadFromFile(fname) {
106  filename = fname;
107  var from;
108  maxLevel = 0
109  wordList = parser.parseFromUrl(filename, validateWordlist);
110  if (wordList == null) {
111  error("Wordlist: Invalid wordlist file " + fname);
112  if (useDefault) {
113  // fallback to default file:
114  wordList = parser.parseFromUrl(defaultFilename, validateWordlist);
115  if (wordList == null) {
116  error("Wordlist: Invalid wordlist file " + defaultFilename);
117  return;
118  }
119  from = "default-file " + defaultFilename;
120  }
121  else {
122  error("Wordlist: do not use default list, no list loaded");
123  return;
124  }
125  } else {
126  from = "file " + fname;
127  }
128  // at this point we have valid levels
129  maxLevel = wordList.levels.length;
130  console.log("Wordlist: loaded " + maxLevel + " levels from " + from);
131  return wordList;
132  }
133 
140  function getLevelWordList(level) {
141  if (level > maxLevel)
142  return null;
143  return wordList.levels[level - 1];
144  }
145 
152  function getMaxSubLevel(level) {
153  if (level > maxLevel)
154  return null;
155  return wordList.levels[level - 1].sublevels !== undefined ?
156  wordList.levels[level - 1].sublevels : 0;
157  }
158 
170  function initRandomWord(level) {
171  randomWordList = Core.shuffle(wordList.levels[level - 1].words).slice(0)
172  }
173 
180  function appendRandomWord(word) {
181  randomWordList.unshift(word)
182  }
183 
189  function getRandomWord() {
190  return randomWordList.pop()
191  }
192 
194 
195  function validateWordlist(doc)
196  {
197  // minimal syntax check:
198  var i;
199  if (undefined === doc.levels)
200  return false;
201  for (i = 0; i < doc.levels.length; i++) {
202  // check mandatory level properties only (speed, fallspeed and sublevels are optional)
203  if (doc.levels[i].words.length < 1)
204  return false;
205  }
206  if (i < 1)
207  return false;
208  return true;
209  }
210 
212 
213  JsonParser {
214  id: parser
215 
216  onError: wordlist.error(msg);
217  }
218 
219  Component.onCompleted: {
220  if (filename != "")
221  loadFromFile(filename);
222  }
223 }
QtQuick
JsonParser
A QML helper component for loading and validating JSON data.
Definition: JsonParser.qml:29
GCompris
JsonParser::error
void error(string msg)
Emitted upon error.
This file is part of the KDE documentation.
Documentation copyright © 1996-2015 The KDE developers.
Generated on Tue Jun 2 2015 21:47:47 by doxygen 1.8.9.1 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

GCompris-qt

Skip menu "GCompris-qt"
  • Main Page
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • File List
  • Modules

Class Picker

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