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

klettres

klettresview.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2001-2007 by Anne-Marie Mahfouf                              *
00003  *   annemarie.mahfouf@free.fr                                             *
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->setAutoFillBackground(true);
00048 
00049     randomInt          = 0;
00050     m_theme            = 0; // essential
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     //get the next random sound
00064     m_random=m_klettres->soundFactory->randomList[randomInt%m_klettres->soundFactory->sounds];
00065     //The sound is played
00066     kDebug() << "m_random " << m_random;
00067     m_klettres->soundFactory->playSound(m_random);
00068     //store letter or syllable in m_currentLetter
00069     m_currentLetter = m_klettres->soundFactory->namesList[m_random];
00070     //Find the length of the syllable
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     // we don't allow null themes
00086     if (!theme)
00087         return;
00088 
00089     QString svgpath = KStandardDirs::locate("data", QString("klettres/pics/%1/%2").arg(theme->name(), theme->svgFileName()));
00090 
00091     // we don't allow themes with no svg installed
00092     if (!QFile::exists(svgpath))
00093         return;
00094 
00095     delete m_theme;
00096     m_theme = theme;
00097 
00098     // stylesheet   
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     // Draw the background
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     //reset everything so when you change language or levels
00148     //it all restarts nicely
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     //check if backspace
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()) //(a1.at(inputLetter.length()).lower().isLetter())
00173     {
00174         m_upperLetter = inputLetter.toUpper();    //put it in uppercase
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)  //if text in lineEdit not equal to text on button
00196         {            //i.e if you still have to allow input
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();  //another syllable
00206         }
00207     }
00208     else   //if not, cut it
00209     {
00210         m_letterEdit->backspace();  //delete the char to the left  and position curseur accordingly
00211         m_letterEdit->setFocus();
00212         //play sound again
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     //TODO wait for the previous sound to be payed before playing again as it won't play if the previous one was not finished
00222     m_klettres->soundFactory->playSound(m_random);
00223 }
00224 
00225 #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
  • klettres
  • kstars
  • libkdeedu
  •   keduvocdocument
  •   docs
  •   src
  • parley
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