File indexing completed on 2023-05-30 10:45:26

0001 /*
0002     SPDX-FileCopyrightText: 2010 Peter Hedlund <peter.hedlund@kdemail.net>
0003     SPDX-License-Identifier: GPL-2.0-or-later
0004 */
0005 
0006 #include "kwqquizview.h"
0007 
0008 
0009 #include "kwqquizmodel.h"
0010 
0011 KWQQuizView::KWQQuizView(QWidget* parent, KActionCollection* actionCollection) :  QWidget(parent), m_actionCollection(actionCollection)
0012 {
0013   m_player = 0;
0014   m_quiz = 0;
0015 }
0016 
0017 void KWQQuizView::setQuiz(KWQQuizModel* quiz)
0018 {
0019   m_quiz = quiz;
0020 }
0021 
0022 void KWQQuizView::slotRestart()
0023 {
0024   m_quiz->activateBaseList();
0025   init();
0026 }
0027 
0028 void KWQQuizView::slotRepeat()
0029 {
0030   m_quiz->activateErrorList();
0031   init();
0032 }
0033 
0034 void KWQQuizView::slotAudioPlay()
0035 {
0036     // repeat playing last file
0037     audioPlayFile(QUrl(), false);
0038 }
0039 
0040 void KWQQuizView::audioPlayAnswer()
0041 {
0042     audioPlayFile(m_quiz->soundAnswer(), true);
0043 }
0044 
0045 void KWQQuizView::audioPlayQuestion()
0046 {
0047     audioPlayFile(m_quiz->soundQuestion(), true);
0048 }
0049 
0050 void KWQQuizView::audioPlayFile(const QUrl &soundUrl, bool overwrite)
0051 {
0052     static QUrl lastUrl;
0053 
0054     QUrl url = soundUrl;
0055     if (overwrite)
0056        lastUrl = soundUrl;
0057 
0058     if (url.isEmpty()) {
0059         if (lastUrl.isEmpty()) {
0060             m_actionCollection->action(QStringLiteral("quiz_audio_play"))->setEnabled(false);
0061             return;
0062         }
0063         url = lastUrl;
0064     } else {
0065         lastUrl = url;
0066     }
0067     m_actionCollection->action(QStringLiteral("quiz_audio_play"))->setEnabled(true);
0068 
0069     qDebug() << "Attempting to play sound: " << url;
0070 
0071     if (!m_player) {
0072         m_player = new Phonon::MediaObject(this);
0073         Phonon::AudioOutput *audioOutput = new Phonon::AudioOutput(Phonon::NoCategory, this);
0074         createPath(m_player, audioOutput);
0075     } else {
0076         m_player->stop();
0077     }
0078     m_player->setCurrentSource(url);
0079     m_player->play();
0080 }