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

klettres

  • sources
  • kde-4.14
  • 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 
32 //Project headers
33 #include "klettres.h"
34 #include "prefs.h"
35 #include "kltheme.h"
36 #include "langutils.h"
37 
38 KLettresView::KLettresView(KLettres *parent)
39  : QWidget(parent)
40 {
41  m_klettres = parent;
42 
43  //lineEdit for user input
44  m_letterEdit = new KLineEdit( this );
45  m_letterEdit->setToolTip(i18n("Type the letter or syllable that you just heard" ) );
46  m_letterEdit->setFont(Prefs::font());
47  m_letterEdit->setContextMenuPolicy(Qt::NoContextMenu);
48  m_letterEdit->setAutoFillBackground(true);
49 
50  randomInt = 0;
51  m_theme = 0; // essential
52  m_renderer = new QSvgRenderer();
53  setTheme(KLThemeFactory::instance()->buildTheme(0));
54 }
55 
56 KLettresView::~KLettresView()
57 {
58  delete m_renderer;
59  delete m_theme;
60 }
61 
62 void KLettresView::chooseSound()
63 {
64  //get the next random sound
65  m_random=m_klettres->soundFactory->randomList[randomInt%m_klettres->soundFactory->sounds];
66  //The sound is played
67  kDebug() << "m_random " << m_random;
68  m_klettres->soundFactory->playSound(m_random);
69  //store letter or syllable in m_currentLetter
70  m_currentLetter = m_klettres->soundFactory->namesList[m_random];
71  //Find the length of the syllable
72  m_length=m_klettres->soundFactory->namesList[m_random].length();
73  kDebug() << "syllable length " << m_length;
74  int width;
75 
76  if (m_length<3) {
77  width = 200;
78  } else {
79  width = 200+(20*(m_length-2));
80  }
81 
82  m_letterEdit->setMinimumSize( QSize( width, 100 ) );
83  m_letterEdit->setMaximumSize( QSize( width, 100 ) );
84  update();
85 }
86 
87 void KLettresView::setTheme(KLTheme *theme)
88 {
89  // we don't allow null themes
90  if (!theme)
91  return;
92 
93  QString svgpath = KStandardDirs::locate("data", QString("klettres/pics/%1/%2").arg(theme->name(), theme->svgFileName()));
94 
95  // we don't allow themes with no svg installed
96  if (!QFile::exists(svgpath)) {
97  return;
98  }
99  delete m_theme;
100  m_theme = theme;
101 
102  // stylesheet
103  int r1, g1, b1;
104  m_theme->backgroundInputColor().getRgb(&r1, &g1, &b1);
105  int r2, g2, b2;
106  m_theme->letterInputColor().getRgb(&r2, &g2, &b2);
107  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));
108 
109  m_renderer->load(svgpath);
110  m_backgroundCache = QPixmap();
111  update();
112 }
113 
114 void KLettresView::paintEvent( QPaintEvent * e )
115 {
116  QPainter p(this);
117  paintBackground(p, e->rect());
118  paintLetter(p, e->rect());
119  m_letterEdit->setFont(Prefs::font());
120 }
121 
122 void KLettresView::paintBackground(QPainter &p, const QRect& rect)
123 {
124  // Draw the background
125  if (m_backgroundCache.size() != size()) {
126  m_backgroundCache = QPixmap(size());
127  QPainter aux(&m_backgroundCache);
128  m_renderer->render(&aux);
129  }
130  p.drawPixmap(rect.topLeft(), m_backgroundCache, rect);
131 }
132 
133 void KLettresView::paintLetter(QPainter &p, const QRect& rect)
134 {
135  if (Prefs::level()%2==1) {
136  QRect myRect = m_theme->wordRect(size());
137  if (!myRect.intersects(rect)) {
138  return;
139  }
140  p.setPen( m_theme->letterColor());
141  p.setFont(Prefs::font());
142  p.drawText(myRect, m_currentLetter);
143  }
144  m_letterEdit->setGeometry( m_theme->inputRect(size()));
145  m_letterEdit->setFocus();
146 }
147 
148 void KLettresView::game()
149 {
150  m_cursorPos = 1;
151  //reset everything so when you change language or levels
152  //it all restarts nicely
153  QObject::disconnect(m_letterEdit, SIGNAL(textChanged(QString)),this,SLOT(slotProcess(QString)));
154  m_letterEdit->clear();
155  m_letterEdit->setCursorPosition(0);
156  m_letterEdit->setMaxLength( 1 );
157  m_letterEdit->setFocus();
158  m_upperLetter.clear();
159  chooseSound();
160  randomInt++;
161  QObject::connect(m_letterEdit, SIGNAL(textChanged(QString)), this, SLOT(slotProcess(QString)));
162 }
163 
164 void KLettresView::slotProcess(const QString &inputLetter)
165 {
166  QString lang = Prefs::language();
167  QObject::disconnect(m_letterEdit, SIGNAL(textChanged(QString)), this, SLOT(slotProcess(QString)));
168 
169  //check if backspace
170  if (inputLetter.length() != m_cursorPos) {
171  m_cursorPos--;
172  m_letterEdit->setMaxLength( m_cursorPos );
173  QObject::connect(m_letterEdit, SIGNAL(textChanged(QString)),this,SLOT(slotProcess(QString)));
174  return;
175  }
176  QChar input_character = inputLetter.at(inputLetter.length()-1);
177  QChar input_character_u;
178  kDebug() << "input_character " << input_character << endl;
179 
180  if ((!LangUtils::isIndian(lang) && (input_character.isLetter())) || (LangUtils::isIndian(lang)))
181  {
182  if (input_character.unicode() == 0x00DF) { //everything in upper except the ß
183  input_character_u = input_character.toLower();
184  } else {
185  input_character_u = input_character.toUpper();
186  }
187  m_upperLetter.append(input_character_u);
188  m_letterEdit->selectAll();
189  m_letterEdit->cut();
190  m_letterEdit->setText(m_upperLetter);
191  QTimer::singleShot(m_timer*100, this, SLOT(slotTimerDone()));
192  } else {
193  kDebug() << "cursor " << m_cursorPos << endl;
194  m_letterEdit->backspace();
195  QObject::connect(m_letterEdit, SIGNAL(textChanged(const QString&)),this,SLOT(slotProcess(const QString&)) );
196  }
197 
198 }
199 
200 void KLettresView::slotTimerDone()
201 {
202  QString match = m_currentLetter.left(m_cursorPos );
203  kDebug() << "match " << match.toUpper() << endl;
204  kDebug() << "m_upperLetter " << m_upperLetter << endl;
205 
206  if (match == m_upperLetter) {
207  if (m_cursorPos!=m_length) {//if text in lineEdit not equal to text on button
208  //i.e if you still have to allow input
209  m_letterEdit->setMaxLength( m_cursorPos +1 );
210  m_letterEdit->setCursorPosition( m_cursorPos );
211  m_letterEdit->setFocus();
212  m_cursorPos ++;
213  QObject::connect(m_letterEdit, SIGNAL(textChanged(QString)), this, SLOT(slotProcess(QString)));
214  } else {
215  game(); //another syllable
216  }
217  } else { //if not, cut it
218  kDebug() << "wrong letter "<< endl;
219  m_letterEdit->backspace(); //delete the char to the left and position curseur accordingly
220  m_upperLetter.remove(m_upperLetter.size()-1, 1);
221  m_letterEdit->setFocus();
222  //play sound again
223  m_klettres->soundFactory->playSound(m_random);
224  QObject::connect(m_letterEdit, SIGNAL(textChanged(QString)), this, SLOT(slotProcess(QString)));
225  }
226 }
227 
228 void KLettresView::slotPlayAgain()
229 {
230  //TODO wait for the previous sound to be payed before playing again as it won't play if the previous one was not finished
231  m_klettres->soundFactory->playSound(m_random);
232 }
233 
234 void KLettresView::keyReleaseEvent(QKeyEvent * e)
235 {
236  if (e->key() == Qt::Key_Backspace) {
237  m_upperLetter.remove(m_cursorPos-1, 1);
238  }
239 }
240 
241 #include "klettresview.moc"
LangUtils::isIndian
static bool isIndian(const QString &lang)
Indian languages cannot have isLetter()
Definition: langutils.cpp:44
KLettresView::keyReleaseEvent
void keyReleaseEvent(QKeyEvent *e)
If the user hits backpace.
Definition: klettresview.cpp:234
QPixmap::size
QSize size() const
QWidget
QString::append
QString & append(QChar ch)
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:56
QString::toUpper
QString toUpper() const
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
QSvgRenderer::render
void render(QPainter *painter)
KLettresView::slotProcess
void slotProcess(const QString &inputLetter)
Definition: klettresview.cpp:164
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
QList::length
int length() const
KLettresView::game
void game()
Start playing displaying a new letter/syllable, playing the associated sound.
Definition: klettresview.cpp:148
QChar
KLettresView::slotTimerDone
void slotTimerDone()
Definition: klettresview.cpp:200
QSvgRenderer
QString::size
int size() const
klettres.h
QRect::intersects
bool intersects(const QRect &rectangle) const
Prefs::language
static QString language()
Get Language.
Definition: prefs.h:43
KLettresView::setTheme
void setTheme(KLTheme *theme)
set the chosen theme
Definition: klettresview.cpp:87
prefs.h
QFile::exists
bool exists() const
Prefs::level
static int level()
Get Difficulty level.
Definition: prefs.h:131
QString::remove
QString & remove(int position, int n)
QObject::disconnect
bool disconnect(const QObject *sender, const char *signal, const QObject *receiver, const char *method)
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
QWidget::update
void update()
SoundFactory::namesList
QStringList namesList
List of sound names.
Definition: soundfactory.h:65
QChar::isLetter
bool isLetter() const
QString::clear
void clear()
KLettresView::m_length
int m_length
Length of the syllables.
Definition: klettresview.h:78
KLettres
Application Main Window.
Definition: klettres.h:41
QWidget::width
int width() const
QWidget::size
QSize size() const
QPaintEvent::rect
const QRect & rect() const
QRect
QPainter::setFont
void setFont(const QFont &font)
KLettresView::paintLetter
void paintLetter(QPainter &p, const QRect &rect)
Paint the letter/syllable in levels 1 and 3.
Definition: klettresview.cpp:133
KLTheme::name
virtual QString name() const =0
QSvgRenderer::load
bool load(const QString &filename)
QPainter::setPen
void setPen(const QColor &color)
KLettresView::chooseSound
void chooseSound()
Choose a sound in random and ensure that it's not the same than the previous one. ...
Definition: klettresview.cpp:62
QPainter::drawPixmap
void drawPixmap(const QRectF &target, const QPixmap &pixmap, const QRectF &source)
QPainter
klettresview.h
QPainter::drawText
void drawText(const QPointF &position, const QString &text)
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
QString
QChar::unicode
ushort unicode() const
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:122
QWidget::rect
QRect rect() const
KLettres::soundFactory
SoundFactory * soundFactory
Sound class.
Definition: klettres.h:52
QPixmap
SoundFactory::randomList
QList< int > randomList
The random sequence of integers.
Definition: soundfactory.h:77
QKeyEvent::key
int key() const
QSize
KLThemeFactory::instance
static KLThemeFactory * instance()
Definition: kltheme.cpp:234
QChar::toLower
QChar toLower() const
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
QKeyEvent
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:71
KLettresView::m_currentLetter
QString m_currentLetter
Current letter or syllable stored.
Definition: klettresview.h:82
QChar::toUpper
QChar toUpper() const
QString::at
const QChar at(int position) const
KLettresView::paintEvent
void paintEvent(QPaintEvent *)
Paint the letter/syllable in levels 1 and 3 and the background.
Definition: klettresview.cpp:114
QColor::getRgb
void getRgb(int *r, int *g, int *b, int *a) const
langutils.h
QRect::topLeft
QPoint topLeft() const
QString::length
int length() const
KLettresView::KLettresView
KLettresView(KLettres *parent)
Default constructor.
Definition: klettresview.cpp:38
QString::left
QString left(int n) const
KLTheme
Definition: kltheme.h:26
QPaintEvent
kltheme.h
KLTheme::svgFileName
virtual QString svgFileName() const =0
QObject::connect
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QObject::parent
QObject * parent() const
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:228
KLettresView::m_theme
KLTheme * m_theme
Current theme.
Definition: klettresview.h:92
QTimer::singleShot
singleShot
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:12:27 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
  • 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
  • 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