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