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

klettres

  • sources
  • kde-4.12
  • kdeedu
  • klettres
  • src
klettresview.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright 2001-2008 by Anne-Marie Mahfouf *
3  * annma@kde.org *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program 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 *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
19  ***************************************************************************/
20 
21 #include "klettresview.h"
22 
23 #include <QTimer>
24 #include <QPainter>
25 #include <QSvgRenderer>
26 #include <QFile>
27 #include <QPaintEvent>
28 
29 #include <KLocale>
30 #include <KStandardDirs>
31 #include <KDebug>
32 
33 //Project headers
34 #include "klettres.h"
35 #include "prefs.h"
36 #include "kltheme.h"
37 #include "langutils.h"
38 
39 KLettresView::KLettresView(KLettres *parent)
40  : QWidget(parent)
41 {
42  m_klettres = parent;
43 
44  //lineEdit for user input
45  m_letterEdit = new KLineEdit( this );
46  m_letterEdit->setToolTip(i18n("Type the letter or syllable that you just heard" ) );
47  m_letterEdit->setFont(Prefs::font());
48  m_letterEdit->setContextMenuPolicy(Qt::NoContextMenu);
49  m_letterEdit->setAutoFillBackground(true);
50 
51  randomInt = 0;
52  m_theme = 0; // essential
53  m_renderer = new QSvgRenderer();
54  setTheme(KLThemeFactory::instance()->buildTheme(0));
55 }
56 
57 KLettresView::~KLettresView()
58 {
59  delete m_renderer;
60  delete m_theme;
61 }
62 
63 void KLettresView::chooseSound()
64 {
65  //get the next random sound
66  m_random=m_klettres->soundFactory->randomList[randomInt%m_klettres->soundFactory->sounds];
67  //The sound is played
68  kDebug() << "m_random " << m_random;
69  m_klettres->soundFactory->playSound(m_random);
70  //store letter or syllable in m_currentLetter
71  m_currentLetter = m_klettres->soundFactory->namesList[m_random];
72  //Find the length of the syllable
73  m_length=m_klettres->soundFactory->namesList[m_random].length();
74  kDebug() << "syllable length " << m_length;
75  int width;
76 
77  if (m_length<3) {
78  width = 200;
79  } else {
80  width = 200+(20*(m_length-2));
81  }
82 
83  m_letterEdit->setMinimumSize( QSize( width, 100 ) );
84  m_letterEdit->setMaximumSize( QSize( width, 100 ) );
85  update();
86 }
87 
88 void KLettresView::setTheme(KLTheme *theme)
89 {
90  // we don't allow null themes
91  if (!theme)
92  return;
93 
94  QString svgpath = KStandardDirs::locate("data", QString("klettres/pics/%1/%2").arg(theme->name(), theme->svgFileName()));
95 
96  // we don't allow themes with no svg installed
97  if (!QFile::exists(svgpath)) {
98  return;
99  }
100  delete m_theme;
101  m_theme = theme;
102 
103  // stylesheet
104  int r1, g1, b1;
105  m_theme->backgroundInputColor().getRgb(&r1, &g1, &b1);
106  int r2, g2, b2;
107  m_theme->letterInputColor().getRgb(&r2, &g2, &b2);
108  m_letterEdit->setStyleSheet(QString("border-style: solid; background-color: rgb(%1, %2, %3); color: rgb(%4, %5, %6) ; border-color: rgb(%4, %5, %6); border-bottom-right-radius:10; border-radius: 15px; border-width: 3px").arg(r1).arg(g1).arg(b1).arg(r2).arg(g2).arg(b2));
109 
110  m_renderer->load(svgpath);
111  m_backgroundCache = QPixmap();
112  update();
113 }
114 
115 void KLettresView::paintEvent( QPaintEvent * e )
116 {
117  QPainter p(this);
118  paintBackground(p, e->rect());
119  paintLetter(p, e->rect());
120  m_letterEdit->setFont(Prefs::font());
121 }
122 
123 void KLettresView::paintBackground(QPainter &p, const QRect& rect)
124 {
125  // Draw the background
126  if (m_backgroundCache.size() != size()) {
127  m_backgroundCache = QPixmap(size());
128  QPainter aux(&m_backgroundCache);
129  m_renderer->render(&aux);
130  }
131  p.drawPixmap(rect.topLeft(), m_backgroundCache, rect);
132 }
133 
134 void KLettresView::paintLetter(QPainter &p, const QRect& rect)
135 {
136  if (Prefs::level()%2==1) {
137  QRect myRect = m_theme->wordRect(size());
138  if (!myRect.intersects(rect)) {
139  return;
140  }
141  p.setPen( m_theme->letterColor());
142  p.setFont(Prefs::font());
143  p.drawText(myRect, m_currentLetter);
144  }
145  m_letterEdit->setGeometry( m_theme->inputRect(size()));
146  m_letterEdit->setFocus();
147 }
148 
149 void KLettresView::game()
150 {
151  m_cursorPos = 1;
152  //reset everything so when you change language or levels
153  //it all restarts nicely
154  QObject::disconnect(m_letterEdit, SIGNAL(textChanged(QString)),this,SLOT(slotProcess(QString)));
155  m_letterEdit->clear();
156  m_letterEdit->setCursorPosition(0);
157  m_letterEdit->setMaxLength( 1 );
158  m_letterEdit->setFocus();
159  m_upperLetter.clear();
160  chooseSound();
161  randomInt++;
162  QObject::connect(m_letterEdit, SIGNAL(textChanged(QString)), this, SLOT(slotProcess(QString)));
163 }
164 
165 void KLettresView::slotProcess(const QString &inputLetter)
166 {
167  QString lang = Prefs::language();
168  QObject::disconnect(m_letterEdit, SIGNAL(textChanged(QString)), this, SLOT(slotProcess(QString)));
169 
170  //check if backspace
171  if (inputLetter.length() != m_cursorPos) {
172  m_cursorPos--;
173  m_letterEdit->setMaxLength( m_cursorPos );
174  QObject::connect(m_letterEdit, SIGNAL(textChanged(QString)),this,SLOT(slotProcess(QString)));
175  return;
176  }
177  QChar input_character = inputLetter.at(inputLetter.length()-1);
178  QChar input_character_u;
179  kDebug() << "input_character " << input_character << endl;
180 
181  if ((!LangUtils::isIndian(lang) && (input_character.isLetter())) || (LangUtils::isIndian(lang)))
182  {
183  if (input_character.unicode() == 0x00DF) { //everything in upper except the ß
184  input_character_u = input_character.toLower();
185  } else {
186  input_character_u = input_character.toUpper();
187  }
188  m_upperLetter.append(input_character_u);
189  m_letterEdit->selectAll();
190  m_letterEdit->cut();
191  m_letterEdit->setText(m_upperLetter);
192  QTimer::singleShot(m_timer*100, this, SLOT(slotTimerDone()));
193  } else {
194  kDebug() << "cursor " << m_cursorPos << endl;
195  m_letterEdit->backspace();
196  QObject::connect(m_letterEdit, SIGNAL(textChanged(const QString&)),this,SLOT(slotProcess(const QString&)) );
197  }
198 
199 }
200 
201 void KLettresView::slotTimerDone()
202 {
203  QString match = m_currentLetter.left(m_cursorPos );
204  kDebug() << "match " << match.toUpper() << endl;
205  kDebug() << "m_upperLetter " << m_upperLetter << endl;
206 
207  if (match == m_upperLetter) {
208  if (m_cursorPos!=m_length) {//if text in lineEdit not equal to text on button
209  //i.e if you still have to allow input
210  m_letterEdit->setMaxLength( m_cursorPos +1 );
211  m_letterEdit->setCursorPosition( m_cursorPos );
212  m_letterEdit->setFocus();
213  m_cursorPos ++;
214  QObject::connect(m_letterEdit, SIGNAL(textChanged(QString)), this, SLOT(slotProcess(QString)));
215  } else {
216  game(); //another syllable
217  }
218  } else { //if not, cut it
219  kDebug() << "wrong letter "<< endl;
220  m_letterEdit->backspace(); //delete the char to the left and position curseur accordingly
221  m_upperLetter.remove(m_upperLetter.size()-1, 1);
222  m_letterEdit->setFocus();
223  //play sound again
224  m_klettres->soundFactory->playSound(m_random);
225  QObject::connect(m_letterEdit, SIGNAL(textChanged(QString)), this, SLOT(slotProcess(QString)));
226  }
227 }
228 
229 void KLettresView::slotPlayAgain()
230 {
231  //TODO wait for the previous sound to be payed before playing again as it won't play if the previous one was not finished
232  m_klettres->soundFactory->playSound(m_random);
233 }
234 
235 void KLettresView::keyReleaseEvent(QKeyEvent * e)
236 {
237  if (e->key() == Qt::Key_Backspace) {
238  m_upperLetter.remove(m_cursorPos-1, 1);
239  }
240 }
241 
242 #include "klettresview.moc"
LangUtils::isIndian
static bool isIndian(const QString &lang)
Indian languages cannot have isLetter()
Definition: langutils.cpp:45
KLettresView::keyReleaseEvent
void keyReleaseEvent(QKeyEvent *e)
If the user hits backpace.
Definition: klettresview.cpp:235
KLettresView::m_random
int m_random
Random number that decides on the letter/syllable and sound.
Definition: klettresview.h:76
KLettresView::~KLettresView
virtual ~KLettresView()
Destructor.
Definition: klettresview.cpp:57
KLTheme::letterInputColor
virtual QColor letterInputColor() const =0
returns the color for the letter in the LineEdit box
KLTheme::inputRect
virtual QRect inputRect(const QSize &windowsize) const =0
KLettresView::slotProcess
void slotProcess(const QString &inputLetter)
Definition: klettresview.cpp:165
KLettresView::m_renderer
QSvgRenderer * m_renderer
Definition: klettresview.h:95
KLettresView::m_upperLetter
QString m_upperLetter
Current letter entered uppercase i.e. m_inputLetter.upper()
Definition: klettresview.h:84
KLettresView::game
void game()
Start playing displaying a new letter/syllable, playing the associated sound.
Definition: klettresview.cpp:149
KLettresView::slotTimerDone
void slotTimerDone()
Definition: klettresview.cpp:201
klettres.h
Prefs::language
static QString language()
Get Language.
Definition: prefs.h:43
KLettresView::setTheme
void setTheme(KLTheme *theme)
set the chosen theme
Definition: klettresview.cpp:88
prefs.h
Prefs::level
static int level()
Get Difficulty level.
Definition: prefs.h:131
KLettresView::m_timer
int m_timer
The timer value i.e. the time for displaying the letters/syllables.
Definition: klettresview.h:59
KLettresView::m_klettres
KLettres * m_klettres
A Klettres object.
Definition: klettresview.h:63
SoundFactory::namesList
QStringList namesList
List of sound names.
Definition: soundfactory.h:65
KLettresView::m_length
int m_length
Length of the syllables.
Definition: klettresview.h:78
KLettres
Application Main Window.
Definition: klettres.h:41
KLettresView::paintLetter
void paintLetter(QPainter &p, const QRect &rect)
Paint the letter/syllable in levels 1 and 3.
Definition: klettresview.cpp:134
KLTheme::name
virtual QString name() const =0
KLettresView::chooseSound
void chooseSound()
Choose a sound in random and ensure that it's not the same than the previous one. ...
Definition: klettresview.cpp:63
klettresview.h
KLTheme::backgroundInputColor
virtual QColor backgroundInputColor() const =0
returns the color for the background of the LineEdit box
Prefs::font
static QFont font()
Get Font.
Definition: prefs.h:188
KLTheme::letterColor
virtual QColor letterColor() const =0
returns the color for displaying the letter/syllable
SoundFactory::sounds
uint sounds
Number of sounds corresponding to the current language and level (alphabet or syllables) ...
Definition: soundfactory.h:63
KLettresView::m_letterEdit
KLineEdit * m_letterEdit
The line where the user enters his/her input.
Definition: klettresview.h:65
KLettresView::paintBackground
void paintBackground(QPainter &p, const QRect &rect)
Paint the background picture.
Definition: klettresview.cpp:123
KLettres::soundFactory
SoundFactory * soundFactory
Sound class.
Definition: klettres.h:52
SoundFactory::randomList
QList< int > randomList
The random sequence of integers.
Definition: soundfactory.h:77
KLThemeFactory::instance
static KLThemeFactory * instance()
Definition: kltheme.cpp:183
KLettresView::m_backgroundCache
QPixmap m_backgroundCache
Definition: klettresview.h:96
KLettresView::m_cursorPos
int m_cursorPos
Cursor position in the line edit.
Definition: klettresview.h:74
KLTheme::wordRect
virtual QRect wordRect(const QSize &windowsize) const =0
SoundFactory::playSound
void playSound(int)
Play the sound associated to int soundRef.
Definition: soundfactory.cpp:72
KLettresView::m_currentLetter
QString m_currentLetter
Current letter or syllable stored.
Definition: klettresview.h:82
KLettresView::paintEvent
void paintEvent(QPaintEvent *)
Paint the letter/syllable in levels 1 and 3 and the background.
Definition: klettresview.cpp:115
langutils.h
KLettresView::KLettresView
KLettresView(KLettres *parent)
Default constructor.
Definition: klettresview.cpp:39
KLTheme
Definition: kltheme.h:26
kltheme.h
KLTheme::svgFileName
virtual QString svgFileName() const =0
KLettresView::randomInt
int randomInt
The index to the random sequence.
Definition: klettresview.h:61
KLettresView::slotPlayAgain
void slotPlayAgain()
Play the same sound again.
Definition: klettresview.cpp:229
KLettresView::m_theme
KLTheme * m_theme
Current theme.
Definition: klettresview.h:92
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:36:13 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
  • Namespace List
  • 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
  • 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