1 /*
2  * Copyright (C) 2014-2018 Christopho, Solarus - http://www.solarus-games.org
3  *
4  * Solarus Quest Editor 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  * Solarus Quest Editor 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 along
15  * with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 #include "audio.h"
18 #include "quest.h"
19 #include "widgets/sound_chooser.h"
20 #include <QHBoxLayout>
21 
22 namespace SolarusEditor {
23 
24 /**
25  * @brief Creates a sound chooser.
26  * @param parent The parent object or nullptr.
27  */
SoundChooser(QWidget * parent)28 SoundChooser::SoundChooser(QWidget* parent) :
29   QWidget(parent),
30   sound_selector(),
31   play_sound_button(),
32   quest() {
33 
34   sound_selector.set_resource_type(ResourceType::SOUND);
35   play_sound_button.setIconSize(QSize(24, 24));
36   play_sound_button.setIcon(QIcon(":/images/icon_start.png"));
37   play_sound_button.setToolTip(tr("Play sound"));
38 
39   QHBoxLayout* layout = new QHBoxLayout(this);
40   layout->setMargin(0);
41 
42   layout->addWidget(&sound_selector);
43   layout->addWidget(&play_sound_button);
44 
45   update_play_button();
46 }
47 
48 /**
49  * @brief Sets the quest whose resources will be shown.
50  * @param quest The quest.
51  */
set_quest(Quest & quest)52 void SoundChooser::set_quest(Quest& quest) {
53 
54   this->quest = &quest;
55 
56   sound_selector.set_quest(quest);
57 
58   connect(&play_sound_button, SIGNAL(clicked(bool)),
59           this, SLOT(play_sound_button_clicked()));
60   connect(&sound_selector, SIGNAL(activated(QString)),
61           this, SLOT(sound_selector_activated(QString)));
62 
63   update_play_button();
64 }
65 
66 /**
67  * @brief Returns the sound selector contained in this widget.
68  * @return The sound selector.
69  */
get_selector()70 ResourceSelector& SoundChooser::get_selector() {
71   return sound_selector;
72 }
73 
74 /**
75  * @brief Returns the sound id in the selector.
76  * @return The selected sound id.
77  */
get_selected_id()78 QString SoundChooser::get_selected_id() {
79   return sound_selector.get_selected_id();
80 }
81 
82 /**
83  * @brief Sets the selected sound.
84  * @param sound_id The new sound id to select.
85  */
set_selected_id(const QString & sound_id)86 void SoundChooser::set_selected_id(const QString& sound_id) {
87 
88   sound_selector.set_selected_id(sound_id);
89   update_play_button();
90 }
91 
92 /**
93  * @brief Slot called when the user changes the sound in the selector.
94  * @param sound_id The new selected sound id.
95  */
sound_selector_activated(const QString & sound_id)96 void SoundChooser::sound_selector_activated(const QString& sound_id) {
97 
98   if (quest == nullptr) {
99     return;
100   }
101 
102   update_play_button();
103 
104   emit activated(sound_id);
105 }
106 
107 /**
108  * @brief Slot called when the user clicks the play sound button.
109  */
play_sound_button_clicked()110 void SoundChooser::play_sound_button_clicked() {
111 
112   if (quest == nullptr) {
113     return;
114   }
115 
116   const QString& selected_sound_id = sound_selector.get_selected_id();
117   if (selected_sound_id.isEmpty()) {
118     return;
119   }
120 
121   Audio::play_sound(*quest, selected_sound_id);
122 }
123 
124 
125 /**
126  * @brief Updates the play sound button depending on the current
127  * selection.
128  */
update_play_button()129 void SoundChooser::update_play_button() {
130 
131   if (quest == nullptr) {
132     play_sound_button.setEnabled(false);
133     return;
134   }
135 
136   QString selected_sound_id = sound_selector.get_selected_id();
137   QString path = quest->get_sound_path(selected_sound_id);
138 
139   if (selected_sound_id.isEmpty() ||
140       !quest->exists(path)) {
141     play_sound_button.setEnabled(false);
142   }
143   else {
144     play_sound_button.setEnabled(true);
145   }
146 }
147 
148 }
149