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