[kde-edu]: [RFC patch] audio play for kwordquiz
Stanislaw Gruszka
stf_xl at wp.pl
Sun Jan 24 11:58:34 CET 2010
Hi
I would like to add playing audio files for kwordquiz. To achieve that
I copied Phonon playback code from parley to kwordquiz. Patch do not play
url taken from kvtml file however, instead audio file path is crated from
word/language pair:
KStandardDirs::locateLocal("sound", "kwordquiz/") + lang + "/" word + .ogg
in my system this is
$HOME/.kde/share/sounds/kwordquiz/lang/word.ogg"
User have to put ogg file in roper dir (I have a dump script for that).
Play sound is only implemented for qaview.
TODO:
- first check if kvtml have defined sound file before playing word/lang
- configuration to chose playing answer or question or desired
language, currently only answer is played
- same behavior (file path from word/lang) for Parley ?
Cheers
Stanislaw
-------------- next part --------------
Index: src/kwordquiz.h
===================================================================
--- src/kwordquiz.h (revision 1079126)
+++ src/kwordquiz.h (working copy)
@@ -300,6 +300,7 @@
KAction* quizRestart;
KAction* quizRepeatErrors;
KAction* quizExportErrors;
+ KAction* quizAudioPlay;
KAction* flashKnow;
KAction* flashDontKnow;
Index: src/multipleview.cpp
===================================================================
--- src/multipleview.cpp (revision 1079126)
+++ src/multipleview.cpp (working copy)
@@ -84,6 +84,7 @@
m_actionCollection->action("quiz_check")->setEnabled(true);
m_actionCollection->action("quiz_repeat_errors")->setEnabled(false);
m_actionCollection->action("quiz_export_errors")->setEnabled(false);
+ m_actionCollection->action("quiz_audio_play")->setEnabled(false);
m_actionCollection->action("quiz_Opt1")->setEnabled(true);
m_actionCollection->action("quiz_Opt2")->setEnabled(true);
m_actionCollection->action("quiz_Opt3")->setEnabled(true);
@@ -142,6 +143,7 @@
m_actionCollection->action("quiz_Opt3")->setEnabled(false);
m_actionCollection->action("quiz_repeat_errors")->setEnabled(m_quiz->hasErrors());
m_actionCollection->action("quiz_export_errors")->setEnabled(m_quiz->hasErrors());
+ m_actionCollection->action("quiz_audio_play")->setEnabled(false);
lblQuestionLanguage->setText(i18n("Summary"));
lblQuestion->clear();
Index: src/qaview.cpp
===================================================================
--- src/qaview.cpp (revision 1079126)
+++ src/qaview.cpp (working copy)
@@ -21,6 +21,8 @@
#include <KIconLoader>
#include <KLocale>
#include <KNotification>
+#include <KUrl>
+#include <KStandardDirs>
#include "prefs.h"
#include "kwqquizmodel.h"
@@ -61,7 +63,7 @@
return result;
}
-QAView::QAView(QWidget *parent, KActionCollection * actionCollection) : QWidget(parent), m_actionCollection(actionCollection)
+QAView::QAView(QWidget *parent, KActionCollection * actionCollection) : QWidget(parent), m_actionCollection(actionCollection), m_player(NULL)
{
setupUi(this);
@@ -105,12 +107,14 @@
m_actionCollection->action("qa_hint")->setEnabled(true);
m_actionCollection->action("quiz_repeat_errors")->setEnabled(false);
m_actionCollection->action("quiz_export_errors")->setEnabled(false);
+ m_actionCollection->action("quiz_audio_play")->setEnabled(true);
showQuestion();
txtAnswer->show();
txtAnswer->setFocus();
}
+
void QAView::slotCheck()
{
if (m_actionCollection->action("quiz_check")->isEnabled())
@@ -151,6 +155,8 @@
KNotification::event("QuizError", i18n("Your answer was incorrect."));
}
+ audioPlay(true);
+
lblPreviousQuestionHeader->setText(i18n("Previous Question"));
lblPreviousQuestion->setText(m_quiz->question());
//lblPreviousQuestion->setFont(m_quiz->fontQuestion(m_question));
@@ -231,6 +237,7 @@
{
lblQuestionLanguage->setText(m_quiz ->langQuestion());
lblQuestion->setText(m_quiz ->question());
+ //audioPlay(false);
picQuestion->setPixmap(KIconLoader::global()->loadIcon(m_quiz->quizIcon(KWQQuizModel::IconLeftCol), KIconLoader::Panel));
@@ -286,4 +293,51 @@
}
}
+void QAView::slotAudioPlay()
+{
+ // repeat playing last file
+ audioPlayFile(KUrl::KUrl());
+}
+
+void QAView::audioPlay(bool answer)
+{
+ static const QString soundDir = KStandardDirs::locateLocal("sound", "kwordquiz/");
+
+ // TODO: check kvtml sound Url first
+
+ const QString lang = answer ? m_quiz->langAnswer() : m_quiz->langQuestion();
+ const QString word = answer ? m_quiz->answer() : m_quiz->question();
+ const QString path = soundDir + lang + "/" + word + ".ogg";
+ if (!KStandardDirs::exists(path))
+ return;
+
+ audioPlayFile(KUrl::fromPath(path));
+}
+
+void QAView::audioPlayFile(const KUrl &soundFile)
+{
+ static KUrl lastFile;
+
+ KUrl *f = const_cast<KUrl *>(&soundFile);
+ if (f->isEmpty()) {
+ if (lastFile.isEmpty())
+ return;
+ f = &lastFile;
+ }
+
+ lastFile = *f;
+ kDebug() << "Attempting to play sound: " << *f;
+
+ if (!m_player) {
+ m_player = new Phonon::MediaObject(this);
+ Phonon::AudioOutput *audioOutput = new Phonon::AudioOutput(Phonon::NoCategory, this);
+ createPath(m_player, audioOutput);
+ } else {
+ m_player->stop();
+ }
+ m_player->setCurrentSource(*f);
+ m_player->play();
+}
+
+
#include "qaview.moc"
Index: src/kwordquiz.cpp
===================================================================
--- src/kwordquiz.cpp (revision 1079126)
+++ src/kwordquiz.cpp (working copy)
@@ -374,6 +374,14 @@
quizRestart->setToolTip(quizRestart->whatsThis());
quizRestart->setStatusTip(quizRestart->whatsThis());
+ quizAudioPlay = actionCollection()->addAction("quiz_audio_play");
+ quizAudioPlay->setIcon(KIcon("media-playback-start"));
+ quizAudioPlay->setText(i18n("&AudioPlay"));
+ quizAudioPlay->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_P));
+ quizAudioPlay->setWhatsThis(i18n("Play sound file with word pronunciation"));
+ quizAudioPlay->setToolTip(quizAudioPlay->whatsThis());
+ quizAudioPlay->setStatusTip(quizAudioPlay->whatsThis());
+
quizRepeatErrors = actionCollection()->addAction("quiz_repeat_errors");
quizRepeatErrors->setIcon(KIcon("repeat"));
quizRepeatErrors->setText(i18n("Repeat &Errors"));
@@ -1085,6 +1093,7 @@
disconnect(flashKnow, 0, 0, 0);
disconnect(flashDontKnow, 0, 0, 0);
disconnect(quizRestart, 0, 0, 0);
+ disconnect(quizAudioPlay, 0, 0, 0);
disconnect(quizRepeatErrors, 0, 0, 0);
disconnect(qaHint, 0, 0, 0);
@@ -1168,6 +1177,7 @@
connect(quizCheck, SIGNAL(triggered(bool)), m_qaView, SLOT(slotCheck()));
connect(qaHint, SIGNAL(triggered(bool)), m_qaView, SLOT(slotHint()));
connect(quizRestart, SIGNAL(triggered(bool)), m_qaView, SLOT(slotRestart()));
+ connect(quizAudioPlay, SIGNAL(triggered(bool)), m_qaView, SLOT(slotAudioPlay()));
connect(quizRepeatErrors, SIGNAL(triggered(bool)), m_qaView, SLOT(slotRepeat()));
connect(this, SIGNAL(settingsChanged()), m_qaView, SLOT(slotApplySettings()));
@@ -1360,6 +1370,7 @@
flashDontKnow->setEnabled((m_pageWidget->currentPage() == m_flashPage) && fQuiz);
qaHint->setEnabled((m_pageWidget->currentPage() == m_qaPage) && fQuiz);
+ quizAudioPlay->setEnabled((m_pageWidget->currentPage() == m_qaPage) && fQuiz);
quizOpt1->setEnabled((m_pageWidget->currentPage() == m_multiplePage) && fQuiz);
quizOpt2->setEnabled((m_pageWidget->currentPage() == m_multiplePage) && fQuiz);
Index: src/kwordquizui.rc
===================================================================
--- src/kwordquizui.rc (revision 1079126)
+++ src/kwordquizui.rc (working copy)
@@ -1,178 +1,240 @@
-<!DOCTYPE kpartgui>
-<kpartgui version="18" name="kwordquiz" >
- <MenuBar>
- <Menu name="file" >
- <Action append="open_merge" name="file_ghns" />
+<!DOCTYPE kpartgui SYSTEM 'kpartgui.dtd'>
+<kpartgui version="18" name="kwordquiz">
+ <MenuBar alreadyVisited="1">
+ <Menu name="file" alreadyVisited="1">
+ <text>&File</text>
+ <Action name="file_new"/>
+ <Action name="file_open"/>
+ <Action name="file_open_recent"/>
+ <Action append="open_merge" name="file_ghns"/>
+ <Separator weakSeparator="1"/>
+ <Action name="file_save"/>
+ <Action name="file_save_as"/>
+ <Separator weakSeparator="1"/>
+ <Action name="file_print"/>
+ <Separator weakSeparator="1"/>
+ <Action name="file_close"/>
+ <Separator weakSeparator="1"/>
+ <Action name="file_quit"/>
</Menu>
- <Menu name="edit" >
- <!--<Separator/>-->
- <Action name="edit_insert" />
- <Action name="edit_delete" />
+ <Menu name="edit" alreadyVisited="1">
+ <text>&Edit</text>
+ <Action name="edit_undo"/>
+ <Action name="edit_redo"/>
+ <Separator weakSeparator="1"/>
+ <Action name="edit_cut"/>
+ <Action name="edit_copy"/>
+ <Action name="edit_paste"/>
+ <Action name="edit_clear"/>
+ <Separator weakSeparator="1"/>
+ <Action name="edit_insert"/>
+ <Action name="edit_delete"/>
<Separator/>
- <Action name="edit_mark_blank" />
- <Action name="edit_unmark_blank" />
+ <Action name="edit_mark_blank"/>
+ <Action name="edit_unmark_blank"/>
</Menu>
- <Menu name="vocabulary" >
+ <Menu name="vocabulary">
<text>V&ocabulary</text>
- <Action name="vocab_languages" />
- <Action name="vocab_font" />
+ <Action name="vocab_languages"/>
+ <Action name="vocab_font"/>
<Separator/>
- <Action name="vocab_adjust_rows" />
+ <Action name="vocab_adjust_rows"/>
<Separator/>
- <Action name="vocab_shuffle" />
+ <Action name="vocab_shuffle"/>
<!--<Action name="vocab_leitner" />
<Action name="vocab_leitner_config" />-->
</Menu>
- <Menu name="mode" >
+ <Menu name="mode">
<text>&Mode</text>
- <Action name="mode_1" />
- <Action name="mode_2" />
- <Action name="mode_3" />
- <Action name="mode_4" />
- <Action name="mode_5" />
+ <Action name="mode_1"/>
+ <Action name="mode_2"/>
+ <Action name="mode_3"/>
+ <Action name="mode_4"/>
+ <Action name="mode_5"/>
</Menu>
- <Menu name="quiz" >
+ <Menu name="quiz">
<text>&Quiz</text>
- <Action name="quiz_check" />
+ <Action name="quiz_check"/>
<Separator/>
- <Action name="flash_know" />
- <Action name="flash_dont_know" />
+ <Action name="flash_know"/>
+ <Action name="flash_dont_know"/>
<Separator/>
- <Action name="qa_hint" />
+ <Action name="qa_hint"/>
<Separator/>
- <Action name="quiz_restart" />
- <Action name="quiz_repeat_errors" />
+ <Action name="quiz_restart"/>
+ <Action name="quiz_repeat_errors"/>
+ <Action name="quiz_audio_play"/>
<Separator/>
- <Action name="quiz_export_errors" />
+ <Action name="quiz_export_errors"/>
</Menu>
- <Menu name="settings" >
- <Action name="config_show_search" />
+ <Menu name="settings" alreadyVisited="1">
+ <text>&Settings</text>
+ <Action name="config_show_search"/>
+ <Separator weakSeparator="1"/>
+ <Merge name="StandardToolBarMenuHandler"/>
+ <Merge name="KMDIViewActions"/>
+ <Action name="options_show_statusbar"/>
+ <Separator weakSeparator="1"/>
+ <Action name="options_configure_keybinding"/>
+ <Action name="options_configure_toolbars"/>
+ <Action name="options_configure_notifications"/>
+ <Action name="options_configure"/>
</Menu>
- </MenuBar>
- <ToolBar name="mainToolBar">
- <Action name="mode_0"/>
- </ToolBar>
- <ToolBar name="quizToolBar" position="right" iconText="icontextright" >
- <text>Quiz</text>
- <Action name="quiz_check" />
- <Separator/>
- <Action name="flash_know" />
- <Action name="flash_dont_know" />
- <Separator/>
- <Action name="qa_hint" />
- <Separator/>
- <Action name="quiz_restart" />
- <Action name="quiz_repeat_errors" />
- </ToolBar>
- <ToolBar name="characterToolBar" iconText="icontextbottom" hidden="true">
- <text>Special Characters</text>
- <Action name="char_1" />
- <Action name="char_2" />
- <Action name="char_3" />
- <Action name="char_4" />
- <Action name="char_5" />
- <Action name="char_6" />
- <Action name="char_7" />
- <Action name="char_8" />
- <Action name="char_9" />
- </ToolBar>
- <State name="switchEditQuiz">
- <enable>
- <Action name="file_save" />
- <Action name="file_save_as" />
- <Action name="file_print" />
- <Action name="edit_find" />
- <Action name="edit_clear" />
- <Action name="edit_insert" />
- <Action name="edit_delete" />
- <Action name="edit_mark_blank" />
- <Action name="edit_unmark_blank" />
- <Action name="vocab_languages" />
- <Action name="vocab_font" />
- <Action name="vocab_sort" />
- <Action name="vocab_shuffle" />
- <Action name="vocab_leitner" />
- <Action name="vocab_leitner_config" />
- </enable>
- </State>
- <State name="showingEdit">
- <enable>
- <Action name="quiz_flash" />
- <Action name="quiz_multiple" />
- <Action name="quiz_qa" />
- </enable>
- <disable>
- <Action name="quiz_editor" />
- <Action name="quiz_repeat_errors" />
- <Action name="quiz_restart" />
- <Action name="quiz_check" />
- <Action name="flash_know" />
- <Action name="flash_dont_know" />
- <Action name="qa_hint" />
- </disable>
- </State>
-
- <State name="showingFlash">
- <enable>
- <Action name="quiz_editor" />
- <Action name="quiz_multiple" />
- <Action name="quiz_qa" />
- <Action name="quiz_check" />
- <Action name="vocab_font" />
- <Action name="flash_know" />
- <Action name="flash_dont_know" />
- <Action name="quiz_restart" />
- </enable>
- <disable>
- <Action name="quiz_flash" />
- <Action name="quiz_repeat_errors" />
- <Action name="qa_hint" />
- </disable>
- </State>
- <State name="showingMultiple">
- <enable>
- <Action name="quiz_editor" />
- <Action name="quiz_flash" />
- <Action name="quiz_qa" />
- <Action name="quiz_check" />
- <Action name="quiz_restart" />
- </enable>
- <disable>
- <Action name="quiz_multiple" />
- <Action name="flash_know" />
- <Action name="flash_dont_know" />
- <Action name="qa_hint" />
- <Action name="quiz_repeat_errors" />
- </disable>
- </State>
- <State name="showingQA">
- <enable>
- <Action name="quiz_editor" />
- <Action name="quiz_flash" />
- <Action name="quiz_multiple" />
- <Action name="quiz_check" />
- <Action name="qa_hint" />
- <Action name="quiz_restart" />
- </enable>
- <disable>
- <Action name="quiz_qa" />
- <Action name="flash_know" />
- <Action name="flash_dont_know" />
- <Action name="quiz_repeat_errors" />
- </disable>
- </State>
- <Menu name="editor_popup">
- <Action name="edit_undo" />
- <Separator/>
- <Action name="edit_cut" />
- <Action name="edit_copy" />
- <Action name="edit_paste" />
- <Separator/>
- <Action name="edit_insert" />
- <Action name="edit_delete" />
- <Separator/>
- <Action name="vocab_languages" />
- <Separator/>
- <Action name="vocab_layouts" />
+ <Separator weakSeparator="1"/>
+ <Menu name="help">
+ <text>&Help</text>
+ <Action name="help_contents"/>
+ <Action name="help_whats_this"/>
+ <Separator weakSeparator="1"/>
+ <Action name="help_report_bug"/>
+ <Separator weakSeparator="1"/>
+ <Action name="switch_application_language"/>
+ <Separator weakSeparator="1"/>
+ <Action name="help_about_app"/>
+ <Action name="help_about_kde"/>
</Menu>
+ </MenuBar>
+ <ToolBar name="mainToolBar" alreadyVisited="1">
+ <text>Main Toolbar</text>
+ <Action name="file_new"/>
+ <Action name="file_open"/>
+ <Action name="file_save"/>
+ <Action name="file_print"/>
+ <Separator weakSeparator="1"/>
+ <Action name="edit_undo"/>
+ <Action name="edit_redo"/>
+ <Action name="edit_cut"/>
+ <Action name="edit_copy"/>
+ <Action name="edit_paste"/>
+ <Separator weakSeparator="1"/>
+ <Action name="mode_0"/>
+ </ToolBar>
+ <ToolBar iconText="icontextright" noMerge="1" position="right" name="quizToolBar">
+ <text>Quiz</text>
+ <Action name="quiz_check"/>
+ <Separator name="separator_0"/>
+ <Action name="flash_know"/>
+ <Action name="flash_dont_know"/>
+ <Separator name="separator_1"/>
+ <Action name="qa_hint"/>
+ <Separator name="separator_2"/>
+ <Action name="quiz_restart"/>
+ <Action name="quiz_play_udio"/>
+ <Action name="quiz_repeat_errors"/>
+ <Action name="quiz_audio_play"/>
+ </ToolBar>
+ <ToolBar iconText="icontextbottom" hidden="true" name="characterToolBar">
+ <text>Special Characters</text>
+ <Action name="char_1"/>
+ <Action name="char_2"/>
+ <Action name="char_3"/>
+ <Action name="char_4"/>
+ <Action name="char_5"/>
+ <Action name="char_6"/>
+ <Action name="char_7"/>
+ <Action name="char_8"/>
+ <Action name="char_9"/>
+ </ToolBar>
+ <State name="switchEditQuiz">
+ <enable>
+ <Action name="file_save"/>
+ <Action name="file_save_as"/>
+ <Action name="file_print"/>
+ <Action name="edit_find"/>
+ <Action name="edit_clear"/>
+ <Action name="edit_insert"/>
+ <Action name="edit_delete"/>
+ <Action name="edit_mark_blank"/>
+ <Action name="edit_unmark_blank"/>
+ <Action name="vocab_languages"/>
+ <Action name="vocab_font"/>
+ <Action name="vocab_sort"/>
+ <Action name="vocab_shuffle"/>
+ <Action name="vocab_leitner"/>
+ <Action name="vocab_leitner_config"/>
+ </enable>
+ </State>
+ <State name="showingEdit">
+ <enable>
+ <Action name="quiz_flash"/>
+ <Action name="quiz_multiple"/>
+ <Action name="quiz_qa"/>
+ <Action name="quiz_audio_play"/>
+ </enable>
+ <disable>
+ <Action name="quiz_editor"/>
+ <Action name="quiz_repeat_errors"/>
+ <Action name="quiz_restart"/>
+ <Action name="quiz_check"/>
+ <Action name="flash_know"/>
+ <Action name="flash_dont_know"/>
+ <Action name="qa_hint"/>
+ </disable>
+ </State>
+ <State name="showingFlash">
+ <enable>
+ <Action name="quiz_editor"/>
+ <Action name="quiz_multiple"/>
+ <Action name="quiz_qa"/>
+ <Action name="quiz_check"/>
+ <Action name="vocab_font"/>
+ <Action name="flash_know"/>
+ <Action name="flash_dont_know"/>
+ <Action name="quiz_restart"/>
+ <Action name="quiz_audio_play"/>
+ </enable>
+ <disable>
+ <Action name="quiz_flash"/>
+ <Action name="quiz_repeat_errors"/>
+ <Action name="qa_hint"/>
+ </disable>
+ </State>
+ <State name="showingMultiple">
+ <enable>
+ <Action name="quiz_editor"/>
+ <Action name="quiz_flash"/>
+ <Action name="quiz_qa"/>
+ <Action name="quiz_check"/>
+ <Action name="quiz_restart"/>
+ <Action name="quiz_audio_play"/>
+ </enable>
+ <disable>
+ <Action name="quiz_multiple"/>
+ <Action name="flash_know"/>
+ <Action name="flash_dont_know"/>
+ <Action name="qa_hint"/>
+ <Action name="quiz_repeat_errors"/>
+ </disable>
+ </State>
+ <State name="showingQA">
+ <enable>
+ <Action name="quiz_editor"/>
+ <Action name="quiz_flash"/>
+ <Action name="quiz_multiple"/>
+ <Action name="quiz_check"/>
+ <Action name="qa_hint"/>
+ <Action name="quiz_restart"/>
+ <Action name="quiz_audio_play"/>
+ </enable>
+ <disable>
+ <Action name="quiz_qa"/>
+ <Action name="flash_know"/>
+ <Action name="flash_dont_know"/>
+ <Action name="quiz_repeat_errors"/>
+ </disable>
+ </State>
+ <Menu name="editor_popup">
+ <Action name="edit_undo"/>
+ <Separator/>
+ <Action name="edit_cut"/>
+ <Action name="edit_copy"/>
+ <Action name="edit_paste"/>
+ <Separator/>
+ <Action name="edit_insert"/>
+ <Action name="edit_delete"/>
+ <Separator/>
+ <Action name="vocab_languages"/>
+ <Separator/>
+ <Action name="vocab_layouts"/>
+ </Menu>
</kpartgui>
Index: src/qaview.h
===================================================================
--- src/qaview.h (revision 1079126)
+++ src/qaview.h (working copy)
@@ -22,6 +22,10 @@
#include "ui_qaviewbase.h"
#include <KActionCollection>
+#include <Phonon/MediaObject>
+#include <Phonon/Path>
+#include <Phonon/AudioOutput>
+#include <Phonon/Global>
class KWQQuizModel;
@@ -44,6 +48,7 @@
void slotRestart();
void slotApplySettings();
void slotSpecChar(const QChar &);
+ void slotAudioPlay();
private:
KWQQuizModel *m_quiz;
@@ -52,6 +57,10 @@
bool m_hintUsed;
void showQuestion();
+
+ class Phonon::MediaObject *m_player;
+ void audioPlay(bool);
+ void audioPlayFile(const KUrl & soundFile);
};
#endif
Index: src/flashview.cpp
===================================================================
--- src/flashview.cpp (revision 1079126)
+++ src/flashview.cpp (working copy)
@@ -49,6 +49,7 @@
m_actionCollection->action("flash_dont_know")->setEnabled(true);
m_actionCollection->action("quiz_repeat_errors")->setEnabled(false);
m_actionCollection->action("quiz_export_errors")->setEnabled(false);
+ m_actionCollection->action("quiz_audio_play")->setEnabled(false);
connect(flashcard, SIGNAL(cardClicked()), this, SLOT(slotFlip()), Qt::UniqueConnection);
m_showFirst = true;
@@ -93,6 +94,7 @@
m_actionCollection->action("flash_dont_know")->setEnabled(false);
m_actionCollection->action("quiz_repeat_errors")->setEnabled(m_quiz->hasErrors());
m_actionCollection->action("quiz_export_errors")->setEnabled(m_quiz->hasErrors());
+ m_actionCollection->action("quiz_audio_play")->setEnabled(false);
disconnect(flashcard, SIGNAL(cardClicked()), 0, 0);
}
}
More information about the kde-edu
mailing list