00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "klettres.h"
00022
00023
00024 #include <QBitmap>
00025 #include <QDir>
00026 #include <QFile>
00027 #include <QLabel>
00028 #include <QPainter>
00029 #include <QTextStream>
00030 #include <QDomDocument>
00031
00032
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
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
00079 setCentralWidget(m_view);
00080
00081 m_languages = LangUtils::getLanguages();
00082 kDebug() << "m_languages " << m_languages;
00083 findLanguages();
00084
00085 setupActions();
00086 setupStatusbar();
00087 setupToolbars();
00088
00089 loadSettings();
00090
00091 soundFactory = new SoundFactory(this, "sounds");
00092
00093 m_view->game();
00094 }
00095
00096 KLettres::~KLettres()
00097 {
00098 }
00099
00100 void KLettres::findLanguages()
00101 {
00102 m_languageNames.clear();
00103
00104
00105
00106
00107
00108 KConfig entry(KStandardDirs::locate("locale", "all_languages"));
00109
00110 foreach(const QString &language, m_languages) {
00111 if (language == "hi-ro")
00112 m_languageNames.append(i18n("Romanized Hindi"));
00113 else if (language == "lug_UG")
00114 m_languageNames.append(i18n("Luganda"));
00115 else
00116 {
00117 KConfigGroup group = entry.group(language);
00118 m_languageNames.append(group.readEntry("Name"));
00119 }
00120 }
00121
00122 }
00123
00124 QString Prefs::defaultLanguage()
00125 {
00126
00127 QStringList defaultLanguages = KGlobal::locale()->languageList();
00128 if (!defaultLanguages.isEmpty()) {
00129
00130 int i = Prefs::self()->m_languages.indexOf(defaultLanguages[0]);
00131 if (Prefs::self()->m_languages.contains(Prefs::language()))
00132 return Prefs::language();
00133 else if (i<1)
00134 return "en";
00135 else
00136 return Prefs::self()->m_languages[i];
00137 }
00138 return QString();
00139 }
00140
00141
00142 bool KLettres::loadLayout(QDomDocument &layoutDocument)
00143 {
00144 QFile layoutFile(KStandardDirs::locate("data", "klettres/"+Prefs::language()+"/sounds.xml"));
00145
00146 if (!layoutFile.exists())
00147 {
00148 kWarning() << "sounds.xml file not found in $KDEDIR/share/apps/klettres/"+Prefs::language() ;
00149 QString mString=i18n("The file sounds.xml was not found in\n"
00150 "$KDEDIR/share/apps/klettres/\n\n"
00151 "Please install this file and start KLettres again.\n\n");
00152 KMessageBox::information( this, mString,"KLettres - Error" );
00153 qApp->quit();
00154 }
00155 if (!layoutFile.open(QIODevice::ReadOnly))
00156 return false;
00157
00158 if (!layoutDocument.setContent(&layoutFile))
00159 {
00160 layoutFile.close();
00161 return false;
00162 }
00163 layoutFile.close();
00164
00165 return true;
00166 }
00167
00168 void KLettres::setupActions()
00169 {
00170 QAction *m_newAction = actionCollection()->addAction("play_new");
00171 m_newAction->setText(i18n("New Sound"));
00172 m_newAction->setShortcut(QKeySequence(Qt::CTRL+Qt::Key_N));
00173 m_newAction->setIcon(KIcon("document-new"));
00174 connect(m_newAction, SIGNAL(triggered(bool)), m_view, SLOT(game()));
00175 m_newAction->setToolTip(i18n("Play a new sound"));
00176 m_newAction->setWhatsThis(i18n("You can play a new sound by clicking this button or using the File menu, New Sound."));
00177
00178 QAction *m_stuffAction = actionCollection()->addAction("downloadnewstuff");
00179 m_stuffAction->setText(i18n("Get Alphabet in New Language..."));
00180 m_stuffAction->setIcon(KIcon("get-hot-new-stuff"));
00181 connect(m_stuffAction, SIGNAL(triggered(bool)), this, SLOT( slotDownloadNewStuff() ));
00182
00183 QAction *m_playAgainAction = actionCollection()->addAction("play_again");
00184 m_playAgainAction->setText(i18n("Replay Sound"));
00185 m_playAgainAction->setShortcut(QKeySequence(Qt::Key_F5));
00186 m_playAgainAction->setIcon(KIcon("media-playback-start"));
00187 m_playAgainAction->setToolTip(i18n("Play the same sound again"));
00188 connect(m_playAgainAction, SIGNAL(triggered(bool)), m_view, SLOT(slotPlayAgain()));
00189 m_playAgainAction->setWhatsThis(i18n("You can replay the same sound again by clicking this button or using the File menu, Replay Sound."));
00190 KStandardAction::quit(kapp, SLOT(quit()), actionCollection());
00191
00192 m_menubarAction = actionCollection()->add<KToggleAction>("menubar");
00193 m_menubarAction->setText(i18n("Show &Menubar"));
00194 m_menubarAction->setIcon(KIcon("edit-clear"));
00195 m_menubarAction->setShortcut(QKeySequence(Qt::CTRL+Qt::Key_M));
00196 connect(m_menubarAction, SIGNAL(triggered(bool)), this, SLOT(slotMenubar()));
00197 m_menubarAction->setCheckedState(KGuiItem(i18n("Hide &Menubar")));
00198 m_menubarAction->setChecked(true);
00199 m_menubarAction->setWhatsThis(i18n("You can show or hide the menubar as you wish by clicking this button."));
00200
00201 m_levelAction = actionCollection()->add<KSelectAction>("levels");
00202 m_levelAction->setText(i18nc("@label:listbox which difficulty level to use", "L&evel"));
00203 m_levelAction->setToolTip(i18n("Select the level"));
00204 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."));
00205
00206 m_languageAction = actionCollection()->add<KSelectAction>("languages");
00207 m_languageAction->setText(i18nc("@label:listbox", "&Language"));
00208 m_languageAction->setItems(m_languageNames);
00209 m_languageAction->setCurrentItem(m_languages.indexOf(Prefs::language()));
00210
00211 m_levelsNames.append(i18nc("@item:inlistbox choose level 1", "Level 1" ));
00212 m_levelsNames.append(i18nc("@item:inlistbox choose level 2", "Level 2" ));
00213 m_levelsNames.append(i18nc("@item:inlistbox choose level 3", "Level 3" ));
00214 m_levelsNames.append(i18nc("@item:inlistbox choose level 4", "Level 4" ));
00215 m_levelAction->setItems(m_levelsNames);
00216 m_levelAction->setCurrentItem(Prefs::level()-1);
00217
00218 m_themeAction = actionCollection()->add<KSelectAction>("looks");
00219 m_themeAction->setText(i18n("Themes"));
00220 m_themeAction->setItems(KLThemeFactory::instance()->themeList());
00221 m_themeAction->setCurrentItem(Prefs::theme());
00222 m_themeAction->setToolTip(i18n("Select the theme"));
00223 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."));
00224
00225 m_kidAction = actionCollection()->add<KToggleAction>("mode_kid");
00226 m_kidAction->setText(i18n("Mode Kid"));
00227 m_kidAction->setShortcut(QKeySequence(Qt::CTRL+Qt::Key_K));
00228 m_kidAction->setIcon(KIcon("klettres_kids"));
00229 connect(m_kidAction, SIGNAL(triggered(bool)), this, SLOT(slotModeKid()));
00230 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."));
00231
00232 m_grownupAction = actionCollection()->add<KToggleAction>("mode_grownup");
00233 m_grownupAction->setText(i18n("Mode Grown-up"));
00234 m_grownupAction->setShortcut(QKeySequence(Qt::CTRL+Qt::Key_G));
00235 m_grownupAction->setIcon(KIcon("klettres_grownup"));
00236 connect(m_grownupAction, SIGNAL(triggered(bool)), this, SLOT(slotModeGrownup()));
00237 m_grownupAction->setWhatsThis(i18n("The Grownup mode is the normal mode where you can see the menubar."));
00238
00239 connect(m_levelAction, SIGNAL(triggered(int)), this, SLOT(slotChangeLevel(int)));
00240 connect(m_languageAction, SIGNAL(triggered(int)), this, SLOT(slotChangeLanguage(int)));
00241 connect(m_themeAction, SIGNAL(triggered(int)), this, SLOT(slotChangeTheme(int)));
00242
00243 KStandardAction::preferences(this, SLOT(optionsPreferences()), actionCollection());
00244
00245 setupGUI();
00246 }
00247
00248 void KLettres::setupStatusbar()
00249 {
00250 KStatusBar *st=statusBar();
00251 m_langLabel = new QLabel(st);
00252 m_levLabel = new QLabel(st);
00253 st->addWidget(m_langLabel);
00254 st->insertFixedItem("", 1);
00255 st->addWidget(m_levLabel);
00256 statusBar();
00257 }
00258
00259 void KLettres::setupToolbars()
00260 {
00261
00262 specialCharToolbar = toolBar("specialCharToolbar");
00263 addToolBar ( Qt::BottomToolBarArea, specialCharToolbar);
00264 }
00265
00266 void KLettres::optionsPreferences()
00267 {
00268 if(KConfigDialog::showDialog("settings"))
00269 return;
00270
00271 KConfigDialog *dialog = new KConfigDialog(this, "settings", Prefs::self());
00272 dialog->addPage(new fontsdlg(0), i18n("Font Settings"), "preferences-desktop-font");
00273
00274
00275 Timer *m_timer = new Timer();
00276 dialog->addPage(m_timer, i18n("Timer"), "clock");
00277 connect(dialog, SIGNAL(settingsChanged( const QString &)), this, SLOT(slotUpdateSettings()));
00278 dialog->setAttribute( Qt::WA_DeleteOnClose );
00279 dialog->setHelp(QString(), "klettres");
00280 dialog->show();
00281
00282
00283 }
00284
00285 void KLettres::loadSettings()
00286 {
00287 QString langString = m_languageNames[m_languages.indexOf(Prefs::language())];
00288 langString.replace("&", QString());
00289 m_langLabel->setText(langString);
00290 loadLangToolBar();
00291
00292 m_levLabel->setText(i18nc("@info:status the current level chosen", "(Level %1)", Prefs::level()));
00293
00294 m_view->setTheme(KLThemeFactory::instance()->buildTheme(Prefs::theme()));
00295
00296 if (Prefs::mode() == Prefs::EnumMode::grownup)
00297 slotModeGrownup();
00298 else
00299 slotModeKid();
00300
00301 m_menubarAction->setChecked(Prefs::menuBarBool());
00302 slotMenubar();
00303 }
00304
00305 void KLettres::slotDownloadNewStuff()
00306 {
00307 KNS::Entry::List entries = KNS::Engine::download();
00308
00309 qDeleteAll(entries);
00310
00311
00312 m_languages = LangUtils::getLanguages();
00313 findLanguages();
00314
00315
00316 m_languageAction->setItems(m_languageNames);
00317 slotChangeLanguage(m_languages.indexOf(Prefs::defaultLanguage()));
00318 m_languageAction->setCurrentItem(m_languages.indexOf(Prefs::defaultLanguage()));
00319 }
00320
00321 void KLettres::slotMenubar()
00322 {
00323 switch (m_menubarAction->isChecked()){
00324 case false:
00325 m_menubarAction->setChecked(false);
00326 m_menubarAction->setText(i18n("Show Menubar"));
00327 m_menubarAction->setToolTip(i18n("Show Menubar"));
00328 menuBar()->hide();
00329 break;
00330 case true:
00331 m_menubarAction->setChecked(true);
00332 m_menubarAction->setText(i18n("Hide Menubar"));
00333 m_menubarAction->setToolTip(i18n("Hide Menubar"));
00334 menuBar()->show();
00335 break;
00336 }
00337 Prefs::setMenuBarBool(m_menubarAction->isChecked());
00338 Prefs::self()->writeConfig();
00339 }
00340
00341 void KLettres::slotUpdateSettings()
00342 {
00343 m_view->m_timer = Prefs::kidTimer();
00344 m_view->m_timer = Prefs::grownTimer();
00345
00346 m_view->setFont(Prefs::font());
00347 }
00348
00349 void KLettres::slotChangeLevel(int newLevel)
00350 {
00351 Prefs::setLevel(newLevel+1);
00352 Prefs::self()->writeConfig();
00353 updateLevMenu(newLevel);
00354
00355
00356
00357 soundFactory->change(Prefs::language());
00358
00359 m_view->randomInt = 0;
00360 m_view->game();
00361 }
00362
00363 void KLettres::updateLevMenu(int id)
00364 {
00365
00366 m_levelAction->setCurrentItem(id);
00367 m_levLabel->setText(i18nc("@info:status the current level chosen", "(Level %1)", Prefs::level()));
00368 }
00369
00370 void KLettres::slotChangeLanguage(int newLanguage)
00371 {
00372
00373 Prefs::setLanguage(m_languages[newLanguage]);
00374 Prefs::self()->writeConfig();
00375
00376 QString langString = m_languageNames[newLanguage];
00377 langString.replace("&", QString());
00378 m_langLabel->setText(langString);
00379 loadLangToolBar();
00380
00381 bool ok = loadLayout(soundFactory->m_layoutsDocument);
00382 if (ok)
00383 soundFactory->change(Prefs::language());
00384 m_view->randomInt = 0;
00385 m_view->game();
00386 }
00387
00388 void KLettres::slotChangeTheme(int index)
00389 {
00390 Prefs::setTheme(index);
00391 Prefs::self()->writeConfig();
00392 m_view->setTheme(KLThemeFactory::instance()->buildTheme(index));
00393 }
00394
00395 void KLettres::slotModeGrownup()
00396 {
00397 QPalette pal;
00398 pal.setColor( QPalette::Background, Qt::white);
00399 statusBar()->setPalette( pal );
00400 QFont f_lab( "Serif" , 10);
00401 m_levLabel->setFont(f_lab);
00402 m_langLabel->setFont(f_lab);
00403 m_menubarAction->setChecked(true);
00404 m_grownupAction->setChecked(true);
00405 m_kidAction->setChecked(false);
00406 m_grownupAction->setToolTip(i18n("Grown-up mode is currently active"));
00407 m_kidAction->setToolTip(i18n("Switch to Kid mode"));
00408 m_menubarAction->setText(i18n("Hide Menubar"));
00409 m_menubarAction->setToolTip(i18n("Hide Menubar"));
00410 menuBar()->show();
00411 m_view->m_timer = Prefs::grownTimer();
00412 Prefs::setMode(Prefs::EnumMode::grownup);
00413 Prefs::self()->writeConfig();
00414 }
00415
00416 void KLettres::slotModeKid()
00417 {
00418 QPalette pal;
00419 pal.setColor( QPalette::Background, Qt::white);
00420 statusBar()->setPalette( pal );
00421 QFont f_lab( "Serif" , 12);
00422 f_lab.setBold(true);
00423 m_levLabel->setFont(f_lab);
00424 m_langLabel->setFont(f_lab);
00425 m_menubarAction->setChecked(false);
00426 m_kidAction->setChecked(true);
00427 m_kidAction->setToolTip(i18n("Kid mode is currently active"));
00428 m_grownupAction->setToolTip(i18n("Switch to Grown-up mode"));
00429 m_menubarAction->setText(i18n("Show Menubar"));
00430 m_menubarAction->setToolTip(i18n("Show Menubar"));
00431 m_grownupAction->setChecked(false);
00432 menuBar()->hide();
00433 m_view->m_timer = Prefs::kidTimer();
00434 Prefs::setMode(Prefs::EnumMode::kid);
00435 Prefs::self()->writeConfig();
00436 }
00437
00438 void KLettres::loadLangToolBar()
00439 {
00440 QString lang = Prefs::language();
00441
00442 specialCharToolbar->clear();
00443
00444 if (LangUtils::hasSpecialChars(lang))
00445 {
00446 allData.clear();
00447 QString myString=QString("klettres/%1.txt").arg(lang);
00448 QFile myFile;
00449 myFile.setFileName(KStandardDirs::locate("data",myString));
00450 if (!myFile.exists())
00451 {
00452
00453 QString mString=i18n("File $KDEDIR/share/apps/klettres/%1.txt not found;\n"
00454 "please check your installation.", lang);
00455 KMessageBox::sorry( this, mString,
00456 i18n("Error") );
00457 qApp->quit();
00458 }
00459
00460 QFile openFileStream(myFile.fileName());
00461 openFileStream.open(QIODevice::ReadOnly);
00462 QTextStream readFileStr(&openFileStream);
00463 readFileStr.setCodec("UTF-8");
00464
00465 allData = readFileStr.readAll().split("\n");
00466 openFileStream.close();
00467 for (int i=0; i<(int) allData.count(); ++i) {
00468 if (!allData[i].isEmpty()) {
00469 QAction *act = specialCharToolbar->addAction(allData.at(i));
00470 act->setIcon(charIcon(allData.at(i).at(0)));
00471
00472 act->setData(i);
00473 }
00474 }
00475 specialCharToolbar->show();
00476 update();
00477 }
00478 else {
00479 specialCharToolbar->hide();
00480 }
00481 }
00482
00483 void KLettres::slotPasteChar()
00484 {
00485 QAction *act = qobject_cast<QAction*>(sender());
00486 if (!act)
00487 return;
00488
00489 bool ok = true;
00490 int id = act->data().toInt(&ok);
00491 if (!ok || id < 0 || id >= allData.count())
00492 return;
00493
00494 m_view->enterLetter(allData.at(id));
00495 }
00496
00497 QIcon KLettres::charIcon(const QChar & c)
00498 {
00500 QString s = KStandardDirs::locateLocal("icon", "char" + QString::number(c.unicode()) + ".png");
00501
00502 QRect r(4, 4, 120, 120);
00503
00505 QFont font;
00506 font.setFamily( "Arial" );
00507 font.setPixelSize(120);
00508 font.setWeight(QFont::Normal);
00509
00511 QPixmap pm(128, 128);
00512 pm.fill(Qt::white);
00513 QPainter p(&pm);
00514 p.setFont(font);
00515 p.setPen(Qt::black);
00516 p.drawText(r, Qt::AlignCenter, (QString) c);
00517
00519 QBitmap bm(128, 128);
00520 bm.fill(Qt::color0);
00521 QPainter b(&bm);
00522 b.setFont(font);
00523 b.setPen(Qt::color1);
00524 b.drawText(r, Qt::AlignCenter, (QString) c);
00525
00527 pm.setMask(bm);
00528
00530 pm.save(s, "PNG");
00531
00532 return QIcon(pm);
00533 }
00534
00535 #include "klettres.moc"