swaroop: skippable -> disable next/previous.
[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         , _current_disable_next_previous (false)
48 {
49         _button_sizer->Add (_play_button, 0, wxEXPAND);
50         _button_sizer->Add (_pause_button, 0, wxEXPAND);
51         _button_sizer->Add (_stop_button, 0, wxEXPAND);
52         _button_sizer->Add (_next_button, 0, wxEXPAND);
53         _button_sizer->Add (_previous_button, 0, wxEXPAND);
54
55         _spl_view = new wxListCtrl (this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxLC_REPORT | wxLC_NO_HEADER);
56         _spl_view->AppendColumn (wxT(""), wxLIST_FORMAT_LEFT, 740);
57
58         wxBoxSizer* left_sizer = new wxBoxSizer (wxVERTICAL);
59         wxBoxSizer* e_sizer = new wxBoxSizer (wxHORIZONTAL);
60
61         wxFont subheading_font (*wxNORMAL_FONT);
62         subheading_font.SetWeight (wxFONTWEIGHT_BOLD);
63
64         wxBoxSizer* spl_header = new wxBoxSizer (wxHORIZONTAL);
65         {
66                 wxStaticText* m = new StaticText (this, "Playlists");
67                 m->SetFont (subheading_font);
68                 spl_header->Add (m, 1, wxALIGN_CENTER_VERTICAL);
69         }
70         _refresh_spl_view = new Button (this, "Refresh");
71         spl_header->Add (_refresh_spl_view, 0, wxBOTTOM, DCPOMATIC_SIZER_GAP / 2);
72
73         left_sizer->Add (spl_header, 0, wxLEFT | wxRIGHT | wxEXPAND, DCPOMATIC_SIZER_GAP);
74         left_sizer->Add (_spl_view, 1, wxLEFT | wxRIGHT | wxBOTTOM | wxEXPAND, DCPOMATIC_SIZER_GAP);
75
76         _content_view = new ContentView (this);
77
78         wxBoxSizer* content_header = new wxBoxSizer (wxHORIZONTAL);
79         {
80                 wxStaticText* m = new StaticText (this, "Content");
81                 m->SetFont (subheading_font);
82                 content_header->Add (m, 1, wxALIGN_CENTER_VERTICAL);
83         }
84         _refresh_content_view = new Button (this, "Refresh");
85         content_header->Add (_refresh_content_view, 0, wxBOTTOM, DCPOMATIC_SIZER_GAP / 2);
86
87         left_sizer->Add (content_header, 0, wxTOP | wxLEFT | wxRIGHT | wxEXPAND, DCPOMATIC_SIZER_GAP);
88         left_sizer->Add (_content_view, 1, wxLEFT | wxRIGHT | wxBOTTOM | wxEXPAND, DCPOMATIC_SIZER_GAP);
89
90         _current_spl_view = new wxListCtrl (this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxLC_REPORT | wxLC_NO_HEADER);
91         _current_spl_view->AppendColumn (wxT(""), wxLIST_FORMAT_LEFT, 500);
92         _current_spl_view->AppendColumn (wxT(""), wxLIST_FORMAT_LEFT, 80);
93         e_sizer->Add (left_sizer, 0, wxALL | wxEXPAND, DCPOMATIC_SIZER_GAP);
94         e_sizer->Add (_current_spl_view, 1, wxALL | wxEXPAND, DCPOMATIC_SIZER_GAP);
95
96         _v_sizer->Add (e_sizer, 1, wxEXPAND);
97
98         _log = new wxTextCtrl (this, wxID_ANY, wxT(""), wxDefaultPosition, wxSize(-1, 200), wxTE_READONLY | wxTE_MULTILINE);
99         _v_sizer->Add (_log, 0, wxALL | wxEXPAND, DCPOMATIC_SIZER_GAP);
100
101         _play_button->Bind     (wxEVT_BUTTON, boost::bind(&SwaroopControls::play_clicked,  this));
102         _pause_button->Bind    (wxEVT_BUTTON, boost::bind(&SwaroopControls::pause_clicked, this));
103         _stop_button->Bind     (wxEVT_BUTTON, boost::bind(&SwaroopControls::stop_clicked,  this));
104         _next_button->Bind     (wxEVT_BUTTON, boost::bind(&SwaroopControls::next_clicked,  this));
105         _previous_button->Bind (wxEVT_BUTTON, boost::bind(&SwaroopControls::previous_clicked,  this));
106         _spl_view->Bind        (wxEVT_LIST_ITEM_SELECTED,   boost::bind(&SwaroopControls::spl_selection_changed, this));
107         _spl_view->Bind        (wxEVT_LIST_ITEM_DESELECTED, boost::bind(&SwaroopControls::spl_selection_changed, this));
108         _viewer->Finished.connect (boost::bind(&SwaroopControls::viewer_finished, this));
109         _refresh_spl_view->Bind (wxEVT_BUTTON, boost::bind(&SwaroopControls::update_playlist_directory, this));
110         _refresh_content_view->Bind (wxEVT_BUTTON, boost::bind(&ContentView::update, _content_view));
111
112         _content_view->update ();
113         update_playlist_directory ();
114 }
115
116 void
117 SwaroopControls::started ()
118 {
119         Controls::started ();
120         _play_button->Enable (false);
121         _pause_button->Enable (true);
122 }
123
124 void
125 SwaroopControls::stopped ()
126 {
127         Controls::stopped ();
128         _play_button->Enable (true);
129         _pause_button->Enable (false);
130 }
131
132 void
133 SwaroopControls::play_clicked ()
134 {
135         _viewer->start ();
136 }
137
138 void
139 SwaroopControls::setup_sensitivity ()
140 {
141         Controls::setup_sensitivity ();
142         bool const active_job = _active_job && *_active_job != "examine_content";
143         bool const c = _film && !_film->content().empty() && !active_job;
144         _play_button->Enable (c && !_viewer->playing());
145         _pause_button->Enable (_viewer->playing());
146         _slider->Enable (!_current_disable_timeline);
147         _spl_view->Enable (!_viewer->playing());
148         _next_button->Enable (!_current_disable_next_previous && can_do_next());
149         _previous_button->Enable (!_current_disable_next_previous && can_do_previous());
150 }
151
152 void
153 SwaroopControls::pause_clicked ()
154 {
155         _viewer->stop ();
156 }
157
158 void
159 SwaroopControls::stop_clicked ()
160 {
161         _viewer->stop ();
162         _viewer->seek (DCPTime(), true);
163 }
164
165 bool
166 SwaroopControls::can_do_previous ()
167 {
168         return _selected_playlist && (_selected_playlist_position - 1) >= 0;
169 }
170
171 void
172 SwaroopControls::previous_clicked ()
173 {
174         if (!can_do_previous ()) {
175                 return;
176         }
177
178         _selected_playlist_position--;
179         update_current_content ();
180 }
181
182 bool
183 SwaroopControls::can_do_next ()
184 {
185         return _selected_playlist && (_selected_playlist_position + 1) < int(_playlists[*_selected_playlist].get().size());
186 }
187
188 void
189 SwaroopControls::next_clicked ()
190 {
191         if (!can_do_next ()) {
192                 return;
193         }
194
195         _selected_playlist_position++;
196         update_current_content ();
197 }
198
199 void
200 SwaroopControls::log (wxString s)
201 {
202         struct timeval time;
203         gettimeofday (&time, 0);
204         char buffer[64];
205         time_t const sec = time.tv_sec;
206         struct tm* t = localtime (&sec);
207         strftime (buffer, 64, "%c", t);
208         wxString ts = std_to_wx(string(buffer)) + N_(": ");
209         _log->SetValue(_log->GetValue() + ts + s + "\n");
210 }
211
212 void
213 SwaroopControls::add_playlist_to_list (SPL spl)
214 {
215         int const N = _spl_view->GetItemCount();
216
217         wxListItem it;
218         it.SetId(N);
219         it.SetColumn(0);
220         string t = spl.name();
221         if (spl.missing()) {
222                 t += " (content missing)";
223         }
224         it.SetText (std_to_wx(t));
225         _spl_view->InsertItem (it);
226 }
227
228 void
229 SwaroopControls::update_playlist_directory ()
230 {
231         using namespace boost::filesystem;
232
233         _spl_view->DeleteAllItems ();
234         optional<path> dir = Config::instance()->player_playlist_directory();
235         if (!dir) {
236                 return;
237         }
238
239         _playlists.clear ();
240
241         for (directory_iterator i = directory_iterator(*dir); i != directory_iterator(); ++i) {
242                 try {
243                         if (is_regular_file(i->path()) && i->path().extension() == ".xml") {
244                                 SPL spl;
245                                 spl.read (i->path(), _content_view);
246                                 _playlists.push_back (spl);
247                                 add_playlist_to_list (spl);
248                         }
249                 } catch (exception& e) {
250                         /* Never mind */
251                 }
252         }
253 }
254
255 void
256 SwaroopControls::spl_selection_changed ()
257 {
258         long int selected = _spl_view->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
259         if (selected == -1) {
260                 _current_spl_view->DeleteAllItems ();
261                 _selected_playlist = boost::none;
262                 return;
263         }
264
265         if (_playlists[selected].missing()) {
266                 error_dialog (this, "This playlist cannot be loaded as some content is missing.");
267                 _selected_playlist = boost::none;
268                 _spl_view->SetItemState (selected, 0, wxLIST_STATE_SELECTED);
269                 return;
270         }
271
272         if (_playlists[selected].get().empty()) {
273                 error_dialog (this, "This playlist is empty.");
274                 return;
275         }
276
277
278         _current_spl_view->DeleteAllItems ();
279
280         int N = 0;
281         BOOST_FOREACH (SPLEntry i, _playlists[selected].get()) {
282                 wxListItem it;
283                 it.SetId (N);
284                 it.SetColumn (0);
285                 it.SetText (std_to_wx(i.name));
286                 _current_spl_view->InsertItem (it);
287                 ++N;
288         }
289
290         _selected_playlist = selected;
291         _selected_playlist_position = 0;
292         reset_film ();
293 }
294
295 void
296 SwaroopControls::reset_film ()
297 {
298         DCPOMATIC_ASSERT (_selected_playlist);
299         shared_ptr<Film> film (new Film(optional<boost::filesystem::path>()));
300         film->add_content (_playlists[*_selected_playlist].get()[_selected_playlist_position].content);
301         ResetFilm (film);
302 }
303
304 void
305 SwaroopControls::config_changed (int property)
306 {
307         Controls::config_changed (property);
308
309         if (property == Config::PLAYER_CONTENT_DIRECTORY) {
310                 _content_view->update ();
311         } else if (property == Config::PLAYER_PLAYLIST_DIRECTORY) {
312                 update_playlist_directory ();
313         }
314 }
315
316 void
317 SwaroopControls::set_film (shared_ptr<Film> film)
318 {
319         Controls::set_film (film);
320         setup_sensitivity ();
321 }
322
323 void
324 SwaroopControls::update_current_content ()
325 {
326         DCPOMATIC_ASSERT (_selected_playlist);
327
328         _viewer->stop ();
329
330         SPLEntry const & e = _playlists[*_selected_playlist].get()[_selected_playlist_position];
331         _current_disable_timeline = e.disable_timeline;
332         _current_disable_next_previous = e.skippable;
333
334         setup_sensitivity ();
335         reset_film ();
336         _viewer->start ();
337 }
338
339 void
340 SwaroopControls::viewer_finished ()
341 {
342         if (!_selected_playlist) {
343                 return;
344         }
345
346         next_clicked ();
347 }