• 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
  • plasma-active
  • package
  • contents
  • ui
plasma-active/package/contents/ui/GamePage.qml
Go to the documentation of this file.
1 /******************************************************************************
2  * This file is part of the Kanagram project
3  * Copyright (C) 2012 Laszlo Papp <lpapp@kde.org>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  */
19 
20 import QtQuick 1.1
21 
22 import org.kde.plasma.components 0.1
23 
24 import "array.js" as MyArray
25 
26 Page {
27 
28  orientationLock: PageOrientation.LockLandscape;
29 
30  property variant anagram: kanagramEngineHelper.createNextAnagram();
31  property int anagramStatus: anagramStatusEnumeration.init;
32  property int currentOriginalWordIndex: 0;
33  property string originalWordLetterButtonBackground: "image://theme/meegotouch-button-inverted-background";
34  property int countDownTimerValue: kanagramEngineHelper.resolveTime;
35  property bool initialized: false;
36 
37  QtObject { // status enum hackery :)
38  id: anagramStatusEnumeration;
39  property int init: 1;
40  property int active: 2;
41  property int resolved: 3;
42  }
43 
44  onStatusChanged: {
45  if (status == PageStatus.Active && anagramStatus != anagramStatusEnumeration.resolved) {
46  if (rootWindow.languageSelectionChanged == true) {
47 
48  if (kanagramEngineHelper.useSounds) {
49  chalkSoundEffect.play();
50  }
51 
52  categorySelectionDialog.model = kanagramGame.vocabularyList();
53 
54  // Sanitize the selected index, if the translation is not
55  // available for the previously selected index in a different
56  // language.
57 
58  if (categorySelectionDialog.selectedIndex >= categorySelectionDialog.model.length) {
59  categorySelectionDialog.selectedIndex = 0;
60  }
61 
62  kanagramGame.setCurrentCategory(categorySelectionDialog.selectedIndex);
63  kanagramEngineHelper.saveSettings();
64 
65  kanagramGame.useVocabulary(categorySelectionDialog.selectedIndex);
66  nextAnagram();
67  rootWindow.languageSelectionChanged = false;
68  }
69 
70  secondTimer.repeat = true;
71  secondTimer.restart();
72  }
73  }
74 
75  Connections {
76  target: platformWindow;
77 
78  onActiveChanged: {
79  if (platformWindow.active && status == PageStatus.Active
80  && anagramStatus != anagramStatusEnumeration.resolved) {
81  secondTimer.repeat = true;
82  secondTimer.restart();
83  } else {
84  anagramHintInfoBanner.hide();
85 
86  secondTimer.repeat = false;
87  secondTimer.stop();
88  }
89  }
90  }
91 
92  Component.onCompleted: {
93  categorySelectionDialog.selectedIndex = kanagramGame.currentCategory();
94  }
95 
96 
97  function pushPage(file) {
98  var component = Qt.createComponent(file)
99  if (component.status == Component.Ready)
100  pageStack.push(component);
101  else
102  console.log("Error loading component:", component.errorString());
103  }
104 
105  function resolveAnagram() {
106  originalWordLetterRepeater.model = kanagramEngineHelper.anagramOriginalWord();
107  currentOriginalWordIndex = originalWordLetterRepeater.model.length;
108  anagramStatus = anagramStatusEnumeration.resolved;
109  anagramHintInfoBanner.hide();
110  }
111 
112  function nextAnagram() {
113  anagramHintInfoBanner.hide();
114  anagramStatus = anagramStatusEnumeration.init;
115  anagram = kanagramEngineHelper.createNextAnagram();
116  anagramLetterRepeater.model = anagram;
117  originalWordLetterRepeater.model = anagram;
118  currentOriginalWordIndex = 0;
119  countDownTimerValue = kanagramEngineHelper.resolveTime;
120  MyArray.sourceDestinationLetterIndexHash = [];
121  }
122 
123  // Create an info banner with icon
124  InfoBanner {
125  id: anagramHintInfoBanner;
126  text: i18n("This is an info banner with icon");
127  iconSource: "dialog-information.png";
128  }
129 
130  // These tools are available for the main page by assigning the
131  // id to the main page's tools property
132  ToolBarLayout {
133  id: mainPageTools;
134  visible: false;
135 
136  ToolButton {
137  iconSource: "help-hint.png";
138 
139  onClicked: {
140  anagramHintInfoBanner.text = kanagramGame.hint();
141  anagramHintInfoBanner.timerShowTime = kanagramEngineHelper.hintHideTime * 1000;
142 
143  // Display the info banner
144  anagramHintInfoBanner.show();
145  }
146  }
147 
148  ToolButton {
149  iconSource: "games-solve.png";
150 
151  onClicked: {
152  if (kanagramEngineHelper.useSounds) {
153  chalkSoundEffect.play();
154  }
155 
156  resolveAnagram();
157 
158  secondTimer.repeat = false;
159  secondTimer.stop();
160  }
161  }
162 
163  ToolButton {
164  text: categorySelectionDialog.model[categorySelectionDialog.selectedIndex];
165 
166  anchors {
167  centerIn: parent;
168  }
169 
170  onClicked: {
171  categorySelectionDialog.open();
172  }
173  }
174 
175  ToolButton {
176  iconSource: "go-next.png";
177 
178  onClicked: {
179  if (kanagramEngineHelper.useSounds) {
180  chalkSoundEffect.play();
181  }
182 
183  nextAnagram();
184  secondTimer.repeat = true;
185  secondTimer.restart();
186  }
187  }
188 
189  ToolButton {
190  iconSource: "settings.png";
191 
192  onClicked: {
193  anagramHintInfoBanner.hide();
194  pageStack.push(mainSettingsPage);
195 
196  secondTimer.repeat = false;
197  secondTimer.stop();
198  }
199  }
200  }
201 
202  tools: mainPageTools;
203 
204  // Create a selection dialog with the vocabulary titles to choose from.
205 
206  MySelectionDialog {
207  id: categorySelectionDialog;
208  titleText: "Choose an anagram category"
209 
210  model: kanagramGame.vocabularyList();
211 
212  onSelectedIndexChanged: {
213 
214  if (kanagramEngineHelper.useSounds) {
215  initialized == true ? chalkSoundEffect.play() : initialized = true;
216  }
217 
218  kanagramGame.setCurrentCategory(selectedIndex);
219  kanagramEngineHelper.saveSettings();
220  kanagramGame.useVocabulary(selectedIndex);
221  nextAnagram();
222  }
223  }
224 
225  Timer {
226  id: secondTimer;
227  interval: 1000;
228  repeat: true;
229  running: false;
230  triggeredOnStart: false;
231 
232  onTriggered: {
233  if (kanagramEngineHelper.resolveTime != 0 && --countDownTimerValue == 0) {
234  stop();
235  anagramResultTimer.start();
236  originalWordLetterButtonBackground =
237  "image://theme/meegotouch-button-negative-background";
238 
239  resolveAnagram();
240 
241  if (kanagramEngineHelper.useSounds) {
242  wrongSoundEffect.play();
243  }
244  }
245  }
246  }
247 
248  Timer {
249  id: anagramResultTimer;
250  interval: 1000;
251  repeat: false;
252  running: false;
253  triggeredOnStart: false;
254 
255  onTriggered: {
256  originalWordLetterButtonBackground =
257  "image://theme/meegotouch-button-inverted-background";
258  nextAnagram();
259 
260  secondTimer.repeat = true;
261  secondTimer.start();
262  }
263  }
264 
265  Row {
266  spacing: 0;
267 
268  anchors {
269  right: parent.right;
270  top: parent.top;
271  topMargin: 15;
272  }
273 
274  LetterElement {
275  letterText: Math.floor(countDownTimerValue / 60 / 10);
276  visible: kanagramEngineHelper.resolveTime == 0 ? false : true;
277  }
278 
279  LetterElement {
280  letterText: Math.floor(countDownTimerValue / 60 % 10);
281  visible: kanagramEngineHelper.resolveTime == 0 ? false : true;
282  }
283 
284  LetterElement {
285  letterText: ":";
286  visible: kanagramEngineHelper.resolveTime == 0 ? false : true;
287  }
288 
289  LetterElement {
290  letterText: Math.floor(countDownTimerValue % 60 / 10);
291  visible: kanagramEngineHelper.resolveTime == 0 ? false : true;
292  }
293 
294  LetterElement {
295  letterText: Math.floor(countDownTimerValue % 60 % 10);
296  visible: kanagramEngineHelper.resolveTime == 0 ? false : true;
297  }
298 
299  ToolButton {
300  iconSource: "timer-pause.png";
301 
302  onClicked: {
303  anagramHintInfoBanner.hide();
304 
305  pageStack.pop();
306 
307  secondTimer.repeat = false;
308  secondTimer.stop();
309 
310  }
311  }
312  }
313 
314  Column {
315  anchors {
316  centerIn: parent;
317  verticalCenterOffset: 15;
318  }
319 
320  spacing: 30;
321 
322  Row {
323  id: originalWordRow;
324  anchors {
325  horizontalCenter: parent.horizontalCenter;
326  }
327 
328  spacing: 8;
329  Repeater {
330  id: originalWordLetterRepeater;
331  model: anagram;
332  Button {
333  id: originalWordLetterId;
334  text: anagramStatus == anagramStatusEnumeration.init ? "" : modelData;
335 
336  platformStyle: ButtonStyle {
337  background: originalWordLetterButtonBackground;
338  fontFamily: "Arial";
339  fontPixelSize: 40;
340  fontCapitalization: Font.AllUppercase;
341  fontWeight: Font.Bold;
342  horizontalAlignment: Text.AlignHCenter;
343  textColor: "white";
344  pressedTextColor: "pink";
345  disabledTextColor: "gray";
346  checkedTextColor: "blue";
347  buttonWidth: 45;
348  buttonHeight: 60;
349  }
350 
351  onClicked: {
352  if (index + 1 == currentOriginalWordIndex && currentOriginalWordIndex != 0
353  && anagramStatus != anagramStatusEnumeration.resolved) {
354  if (kanagramEngineHelper.useSounds) {
355  anagramLetterPressSoundEffect.play();
356  }
357 
358  var tmpAnagramLetterRepeaterModel = anagramLetterRepeater.model;
359  tmpAnagramLetterRepeaterModel[MyArray.sourceDestinationLetterIndexHash[index]] = originalWordLetterId.text;
360  anagramLetterRepeater.model = tmpAnagramLetterRepeaterModel;
361 
362  MyArray.sourceDestinationLetterIndexHash.pop();
363 
364  originalWordLetterRepeater.model = kanagramEngineHelper.removeInCurrentOriginalWord(index);
365  --currentOriginalWordIndex;
366  }
367  }
368  }
369  }
370  }
371 
372  Image {
373  id: chalkSeparator;
374  width: parent.width;
375  height: 5;
376  fillMode: Image.PreserveAspectFit;
377  source: "chalk-separator.png";
378  }
379 
380  Row {
381  id: anagramRow;
382  anchors {
383  horizontalCenter: parent.horizontalCenter;
384  }
385 
386  spacing: 8;
387  Repeater {
388  id: anagramLetterRepeater;
389  model: anagram;
390  Button {
391  id: anagramLetterId;
392  text: anagramStatus == anagramStatusEnumeration.resolved ? "" : modelData;
393 
394  platformStyle: ButtonStyle {
395  background: "image://theme/meegotouch-button-inverted-background";
396  fontFamily: "Arial";
397  fontPixelSize: 40;
398  fontCapitalization: Font.AllUppercase;
399  fontWeight: Font.Bold;
400  horizontalAlignment: Text.AlignHCenter;
401  textColor: "white";
402  pressedTextColor: "pink";
403  disabledTextColor: "gray";
404  checkedTextColor: "blue";
405  buttonWidth: 45;
406  buttonHeight: 60;
407  }
408 
409  onClicked: {
410  if (anagramStatus != anagramStatusEnumeration.resolved)
411  {
412  if (anagramLetterId.text != "")
413  {
414  if (kanagramEngineHelper.useSounds) {
415  anagramLetterPressSoundEffect.play();
416  }
417 
418  anagramStatus = anagramStatusEnumeration.active;
419 
420  originalWordLetterRepeater.model =
421  kanagramEngineHelper.insertInCurrentOriginalWord(currentOriginalWordIndex, anagramLetterId.text);
422 
423  ++currentOriginalWordIndex;
424 
425  var tmpAnagramLetterRepeaterModel = anagramLetterRepeater.model;
426  tmpAnagramLetterRepeaterModel[[index]] = "";
427  anagramLetterRepeater.model = tmpAnagramLetterRepeaterModel;
428 
429  MyArray.sourceDestinationLetterIndexHash.push(index);
430  }
431 
432  if (currentOriginalWordIndex == originalWordLetterRepeater.model.length)
433  {
434  anagramResultTimer.start();
435  anagramStatus = anagramStatusEnumeration.resolved;
436  anagramHintInfoBanner.hide();
437  if (kanagramEngineHelper.compareWords() == true)
438  {
439  originalWordLetterButtonBackground =
440  "image://theme/meegotouch-button-positive-background";
441 
442  if (kanagramEngineHelper.useSounds) {
443  rightSoundEffect.play();
444  }
445  }
446  else
447  {
448  originalWordLetterButtonBackground =
449  "image://theme/meegotouch-button-negative-background";
450 
451  if (kanagramEngineHelper.useSounds) {
452  wrongSoundEffect.play();
453  }
454  }
455  }
456  }
457  }
458  }
459  }
460  }
461  }
462 }
LetterElement
Definition: LetterElement.qml:20
MySelectionDialog
Definition: MySelectionDialog.qml:42
Page
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:35:34 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