1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
|
/***************************************************************************
* Copyright (C) 2001-2006 by Anne-Marie Mahfouf *
* annemarie.mahfouf@free.fr *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
***************************************************************************/
#include <tqlabel.h>
#include <tqtimer.h>
#include <tqtooltip.h>
#include <tqwhatsthis.h>
#include <tqpainter.h>
#include <tdeapplication.h>
#include <tdelocale.h>
#include <kstandarddirs.h>
#include <kdebug.h>
//Project headers
#include "klettres.h"
#include "klettresview.h"
#include "prefs.h"
KLettresView::KLettresView(KLettres *parent)
: TQWidget(parent)
{
m_klettres = parent;
setMinimumSize( TQSize( 640, 480 ) );
setMaximumSize( TQSize( 640, 480 ) );
//lineEdit for user input
m_letterEdit = new KLineEdit( this, "line1" );
m_letterEdit->setGeometry( TQRect( 40, 310, 161, 160 ) );
TQToolTip::add( m_letterEdit, i18n( "Type the letter or syllable that you just heard" ) );
setFont(Prefs::font());
//load background pics
m_grownupPicture.load(locate("data","klettres/pics/klettres_grownup.png"));
m_kidPicture.load(locate("data","klettres/pics/klettres_kids.jpeg"));
m_desertPicture.load(locate("data","klettres/pics/klettres_desert.png"));
}
KLettresView::~KLettresView()
{
}
void KLettresView::chooseSound()
{
//If there are no sounds loaded
if (m_klettres->soundFactory->sounds ==0)
return;
//get a random sound
m_random=kapp->random()%(m_klettres->soundFactory->sounds);
//have not 2 same sounds consecutively
int temp = 0;
if (temp<0)
temp=m_random;
else
{
while (m_random==temp)
m_random=kapp->random()%(m_klettres->soundFactory->sounds);
temp=m_random;
}
//The sound is played
kdDebug() << "m_random " << m_random << endl;
m_klettres->soundFactory->playSound(m_random);
//store letter or syllable in m_currentLetter
m_currentLetter = m_klettres->soundFactory->namesList[m_random];
//Find the length of the syllable
m_length=m_klettres->soundFactory->namesList[m_random].length();
kdDebug() << "syllable length " << m_length << endl;
int width;
if (m_length<3)
width = 200;
else
width = 200+(20*(m_length-2));
update();
m_letterEdit->setMinimumSize( TQSize( width, 160 ) );
m_letterEdit->setMaximumSize( TQSize( width, 160 ) );
}
void KLettresView::paintEvent( TQPaintEvent * )
{
if (Prefs::level()%2==1) {
TQPainter paint(this);
paint.setFont(Prefs::font());
TQString start = i18n("Start");
paint.setFont(Prefs::font());
if (Prefs::theme() == Prefs::EnumTheme::desert) {
paint.setPen( TQColor(115, 50, 95)); //brown
//m_letterEdit->setPaletteForegroundColor(TQColor(115, 50, 95));
}
else if (Prefs::theme() == Prefs::EnumTheme::arctic) {
paint.setPen( TQt::white );
//m_letterEdit->setPaletteForegroundColor(TQColor(48, 75, 137));
}
else {
paint.setPen( TQt::white );
//m_letterEdit->setPaletteForegroundColor(TQt::black);
}
paint.drawText(50, 230, m_currentLetter);
}
}
void KLettresView::game()
{
m_cursorPos = 1;
//reset everything so when you change language or levels
//it all restarts nicely
TQObject::disconnect(m_letterEdit, TQT_SIGNAL(textChanged(const TQString&)),this,TQT_SLOT(slotProcess(const TQString&)) );
m_letterEdit->clear();
m_letterEdit->setCursorPosition(0);
m_letterEdit->setMaxLength(1); //fix that's in trunk
m_letterEdit->setFocus();
chooseSound();
TQObject::connect(m_letterEdit, TQT_SIGNAL(textChanged(const TQString&)), this, TQT_SLOT(slotProcess(const TQString&)) );
}
void KLettresView::slotProcess(const TQString &inputLetter)
{
TQObject::disconnect(m_letterEdit, TQT_SIGNAL(textChanged(const TQString&)), this, TQT_SLOT(slotProcess(const TQString&)) );
//check if backspace
if(inputLetter.length() != m_cursorPos)
{
m_cursorPos--;
m_letterEdit->setMaxLength( m_cursorPos );
TQObject::connect(m_letterEdit, TQT_SIGNAL(textChanged(const TQString&)),this,TQT_SLOT(slotProcess(const TQString&)) );
return;
}
TQChar input_character = inputLetter.at(inputLetter.length()-1);
if (input_character.isLetter()) //(a1.at(inputLetter.length()).lower().isLetter())
{
m_upperLetter = inputLetter.upper(); //put it in uppercase
m_letterEdit->selectAll();
m_letterEdit->cut();
m_letterEdit->setText(m_upperLetter);
TQTimer::singleShot(m_timer*100, this, TQT_SLOT(slotTimerDone()));
}
else
{
kdDebug() << "in no char loop" << endl;
kdDebug() << "cursor " << m_cursorPos << endl;
m_letterEdit->backspace();
TQObject::connect(m_letterEdit, TQT_SIGNAL(textChanged(const TQString&)),this,TQT_SLOT(slotProcess(const TQString&)) );
}
}
void KLettresView::slotTimerDone()
{
kdDebug() << "in timer done" << endl;
TQString match = m_currentLetter.left(m_cursorPos );
if (match == m_upperLetter)
{
if (m_cursorPos!=m_length) //if text in lineEdit not equal to text on button
{ //i.e if you still have to allow input
m_letterEdit->setMaxLength( m_cursorPos +1 );
m_letterEdit->setCursorPosition( m_cursorPos );
m_letterEdit->setFocus();
m_cursorPos ++;
TQObject::connect(m_letterEdit, TQT_SIGNAL(textChanged(const TQString&)), this, TQT_SLOT(slotProcess(const TQString&)) );
}
else
{
game(); //another syllable
}
}
else //if not, cut it
{
m_letterEdit->backspace(); //delete the char to the left and position curseur accordingly
m_letterEdit->setFocus();
//play sound again
m_klettres->soundFactory->playSound(m_random);
TQObject::connect(m_letterEdit, TQT_SIGNAL(textChanged(const TQString&)), this, TQT_SLOT(slotProcess(const TQString&)) );
}
}
void KLettresView::slotPlayAgain()
{
//TODO wait for the previous sound to be payed before playing again as it won't play if the previous one was not finished
m_klettres->soundFactory->playSound(m_random);
}
void KLettresView::viewThemeArctic()
{
Prefs::setTheme(Prefs::EnumTheme::arctic);
Prefs::writeConfig();
setPaletteBackgroundPixmap(m_grownupPicture);
}
void KLettresView::viewThemeClassroom()
{
Prefs::setTheme(Prefs::EnumTheme::classroom);
Prefs::writeConfig();
setPaletteBackgroundPixmap(m_kidPicture);
}
void KLettresView::viewThemeDesert()
{
Prefs::setTheme(Prefs::EnumTheme::desert);
Prefs::writeConfig();
setPaletteBackgroundPixmap(m_desertPicture);
}
#include "klettresview.moc"
|