swaroop: remove SPL editing from player UI.
[dcpomatic.git] / src / wx / 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 "controls.h"
22 #include "film_viewer.h"
23 #include "wx_util.h"
24 #include "playhead_to_timecode_dialog.h"
25 #include "playhead_to_frame_dialog.h"
26 #include "content_view.h"
27 #include "lib/job_manager.h"
28 #include "lib/player_video.h"
29 #include "lib/dcp_content.h"
30 #include "lib/job.h"
31 #include "lib/examine_content_job.h"
32 #include "lib/content_factory.h"
33 #include "lib/cross.h"
34 #include <dcp/dcp.h>
35 #include <dcp/cpl.h>
36 #include <dcp/reel.h>
37 #include <dcp/reel_picture_asset.h>
38 #include <wx/wx.h>
39 #include <wx/tglbtn.h>
40 #include <wx/listctrl.h>
41 #include <wx/progdlg.h>
42
43 using std::string;
44 using std::list;
45 using std::cout;
46 using std::make_pair;
47 using std::exception;
48 using boost::optional;
49 using boost::shared_ptr;
50 using boost::weak_ptr;
51 using boost::dynamic_pointer_cast;
52
53 Controls::Controls (wxWindow* parent, shared_ptr<FilmViewer> viewer, bool editor_controls)
54         : wxPanel (parent)
55         , _viewer (viewer)
56         , _slider_being_moved (false)
57         , _was_running_before_slider (false)
58         , _outline_content (0)
59         , _eye (0)
60         , _jump_to_selected (0)
61         , _slider (new wxSlider (this, wxID_ANY, 0, 0, 4096))
62         , _rewind_button (new wxButton (this, wxID_ANY, wxT("|<")))
63         , _back_button (new wxButton (this, wxID_ANY, wxT("<")))
64         , _forward_button (new wxButton (this, wxID_ANY, wxT(">")))
65         , _frame_number (new wxStaticText (this, wxID_ANY, wxT("")))
66         , _timecode (new wxStaticText (this, wxID_ANY, wxT("")))
67 #ifdef DCPOMATIC_VARIANT_SWAROOP
68         , _play_button (new wxButton(this, wxID_ANY, _("Play")))
69         , _pause_button (new wxButton(this, wxID_ANY, _("Pause")))
70         , _stop_button (new wxButton(this, wxID_ANY, _("Stop")))
71 #else
72         , _play_button (new wxToggleButton(this, wxID_ANY, _("Play")))
73 #endif
74 {
75         _v_sizer = new wxBoxSizer (wxVERTICAL);
76         SetSizer (_v_sizer);
77
78         wxBoxSizer* view_options = new wxBoxSizer (wxHORIZONTAL);
79         if (editor_controls) {
80                 _outline_content = new wxCheckBox (this, wxID_ANY, _("Outline content"));
81                 view_options->Add (_outline_content, 0, wxRIGHT | wxALIGN_CENTER_VERTICAL, DCPOMATIC_SIZER_GAP);
82                 _eye = new wxChoice (this, wxID_ANY);
83                 _eye->Append (_("Left"));
84                 _eye->Append (_("Right"));
85                 _eye->SetSelection (0);
86                 view_options->Add (_eye, 0, wxLEFT | wxRIGHT | wxALIGN_CENTER_VERTICAL, DCPOMATIC_SIZER_GAP);
87                 _jump_to_selected = new wxCheckBox (this, wxID_ANY, _("Jump to selected content"));
88                 view_options->Add (_jump_to_selected, 0, wxLEFT | wxRIGHT | wxALIGN_CENTER_VERTICAL, DCPOMATIC_SIZER_GAP);
89         }
90
91         _v_sizer->Add (view_options, 0, wxALL, DCPOMATIC_SIZER_GAP);
92
93         wxBoxSizer* left_sizer = new wxBoxSizer (wxVERTICAL);
94
95         _spl_view = new wxListCtrl (this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxLC_REPORT | wxLC_NO_HEADER);
96         _spl_view->AppendColumn (wxT(""), wxLIST_FORMAT_LEFT, 740);
97         left_sizer->Add (_spl_view, 1, wxALL | wxEXPAND, DCPOMATIC_SIZER_GAP);
98
99         _content_view = new ContentView (this, _film);
100         left_sizer->Add (_content_view, 1, wxALL | wxEXPAND, DCPOMATIC_SIZER_GAP);
101
102         wxBoxSizer* e_sizer = new wxBoxSizer (wxHORIZONTAL);
103         e_sizer->Add (left_sizer, 1, wxALL | wxEXPAND, DCPOMATIC_SIZER_GAP);
104
105         _current_spl_view = new wxListCtrl (this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxLC_REPORT | wxLC_NO_HEADER);
106         _current_spl_view->AppendColumn (wxT(""), wxLIST_FORMAT_LEFT, 80);
107         _current_spl_view->AppendColumn (wxT(""), wxLIST_FORMAT_LEFT, 80);
108         _current_spl_view->AppendColumn (wxT(""), wxLIST_FORMAT_LEFT, 580);
109         e_sizer->Add (_current_spl_view, 1, wxALL | wxEXPAND, DCPOMATIC_SIZER_GAP);
110
111         _v_sizer->Add (e_sizer, 1, wxEXPAND);
112
113         _log = new wxTextCtrl (this, wxID_ANY, wxT(""), wxDefaultPosition, wxSize(-1, 200), wxTE_READONLY | wxTE_MULTILINE);
114         _v_sizer->Add (_log, 0, wxALL | wxEXPAND, DCPOMATIC_SIZER_GAP);
115
116         _content_view->Show (false);
117         _spl_view->Show (false);
118         _current_spl_view->Show (false);
119         _log->Show (false);
120
121         wxBoxSizer* h_sizer = new wxBoxSizer (wxHORIZONTAL);
122
123         wxBoxSizer* time_sizer = new wxBoxSizer (wxVERTICAL);
124         time_sizer->Add (_frame_number, 0, wxEXPAND);
125         time_sizer->Add (_timecode, 0, wxEXPAND);
126
127         h_sizer->Add (_rewind_button, 0, wxALL, 2);
128         h_sizer->Add (_back_button, 0, wxALL, 2);
129         h_sizer->Add (time_sizer, 0, wxEXPAND);
130         h_sizer->Add (_forward_button, 0, wxALL, 2);
131         h_sizer->Add (_play_button, 0, wxEXPAND);
132 #ifdef DCPOMATIC_VARIANT_SWAROOP
133         h_sizer->Add (_pause_button, 0, wxEXPAND);
134         h_sizer->Add (_stop_button, 0, wxEXPAND);
135 #endif
136         h_sizer->Add (_slider, 1, wxEXPAND);
137
138         _v_sizer->Add (h_sizer, 0, wxEXPAND | wxALL, 6);
139
140         _frame_number->SetMinSize (wxSize (84, -1));
141         _rewind_button->SetMinSize (wxSize (32, -1));
142         _back_button->SetMinSize (wxSize (32, -1));
143         _forward_button->SetMinSize (wxSize (32, -1));
144
145         if (_eye) {
146                 _eye->Bind (wxEVT_CHOICE, boost::bind (&Controls::eye_changed, this));
147         }
148         if (_outline_content) {
149                 _outline_content->Bind (wxEVT_CHECKBOX, boost::bind (&Controls::outline_content_changed, this));
150         }
151
152         _slider->Bind           (wxEVT_SCROLL_THUMBTRACK,    boost::bind(&Controls::slider_moved,    this, false));
153         _slider->Bind           (wxEVT_SCROLL_PAGEUP,        boost::bind(&Controls::slider_moved,    this, true));
154         _slider->Bind           (wxEVT_SCROLL_PAGEDOWN,      boost::bind(&Controls::slider_moved,    this, true));
155         _slider->Bind           (wxEVT_SCROLL_CHANGED,       boost::bind(&Controls::slider_released, this));
156 #ifdef DCPOMATIC_VARIANT_SWAROOP
157         _play_button->Bind      (wxEVT_BUTTON,               boost::bind(&Controls::play_clicked,    this));
158         _pause_button->Bind     (wxEVT_BUTTON,               boost::bind(&Controls::pause_clicked,   this));
159         _stop_button->Bind      (wxEVT_BUTTON,               boost::bind(&Controls::stop_clicked,    this));
160 #else
161         _play_button->Bind      (wxEVT_TOGGLEBUTTON,         boost::bind(&Controls::play_clicked,    this));
162 #endif
163         _rewind_button->Bind    (wxEVT_LEFT_DOWN,            boost::bind(&Controls::rewind_clicked,  this, _1));
164         _back_button->Bind      (wxEVT_LEFT_DOWN,            boost::bind(&Controls::back_clicked,    this, _1));
165         _forward_button->Bind   (wxEVT_LEFT_DOWN,            boost::bind(&Controls::forward_clicked, this, _1));
166         _frame_number->Bind     (wxEVT_LEFT_DOWN,            boost::bind(&Controls::frame_number_clicked, this));
167         _timecode->Bind         (wxEVT_LEFT_DOWN,            boost::bind(&Controls::timecode_clicked, this));
168         _content_view->Bind     (wxEVT_LIST_ITEM_SELECTED,   boost::bind(&Controls::setup_sensitivity, this));
169         _content_view->Bind     (wxEVT_LIST_ITEM_DESELECTED, boost::bind(&Controls::setup_sensitivity, this));
170         if (_jump_to_selected) {
171                 _jump_to_selected->Bind (wxEVT_CHECKBOX, boost::bind (&Controls::jump_to_selected_clicked, this));
172                 _jump_to_selected->SetValue (Config::instance()->jump_to_selected ());
173         }
174
175         _viewer->PositionChanged.connect (boost::bind(&Controls::position_changed, this));
176         _viewer->Started.connect (boost::bind(&Controls::started, this));
177         _viewer->Stopped.connect (boost::bind(&Controls::stopped, this));
178         _viewer->FilmChanged.connect (boost::bind(&Controls::film_changed, this));
179         _viewer->ImageChanged.connect (boost::bind(&Controls::image_changed, this, _1));
180
181         film_changed ();
182
183         setup_sensitivity ();
184         update_playlist_directory ();
185
186         JobManager::instance()->ActiveJobsChanged.connect (
187                 bind (&Controls::active_jobs_changed, this, _2)
188                 );
189
190         _config_changed_connection = Config::instance()->Changed.connect (bind(&Controls::config_changed, this, _1));
191         config_changed (Config::OTHER);
192 }
193
194 void
195 Controls::config_changed (int property)
196 {
197         if (property == Config::PLAYER_CONTENT_DIRECTORY) {
198                 _content_view->update ();
199         } else if (property == Config::PLAYER_PLAYLIST_DIRECTORY) {
200                 update_playlist_directory ();
201         } else {
202                 setup_sensitivity ();
203         }
204 }
205
206 void
207 Controls::started ()
208 {
209 #ifdef DCPOMATIC_VARIANT_SWAROOP
210         _play_button->Enable (false);
211         _pause_button->Enable (true);
212 #else
213         _play_button->SetValue (true);
214 #endif
215         setup_sensitivity ();
216 }
217
218 void
219 Controls::stopped ()
220 {
221 #ifdef DCPOMATIC_VARIANT_SWAROOP
222         _play_button->Enable (true);
223         _pause_button->Enable (false);
224 #else
225         _play_button->SetValue (false);
226 #endif
227         setup_sensitivity ();
228 }
229
230 void
231 Controls::position_changed ()
232 {
233         if (!_slider_being_moved) {
234                 update_position_label ();
235                 update_position_slider ();
236         }
237 }
238
239 void
240 Controls::eye_changed ()
241 {
242         _viewer->set_eyes (_eye->GetSelection() == 0 ? EYES_LEFT : EYES_RIGHT);
243 }
244
245 void
246 Controls::outline_content_changed ()
247 {
248         _viewer->set_outline_content (_outline_content->GetValue());
249 }
250
251 void
252 Controls::film_change (ChangeType type, Film::Property p)
253 {
254         if (type != CHANGE_TYPE_DONE) {
255                 return;
256         }
257
258         if (p == Film::CONTENT || p == Film::THREE_D) {
259                 setup_sensitivity ();
260         }
261 }
262
263 /** @param page true if this was a PAGEUP/PAGEDOWN event for which we won't receive a THUMBRELEASE */
264 void
265 Controls::slider_moved (bool page)
266 {
267         if (!_film) {
268                 return;
269         }
270
271         if (!page && !_slider_being_moved) {
272                 /* This is the first event of a drag; stop playback for the duration of the drag */
273                 _was_running_before_slider = _viewer->stop ();
274                 _slider_being_moved = true;
275         }
276
277         DCPTime t (_slider->GetValue() * _film->length().get() / 4096);
278         t = t.round (_film->video_frame_rate());
279         /* Ensure that we hit the end of the film at the end of the slider */
280         if (t >= _film->length ()) {
281                 t = _film->length() - _viewer->one_video_frame();
282         }
283         _viewer->seek (t, false);
284         update_position_label ();
285 }
286
287 void
288 Controls::slider_released ()
289 {
290         if (_was_running_before_slider) {
291                 /* Restart after a drag */
292                 _viewer->start ();
293         }
294         _slider_being_moved = false;
295 }
296
297 void
298 Controls::play_clicked ()
299 {
300 #ifdef DCPOMATIC_VARIANT_SWAROOP
301         _viewer->start ();
302 #else
303         check_play_state ();
304 #endif
305 }
306
307
308 #ifndef DCPOMATIC_VARIANT_SWAROOP
309 void
310 Controls::check_play_state ()
311 {
312         if (!_film || _film->video_frame_rate() == 0) {
313                 return;
314         }
315
316         if (_play_button->GetValue()) {
317                 _viewer->start ();
318         } else {
319                 _viewer->stop ();
320         }
321 }
322 #endif
323
324 void
325 Controls::update_position_slider ()
326 {
327         if (!_film) {
328                 _slider->SetValue (0);
329                 return;
330         }
331
332         DCPTime const len = _film->length ();
333
334         if (len.get ()) {
335                 int const new_slider_position = 4096 * _viewer->position().get() / len.get();
336                 if (new_slider_position != _slider->GetValue()) {
337                         _slider->SetValue (new_slider_position);
338                 }
339         }
340 }
341
342 void
343 Controls::update_position_label ()
344 {
345         if (!_film) {
346                 _frame_number->SetLabel ("0");
347                 _timecode->SetLabel ("0:0:0.0");
348                 return;
349         }
350
351         double const fps = _film->video_frame_rate ();
352         /* Count frame number from 1 ... not sure if this is the best idea */
353         _frame_number->SetLabel (wxString::Format (wxT("%ld"), lrint (_viewer->position().seconds() * fps) + 1));
354         _timecode->SetLabel (time_to_timecode (_viewer->position(), fps));
355 }
356
357 void
358 Controls::active_jobs_changed (optional<string> j)
359 {
360         _active_job = j;
361         setup_sensitivity ();
362 }
363
364 DCPTime
365 Controls::nudge_amount (wxKeyboardState& ev)
366 {
367         DCPTime amount = _viewer->one_video_frame ();
368
369         if (ev.ShiftDown() && !ev.ControlDown()) {
370                 amount = DCPTime::from_seconds (1);
371         } else if (!ev.ShiftDown() && ev.ControlDown()) {
372                 amount = DCPTime::from_seconds (10);
373         } else if (ev.ShiftDown() && ev.ControlDown()) {
374                 amount = DCPTime::from_seconds (60);
375         }
376
377         return amount;
378 }
379
380 void
381 Controls::rewind_clicked (wxMouseEvent& ev)
382 {
383         _viewer->seek (DCPTime(), true);
384         ev.Skip();
385 }
386
387 void
388 Controls::back_frame ()
389 {
390         _viewer->seek_by (-_viewer->one_video_frame(), true);
391 }
392
393 void
394 Controls::forward_frame ()
395 {
396         _viewer->seek_by (_viewer->one_video_frame(), true);
397 }
398
399 void
400 Controls::back_clicked (wxKeyboardState& ev)
401 {
402         _viewer->seek_by (-nudge_amount(ev), true);
403 }
404
405 void
406 Controls::forward_clicked (wxKeyboardState& ev)
407 {
408         _viewer->seek_by (nudge_amount(ev), true);
409 }
410
411 void
412 Controls::setup_sensitivity ()
413 {
414         /* examine content is the only job which stops the viewer working */
415         bool const active_job = _active_job && *_active_job != "examine_content";
416         bool const c = _film && !_film->content().empty() && !active_job;
417
418         _slider->Enable (c);
419         _rewind_button->Enable (c);
420         _back_button->Enable (c);
421         _forward_button->Enable (c);
422 #ifdef DCPOMATIC_VARIANT_SWAROOP
423         _play_button->Enable (c && !_viewer->playing());
424         _pause_button->Enable (c && (!_current_kind || _current_kind != dcp::ADVERTISEMENT) && _viewer->playing());
425         _stop_button->Enable (c && (!_current_kind || _current_kind != dcp::ADVERTISEMENT));
426         _slider->Enable (c && (!_current_kind || _current_kind != dcp::ADVERTISEMENT));
427 #else
428         _play_button->Enable (c);
429 #endif
430         if (_outline_content) {
431                 _outline_content->Enable (c);
432         }
433         _frame_number->Enable (c);
434         _timecode->Enable (c);
435         if (_jump_to_selected) {
436                 _jump_to_selected->Enable (c);
437         }
438
439         if (_eye) {
440                 _eye->Enable (c && _film->three_d ());
441         }
442 }
443
444 void
445 Controls::timecode_clicked ()
446 {
447         PlayheadToTimecodeDialog* dialog = new PlayheadToTimecodeDialog (this, _film->video_frame_rate ());
448         if (dialog->ShowModal() == wxID_OK) {
449                 _viewer->seek (dialog->get(), true);
450         }
451         dialog->Destroy ();
452 }
453
454 void
455 Controls::frame_number_clicked ()
456 {
457         PlayheadToFrameDialog* dialog = new PlayheadToFrameDialog (this, _film->video_frame_rate ());
458         if (dialog->ShowModal() == wxID_OK) {
459                 _viewer->seek (dialog->get(), true);
460         }
461         dialog->Destroy ();
462 }
463
464 void
465 Controls::jump_to_selected_clicked ()
466 {
467         Config::instance()->set_jump_to_selected (_jump_to_selected->GetValue ());
468 }
469
470 void
471 Controls::film_changed ()
472 {
473         shared_ptr<Film> film = _viewer->film ();
474
475         if (_film == film) {
476                 return;
477         }
478
479         _film = film;
480
481         setup_sensitivity ();
482
483         update_position_slider ();
484         update_position_label ();
485
486         if (_film) {
487                 _film->Change.connect (boost::bind (&Controls::film_change, this, _1, _2));
488         }
489
490         _content_view->set_film (film);
491         _content_view->update ();
492 }
493
494 shared_ptr<Film>
495 Controls::film () const
496 {
497         return _film;
498 }
499
500 void
501 Controls::show_extended_player_controls (bool s)
502 {
503         _content_view->Show (s);
504         _spl_view->Show (s);
505         if (s) {
506                 _content_view->update ();
507                 update_playlist_directory ();
508         }
509         _current_spl_view->Show (s);
510         _log->Show (s);
511         _v_sizer->Layout ();
512 }
513
514 void
515 Controls::add_playlist_to_list (shared_ptr<Film> film)
516 {
517         int const N = _spl_view->GetItemCount();
518
519         wxListItem it;
520         it.SetId(N);
521         it.SetColumn(0);
522         it.SetText (std_to_wx(film->name()));
523         _spl_view->InsertItem (it);
524 }
525
526 void
527 Controls::update_playlist_directory ()
528 {
529         if (!_spl_view->IsShown()) {
530                 return;
531         }
532
533         using namespace boost::filesystem;
534
535         _spl_view->DeleteAllItems ();
536         optional<path> dir = Config::instance()->player_playlist_directory();
537         if (!dir) {
538                 return;
539         }
540
541         for (directory_iterator i = directory_iterator(*dir); i != directory_iterator(); ++i) {
542                 try {
543                         shared_ptr<Film> film (new Film(optional<path>()));
544                         film->read_metadata (i->path());
545                         _playlists.push_back (film);
546                         add_playlist_to_list (film);
547                 } catch (exception& e) {
548                         /* Never mind */
549                 }
550         }
551 }
552
553 #ifdef DCPOMATIC_VARIANT_SWAROOP
554 void
555 Controls::pause_clicked ()
556 {
557         _viewer->stop ();
558 }
559
560 void
561 Controls::stop_clicked ()
562 {
563         _viewer->stop ();
564         _viewer->seek (DCPTime(), true);
565 }
566 #endif
567
568 void
569 Controls::log (wxString s)
570 {
571         struct timeval time;
572         gettimeofday (&time, 0);
573         char buffer[64];
574         time_t const sec = time.tv_sec;
575         struct tm* t = localtime (&sec);
576         strftime (buffer, 64, "%c", t);
577         wxString ts = std_to_wx(string(buffer)) + N_(": ");
578         _log->SetValue(_log->GetValue() + ts + s + "\n");
579 }
580
581 void
582 Controls::image_changed (boost::weak_ptr<PlayerVideo> weak_pv)
583 {
584 #ifdef DCPOMATIC_VARIANT_SWAROOP
585         shared_ptr<PlayerVideo> pv = weak_pv.lock ();
586         if (!pv) {
587                 return;
588         }
589
590         shared_ptr<Content> c = pv->content().lock();
591         if (!c) {
592                 return;
593         }
594
595         shared_ptr<DCPContent> dc = dynamic_pointer_cast<DCPContent> (c);
596         if (!dc) {
597                 return;
598         }
599
600         if (!_current_kind || *_current_kind != dc->content_kind()) {
601                 _current_kind = dc->content_kind ();
602                 setup_sensitivity ();
603         }
604 #endif
605 }