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