swaroop: next/previous buttons for playlist.
[dcpomatic.git] / src / wx / swaroop_controls.cc
1 /*
2     Copyright (C) 2018 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     DCP-o-matic is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21 #include "swaroop_controls.h"
22 #include "film_viewer.h"
23 #include "wx_util.h"
24 #include "content_view.h"
25 #include "dcpomatic_button.h"
26 #include "static_text.h"
27 #include "lib/player_video.h"
28 #include "lib/dcp_content.h"
29 #include <wx/listctrl.h>
30 #include <wx/progdlg.h>
31
32 using std::string;
33 using std::cout;
34 using std::exception;
35 using boost::shared_ptr;
36 using boost::dynamic_pointer_cast;
37 using boost::optional;
38
39 SwaroopControls::SwaroopControls (wxWindow* parent, shared_ptr<FilmViewer> viewer)
40         : Controls (parent, viewer, false)
41         , _play_button (new Button(this, _("Play")))
42         , _pause_button (new Button(this, _("Pause")))
43         , _stop_button (new Button(this, _("Stop")))
44         , _next_button (new Button(this, "Next"))
45         , _previous_button (new Button(this, "Previous"))
46         , _current_disable_timeline (false)
47 {
48         _button_sizer->Add (_play_button, 0, wxEXPAND);
49         _button_sizer->Add (_pause_button, 0, wxEXPAND);
50         _button_sizer->Add (_stop_button, 0, wxEXPAND);
51         _button_sizer->Add (_next_button, 0, wxEXPAND);
52         _button_sizer->Add (_previous_button, 0, wxEXPAND);
53
54         _spl_view = new wxListCtrl (this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxLC_REPORT | wxLC_NO_HEADER);
55         _spl_view->AppendColumn (wxT(""), wxLIST_FORMAT_LEFT, 740);
56
57         wxBoxSizer* left_sizer = new wxBoxSizer (wxVERTICAL);
58         wxBoxSizer* e_sizer = new wxBoxSizer (wxHORIZONTAL);
59
60         wxFont subheading_font (*wxNORMAL_FONT);
61         subheading_font.SetWeight (wxFONTWEIGHT_BOLD);
62
63         wxBoxSizer* spl_header = new wxBoxSizer (wxHORIZONTAL);
64         {
65                 wxStaticText* m = new StaticText (this, "Playlists");
66                 m->SetFont (subheading_font);
67                 spl_header->Add (m, 1, wxALIGN_CENTER_VERTICAL);
68         }
69         _refresh_spl_view = new Button (this, "Refresh");
70         spl_header->Add (_refresh_spl_view, 0, wxBOTTOM, DCPOMATIC_SIZER_GAP / 2);
71
72         left_sizer->Add (spl_header, 0, wxLEFT | wxRIGHT | wxEXPAND, DCPOMATIC_SIZER_GAP);
73         left_sizer->Add (_spl_view, 1, wxLEFT | wxRIGHT | wxBOTTOM | wxEXPAND, DCPOMATIC_SIZER_GAP);
74
75         _content_view = new ContentView (this);
76
77         wxBoxSizer* content_header = new wxBoxSizer (wxHORIZONTAL);
78         {
79                 wxStaticText* m = new StaticText (this, "Content");
80                 m->SetFont (subheading_font);
81                 content_header->Add (m, 1, wxALIGN_CENTER_VERTICAL);
82         }
83         _refresh_content_view = new Button (this, "Refresh");
84         content_header->Add (_refresh_content_view, 0, wxBOTTOM, DCPOMATIC_SIZER_GAP / 2);
85
86         left_sizer->Add (content_header, 0, wxTOP | wxLEFT | wxRIGHT | wxEXPAND, DCPOMATIC_SIZER_GAP);
87         left_sizer->Add (_content_view, 1, wxLEFT | wxRIGHT | wxBOTTOM | wxEXPAND, DCPOMATIC_SIZER_GAP);
88
89         _current_spl_view = new wxListCtrl (this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxLC_REPORT | wxLC_NO_HEADER);
90         _current_spl_view->AppendColumn (wxT(""), wxLIST_FORMAT_LEFT, 500);
91         _current_spl_view->AppendColumn (wxT(""), wxLIST_FORMAT_LEFT, 80);
92         e_sizer->Add (left_sizer, 0, wxALL | wxEXPAND, DCPOMATIC_SIZER_GAP);
93         e_sizer->Add (_current_spl_view, 1, wxALL | wxEXPAND, DCPOMATIC_SIZER_GAP);
94
95         _v_sizer->Add (e_sizer, 1, wxEXPAND);
96
97         _log = new wxTextCtrl (this, wxID_ANY, wxT(""), wxDefaultPosition, wxSize(-1, 200), wxTE_READONLY | wxTE_MULTILINE);
98         _v_sizer->Add (_log, 0, wxALL | wxEXPAND, DCPOMATIC_SIZER_GAP);
99
100         _play_button->Bind     (wxEVT_BUTTON, boost::bind(&SwaroopControls::play_clicked,  this));
101         _pause_button->Bind    (wxEVT_BUTTON, boost::bind(&SwaroopControls::pause_clicked, this));
102         _stop_button->Bind     (wxEVT_BUTTON, boost::bind(&SwaroopControls::stop_clicked,  this));
103         _next_button->Bind     (wxEVT_BUTTON, boost::bind(&SwaroopControls::next_clicked,  this));
104         _previous_button->Bind (wxEVT_BUTTON, boost::bind(&SwaroopControls::previous_clicked,  this));
105         _spl_view->Bind        (wxEVT_LIST_ITEM_SELECTED,   boost::bind(&SwaroopControls::spl_selection_changed, this));
106         _spl_view->Bind        (wxEVT_LIST_ITEM_DESELECTED, boost::bind(&SwaroopControls::spl_selection_changed, this));
107         _viewer->Finished.connect (boost::bind(&SwaroopControls::viewer_finished, this));
108         _refresh_spl_view->Bind (wxEVT_BUTTON, boost::bind(&SwaroopControls::update_playlist_directory, this));
109         _refresh_content_view->Bind (wxEVT_BUTTON, boost::bind(&ContentView::update, _content_view));
110
111         _content_view->update ();
112         update_playlist_directory ();
113 }
114
115 void
116 SwaroopControls::started ()
117 {
118         Controls::started ();
119         _play_button->Enable (false);
120         _pause_button->Enable (true);
121 }
122
123 void
124 SwaroopControls::stopped ()
125 {
126         Controls::stopped ();
127         _play_button->Enable (true);
128         _pause_button->Enable (false);
129 }
130
131 void
132 SwaroopControls::play_clicked ()
133 {
134         _viewer->start ();
135 }
136
137 void
138 SwaroopControls::setup_sensitivity ()
139 {
140         Controls::setup_sensitivity ();
141         bool const active_job = _active_job && *_active_job != "examine_content";
142         bool const c = _film && !_film->content().empty() && !active_job;
143         _play_button->Enable (c && !_viewer->playing());
144         _pause_button->Enable (_viewer->playing());
145         _slider->Enable (!_current_disable_timeline);
146         _spl_view->Enable (!_viewer->playing());
147 }
148
149 void
150 SwaroopControls::pause_clicked ()
151 {
152         _viewer->stop ();
153 }
154
155 void
156 SwaroopControls::stop_clicked ()
157 {
158         _viewer->stop ();
159         _viewer->seek (DCPTime(), true);
160 }
161
162 void
163 SwaroopControls::previous_clicked ()
164 {
165         if (!_selected_playlist || (_selected_playlist_position - 1) < 0) {
166                 return;
167         }
168
169         _selected_playlist_position--;
170         update_current_content ();
171 }
172
173 void
174 SwaroopControls::next_clicked ()
175 {
176         if (!_selected_playlist || (_selected_playlist_position + 1) >= int(_playlists[*_selected_playlist].get().size())) {
177                 return;
178         }
179
180         _selected_playlist_position++;
181         update_current_content ();
182 }
183
184 void
185 SwaroopControls::log (wxString s)
186 {
187         struct timeval time;
188         gettimeofday (&time, 0);
189         char buffer[64];
190         time_t const sec = time.tv_sec;
191         struct tm* t = localtime (&sec);
192         strftime (buffer, 64, "%c", t);
193         wxString ts = std_to_wx(string(buffer)) + N_(": ");
194         _log->SetValue(_log->GetValue() + ts + s + "\n");
195 }
196
197 void
198 SwaroopControls::add_playlist_to_list (SPL spl)
199 {
200         int const N = _spl_view->GetItemCount();
201
202         wxListItem it;
203         it.SetId(N);
204         it.SetColumn(0);
205         string t = spl.name();
206         if (spl.missing()) {
207                 t += " (content missing)";
208         }
209         it.SetText (std_to_wx(t));
210         _spl_view->InsertItem (it);
211 }
212
213 void
214 SwaroopControls::update_playlist_directory ()
215 {
216         using namespace boost::filesystem;
217
218         _spl_view->DeleteAllItems ();
219         optional<path> dir = Config::instance()->player_playlist_directory();
220         if (!dir) {
221                 return;
222         }
223
224         _playlists.clear ();
225
226         for (directory_iterator i = directory_iterator(*dir); i != directory_iterator(); ++i) {
227                 try {
228                         if (is_regular_file(i->path()) && i->path().extension() == ".xml") {
229                                 SPL spl;
230                                 spl.read (i->path(), _content_view);
231                                 _playlists.push_back (spl);
232                                 add_playlist_to_list (spl);
233                         }
234                 } catch (exception& e) {
235                         /* Never mind */
236                 }
237         }
238 }
239
240 void
241 SwaroopControls::spl_selection_changed ()
242 {
243         long int selected = _spl_view->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
244         if (selected == -1) {
245                 _current_spl_view->DeleteAllItems ();
246                 _selected_playlist = boost::none;
247                 return;
248         }
249
250         if (_playlists[selected].missing()) {
251                 error_dialog (this, "This playlist cannot be loaded as some content is missing.");
252                 _selected_playlist = boost::none;
253                 _spl_view->SetItemState (selected, 0, wxLIST_STATE_SELECTED);
254                 return;
255         }
256
257         if (_playlists[selected].get().empty()) {
258                 error_dialog (this, "This playlist is empty.");
259                 return;
260         }
261
262
263         _current_spl_view->DeleteAllItems ();
264
265         int N = 0;
266         BOOST_FOREACH (SPLEntry i, _playlists[selected].get()) {
267                 wxListItem it;
268                 it.SetId (N);
269                 it.SetColumn (0);
270                 it.SetText (std_to_wx(i.name));
271                 _current_spl_view->InsertItem (it);
272                 ++N;
273         }
274
275         _selected_playlist = selected;
276         _selected_playlist_position = 0;
277         reset_film ();
278 }
279
280 void
281 SwaroopControls::reset_film ()
282 {
283         DCPOMATIC_ASSERT (_selected_playlist);
284         shared_ptr<Film> film (new Film(optional<boost::filesystem::path>()));
285         film->add_content (_playlists[*_selected_playlist].get()[_selected_playlist_position].content);
286         ResetFilm (film);
287 }
288
289 void
290 SwaroopControls::config_changed (int property)
291 {
292         Controls::config_changed (property);
293
294         if (property == Config::PLAYER_CONTENT_DIRECTORY) {
295                 _content_view->update ();
296         } else if (property == Config::PLAYER_PLAYLIST_DIRECTORY) {
297                 update_playlist_directory ();
298         }
299 }
300
301 void
302 SwaroopControls::set_film (shared_ptr<Film> film)
303 {
304         Controls::set_film (film);
305         setup_sensitivity ();
306 }
307
308 void
309 SwaroopControls::update_current_content ()
310 {
311         DCPOMATIC_ASSERT (_selected_playlist);
312
313         _viewer->stop ();
314         _current_disable_timeline = _playlists[*_selected_playlist].get()[_selected_playlist_position].disable_timeline;
315         setup_sensitivity ();
316         reset_film ();
317         _viewer->start ();
318 }
319
320 void
321 SwaroopControls::viewer_finished ()
322 {
323         if (!_selected_playlist) {
324                 return;
325         }
326
327         next_clicked ();
328 }