moved kdeaccessibility kdeaddons kdeadmin kdeartwork kdebindings kdeedu kdegames...
[kdeedu.git] / klettres / klettres / klettres.cpp
blobf0b2e6f0bb0737f496ec20fa8d9ae5257ce8ef3d
1 /***************************************************************************
2 * Copyright (C) 2001-2005 by Anne-Marie Mahfouf *
3 * annemarie.mahfouf@free.fr *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
21 //Qt includes
22 #include <qbitmap.h>
23 #include <qdir.h>
24 #include <qfile.h>
25 #include <qlabel.h>
26 #include <qpainter.h>
27 #include <qstring.h>
28 #include <qtooltip.h>
29 #include <qwhatsthis.h>
31 //KDE includes
32 #include <kaction.h>
33 #include <kapplication.h>
34 #include <kcombobox.h>
35 #include <kconfigdialog.h>
36 #include <kdebug.h>
37 #include <klocale.h>
38 #include <kmenubar.h>
39 #include <kmessagebox.h>
40 #include <knuminput.h>
41 #include <kstandarddirs.h>
42 #include <kstatusbar.h>
43 #include <ktoolbar.h>
44 #include <ktoolbarbutton.h>
45 //Project includes
46 #include "klnewstuff.h"
47 #include "klettres.h"
48 #include "fontsdlg.h"
49 #include "timerdlg.h"
50 #include "prefs.h"
52 const int ID_KIDB = 100;
53 const int ID_GROWNB = 101;
54 const int ID_MENUBARB = 102;
56 KLettres::KLettres()
57 : KMainWindow( 0, "KLettres" )
59 m_view = new KLettresView(this);
60 // tell the KMainWindow that this is indeed the main widget
61 setCentralWidget(m_view);
62 //Scan for existing languages -> m_languages
63 findLanguages();
64 //MainWindow GUI: menus, tolbars and statusbar
65 setupActions();
66 setupStatusbar();
67 setupToolbars();
68 //Load Settings
69 loadSettings();
70 //Setup current language sounds
71 soundFactory = new SoundFactory(this, "sounds");
72 setMinimumSize( QSize( 640, 550 ) );
73 setMaximumSize( QSize( 640, 550 ) );
74 //Start game
75 m_view->game();
78 KLettres::~KLettres()
82 void KLettres::findLanguages()
84 m_languages.clear();
85 m_languageNames.clear();
86 //m_sortedNames.clear();
87 //the program scans in khangman/data/ to see what languages data is found
88 QStringList mdirs = KGlobal::dirs()->findDirs("data", "klettres/");
89 if (mdirs.isEmpty()) return;
90 for (QStringList::Iterator it =mdirs.begin(); it !=mdirs.end(); ++it ) {
91 QDir dir(*it);
92 m_languages += dir.entryList(QDir::Dirs, QDir::Name);
93 m_languages.remove(m_languages.find("."));
94 m_languages.remove(m_languages.find(".."));
96 m_languages.remove(m_languages.find("pics"));
97 m_languages.remove(m_languages.find("data"));
98 m_languages.remove(m_languages.find("icons"));
99 m_languages.sort();
100 if (m_languages.isEmpty()) return;
101 Prefs::setLanguages(m_languages);
102 Prefs::writeConfig();
103 //find duplicated entries in KDEDIR and KDEHOME
104 for (uint i=0; i<m_languages.count(); i++)
106 if (m_languages.contains(m_languages[i])>1)
107 m_languages.remove(m_languages[i]);
109 //write the present languages in config so they cannot be downloaded
110 KConfig *config=kapp->config();
111 config->setGroup("KNewStuffStatus");
112 for (uint i=0; i<m_languages.count(); i++)
114 QString tmp = m_languages[i];
115 if (!config->readEntry(tmp))
116 config->writeEntry(tmp, QDate::currentDate().toString());
118 //we look in $KDEDIR/share/locale/all_languages from /kdelibs/kdecore/all_languages
119 //to find the name of the country
120 //corresponding to the code and the language the user set
121 kdDebug() << "m_languages :" << m_languages << endl;
122 KConfig entry(locate("locale", "all_languages"));
123 const QStringList::ConstIterator itEnd = m_languages.end();
124 for (QStringList::Iterator it = m_languages.begin(); it != m_languages.end(); ++it) {
125 if (*it == "hi-ro")
126 m_languageNames.append(i18n("Romanized Hindi"));
127 else if (*it =="lug_UG")
128 m_languageNames.append(i18n("Luganda"));
129 else
131 entry.setGroup(*it);
132 m_languageNames.append(entry.readEntry("Name"));
135 kdDebug() << "m_languageNames :" << m_languageNames << endl;
136 kdDebug() << "Index: " << m_languages.findIndex(Prefs::defaultLanguage()) << endl;
137 //never sort m_languageNames as it's m_languages translated
138 //m_sortedNames = m_languageNames;
141 QString Prefs::defaultLanguage()
143 //see what is the user language for KDE
144 QStringList defaultLanguages = KGlobal::locale()->languagesTwoAlpha();
145 kdDebug() << defaultLanguages << endl;
146 if (!defaultLanguages.isEmpty()) {
147 //scan to see if defaultLanguages[0] belongs to m_languages. If not, en is default.
148 int i = Prefs::self()->m_languages.findIndex(defaultLanguages[0]);
149 if (i<1)
150 return "en";
151 else
152 return defaultLanguages[0];
154 return QString::null;
157 bool KLettres::loadLayout(QDomDocument &layoutDocument)
159 QFile layoutFile(locate("data", "klettres/"+Prefs::language()+"/sounds.xml"));
160 //if xml file is not found, program exits
161 if (!layoutFile.exists())
163 kdWarning() << "sounds.xml file not found in $KDEDIR/share/apps/klettres/"+Prefs::language() << endl;
164 QString mString=i18n("The file sounds.xml was not found in\n"
165 "$KDEDIR/share/apps/klettres/\n\n"
166 "Please install this file and start KLettres again.\n\n");
167 KMessageBox::information( this, mString,"KLettres - Error" );
168 kapp->quit();//exit(1);
170 if (!layoutFile.open(IO_ReadOnly))
171 return false;
172 //Check if document is well-formed
173 if (!layoutDocument.setContent(&layoutFile))
175 layoutFile.close();
176 return false;
178 layoutFile.close();
180 return true;
183 void KLettres::setupActions()
185 new KAction( i18n("Get Alphabet in New Language..."), "knewstuff", 0, this, SLOT( slotDownloadNewStuff() ), actionCollection(), "downloadnewstuff" );
186 KAction *m_playAgainAction = new KAction(i18n("Replay Sound"),"player_play", CTRL+Key_P, m_view, SLOT(slotPlayAgain()), actionCollection(), "play_again");
187 m_playAgainAction->setToolTip(i18n("Play the same sound again"));
188 m_playAgainAction->setWhatsThis(i18n("You can replay the same sound again by clicking this button or using the File menu, Replay Sound."));
189 KStdAction::quit(kapp, SLOT(quit()), actionCollection());
191 m_menubarAction = new KToggleAction(i18n("Show &Menubar"),"editclear", CTRL+Key_M, this, SLOT(slotMenubar()), actionCollection(), "menubar");
192 m_menubarAction->setCheckedState(i18n("Hide &Menubar"));
193 m_menubarAction->setChecked(true);
194 m_menubarAction->setWhatsThis(i18n("You can show or hide the menubar as you wish by clicking this button."));
196 m_levelAction = new KSelectAction(i18n("L&evel"), KShortcut(), actionCollection(), "levels");
197 m_levelAction->setToolTip(i18n("Select the level"));
198 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."));
200 m_languageAction = new KSelectAction(i18n("&Language"), KShortcut(), actionCollection(), "languages");
201 m_languageAction->setItems(m_languageNames);
203 m_levelsNames.append(i18n( "Level 1" ));
204 m_levelsNames.append(i18n( "Level 2" ));
205 m_levelsNames.append(i18n( "Level 3" ));
206 m_levelsNames.append(i18n( "Level 4" ));
207 m_levelAction->setItems(m_levelsNames);
209 m_themeAction = new KSelectAction(i18n("Themes"), KShortcut(), actionCollection(), "looks");
210 m_themesNames.append(i18n("Classroom"));
211 m_themesNames.append(i18n("Arctic"));
212 m_themesNames.append(i18n("Desert"));
213 m_themeAction->setItems(m_themesNames);
214 m_themeAction->setToolTip(i18n("Select the theme"));
215 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."));
217 m_kidAction = new KToggleAction(i18n("Mode Kid"), "klettres_kids", CTRL+Key_K, this, SLOT(slotModeKid()), actionCollection(), "mode_kid");
218 m_kidAction->setWhatsThis(i18n("If you are in the Grownup 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."));
219 m_grownupAction = new KToggleAction(i18n("Mode Grownup"), "klettres_grownup", CTRL+Key_G, this, SLOT(slotModeGrownup()), actionCollection(), "mode_grownup");
220 m_grownupAction->setWhatsThis(i18n("The Grownup mode is the normal mode where you can see the menubar."));
222 connect(m_levelAction, SIGNAL(activated(int)), this, SLOT(slotChangeLevel(int)));
223 connect(m_languageAction, SIGNAL(activated(int)), this, SLOT(slotChangeLanguage(int)));
224 connect(m_themeAction, SIGNAL(activated(int)), this, SLOT(slotChangeTheme(int)));
226 KStdAction::preferences(this, SLOT(optionsPreferences()), actionCollection());
228 setupGUI();
231 void KLettres::setupStatusbar()
233 KStatusBar *st=statusBar();
234 m_langLabel = new QLabel(st);
235 m_levLabel = new QLabel(st);
236 st->addWidget(m_levLabel);
237 st->insertFixedItem("", 1);//add a space
238 st->addWidget(m_langLabel);
239 statusBar();
242 void KLettres::setupToolbars()
244 //toolbar for special characters
245 m_secondToolbar = toolBar("secondToolbar");
246 m_secondToolbar->setBarPos(KToolBar::Bottom);
249 void KLettres::optionsPreferences()
251 if(KConfigDialog::showDialog("settings"))
252 return;
254 KConfigDialog *dialog = new KConfigDialog(this, "settings", Prefs::self());
255 dialog->addPage(new fontsdlg(0, "mFont"), i18n("Font Settings"), "fonts");
256 //fontsdlg is the page name, mFont is the widget name, Font Settings is the page display string
257 //fonts is the icon
258 timerdlg *m_timer = new timerdlg();
259 dialog->addPage(m_timer, i18n("Timer"), "clock");
260 m_timer->kcfg_KidTimer->setRange(0, 20, 2, true);
261 m_timer->kcfg_GrownTimer->setRange(0, 20, 2, true);
262 connect(dialog, SIGNAL(settingsChanged()), this, SLOT(slotUpdateSettings()));
263 dialog->show();
266 void KLettres::loadSettings()
268 //TODO load default language
269 selectedLanguage = Prefs::languageNumber();
270 m_view->selectedLanguage = selectedLanguage;
271 m_languageAction->setCurrentItem(selectedLanguage);
272 QString langString = m_languageNames[selectedLanguage];
273 langString.replace("&", QString::null);
274 m_langLabel->setText(i18n("Current language is %1").arg(langString));
275 loadLangToolBar();
276 // load default level
277 kdDebug() << "Level ---- : " << Prefs::level() << endl;
278 //m_levelCombo->setCurrentItem(Prefs::level()-1);
279 m_levelAction->setCurrentItem(Prefs::level()-1);
280 m_levLabel->setText(i18n("Current level is %1").arg(Prefs::level()));
282 if (Prefs::theme() == Prefs::EnumTheme::classroom) {
283 m_themeAction->setCurrentItem(0);
284 m_view->viewThemeClassroom();
286 else if (Prefs::theme() == Prefs::EnumTheme::arctic) {
287 m_themeAction->setCurrentItem(1);
288 m_view->viewThemeArctic();
290 else {
291 m_themeAction->setCurrentItem(2);
292 m_view->viewThemeDesert();
295 if (Prefs::mode() == Prefs::EnumMode::grownup)
296 slotModeGrownup();
297 else
298 slotModeKid();
301 void KLettres::slotDownloadNewStuff()
303 if ( !mNewStuff )
304 mNewStuff = new KLNewStuff( m_view );
305 mNewStuff->download();
308 void KLettres::slotMenubar()
310 switch (m_menubarAction->isChecked()){
311 case false:
312 m_menubarAction->setChecked(false);
313 m_menubarAction->setToolTip(i18n("Show menubar"));
314 menuBar()->hide();
315 break;
316 case true:
317 m_menubarAction->setChecked(true);
318 m_menubarAction->setToolTip(i18n("Hide menubar"));
319 menuBar()->show();
320 break;
324 void KLettres::slotUpdateSettings()
326 m_view->m_timer = Prefs::kidTimer();
327 m_view->m_timer = Prefs::grownTimer();
328 //apply the font
329 m_view->setFont(Prefs::font());
332 void KLettres::slotChangeLevel(int newLevel)
334 Prefs::setLevel(newLevel+1);
335 Prefs::writeConfig();
336 updateLevMenu(newLevel);
337 //TODO is that necessary? Change level effectively by reloading sounds
339 //this is duplicated in changeLanguage()
340 soundFactory->change(Prefs::language());
341 //update game effectively
342 m_view->game();
345 void KLettres::updateLevMenu(int id)
347 //m_levelCombo->setCurrentItem(id);
348 m_levelAction->setCurrentItem(id);
349 m_levLabel->setText(i18n("Current level is %1").arg(Prefs::level()));
352 void KLettres::slotChangeLanguage(int newLanguage)
354 // Change language
355 selectedLanguage = newLanguage;
356 // Write new language in config
357 Prefs::setLanguageNumber(newLanguage);
358 Prefs::writeConfig();
359 // Update the StatusBar
360 QString langString = m_languageNames[newLanguage];
361 langString.replace("&", QString::null);
362 m_langLabel->setText(i18n("Current language is %1").arg(langString));
363 loadLangToolBar();
364 // Change language effectively
365 kdDebug() << "In change language " << Prefs::language() << endl;
366 bool ok = loadLayout(soundFactory->m_layoutsDocument);
367 if (ok)
368 soundFactory->change(Prefs::language());
369 m_view->game();
372 void KLettres::slotChangeTheme(int index)
374 switch (index) {
375 case 0:
376 m_view->viewThemeClassroom();
377 break;
379 case 1:
380 m_view->viewThemeArctic();
381 break;
383 case 2:
384 m_view->viewThemeDesert();
385 break;
389 void KLettres::slotModeGrownup()
391 QPalette pal;
392 QColorGroup cg;
393 cg.setColor( QColorGroup::Background, white);
394 pal.setActive( cg );
395 statusBar()->setPalette( pal );
396 QFont f_lab( "times" , 12); //font for statusBar
397 m_levLabel->setFont(f_lab);
398 m_langLabel->setFont(f_lab);
399 m_menubarAction->setChecked(true);
400 m_grownupAction->setChecked(true);
401 m_kidAction->setChecked(false);
402 m_grownupAction->setToolTip(i18n("Grownup mode is currently active"));
403 m_kidAction->setToolTip(i18n("Switch to Kid mode"));
404 m_menubarAction->setToolTip(i18n("Hide menubar"));
405 slotMenubar();
406 m_secondToolbar->setIconSize(22);
407 setMinimumSize( QSize( 640, 538 ) );
408 setMaximumSize( QSize( 640, 538 ) );
409 m_view->m_timer = Prefs::grownTimer();
410 Prefs::setMode(Prefs::EnumMode::grownup);
411 Prefs::writeConfig();
414 void KLettres::slotModeKid()
416 QPalette pal;
417 QColorGroup cg;
418 cg.setColor( QColorGroup::Background, white);
419 pal.setActive( cg );
420 statusBar()->setPalette( pal );
421 QFont f_lab( "times" , 14); //font for statusBar
422 f_lab.setBold(true);
423 m_levLabel->setFont(f_lab);
424 m_langLabel->setFont(f_lab);
425 m_menubarAction->setChecked(false);
426 slotMenubar();
427 m_kidAction->setChecked(true);
428 m_kidAction->setToolTip(i18n("Kid mode is currently active"));
429 m_grownupAction->setToolTip(i18n("Switch to Grownup mode"));
430 m_menubarAction->setToolTip(i18n("Show menubar"));
431 m_grownupAction->setChecked(false);
432 m_secondToolbar->setIconSize(32);
433 setMinimumSize( QSize( 640, 480 ) );
434 setMaximumSize( QSize( 640, 480 ) );
435 m_view->m_timer = Prefs::kidTimer();
436 Prefs::setMode(Prefs::EnumMode::kid);
437 Prefs::writeConfig();
440 void KLettres::loadLangToolBar()
442 m_secondToolbar->clear();
443 if (m_languages[selectedLanguage]== "cs" || m_languages[selectedLanguage]== "da" || m_languages[selectedLanguage]== "sk" || m_languages[selectedLanguage]== "es")//Dutch, English, French and Italian have no special characters
445 allData.clear();
446 QString myString=QString("klettres/%1.txt").arg(m_languages[selectedLanguage]);
447 QFile myFile;
448 myFile.setName(locate("data",myString));
449 if (!myFile.exists())
452 QString mString=i18n("File $KDEDIR/share/apps/klettres/%1.txt not found;\n"
453 "please check your installation.").arg(m_languages[selectedLanguage]);
454 KMessageBox::sorry( this, mString,
455 i18n("Error") );
456 kapp->quit();
458 update();
459 //we open the file and store info into the stream...
460 QFile openFileStream(myFile.name());
461 openFileStream.open(IO_ReadOnly);
462 QTextStream readFileStr(&openFileStream);
463 readFileStr.setEncoding(QTextStream::UnicodeUTF8);
464 //allData contains all the words from the file
465 allData = QStringList::split("\n", readFileStr.read(), true);
466 openFileStream.close();
467 for (int i=0; i<(int) allData.count(); i++) {
468 m_secondToolbar->insertButton (charIcon(allData[i].at(0)), i, SIGNAL( clicked() ), this, SLOT( slotPasteChar()), true, i18n("Inserts the character %1").arg(allData[i]), i+1 );
473 void KLettres::slotPasteChar()
475 KToolBarButton *charBut = (KToolBarButton* ) sender();
476 m_view->m_letterEdit->setText(allData[charBut->id()]);
479 QString KLettres::charIcon(const QChar & c)
481 ///Create a name and path for the icon
482 QString s = locateLocal("icon", "char" + QString::number(c.unicode()) + ".png");
484 QRect r(4, 4, 120, 120);
486 ///A font to draw the character with
487 QFont font;
488 font.setFamily( "Arial" );
489 font.setPixelSize(120);
490 font.setWeight(QFont::Normal);
492 ///Create the pixmap
493 QPixmap pm(128, 128);
494 pm.fill(Qt::white);
495 QPainter p(&pm);
496 p.setFont(font);
497 p.setPen(Qt::black);
498 p.drawText(r, Qt::AlignCenter, (QString) c);
500 ///Create transparency mask
501 QBitmap bm(128, 128);
502 bm.fill(Qt::color0);
503 QPainter b(&bm);
504 b.setFont(font);
505 b.setPen(Qt::color1);
506 b.drawText(r, Qt::AlignCenter, (QString) c);
508 ///Mask the pixmap
509 pm.setMask(bm);
511 ///Save the icon to disk
512 pm.save(s, "PNG");
514 return s;
517 #include "klettres.moc"