1d80dbfb15c660fe1232a1b1aa55a2e56f6bc86e
[jspeed] / src / soundselector.cpp
1 /*
2  * This file is part of jSpeed.
3  *
4  * jSpeed is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * jSpeed is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with jSpeed.  If not, see <http://www.gnu.org/licenses/>.
16  *
17  */
18
19 #include <QtCore/QFile>
20 #include <QtGui/QHBoxLayout>
21 #include "soundselector.h"
22 #include "fileselector.h"
23 #include "mediaplayer.h"
24
25 SoundSelector::SoundSelector(QWidget* parent): QWidget(parent)
26 {
27     selector_ = new FileSelector(tr("Sound"));
28     QPushButton* playButton = new QPushButton(tr("Play"));
29     connect(playButton, SIGNAL(clicked(bool)), this, SLOT(playSound()));
30     QHBoxLayout* layout = new QHBoxLayout;
31     layout->addWidget(selector_, Qt::AlignLeft);
32     layout->addWidget(playButton);
33     setLayout(layout);
34 }
35
36 void SoundSelector::playSound()
37 {
38     QString sound = selector_->value().toString();
39
40     if(sound.isEmpty())
41     {
42         return;
43     }
44
45     QString soundDir = MediaPlayer::getSoundDir();
46     QString localDir = MediaPlayer::getLocalSoundDir();
47
48     if(QFile::exists(soundDir + sound))
49     {
50         MediaPlayer::play(soundDir + sound);
51     }
52     else if(QFile::exists(localDir + sound))
53     {
54         MediaPlayer::play(localDir + sound);
55     }
56 }
57
58 void SoundSelector::setVisible(bool visible)
59 {
60     if(visible)
61     {
62         QString currentValue = selector_->value().toString();
63         selector_->clear();
64         QString pattern = MediaPlayer::getFormatPattern();
65         selector_->loadFiles(MediaPlayer::getLocalSoundDir(), pattern);
66         selector_->loadFiles(MediaPlayer::getSoundDir(), pattern);
67         setValue(currentValue);
68     }
69
70     QWidget::setVisible(visible);
71 }
72
73 void SoundSelector::setValue(QString const& value)
74 {
75     selector_->selectByValue(value);
76 }
77
78 QString SoundSelector::value() const
79 {
80     return selector_->value().toString();
81 }