Main Page   Compound List   File List   Compound Members   File Members  

klettresview.cpp

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2001-2003 Anne-Marie Mahfouf <annma@kde.org>
00003 
00004     This program is free software; you can redistribute it and/or
00005     modify it under the terms of version 2 of the GNU General Public
00006     License as published by the Free Software Foundation.
00007 
00008     This program is distributed in the hope that it will be useful,
00009     but WITHOUT ANY WARRANTY; without even the implied warranty of
00010     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011     GNU General Public License for more details.
00012 
00013     You should have received a copy of the GNU General Public License
00014     along with this program; if not, write to the Free Software
00015     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00016  */
00017 
00018 //Qt headers
00019 #include <qtimer.h>
00020 #include <qtooltip.h>
00021 //KDE headers
00022 #include <kdebug.h>
00023 #include <klocale.h>
00024 //C++ includes
00025 #include <stdlib.h>
00026 #include <string.h>
00027 #include <time.h>
00028 //Project headers
00029 #include "klettres.h"
00030 #include "klettresview.h"
00031 
00032 
00033 KLettresView::KLettresView(KLettres *parent)
00034     : QWidget(parent)
00035 {
00036     klettres = parent;
00037 
00038     setMinimumSize( QSize( 640, 538 ) );
00039     setMaximumSize( QSize( 640, 538 ) );
00040 
00041     //Button with letter or syllable
00042     button1 = new QLabel( this, "button1" );
00043     button1->setGeometry( QRect( 50, 100, 160, 160 ) );
00044     cg.setColor( QColorGroup::Foreground, white );
00045     cg.setColor( QColorGroup::Background, QColor(53,87,158));
00046     pal.setActive( cg );
00047     button1->setPalette( pal );
00048     button1->setText( i18n( "A" ) );
00049     QToolTip::add( button1, i18n( "You must type the letter/syllable you hear and/or see in the field below" ) );
00050     //lineEdit for user input
00051     line1 = new QLineEdit( this, "line1" );
00052     line1->setGeometry( QRect( 40, 310, 161, 160 ) );
00053     QToolTip::add( line1, i18n( "Type the letter or syllable that you just heard" ) );
00054 
00055     //load background pics
00056     pm_a.load(locate("data","klettres/pics/background1.png"));
00057     pm_k.load(locate("data","klettres/pics/klettres_back.jpeg"));
00058     //maybe a warning if background pics are not found
00059     n = 0;
00060     temp=-1;
00061 
00062 }
00063 
00064 KLettresView::~KLettresView()
00065 {
00066 }
00067 
00069 void KLettresView::slotGrownup()
00070 {
00071     style="grownup";
00072     setMinimumSize( QSize( 640, 480 ) );
00073     setMaximumSize( QSize( 640, 480 ) );
00074     setBackgroundPixmap(pm_a);
00075     //button1 background
00076     cg.setColor( QColorGroup::Foreground, white );
00077     cg.setColor( QColorGroup::Background, QColor(53,87,158));
00078     pal.setActive( cg );
00079     button1->setPalette( pal );
00080 }
00081 
00083 void KLettresView::slotKid()
00084 {
00085     style="kid";
00086     setMinimumSize( QSize( 640, 480 ) );
00087     setMaximumSize( QSize( 640, 480 ) );
00088     setBackgroundPixmap(pm_k);
00089     //change button1 background
00090     cg.setColor( QColorGroup::Foreground, white );
00091     cg.setColor( QColorGroup::Background, black);
00092     pal.setActive( cg );
00093     button1->setPalette( pal );
00094 }
00095 
00097 void KLettresView::game()
00098 {
00099  //reset everything so when you change language or levels
00100  //it all restart nicely
00101  QObject::disconnect(line1, SIGNAL(textChanged(const
00102  QString&)),this,SLOT(slotLet2(const QString&)) );
00103  input = 1;
00104  line1->clear();
00105  line1->setCursorPosition(0);
00106  line1->setFocus();
00107 
00108  if (niveau==1)
00109         button1->show();
00110 
00111     if (niveau==2)
00112         button1->hide();
00113 
00114     if (niveau==1||niveau==2)
00115     {
00116         button1->setMinimumSize( QSize( 200, 160 ) );
00117         button1->setMaximumSize( QSize( 200, 160 ) );
00118         line1->setMinimumSize( QSize( 140, 160 ) );
00119         line1->setMaximumSize( QSize( 140, 160 ) );
00120         chooseSound();
00121 
00122         QObject::connect(line1, SIGNAL(textChanged(const
00123  QString&)),this,SLOT(treat1(const QString&)) );
00124          QObject::disconnect(line1, SIGNAL(textChanged(const
00125  QString&)),this,SLOT(slotLet2(const QString&)) );
00126     }
00127 
00128     if (niveau==3)
00129         button1->show();
00130 
00131     if (niveau==4)
00132         button1->hide();
00133 
00134     if (niveau==3 || niveau==4)
00135     {
00136         chooseSound();
00137         if (length==2)
00138         {
00139                 button1->setMinimumSize( QSize( 200, 160 ) );
00140                 button1->setMaximumSize( QSize( 200, 160 ) );
00141                 line1->setMinimumSize( QSize( 200, 160 ) );
00142                 line1->setMaximumSize( QSize( 200, 160 ) );
00143         }
00144         if (length==3)
00145         {
00146                 button1->setMinimumSize( QSize(250, 160 ) );
00147                 button1->setMaximumSize( QSize( 250, 160 ) );
00148                 line1->setMinimumSize( QSize( 250, 160 ) );
00149                 line1->setMaximumSize( QSize( 250, 160 ) );
00150         }
00151         QObject::connect(line1, SIGNAL(textChanged(const
00152  QString&)),this,SLOT(slotLet2(const QString&)) );
00153         QObject::disconnect(line1, SIGNAL(textChanged(const
00154  QString&)),this,SLOT(treat1(const QString&)) );
00155         }
00156         line1->setMaxLength( 1 );
00157         line1->setCursorPosition(0);
00158         line1->setFocus();
00159 }
00160 
00161 void KLettresView::treat1(const QString& )
00162 {
00163         QObject::disconnect(line1, SIGNAL(textChanged(const
00164  QString&)),this,SLOT(treat1(const QString&)) );
00165         QObject::disconnect(line1, SIGNAL(textChanged(const
00166  QString&)),this,SLOT(slotLet2(const QString&)) );
00167         a1=line1->text();   //get text from LineEdit
00168         if (!a1.at(0).isLetter()) //if it's not a letter which was typed
00169         {
00170         QObject::connect(line1, SIGNAL(textChanged(const
00171  QString&)),this,SLOT(treat1(const QString&)) );
00172         QObject::connect(line1, SIGNAL(textChanged(const
00173  QString&)),this,SLOT(slotLet2(const QString&)) );
00174         }
00175         t1 = a1.upper();    //put it in uppercase
00176         line1->selectAll();
00177         line1->cut();
00178         line1->setText(t1);     //display it in uppercase
00179         if (niveau==2)
00180                 button1->hide();
00181         QTimer *timer = new QTimer( this );
00182         connect( timer, SIGNAL(timeout()),
00183                  this, SLOT(timer1()) );
00184         timer->start( 2000, TRUE );
00185 }
00186 
00187 void KLettresView::timer1()
00188 {
00189         line1->selectAll();
00190         if ((button1->text())==t1)
00191         {
00192                 chooseSound();
00193         }
00194         else
00195         {
00196                 if (niveau==2)
00197                 button1->show(); //show letter after first miss
00198                 klettres->soundFactory->playSound(n);//replay sound
00199         }
00200 
00201         line1->cut();
00202 
00203         QObject::connect(line1, SIGNAL(textChanged(const
00204                                 QString&)),this,SLOT(treat1(const QString&)) );
00205         line1->setFocus();
00206 }
00207 
00208 //levels 3 and 4
00209 void KLettresView::slotLet2(const QString& )
00210 {
00211         line1->clearFocus();
00212         //disconnect
00213         QObject::disconnect(line1, SIGNAL(textChanged(const
00214                 QString&)),this,SLOT(slotLet2(const QString&)) );
00215 
00216         line1->setSelection(input-1,1);
00217         a1=line1->selectedText();   //get the input letter
00218         t1 = a1.upper(); //input in uppercase
00219         line1->cut();
00220         line1->setText(line1->text()+t1);
00221         t1=line1->text(); //t1 is the whole lineEdit text now
00222         sj=st.left(input);
00223         QTimer *timer = new QTimer( this );
00224         connect( timer, SIGNAL(timeout()),
00225                  this, SLOT(timerDone()) );
00226         timer->start( 1000, TRUE );
00227 }
00228 
00229 void KLettresView::timerDone()
00230 {
00231         if (t1==sj)   //if letter input is correct
00232         {
00233            if (sj!=st)  //if text in lineEdit not equal to text on button
00234                 {            //i.e if you still have to allow input
00235                         line1->setMaxLength( input+1 );
00236                         line1->setCursorPosition( input );
00237                         line1->setFocus();
00238                         input++;
00239                         QObject::connect(line1, SIGNAL(textChanged(const
00240                                         QString&)),this,SLOT(slotLet2(const QString&)) );
00241        }
00242                 else
00243                 {
00244                         line1->selectAll();
00245                         line1->cut();
00246                         line1->setCursorPosition(0 );
00247                         line1->setFocus();
00248                         line1->setMaxLength( 1 );
00249                         if (niveau==4)
00250                         button1->hide();
00251                         game();  //another syllable
00252                 }
00253         }
00254         else   //if not, cut it
00255         {
00256                 line1->backspace();  //delete the char to the left  and position curseur accordingly
00257                 line1->setFocus();
00258                 //play sound again
00259                 klettres->soundFactory->playSound(n);
00260 
00261                 QObject::connect(line1, SIGNAL(textChanged(const
00262  QString&)),this,SLOT(slotLet2(const QString&)) );
00263         }
00264 }
00265 
00266 void KLettresView::chooseSound()
00267 {
00268         input =1;
00269         srand((unsigned int)time((time_t *)NULL));
00270         //If there are no sounds loaded
00271         if (klettres->soundFactory->sounds ==0)
00272                 return;
00273         n=rand()%(klettres->soundFactory->sounds);//l;
00274         //have not 2 same sounds consecutively
00275         if (temp<0)
00276                 temp=n;
00277         else
00278         {
00279                 while (n==temp)
00280                         n=rand()%(klettres->soundFactory->sounds);
00281                 temp=n;
00282         }
00283 
00284         //The sound is played
00285         klettres->soundFactory->playSound(n);
00286         //The letter/syllable is displayed
00287         button1->setText(klettres->soundFactory->namesList[n]);
00288         //store letter or syllable in st
00289         st = klettres->soundFactory->namesList[n];
00290         //Find the length of the syllable
00291         length=klettres->soundFactory->namesList[n].length();
00292 }
00293 
00294 #include "klettresview.moc"

Generated on Sat Oct 18 21:37:50 2003 by doxygen1.2.18