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

klettres

klettres.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 "klettres.h"
00022 
00023 //Qt includes
00024 #include <QBitmap>
00025 #include <QDir>
00026 #include <QFile>
00027 #include <QLabel>
00028 #include <QPainter>
00029 #include <QTextStream>
00030 #include <QDomDocument>
00031 
00032 //KDE includes
00033 #include <KAction>
00034 #include <KActionCollection>
00035 #include <KComboBox>
00036 #include <KConfigDialog>
00037 #include <KDebug>
00038 #include <KLocale>
00039 #include <KMenuBar>
00040 #include <KMessageBox>
00041 #include <KNumInput>
00042 #include <KSelectAction>
00043 #include <KStandardAction>
00044 #include <KStandardDirs>
00045 #include <KStatusBar>
00046 #include <KToggleAction>
00047 #include <KToolBar>
00048 #include <KIcon>
00049 #include <KNS/Engine>
00050 #include <KApplication>
00051 #include <KGlobal>
00052 
00053 //Project includes
00054 #include "ui_fontsdlg.h"
00055 #include "timer.h"
00056 #include "prefs.h"
00057 #include "langutils.h"
00058 #include "kltheme.h"
00059 
00060 class fontsdlg : public QDialog, public Ui::fontsdlg
00061 {    
00062     public:
00063         fontsdlg( QWidget * parent ) : QDialog(parent)
00064         { setupUi(this); }
00065 };
00066 
00067 const int ID_KIDB      = 100;
00068 const int ID_GROWNB    = 101;
00069 const int ID_MENUBARB  = 102;
00070 
00071 KLettres::KLettres()
00072         : KXmlGuiWindow( 0)
00073 {
00074     setObjectName( QLatin1String("KLettres") );
00075     mNewStuff = 0;
00076     m_view = new KLettresView(this);
00077     setMinimumSize( QSize( 800, 600 ) );
00078     //Tell the KXmlGuiWindow that this is indeed the main widget
00079     setCentralWidget(m_view);
00080     //Populate Languages menu with m_languageNames
00081     m_languageNames = LangUtils::getLanguagesNames(LangUtils::getLanguages());
00082     //MainWindow GUI: menus, tolbars and statusbar
00083     setupActions();
00084     setupStatusbar();
00085     setupToolbars();
00086     //Load Settings
00087     loadSettings();
00088     //Setup current language sounds
00089     soundFactory = new SoundFactory(this, "sounds");
00090     //Start game
00091     m_view->game();
00092 }
00093 
00094 KLettres::~KLettres()
00095 {
00096 }
00097 
00098 bool KLettres::loadLayout(QDomDocument &layoutDocument)
00099 {
00100     QFile layoutFile(KStandardDirs::locate("data", "klettres/"+Prefs::language()+"/sounds.xml"));
00101     //if xml file is not found, program exits
00102     if (!layoutFile.exists())
00103     {
00104         kWarning() << "sounds.xml file not found in $KDEDIR/share/apps/klettres/"+Prefs::language() ;
00105         QString mString=i18n("The file sounds.xml was not found in\n"
00106                              "$KDEDIR/share/apps/klettres/\n\n"
00107                              "Please install this file and start KLettres again.\n\n");
00108         KMessageBox::information( this, mString,i18n("KLettres - Error") );
00109         qApp->quit();//exit(1);
00110     }
00111     if (!layoutFile.open(QIODevice::ReadOnly))
00112         return false;
00113     //Check if document is well-formed
00114     if (!layoutDocument.setContent(&layoutFile))
00115     {
00116         layoutFile.close();
00117         return false;
00118     }
00119     layoutFile.close();
00120 
00121     return true;
00122 }
00123 
00124 void KLettres::setupActions()
00125 {
00126     QAction *m_newAction = actionCollection()->addAction("play_new");
00127     m_newAction->setText(i18n("New Sound"));
00128     m_newAction->setShortcut(QKeySequence(Qt::CTRL+Qt::Key_N));
00129     m_newAction->setIcon(KIcon("document-new")); // ### better icon for this?
00130     connect(m_newAction, SIGNAL(triggered(bool)), m_view, SLOT(game()));
00131     m_newAction->setToolTip(i18n("Play a new sound"));
00132     m_newAction->setWhatsThis(i18n("You can play a new sound by clicking this button or using the File menu, New Sound."));
00133 
00134     QAction *m_stuffAction = actionCollection()->addAction("downloadnewstuff");
00135     m_stuffAction->setText(i18n("Get Alphabet in New Language..."));
00136     m_stuffAction->setIcon(KIcon("get-hot-new-stuff"));
00137     connect(m_stuffAction, SIGNAL(triggered(bool)),  this, SLOT( slotDownloadNewStuff() ));
00138 
00139     QAction *m_playAgainAction = actionCollection()->addAction("play_again");
00140     m_playAgainAction->setText(i18n("Replay Sound"));
00141     m_playAgainAction->setShortcut(QKeySequence(Qt::Key_F5));
00142     m_playAgainAction->setIcon(KIcon("media-playback-start"));
00143     m_playAgainAction->setToolTip(i18n("Play the same sound again"));
00144     connect(m_playAgainAction, SIGNAL(triggered(bool)), m_view, SLOT(slotPlayAgain()));
00145     m_playAgainAction->setWhatsThis(i18n("You can replay the same sound again by clicking this button or using the File menu, Replay Sound."));
00146     KStandardAction::quit(kapp, SLOT(quit()), actionCollection());
00147 
00148     m_menubarAction = actionCollection()->add<KToggleAction>("menubar");
00149     m_menubarAction->setText(i18n("Show &Menubar"));
00150     m_menubarAction->setIcon(KIcon("edit-clear"));
00151     m_menubarAction->setShortcut(QKeySequence(Qt::CTRL+Qt::Key_M));
00152     connect(m_menubarAction, SIGNAL(triggered(bool)), this, SLOT(slotMenubar()));
00153     m_menubarAction->setCheckedState(KGuiItem(i18n("Hide &Menubar")));
00154     m_menubarAction->setChecked(true);
00155     m_menubarAction->setWhatsThis(i18n("You can show or hide the menubar as you wish by clicking this button."));
00156 
00157     m_levelAction = actionCollection()->add<KSelectAction>("levels");
00158     m_levelAction->setText(i18nc("@label:listbox which difficulty level to use", "L&evel"));
00159     m_levelAction->setToolTip(i18n("Select the level"));
00160     m_levelAction->setWhatsThis(i18n("You can select the level: level 1 displays a letter and you hear it; level 2 does not display the letter, you only hear it; level 3 displays a syllable and you hear it; level 4 does not display the syllable, you only hear it."));
00161 
00162     m_languageAction = actionCollection()->add<KSelectAction>("languages");
00163     m_languageAction->setText(i18nc("@label:listbox", "&Language"));
00164     m_languageAction->setItems(m_languageNames);
00165 
00166     m_levelsNames.append(i18nc("@item:inlistbox choose level 1", "Level 1" ));
00167     m_levelsNames.append(i18nc("@item:inlistbox choose level 2",  "Level 2" ));
00168     m_levelsNames.append(i18nc("@item:inlistbox choose level 3",  "Level 3" ));
00169     m_levelsNames.append(i18nc("@item:inlistbox choose level 4",  "Level 4" ));
00170     m_levelAction->setItems(m_levelsNames);
00171     m_levelAction->setCurrentItem(Prefs::level()-1);
00172 
00173     m_themeAction = actionCollection()->add<KSelectAction>("looks");
00174     m_themeAction->setText(i18n("Themes"));
00175     m_themeAction->setItems(KLThemeFactory::instance()->themeList());
00176     m_themeAction->setCurrentItem(Prefs::theme());
00177     m_themeAction->setToolTip(i18n("Select the theme"));
00178     m_themeAction->setWhatsThis(i18n("Here you can change the theme for KLettres. A theme consists in the background picture and the font color for the letter displayed."));
00179 
00180     m_kidAction = actionCollection()->add<KToggleAction>("mode_kid");
00181     m_kidAction->setText(i18n("Mode Kid"));
00182     m_kidAction->setShortcut(QKeySequence(Qt::CTRL+Qt::Key_K));
00183     m_kidAction->setIcon(KIcon("klettres_kids"));
00184     connect(m_kidAction, SIGNAL(triggered(bool)), this, SLOT(slotModeKid()));
00185     m_kidAction->setWhatsThis(i18n("If you are in the Grown-up mode, clicking on this button will set up the Kid mode. The Kid mode has no menubar and the font is bigger in the statusbar."));
00186 
00187     m_grownupAction = actionCollection()->add<KToggleAction>("mode_grownup");
00188     m_grownupAction->setText(i18n("Mode Grown-up"));
00189     m_grownupAction->setShortcut(QKeySequence(Qt::CTRL+Qt::Key_G));
00190     m_grownupAction->setIcon(KIcon("klettres_grownup"));
00191     connect(m_grownupAction, SIGNAL(triggered(bool)), this, SLOT(slotModeGrownup()));
00192     m_grownupAction->setWhatsThis(i18n("The Grownup mode is the normal mode where you can see the menubar."));
00193 
00194     connect(m_levelAction, SIGNAL(triggered(int)), this, SLOT(slotChangeLevel(int)));
00195     connect(m_languageAction, SIGNAL(triggered(int)), this, SLOT(slotChangeLanguage(int)));
00196     connect(m_themeAction, SIGNAL(triggered(int)), this, SLOT(slotChangeTheme(int)));
00197 
00198     KStandardAction::preferences(this, SLOT(optionsPreferences()), actionCollection());
00199 
00200     setupGUI();
00201 }
00202 
00203 void KLettres::setupStatusbar()
00204 {
00205     KStatusBar *st=statusBar();
00206     m_langLabel = new QLabel(st);
00207     m_levLabel = new QLabel(st);
00208     st->addWidget(m_langLabel);
00209     st->insertFixedItem("", 1);//add a space
00210     st->addWidget(m_levLabel);   
00211     statusBar();
00212 }
00213 
00214 void KLettres::setupToolbars()
00215 {
00216     // Toolbar for special characters
00217     specialCharToolbar = toolBar("specialCharToolbar");
00218     addToolBar ( Qt::BottomToolBarArea, specialCharToolbar);
00219 }
00220 
00221 void KLettres::optionsPreferences()
00222 {
00223     if(KConfigDialog::showDialog("settings"))
00224         return;
00225 
00226     KConfigDialog *dialog = new KConfigDialog(this, "settings", Prefs::self());
00227     dialog->addPage(new fontsdlg(0), i18n("Font Settings"), "preferences-desktop-font");
00228     //fontsdlg is the page name, mFont is the widget name, Font Settings is the page display string
00229     //fonts is the icon
00230     Timer *m_timer = new Timer();
00231     dialog->addPage(m_timer, i18n("Timer"), "clock");
00232     connect(dialog, SIGNAL(settingsChanged( const QString &)), this, SLOT(slotUpdateSettings()));
00233     dialog->setAttribute( Qt::WA_DeleteOnClose );
00234     dialog->setHelp(QString(), "klettres");
00235     dialog->show();
00236     
00237     
00238 }
00239 
00240 void KLettres::loadSettings()
00241 {
00242     if (LangUtils::getLanguages().indexOf(Prefs::language()) <0)  {
00243     Prefs::setLanguage("en");
00244     }
00245     QString langString = LangUtils::getLanguagesNames(LangUtils::getLanguages())[LangUtils::getLanguages().indexOf(Prefs::language())];
00246     m_languageAction->setCurrentItem(LangUtils::getLanguages().indexOf(Prefs::language()));
00247     langString.replace("&", QString());
00248     m_langLabel->setText(langString);
00249     loadLangToolBar();
00250     // load default level
00251     m_levLabel->setText(i18nc("@info:status the current level chosen", "(Level %1)", Prefs::level()));
00252 
00253     m_view->setTheme(KLThemeFactory::instance()->buildTheme(Prefs::theme()));
00254 
00255     if (Prefs::mode() == Prefs::EnumMode::grownup)
00256         slotModeGrownup();
00257     else
00258         slotModeKid();
00259 
00260     m_menubarAction->setChecked(Prefs::menuBarBool());
00261     slotMenubar();
00262 }
00263 
00264 void KLettres::slotDownloadNewStuff()
00265 {
00266     KNS::Entry::List entries = KNS::Engine::download();
00267     // we need to delete the entry* items in the returned list
00268     qDeleteAll(entries);
00269 
00270     //look for languages dirs installed
00271     QStringList languages = LangUtils::getLanguages();
00272     m_languageNames = LangUtils::getLanguagesNames(languages);
00273 
00274     //refresh Languages menu
00275     m_languageAction->setItems(m_languageNames);
00276     slotChangeLanguage(languages.indexOf(Prefs::language()));
00277     m_languageAction->setCurrentItem(languages.indexOf(Prefs::language()));
00278 }
00279 
00280 void KLettres::slotMenubar()
00281 {
00282     switch (m_menubarAction->isChecked()){
00283     case false:
00284     m_menubarAction->setChecked(false);
00285         m_menubarAction->setText(i18n("Show Menubar"));
00286         m_menubarAction->setToolTip(i18n("Show Menubar"));
00287         menuBar()->hide();
00288         break;
00289     case true:
00290         m_menubarAction->setChecked(true);
00291         m_menubarAction->setText(i18n("Hide Menubar"));
00292         m_menubarAction->setToolTip(i18n("Hide Menubar"));
00293         menuBar()->show();
00294         break;
00295     }
00296     Prefs::setMenuBarBool(m_menubarAction->isChecked());
00297     Prefs::self()->writeConfig();
00298 }
00299 
00300 void KLettres::slotUpdateSettings()
00301 {
00302     m_view->m_timer = Prefs::kidTimer();
00303     m_view->m_timer = Prefs::grownTimer();
00304     //apply the font
00305     m_view->setFont(Prefs::font());
00306 }
00307 
00308 void KLettres::slotChangeLevel(int newLevel)
00309 {
00310     Prefs::setLevel(newLevel+1);
00311     Prefs::self()->writeConfig();
00312     updateLevMenu(newLevel);
00313     //TODO is that necessary? Change level effectively by reloading sounds
00314 
00315     //this is duplicated in changeLanguage()
00316     soundFactory->change(Prefs::language());
00317     //update game effectively
00318     m_view->randomInt = 0;
00319     m_view->game();
00320 }
00321 
00322 void KLettres::updateLevMenu(int id)
00323 {
00324     //m_levelCombo->setCurrentItem(id);
00325     m_levelAction->setCurrentItem(id);
00326     m_levLabel->setText(i18nc("@info:status the current level chosen", "(Level %1)", Prefs::level()));
00327 }
00328 
00329 void KLettres::slotChangeLanguage(int newIndex)
00330 {
00331     // Write new language ISO in config
00332     QString newLanguage = LangUtils::getLanguages()[newIndex];
00333     Prefs::setLanguage(newLanguage);
00334     Prefs::self()->writeConfig();
00335     // Update the StatusBar
00336     QString langString = LangUtils::getLanguagesNames(LangUtils::getLanguages())[newIndex];
00337     langString.replace("&", QString());
00338     m_langLabel->setText(langString);
00339     loadLangToolBar();
00340     // Change language effectively
00341     bool ok = loadLayout(soundFactory->m_layoutsDocument);
00342     if (ok)
00343         soundFactory->change(Prefs::language());
00344     m_view->randomInt = 0;
00345     m_view->game();
00346 }
00347 
00348 void KLettres::slotChangeTheme(int index)
00349 {
00350     Prefs::setTheme(index);
00351     Prefs::self()->writeConfig();
00352     m_view->setTheme(KLThemeFactory::instance()->buildTheme(index));
00353 }
00354 
00355 void KLettres::slotModeGrownup()
00356 {
00357     QPalette pal;
00358     pal.setColor( QPalette::Background, Qt::white);
00359     statusBar()->setPalette( pal );
00360     QFont f_lab( "Serif" , 10);  //font for statusBar
00361     m_levLabel->setFont(f_lab);
00362     m_langLabel->setFont(f_lab);
00363     m_menubarAction->setChecked(true);
00364     m_grownupAction->setChecked(true);
00365     m_kidAction->setChecked(false);
00366     m_grownupAction->setToolTip(i18n("Grown-up mode is currently active"));
00367     m_kidAction->setToolTip(i18n("Switch to Kid mode"));
00368     m_menubarAction->setText(i18n("Hide Menubar"));
00369     m_menubarAction->setToolTip(i18n("Hide Menubar"));
00370     menuBar()->show();
00371     m_view->m_timer = Prefs::grownTimer();
00372     Prefs::setMode(Prefs::EnumMode::grownup);
00373     Prefs::self()->writeConfig();
00374 }
00375 
00376 void KLettres::slotModeKid()
00377 {
00378     QPalette pal;
00379     pal.setColor( QPalette::Background, Qt::white);
00380     statusBar()->setPalette( pal );
00381     QFont f_lab( "Serif" , 12);  //font for statusBar
00382     f_lab.setBold(true);
00383     m_levLabel->setFont(f_lab);
00384     m_langLabel->setFont(f_lab);
00385     m_menubarAction->setChecked(false);
00386     m_kidAction->setChecked(true);
00387     m_kidAction->setToolTip(i18n("Kid mode is currently active"));
00388     m_grownupAction->setToolTip(i18n("Switch to Grown-up mode"));
00389     m_menubarAction->setText(i18n("Show Menubar"));
00390     m_menubarAction->setToolTip(i18n("Show Menubar"));
00391     m_grownupAction->setChecked(false);
00392     menuBar()->hide();
00393     m_view->m_timer = Prefs::kidTimer();
00394     Prefs::setMode(Prefs::EnumMode::kid);
00395     Prefs::self()->writeConfig();
00396 }
00397 
00398 void KLettres::loadLangToolBar()
00399 {
00400     QString lang = Prefs::language();
00401 
00402     specialCharToolbar->clear();
00403 
00404     if (LangUtils::hasSpecialChars(lang))//Dutch, English, French and Italian have no special characters
00405     {
00406         allData.clear();
00407         QString myString=QString("klettres/%1.txt").arg(lang);
00408         QFile myFile;
00409         myFile.setFileName(KStandardDirs::locate("data",myString));
00410         if (!myFile.exists())
00411         {
00412 
00413             QString mString=i18n("File $KDEDIR/share/apps/klettres/%1.txt not found;\n"
00414                                     "please check your installation.", lang);
00415             KMessageBox::sorry( this, mString,
00416                                     i18n("Error") );
00417             qApp->quit();
00418         }
00419         //we open the file and store info into the stream...
00420         QFile openFileStream(myFile.fileName());
00421         openFileStream.open(QIODevice::ReadOnly);
00422         QTextStream readFileStr(&openFileStream);
00423         readFileStr.setCodec("UTF-8");
00424         //allData contains all the words from the file
00425         allData = readFileStr.readAll().split("\n");
00426         openFileStream.close();
00427         for (int i=0; i<(int) allData.count(); ++i) {
00428             if (!allData[i].isEmpty()) {
00429                 QAction *act = specialCharToolbar->addAction(allData.at(i));
00430                 act->setIcon(charIcon(allData.at(i).at(0)));
00431                 // used to carry the id
00432                 act->setData(i);
00433         connect(act, SIGNAL(triggered(bool)), this, SLOT(slotPasteChar()));
00434         }
00435         }
00436     specialCharToolbar->show();
00437     update();
00438     }
00439     else {
00440       specialCharToolbar->hide();
00441     }
00442 }
00443 
00444 void KLettres::slotPasteChar()
00445 {
00446     QAction *act = qobject_cast<QAction*>(sender());
00447     if (!act)  {
00448         return;
00449     }
00450 
00451     bool ok = true;
00452     int id = act->data().toInt(&ok);
00453     if (!ok || id < 0 || id >= allData.count())
00454         return;
00455 
00456     m_view->m_letterEdit->insert(allData.at(id));
00457 }
00458 
00459 QIcon KLettres::charIcon(const QChar & c)
00460 {
00462     QString s = KStandardDirs::locateLocal("icon", "char" + QString::number(c.unicode()) + ".png");
00463 
00464     QRect r(4, 4, 120, 120);
00465 
00467     QFont font;
00468     font.setFamily( "Arial" );
00469     font.setPixelSize(120);
00470     font.setWeight(QFont::Normal);
00471 
00473     QPixmap pm(128, 128);
00474     pm.fill(Qt::white);
00475     QPainter p(&pm);
00476     p.setFont(font);
00477     p.setPen(Qt::black);
00478     p.drawText(r, Qt::AlignCenter, (QString) c);
00479 
00481     QBitmap bm(128, 128);
00482     bm.fill(Qt::color0);
00483     QPainter b(&bm);
00484     b.setFont(font);
00485     b.setPen(Qt::color1);
00486     b.drawText(r, Qt::AlignCenter, (QString) c);
00487 
00489     pm.setMask(bm);
00490 
00492     pm.save(s, "PNG");
00493 
00494     return QIcon(pm);
00495 }
00496 
00497 #include "klettres.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