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