d3bdc923d5eb12a82cb750385c80a5ed80169a5d
[dcpomatic.git] / src / wx / timing_panel.cc
1 /*
2     Copyright (C) 2012-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 "timing_panel.h"
22 #include "wx_util.h"
23 #include "film_viewer.h"
24 #include "timecode.h"
25 #include "content_panel.h"
26 #include "move_to_dialog.h"
27 #include "static_text.h"
28 #include "dcpomatic_button.h"
29 #include "lib/content.h"
30 #include "lib/image_content.h"
31 #include "lib/text_content.h"
32 #include "lib/dcp_subtitle_content.h"
33 #include "lib/audio_content.h"
34 #include "lib/string_text_file_content.h"
35 #include "lib/video_content.h"
36 #include "lib/dcp_content.h"
37 #include "lib/ffmpeg_content.h"
38 #include <dcp/locale_convert.h>
39 #include <boost/foreach.hpp>
40 #include <set>
41 #include <iostream>
42
43 using std::cout;
44 using std::string;
45 using std::set;
46 using boost::shared_ptr;
47 using boost::weak_ptr;
48 using boost::dynamic_pointer_cast;
49 using boost::optional;
50 using dcp::locale_convert;
51
52 TimingPanel::TimingPanel (ContentPanel* p, weak_ptr<FilmViewer> viewer)
53         /* horrid hack for apparent lack of context support with wxWidgets i18n code */
54         /// TRANSLATORS: translate the word "Timing" here; do not include the "Timing|" prefix
55         : ContentSubPanel (p, S_("Timing|Timing"))
56         , _viewer (viewer)
57 {
58         wxSize size = TimecodeBase::size (this);
59
60         for (int i = 0; i < 3; ++i) {
61                 _colon[i] = create_label (this, wxT(":"), false);
62         }
63
64         //// TRANSLATORS: this is an abbreviation for "hours"
65         _h_label = new StaticText (this, _("h"), wxDefaultPosition, size, wxALIGN_CENTRE_HORIZONTAL);
66 #ifdef DCPOMATIC_LINUX
67         /* Hack to work around failure to centre text on GTK */
68         gtk_label_set_line_wrap (GTK_LABEL(_h_label->GetHandle()), FALSE);
69 #endif
70         //// TRANSLATORS: this is an abbreviation for "minutes"
71         _m_label = new StaticText (this, _("m"), wxDefaultPosition, size, wxALIGN_CENTRE_HORIZONTAL);
72 #ifdef DCPOMATIC_LINUX
73         gtk_label_set_line_wrap (GTK_LABEL (_m_label->GetHandle()), FALSE);
74 #endif
75         //// TRANSLATORS: this is an abbreviation for "seconds"
76         _s_label = new StaticText (this, _("s"), wxDefaultPosition, size, wxALIGN_CENTRE_HORIZONTAL);
77 #ifdef DCPOMATIC_LINUX
78         gtk_label_set_line_wrap (GTK_LABEL(_s_label->GetHandle()), FALSE);
79 #endif
80         //// TRANSLATORS: this is an abbreviation for "frames"
81         _f_label = new StaticText (this, _("f"), wxDefaultPosition, size, wxALIGN_CENTRE_HORIZONTAL);
82 #ifdef DCPOMATIC_LINUX
83         gtk_label_set_line_wrap (GTK_LABEL(_f_label->GetHandle()), FALSE);
84 #endif
85
86         _position_label = create_label (this, _("Position"), true);
87         _position = new Timecode<DCPTime> (this);
88         _move_to_start_of_reel = new Button (this, _("Move to start of reel"));
89         _full_length_label = create_label (this, _("Full length"), true);
90         _full_length = new Timecode<DCPTime> (this);
91         _trim_start_label = create_label (this, _("Trim from start"), true);
92         _trim_start = new Timecode<ContentTime> (this);
93         _trim_start_to_playhead = new Button (this, _("Trim up to current position"));
94         _trim_end_label = create_label (this, _("Trim from end"), true);
95         _trim_end = new Timecode<ContentTime> (this);
96         _trim_end_to_playhead = new Button (this, _("Trim after current position"));
97         _play_length_label = create_label (this, _("Play length"), true);
98         _play_length = new Timecode<DCPTime> (this);
99
100         _video_frame_rate_label = create_label (this, _("Video frame rate"), true);
101         _video_frame_rate = new wxTextCtrl (this, wxID_ANY);
102         _set_video_frame_rate = new Button (this, _("Set"));
103         _set_video_frame_rate->Enable (false);
104
105         /* We can't use Wrap() here as it doesn't work with markup:
106          * http://trac.wxwidgets.org/ticket/13389
107          */
108
109         wxString in = _("<i>Only change this if the content's frame rate has been read incorrectly.</i>");
110         wxString out;
111         int const width = 20;
112         int current = 0;
113         for (size_t i = 0; i < in.Length(); ++i) {
114                 if (in[i] == ' ' && current >= width) {
115                         out += '\n';
116                         current = 0;
117                 } else {
118                         out += in[i];
119                         ++current;
120                 }
121         }
122
123         _tip = new StaticText (this, wxT (""));
124         _tip->SetLabelMarkup (out);
125 #ifdef DCPOMATIC_OSX
126         /* Hack to stop hidden text on some versions of OS X */
127         _tip->SetMinSize (wxSize (-1, 256));
128 #endif
129
130         _position->Changed.connect    (boost::bind (&TimingPanel::position_changed, this));
131         _move_to_start_of_reel->Bind  (wxEVT_BUTTON, boost::bind (&TimingPanel::move_to_start_of_reel_clicked, this));
132         _full_length->Changed.connect (boost::bind (&TimingPanel::full_length_changed, this));
133         _trim_start->Changed.connect  (boost::bind (&TimingPanel::trim_start_changed, this));
134         _trim_start_to_playhead->Bind (wxEVT_BUTTON, boost::bind (&TimingPanel::trim_start_to_playhead_clicked, this));
135         _trim_end->Changed.connect    (boost::bind (&TimingPanel::trim_end_changed, this));
136         _trim_end_to_playhead->Bind   (wxEVT_BUTTON, boost::bind (&TimingPanel::trim_end_to_playhead_clicked, this));
137         _play_length->Changed.connect (boost::bind (&TimingPanel::play_length_changed, this));
138         _video_frame_rate->Bind       (wxEVT_TEXT, boost::bind (&TimingPanel::video_frame_rate_changed, this));
139         _set_video_frame_rate->Bind   (wxEVT_BUTTON, boost::bind (&TimingPanel::set_video_frame_rate, this));
140
141         shared_ptr<FilmViewer> fv = _viewer.lock ();
142         DCPOMATIC_ASSERT (fv);
143         fv->ImageChanged.connect (boost::bind (&TimingPanel::setup_sensitivity, this));
144
145         setup_sensitivity ();
146         add_to_grid ();
147 }
148
149 void
150 TimingPanel::add_to_grid ()
151 {
152         bool const full = Config::instance()->interface_complexity() == Config::INTERFACE_FULL;
153
154         int r = 0;
155
156         wxSizer* labels = new wxBoxSizer (wxHORIZONTAL);
157         labels->Add (_h_label, 1, wxEXPAND);
158         add_label_to_sizer (labels, _colon[0], false);
159         labels->Add (_m_label, 1, wxEXPAND);
160         add_label_to_sizer (labels, _colon[1], false);
161         labels->Add (_s_label, 1, wxEXPAND);
162         add_label_to_sizer (labels, _colon[2], false);
163         labels->Add (_f_label, 1, wxEXPAND);
164         _grid->Add (labels, wxGBPosition(r, 1));
165         ++r;
166
167         add_label_to_sizer (_grid, _position_label, true, wxGBPosition(r, 0));
168         _grid->Add (_position, wxGBPosition(r, 1));
169         ++r;
170
171         _move_to_start_of_reel->Show (full);
172         _full_length_label->Show (full);
173         _full_length->Show (full);
174         _play_length_label->Show (full);
175         _play_length->Show (full);
176         _video_frame_rate_label->Show (full);
177         _video_frame_rate->Show (full);
178         _set_video_frame_rate->Show (full);
179         _tip->Show (full);
180
181         if (full) {
182                 _grid->Add (_move_to_start_of_reel, wxGBPosition(r, 1));
183                 ++r;
184
185                 add_label_to_sizer (_grid, _full_length_label, true, wxGBPosition(r, 0));
186                 _grid->Add (_full_length, wxGBPosition(r, 1));
187                 ++r;
188         }
189
190         add_label_to_sizer (_grid, _trim_start_label, true, wxGBPosition(r, 0));
191         _grid->Add (_trim_start, wxGBPosition(r, 1));
192         ++r;
193
194         _grid->Add (_trim_start_to_playhead, wxGBPosition(r, 1));
195         ++r;
196
197         add_label_to_sizer (_grid, _trim_end_label, true, wxGBPosition(r, 0));
198         _grid->Add (_trim_end, wxGBPosition(r, 1));
199         ++r;
200
201         _grid->Add (_trim_end_to_playhead, wxGBPosition(r, 1));
202         ++r;
203
204         if (full) {
205                 add_label_to_sizer (_grid, _play_length_label, true, wxGBPosition(r, 0));
206                 _grid->Add (_play_length, wxGBPosition(r, 1));
207                 ++r;
208
209                 {
210                         add_label_to_sizer (_grid, _video_frame_rate_label, true, wxGBPosition(r, 0));
211                         wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
212                         s->Add (_video_frame_rate, 1, wxEXPAND);
213                         s->Add (_set_video_frame_rate, 0, wxLEFT | wxRIGHT, 8);
214                         _grid->Add (s, wxGBPosition(r, 1), wxGBSpan(1, 2));
215                 }
216                 ++r;
217
218                 _grid->Add (_tip, wxGBPosition(r, 1), wxGBSpan(1, 2));
219         }
220
221         /* Completely speculative fix for #891 */
222         _grid->Layout ();
223 }
224
225 void
226 TimingPanel::update_full_length ()
227 {
228         set<DCPTime> check;
229         BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
230                 check.insert (i->full_length(_parent->film()));
231         }
232
233         if (check.size() == 1) {
234                 _full_length->set (_parent->selected().front()->full_length(_parent->film()), _parent->film()->video_frame_rate());
235         } else {
236                 _full_length->clear ();
237         }
238 }
239
240 void
241 TimingPanel::update_play_length ()
242 {
243         set<DCPTime> check;
244         BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
245                 check.insert (i->length_after_trim(_parent->film()));
246         }
247
248         if (check.size() == 1) {
249                 _play_length->set (_parent->selected().front()->length_after_trim(_parent->film()), _parent->film()->video_frame_rate());
250         } else {
251                 _play_length->clear ();
252         }
253 }
254
255 void
256 TimingPanel::film_content_changed (int property)
257 {
258         int const film_video_frame_rate = _parent->film()->video_frame_rate ();
259
260         /* Here we check to see if we have exactly one different value of various
261            properties, and fill the controls with that value if so.
262         */
263
264         if (property == ContentProperty::POSITION) {
265
266                 set<DCPTime> check;
267                 BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
268                         check.insert (i->position ());
269                 }
270
271                 if (check.size() == 1) {
272                         _position->set (_parent->selected().front()->position(), film_video_frame_rate);
273                 } else {
274                         _position->clear ();
275                 }
276
277         } else if (
278                 property == ContentProperty::LENGTH ||
279                 property == ContentProperty::VIDEO_FRAME_RATE ||
280                 property == VideoContentProperty::FRAME_TYPE
281                 ) {
282
283                 update_full_length ();
284
285         } else if (property == ContentProperty::TRIM_START) {
286
287                 set<ContentTime> check;
288                 BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
289                         check.insert (i->trim_start ());
290                 }
291
292                 if (check.size() == 1) {
293                         _trim_start->set (_parent->selected().front()->trim_start (), film_video_frame_rate);
294                 } else {
295                         _trim_start->clear ();
296                 }
297
298         } else if (property == ContentProperty::TRIM_END) {
299
300                 set<ContentTime> check;
301                 BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
302                         check.insert (i->trim_end ());
303                 }
304
305                 if (check.size() == 1) {
306                         _trim_end->set (_parent->selected().front()->trim_end (), film_video_frame_rate);
307                 } else {
308                         _trim_end->clear ();
309                 }
310         }
311
312         if (
313                 property == ContentProperty::LENGTH ||
314                 property == ContentProperty::TRIM_START ||
315                 property == ContentProperty::TRIM_END ||
316                 property == ContentProperty::VIDEO_FRAME_RATE ||
317                 property == VideoContentProperty::FRAME_TYPE
318                 ) {
319
320                 update_play_length ();
321         }
322
323         if (property == ContentProperty::VIDEO_FRAME_RATE) {
324                 set<double> check_vc;
325                 shared_ptr<const Content> content;
326                 int count_ac = 0;
327                 int count_sc = 0;
328                 BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
329                         if (i->video && i->video_frame_rate()) {
330                                 check_vc.insert (i->video_frame_rate().get());
331                                 content = i;
332                         }
333                         if (i->audio && i->video_frame_rate()) {
334                                 ++count_ac;
335                                 content = i;
336                         }
337                         if (!i->text.empty() && i->video_frame_rate()) {
338                                 ++count_sc;
339                                 content = i;
340                         }
341
342                 }
343
344                 bool const single_frame_image_content = content && dynamic_pointer_cast<const ImageContent> (content) && content->number_of_paths() == 1;
345
346                 if ((check_vc.size() == 1 || count_ac == 1 || count_sc == 1) && !single_frame_image_content) {
347                         checked_set (_video_frame_rate, locale_convert<string> (content->video_frame_rate().get(), 5));
348                         _video_frame_rate->Enable (true);
349                 } else {
350                         checked_set (_video_frame_rate, wxT (""));
351                         _video_frame_rate->Enable (false);
352                 }
353         }
354
355         bool have_still = false;
356         BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
357                 shared_ptr<const ImageContent> ic = dynamic_pointer_cast<const ImageContent> (i);
358                 if (ic && ic->still ()) {
359                         have_still = true;
360                 }
361         }
362
363         _full_length->set_editable (have_still);
364         _play_length->set_editable (!have_still);
365         _set_video_frame_rate->Enable (false);
366         setup_sensitivity ();
367 }
368
369 void
370 TimingPanel::position_changed ()
371 {
372         DCPTime const pos = _position->get (_parent->film()->video_frame_rate ());
373         BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
374                 i->set_position (_parent->film(), pos);
375         }
376 }
377
378 void
379 TimingPanel::full_length_changed ()
380 {
381         int const vfr = _parent->film()->video_frame_rate ();
382         Frame const len = _full_length->get (vfr).frames_round (vfr);
383         BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
384                 shared_ptr<ImageContent> ic = dynamic_pointer_cast<ImageContent> (i);
385                 if (ic && ic->still ()) {
386                         ic->video->set_length (len);
387                 }
388         }
389 }
390
391 void
392 TimingPanel::trim_start_changed ()
393 {
394         shared_ptr<FilmViewer> fv = _viewer.lock ();
395         if (!fv) {
396                 return;
397         }
398
399         DCPTime const ph = fv->position ();
400
401         fv->set_coalesce_player_changes (true);
402
403         shared_ptr<Content> ref;
404         optional<FrameRateChange> ref_frc;
405         optional<DCPTime> ref_ph;
406
407
408         BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
409                 if (i->position() <= ph && ph < i->end(_parent->film())) {
410                         /* The playhead is in i.  Use it as a reference to work out
411                            where to put the playhead post-trim; we're trying to keep the playhead
412                            at the same frame of content that we're looking at pre-trim.
413                         */
414                         ref = i;
415                         ref_frc = _parent->film()->active_frame_rate_change (i->position ());
416                         ref_ph = ph - i->position() + DCPTime (i->trim_start(), ref_frc.get());
417                 }
418
419                 ContentTime const trim = _trim_start->get (i->video_frame_rate().get_value_or(_parent->film()->video_frame_rate()));
420                 i->set_trim_start (trim);
421         }
422
423         if (ref) {
424                 fv->seek (max(DCPTime(), ref_ph.get() + ref->position() - DCPTime(ref->trim_start(), ref_frc.get())), true);
425         }
426
427         fv->set_coalesce_player_changes (false);
428 }
429
430 void
431 TimingPanel::trim_end_changed ()
432 {
433         shared_ptr<FilmViewer> fv = _viewer.lock ();
434         if (!fv) {
435                 return;
436         }
437
438         fv->set_coalesce_player_changes (true);
439
440         BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
441                 ContentTime const trim = _trim_end->get (i->video_frame_rate().get_value_or(_parent->film()->video_frame_rate()));
442                 i->set_trim_end (trim);
443         }
444
445         /* XXX: maybe playhead-off-the-end-of-the-film should be handled elsewhere */
446         if (fv->position() >= _parent->film()->length()) {
447                 fv->seek (_parent->film()->length() - DCPTime::from_frames(1, _parent->film()->video_frame_rate()), true);
448         }
449
450         fv->set_coalesce_player_changes (true);
451 }
452
453 void
454 TimingPanel::play_length_changed ()
455 {
456         DCPTime const play_length = _play_length->get (_parent->film()->video_frame_rate());
457         BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
458                 FrameRateChange const frc = _parent->film()->active_frame_rate_change (i->position ());
459                 i->set_trim_end (
460                         ContentTime (max(DCPTime(), i->full_length(_parent->film()) - play_length), frc) - i->trim_start()
461                         );
462         }
463 }
464
465 void
466 TimingPanel::video_frame_rate_changed ()
467 {
468         bool enable = true;
469         if (_video_frame_rate->GetValue() == wxT("")) {
470                 /* No frame rate has been entered; if the user clicks "set" now it would unset the video
471                    frame rate in the selected content.  This can't be allowed for some content types.
472                 */
473                 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected()) {
474                         if (
475                                 dynamic_pointer_cast<DCPContent>(i) ||
476                                 dynamic_pointer_cast<FFmpegContent>(i)
477                                 ) {
478                                 enable = false;
479                         }
480                 }
481         }
482
483         _set_video_frame_rate->Enable (enable);
484 }
485
486 void
487 TimingPanel::set_video_frame_rate ()
488 {
489         optional<double> fr;
490         if (_video_frame_rate->GetValue() != wxT("")) {
491                 fr = locale_convert<double> (wx_to_std (_video_frame_rate->GetValue ()));
492         }
493         BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
494                 if (fr) {
495                         i->set_video_frame_rate (*fr);
496                 } else {
497                         i->unset_video_frame_rate ();
498                 }
499         }
500
501         _set_video_frame_rate->Enable (false);
502 }
503
504 void
505 TimingPanel::content_selection_changed ()
506 {
507         setup_sensitivity ();
508
509         film_content_changed (ContentProperty::POSITION);
510         film_content_changed (ContentProperty::LENGTH);
511         film_content_changed (ContentProperty::TRIM_START);
512         film_content_changed (ContentProperty::TRIM_END);
513         film_content_changed (ContentProperty::VIDEO_FRAME_RATE);
514 }
515
516 void
517 TimingPanel::film_changed (Film::Property p)
518 {
519         if (p == Film::VIDEO_FRAME_RATE) {
520                 update_full_length ();
521                 update_play_length ();
522         }
523 }
524
525 void
526 TimingPanel::trim_start_to_playhead_clicked ()
527 {
528         shared_ptr<FilmViewer> fv = _viewer.lock ();
529         if (!fv) {
530                 return;
531         }
532
533         shared_ptr<const Film> film = _parent->film ();
534         DCPTime const ph = fv->position().floor (film->video_frame_rate ());
535         optional<DCPTime> new_ph;
536
537         fv->set_coalesce_player_changes (true);
538
539         BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
540                 if (i->position() < ph && ph < i->end(film)) {
541                         FrameRateChange const frc = film->active_frame_rate_change (i->position());
542                         i->set_trim_start (i->trim_start() + ContentTime (ph - i->position(), frc));
543                         new_ph = i->position ();
544                 }
545         }
546
547         if (new_ph) {
548                 fv->seek (new_ph.get(), true);
549         }
550
551         fv->set_coalesce_player_changes (false);
552 }
553
554 void
555 TimingPanel::trim_end_to_playhead_clicked ()
556 {
557         shared_ptr<FilmViewer> fv = _viewer.lock ();
558         if (!fv) {
559                 return;
560         }
561
562         shared_ptr<const Film> film = _parent->film ();
563         DCPTime const ph = fv->position().floor (film->video_frame_rate ());
564         BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
565                 if (i->position() < ph && ph < i->end(film)) {
566                         FrameRateChange const frc = film->active_frame_rate_change (i->position ());
567                         i->set_trim_end (ContentTime(i->position() + i->full_length(film) - ph - DCPTime::from_frames(1, frc.dcp), frc) - i->trim_start());
568                 }
569         }
570 }
571
572 void
573 TimingPanel::setup_sensitivity ()
574 {
575         bool const e = !_parent->selected().empty ();
576
577         _position->Enable (e);
578         _move_to_start_of_reel->Enable (e);
579         _full_length->Enable (e);
580         _trim_start->Enable (e);
581         _trim_end->Enable (e);
582         _play_length->Enable (e);
583         _video_frame_rate->Enable (e);
584
585         shared_ptr<FilmViewer> fv = _viewer.lock ();
586         DCPOMATIC_ASSERT (fv);
587         DCPTime const ph = fv->position ();
588         bool any_over_ph = false;
589         BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
590                 if (i->position() <= ph && ph < i->end(_parent->film())) {
591                         any_over_ph = true;
592                 }
593         }
594
595         _trim_start_to_playhead->Enable (any_over_ph);
596         _trim_end_to_playhead->Enable (any_over_ph);
597 }
598
599 void
600 TimingPanel::move_to_start_of_reel_clicked ()
601 {
602         /* Find common position of all selected content, if it exists */
603
604         optional<DCPTime> position;
605         BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
606                 if (!position) {
607                         position = i->position();
608                 } else {
609                         if (position.get() != i->position()) {
610                                 position.reset ();
611                                 break;
612                         }
613                 }
614         }
615
616         MoveToDialog* d = new MoveToDialog (this, position, _parent->film());
617
618         if (d->ShowModal() == wxID_OK) {
619                 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected()) {
620                         i->set_position (_parent->film(), d->position());
621                 }
622         }
623         d->Destroy ();
624 }