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

klettres

klettresview.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright 2001-2008 by Anne-Marie Mahfouf                              *
00003  *   annma@kde.org                                             *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  *                                                                         *
00010  *   This program is distributed in the hope that it will be useful,       *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00013  *   GNU General Public License for more details.                          *
00014  *                                                                         *
00015  *   You should have received a copy of the GNU General Public License     *
00016  *   along with this program; if not, write to the                         *
00017  *   Free Software Foundation, Inc.,                                       *
00018  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.             *
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 //Project headers
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     //lineEdit for user input
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; // essential
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     //get the next random sound
00065     m_random=m_klettres->soundFactory->randomList[randomInt%m_klettres->soundFactory->sounds];
00066     //The sound is played
00067     kDebug() << "m_random " << m_random;
00068     m_klettres->soundFactory->playSound(m_random);
00069     //store letter or syllable in m_currentLetter
00070     m_currentLetter = m_klettres->soundFactory->namesList[m_random];
00071     //Find the length of the syllable
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     // we don't allow null themes
00087     if (!theme)
00088         return;
00089 
00090     QString svgpath = KStandardDirs::locate("data", QString("klettres/pics/%1/%2").arg(theme->name(), theme->svgFileName()));
00091 
00092     // we don't allow themes with no svg installed
00093     if (!QFile::exists(svgpath))
00094         return;
00095 
00096     delete m_theme;
00097     m_theme = theme;
00098 
00099     // stylesheet   
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     // Draw the background
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     //reset everything so when you change language or levels
00149     //it all restarts nicely
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     //check if backspace
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) { //everything in upper except the ß
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)  {//if text in lineEdit not equal to text on button
00203             //i.e if you still have to allow input
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();  //another syllable
00211         }
00212     }  else  { //if not, cut it
00213     kDebug() << "wrong letter "<< endl;
00214         m_letterEdit->backspace();  //delete the char to the left  and position curseur accordingly
00215     m_upperLetter.remove(m_upperLetter.size()-1, 1);
00216         m_letterEdit->setFocus();
00217         //play sound again
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     //TODO wait for the previous sound to be payed before playing again as it won't play if the previous one was not finished
00227     m_klettres->soundFactory->playSound(m_random);
00228 }
00229 
00230 #include "klettresview.moc"

klettres

Skip menu "klettres"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Class Members

kdeedu

Skip menu "kdeedu"
  • kalzium
  • kanagram
  • kig
  •   lib
  • klettres
  • kstars
  • libkdeedu
  •   keduvocdocument
  •   docs
  •   src
  • parley
  •   stepcore
Generated for kdeedu by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal