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/music_chooser.h"
20 #include <QHBoxLayout>
21 
22 namespace SolarusEditor {
23 
24 /**
25  * @brief Creates a music chooser.
26  * @param parent The parent object or nullptr.
27  */
MusicChooser(QWidget * parent)28 MusicChooser::MusicChooser(QWidget* parent) :
29   QWidget(parent),
30   music_selector(),
31   play_music_button(),
32   previous_selected_music_id(),
33   quest() {
34 
35   music_selector.set_resource_type(ResourceType::MUSIC);
36   play_music_button.setIconSize(QSize(24, 24));
37 
38   QHBoxLayout* layout = new QHBoxLayout(this);
39   layout->setMargin(0);
40 
41   layout->addWidget(&music_selector);
42   layout->addWidget(&play_music_button);
43 
44   update_play_button();
45 }
46 
47 /**
48  * @brief Sets the quest whose resources will be shown.
49  * @param quest The quest.
50  */
set_quest(Quest & quest)51 void MusicChooser::set_quest(Quest& quest) {
52 
53   this->quest = &quest;
54 
55   music_selector.set_quest(quest);
56 
57   connect(&play_music_button, SIGNAL(clicked(bool)),
58           this, SLOT(play_music_button_clicked()));
59   connect(&music_selector, SIGNAL(activated(QString)),
60           this, SLOT(music_selector_activated(QString)));
61   connect(&quest, SIGNAL(current_music_changed(QString)),
62           this, SLOT(quest_music_changed(QString)));
63 
64   update_play_button();
65 }
66 
67 /**
68  * @brief Returns the music selector contained in this widget.
69  * @return The music selector.
70  */
get_selector()71 ResourceSelector& MusicChooser::get_selector() {
72   return music_selector;
73 }
74 
75 /**
76  * @brief Returns the music id in the selector.
77  * @return The selected music id.
78  */
get_selected_id()79 QString MusicChooser::get_selected_id() {
80   return music_selector.get_selected_id();
81 }
82 
83 /**
84  * @brief Sets the selected music.
85  * @param music_id The new music id to select.
86  */
set_selected_id(const QString & music_id)87 void MusicChooser::set_selected_id(const QString& music_id) {
88 
89   music_selector.set_selected_id(music_id);
90   update_play_button();
91 }
92 
93 /**
94  * @brief Slot called when the user changes the music in the selector.
95  * @param music_id The new selected music id.
96  */
music_selector_activated(const QString & music_id)97 void MusicChooser::music_selector_activated(const QString& music_id) {
98 
99   if (quest == nullptr) {
100     return;
101   }
102 
103   const QString& old_music_id = previous_selected_music_id;
104   const QString& new_music_id = music_selector.get_selected_id();
105   if (new_music_id != old_music_id) {
106     // There is a change.
107     previous_selected_music_id = new_music_id;
108 
109     // Stop playing any music if there is a change.
110     Audio::stop_music(*quest);
111 
112     update_play_button();
113   }
114 
115   emit activated(music_id);
116 }
117 
118 /**
119  * @brief Slot called when the user clicks the play/stop music button.
120  */
play_music_button_clicked()121 void MusicChooser::play_music_button_clicked() {
122 
123   if (quest == nullptr) {
124     return;
125   }
126 
127   const QString& selected_music_id = music_selector.get_selected_id();
128   if (selected_music_id.isEmpty() ||
129       selected_music_id == "none" ||
130       selected_music_id == "same") {
131     return;
132   }
133 
134   const QString& current_music_id = Audio::get_current_music_id(*quest);
135   if (current_music_id == selected_music_id) {
136     // This music is already playing, stop it.
137     Audio::stop_music(*quest);
138   }
139   else {
140     Audio::play_music(*quest, selected_music_id);
141   }
142 }
143 
144 /**
145  * @brief Slot called when a music is being played or stopped.
146  * @param music_id The new current music or an empty string.
147  */
quest_music_changed(const QString & music_id)148 void MusicChooser::quest_music_changed(const QString& music_id) {
149 
150   Q_UNUSED(music_id);
151   update_play_button();
152 }
153 
154 /**
155  * @brief Updates the play/stop music button depending on the current
156  * selection and the music currently playing if any.
157  */
update_play_button()158 void MusicChooser::update_play_button() {
159 
160   play_music_button.setIcon(QIcon(":/images/icon_start.png"));
161   play_music_button.setToolTip(tr("Play music"));
162 
163   if (quest == nullptr) {
164     play_music_button.setEnabled(false);
165     return;
166   }
167 
168   QString selected_music_id = music_selector.get_selected_id();
169   QString path = quest->get_music_path(selected_music_id);
170 
171   if (selected_music_id.isEmpty() ||
172       selected_music_id == "none" ||
173       selected_music_id == "same" ||
174       !quest->exists(path)) {
175     play_music_button.setEnabled(false);
176   }
177   else {
178     play_music_button.setEnabled(true);
179     const QString& music_playing_id = Audio::get_current_music_id(*quest);
180     if (music_playing_id == selected_music_id) {
181       play_music_button.setIcon(QIcon(":/images/icon_stop.png"));
182       play_music_button.setToolTip(tr("Stop music"));
183     }
184   }
185 }
186 
187 }
188