00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "klettresview.h"
00022
00023 #include <QTimer>
00024 #include <QPainter>
00025 #include <QSvgRenderer>
00026 #include <QFile>
00027 #include <QPaintEvent>
00028
00029 #include <KLocale>
00030 #include <KStandardDirs>
00031 #include <KDebug>
00032
00033
00034 #include "klettres.h"
00035 #include "prefs.h"
00036 #include "kltheme.h"
00037
00038 KLettresView::KLettresView(KLettres *parent)
00039 : QWidget(parent)
00040 {
00041 m_klettres = parent;
00042
00043
00044 m_letterEdit = new KLineEdit( this );
00045 m_letterEdit->setToolTip(i18n("Type the letter or syllable that you just heard" ) );
00046 m_letterEdit->setFont(Prefs::font());
00047 m_letterEdit->setContextMenuPolicy(Qt::NoContextMenu);
00048 m_letterEdit->setAutoFillBackground(true);
00049
00050 randomInt = 0;
00051 m_theme = 0;
00052 m_renderer = new QSvgRenderer();
00053 setTheme(KLThemeFactory::instance()->buildTheme(0));
00054 }
00055
00056 KLettresView::~KLettresView()
00057 {
00058 delete m_renderer;
00059 delete m_theme;
00060 }
00061
00062 void KLettresView::chooseSound()
00063 {
00064
00065 m_random=m_klettres->soundFactory->randomList[randomInt%m_klettres->soundFactory->sounds];
00066
00067 kDebug() << "m_random " << m_random;
00068 m_klettres->soundFactory->playSound(m_random);
00069
00070 m_currentLetter = m_klettres->soundFactory->namesList[m_random];
00071
00072 m_length=m_klettres->soundFactory->namesList[m_random].length();
00073 kDebug() << "syllable length " << m_length;
00074 int width;
00075 if (m_length<3)
00076 width = 200;
00077 else
00078 width = 200+(20*(m_length-2));
00079 m_letterEdit->setMinimumSize( QSize( width, 100 ) );
00080 m_letterEdit->setMaximumSize( QSize( width, 100 ) );
00081 update();
00082 }
00083
00084 void KLettresView::setTheme(KLTheme *theme)
00085 {
00086
00087 if (!theme)
00088 return;
00089
00090 QString svgpath = KStandardDirs::locate("data", QString("klettres/pics/%1/%2").arg(theme->name(), theme->svgFileName()));
00091
00092
00093 if (!QFile::exists(svgpath))
00094 return;
00095
00096 delete m_theme;
00097 m_theme = theme;
00098
00099
00100 int r1, g1, b1;
00101 m_theme->backgroundInputColor().getRgb(&r1, &g1, &b1);
00102 int r2, g2, b2;
00103 m_theme->letterInputColor().getRgb(&r2, &g2, &b2);
00104 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));
00105
00106 m_renderer->load(svgpath);
00107 m_backgroundCache = QPixmap();
00108 update();
00109 }
00110
00111 void KLettresView::paintEvent( QPaintEvent * e )
00112 {
00113 QPainter p(this);
00114 paintBackground(p, e->rect());
00115 paintLetter(p, e->rect());
00116 m_letterEdit->setFont(Prefs::font());
00117 }
00118
00119 void KLettresView::paintBackground(QPainter &p, const QRect& rect)
00120 {
00121
00122 if (m_backgroundCache.size() != size()) {
00123 m_backgroundCache = QPixmap(size());
00124 QPainter aux(&m_backgroundCache);
00125 m_renderer->render(&aux);
00126 }
00127 p.drawPixmap(rect.topLeft(), m_backgroundCache, rect);
00128 }
00129
00130 void KLettresView::paintLetter(QPainter &p, const QRect& rect)
00131 {
00132 if (Prefs::level()%2==1) {
00133 QRect myRect = m_theme->wordRect(size());
00134 if (!myRect.intersects(rect))
00135 return;
00136
00137 p.setPen( m_theme->letterColor());
00138 p.setFont(Prefs::font());
00139 p.drawText(myRect, m_currentLetter);
00140 }
00141 m_letterEdit->setGeometry( m_theme->inputRect(size()));
00142 m_letterEdit->setFocus();
00143 }
00144
00145 void KLettresView::game()
00146 {
00147 m_cursorPos = 1;
00148
00149
00150 QObject::disconnect(m_letterEdit, SIGNAL(textChanged(const QString&)),this,SLOT(slotProcess(const QString&)) );
00151 m_letterEdit->clear();
00152 m_letterEdit->setCursorPosition(0);
00153 m_letterEdit->setMaxLength( 1 );
00154 m_letterEdit->setFocus();
00155 m_upperLetter.clear();
00156 chooseSound();
00157 randomInt++;
00158 QObject::connect(m_letterEdit, SIGNAL(textChanged(const QString&)), this, SLOT(slotProcess(const QString&)) );
00159 }
00160
00161 void KLettresView::slotProcess(const QString &inputLetter)
00162 {
00163 QObject::disconnect(m_letterEdit, SIGNAL(textChanged(const QString&)), this, SLOT(slotProcess(const QString&)) );
00164
00165 if(inputLetter.length() != m_cursorPos) {
00166 m_cursorPos--;
00167 m_letterEdit->setMaxLength( m_cursorPos );
00168 QObject::connect(m_letterEdit, SIGNAL(textChanged(const QString&)),this,SLOT(slotProcess(const QString&)) );
00169 return;
00170 }
00171 QChar input_character = inputLetter.at(inputLetter.length()-1);
00172 QChar input_character_u;
00173 kDebug() << "input_character " << input_character << endl;
00174 if (input_character.isLetter())
00175 {
00176 if (input_character.unicode() == 0x00DF) {
00177 input_character_u = input_character.toLower();
00178 } else {
00179 input_character_u = input_character.toUpper();
00180 }
00181 m_upperLetter.append(input_character_u);
00182 kDebug() << "input_character_u " << input_character_u << endl;
00183 m_letterEdit->selectAll();
00184 m_letterEdit->cut();
00185 m_letterEdit->setText(m_upperLetter);
00186 QTimer::singleShot(m_timer*100, this, SLOT(slotTimerDone()));
00187 } else {
00188 kDebug() << "cursor " << m_cursorPos << endl;
00189 m_letterEdit->backspace();
00190 QObject::connect(m_letterEdit, SIGNAL(textChanged(const QString&)),this,SLOT(slotProcess(const QString&)) );
00191 }
00192 }
00193
00194 void KLettresView::slotTimerDone()
00195 {
00196 kDebug() << "in timer done";
00197 QString match = m_currentLetter.left(m_cursorPos );
00198 kDebug() << "match " << match.toUpper().data() << endl;
00199 kDebug() << "m_upperLetter " << m_upperLetter.data()<< endl;
00200 if (match == m_upperLetter)
00201 {
00202 if (m_cursorPos!=m_length) {
00203
00204 m_letterEdit->setMaxLength( m_cursorPos +1 );
00205 m_letterEdit->setCursorPosition( m_cursorPos );
00206 m_letterEdit->setFocus();
00207 m_cursorPos ++;
00208 QObject::connect(m_letterEdit, SIGNAL(textChanged(const QString&)), this, SLOT(slotProcess(const QString&)) );
00209 } else {
00210 game();
00211 }
00212 } else {
00213 kDebug() << "wrong letter "<< endl;
00214 m_letterEdit->backspace();
00215 m_upperLetter.remove(m_upperLetter.size()-1, 1);
00216 m_letterEdit->setFocus();
00217
00218 m_klettres->soundFactory->playSound(m_random);
00219
00220 QObject::connect(m_letterEdit, SIGNAL(textChanged(const QString&)), this, SLOT(slotProcess(const QString&)) );
00221 }
00222 }
00223
00224 void KLettresView::slotPlayAgain()
00225 {
00226
00227 m_klettres->soundFactory->playSound(m_random);
00228 }
00229
00230 #include "klettresview.moc"