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