Allow single-frame image contents to adjust their video frame rates to that of the...
[dcpomatic.git] / src / wx / timing_panel.cc
1 /*
2     Copyright (C) 2012-2015 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include "timing_panel.h"
21 #include "wx_util.h"
22 #include "film_viewer.h"
23 #include "timecode.h"
24 #include "content_panel.h"
25 #include "lib/content.h"
26 #include "lib/image_content.h"
27 #include "lib/raw_convert.h"
28 #include "lib/subtitle_content.h"
29 #include "lib/dcp_subtitle_content.h"
30 #include "lib/subrip_content.h"
31 #include <boost/foreach.hpp>
32 #include <set>
33 #include <iostream>
34
35 using std::cout;
36 using std::string;
37 using std::set;
38 using boost::shared_ptr;
39 using boost::dynamic_pointer_cast;
40
41 TimingPanel::TimingPanel (ContentPanel* p, FilmViewer* viewer)
42         /* horrid hack for apparent lack of context support with wxWidgets i18n code */
43         : ContentSubPanel (p, S_("Timing|Timing"))
44         , _viewer (viewer)
45 {
46         wxFlexGridSizer* grid = new wxFlexGridSizer (2, 4, 4);
47         _sizer->Add (grid, 0, wxALL, 8);
48
49         wxSize size = TimecodeBase::size (this);
50
51         wxSizer* labels = new wxBoxSizer (wxHORIZONTAL);
52         //// TRANSLATORS: this is an abbreviation for "hours"
53         wxStaticText* t = new wxStaticText (this, wxID_ANY, _("h"), wxDefaultPosition, size, wxALIGN_CENTRE_HORIZONTAL);
54 #ifdef DCPOMATIC_LINUX
55         /* Hack to work around failure to centre text on GTK */
56         gtk_label_set_line_wrap (GTK_LABEL (t->GetHandle()), FALSE);
57 #endif
58         labels->Add (t, 1, wxEXPAND);
59         add_label_to_sizer (labels, this, wxT (":"), false);
60         //// TRANSLATORS: this is an abbreviation for "minutes"
61         t = new wxStaticText (this, wxID_ANY, _("m"), wxDefaultPosition, size, wxALIGN_CENTRE_HORIZONTAL);
62 #ifdef DCPOMATIC_LINUX
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 "seconds"
68         t = new wxStaticText (this, wxID_ANY, _("s"), 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 "frames"
75         t = new wxStaticText (this, wxID_ANY, _("f"), 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         grid->Add (new wxStaticText (this, wxID_ANY, wxT ("")));
81         grid->Add (labels);
82
83         add_label_to_sizer (grid, this, _("Position"), true);
84         _position = new Timecode<DCPTime> (this);
85         grid->Add (_position);
86         add_label_to_sizer (grid, this, _("Full length"), true);
87         _full_length = new Timecode<DCPTime> (this);
88         grid->Add (_full_length);
89         add_label_to_sizer (grid, this, _("Trim from start"), true);
90         _trim_start = new Timecode<ContentTime> (this);
91         grid->Add (_trim_start);
92         _trim_start_to_playhead = new wxButton (this, wxID_ANY, _("Trim up to current position"));
93         grid->AddSpacer (0);
94         grid->Add (_trim_start_to_playhead);
95         add_label_to_sizer (grid, this, _("Trim from end"), true);
96         _trim_end = new Timecode<ContentTime> (this);
97         grid->Add (_trim_end);
98         _trim_end_to_playhead = new wxButton (this, wxID_ANY, _("Trim after current position"));
99         grid->AddSpacer (0);
100         grid->Add (_trim_end_to_playhead);
101         add_label_to_sizer (grid, this, _("Play length"), true);
102         _play_length = new Timecode<DCPTime> (this);
103         grid->Add (_play_length);
104
105         {
106                 add_label_to_sizer (grid, this, _("Video frame rate"), true);
107                 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
108                 _video_frame_rate = new wxTextCtrl (this, wxID_ANY);
109                 s->Add (_video_frame_rate, 1, wxEXPAND);
110                 _set_video_frame_rate = new wxButton (this, wxID_ANY, _("Set"));
111                 _set_video_frame_rate->Enable (false);
112                 s->Add (_set_video_frame_rate, 0, wxLEFT | wxRIGHT, 8);
113                 grid->Add (s, 1, wxEXPAND);
114         }
115
116         grid->AddSpacer (0);
117
118         /* We can't use Wrap() here as it doesn't work with markup:
119          * http://trac.wxwidgets.org/ticket/13389
120          */
121
122         wxString in = _("<i>Only change this if it the content's frame rate has been read incorrectly.</i>");
123         wxString out;
124         int const width = 20;
125         int current = 0;
126         for (size_t i = 0; i < in.Length(); ++i) {
127                 if (in[i] == ' ' && current >= width) {
128                         out += '\n';
129                         current = 0;
130                 } else {
131                         out += in[i];
132                         ++current;
133                 }
134         }
135
136         t = new wxStaticText (this, wxID_ANY, wxT (""));
137         t->SetLabelMarkup (out);
138         grid->Add (t, 0, wxALIGN_CENTER_VERTICAL | wxLEFT | wxRIGHT, 6);
139
140         _position->Changed.connect    (boost::bind (&TimingPanel::position_changed, this));
141         _full_length->Changed.connect (boost::bind (&TimingPanel::full_length_changed, this));
142         _trim_start->Changed.connect  (boost::bind (&TimingPanel::trim_start_changed, this));
143         _trim_start_to_playhead->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&TimingPanel::trim_start_to_playhead_clicked, this));
144         _trim_end->Changed.connect    (boost::bind (&TimingPanel::trim_end_changed, this));
145         _trim_end_to_playhead->Bind   (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&TimingPanel::trim_end_to_playhead_clicked, this));
146         _play_length->Changed.connect (boost::bind (&TimingPanel::play_length_changed, this));
147         _video_frame_rate->Bind       (wxEVT_COMMAND_TEXT_UPDATED, boost::bind (&TimingPanel::video_frame_rate_changed, this));
148         _set_video_frame_rate->Bind   (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&TimingPanel::set_video_frame_rate, this));
149 }
150
151 void
152 TimingPanel::update_full_length ()
153 {
154         set<DCPTime> check;
155         BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
156                 check.insert (i->full_length ());
157         }
158
159         if (check.size() == 1) {
160                 _full_length->set (_parent->selected().front()->full_length (), _parent->film()->video_frame_rate ());
161         } else {
162                 _full_length->clear ();
163         }
164 }
165
166 void
167 TimingPanel::update_play_length ()
168 {
169         set<DCPTime> check;
170         BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
171                 check.insert (i->length_after_trim ());
172         }
173
174         if (check.size() == 1) {
175                 _play_length->set (_parent->selected().front()->length_after_trim (), _parent->film()->video_frame_rate ());
176         } else {
177                 _play_length->clear ();
178         }
179 }
180
181 void
182 TimingPanel::film_content_changed (int property)
183 {
184         int const film_video_frame_rate = _parent->film()->video_frame_rate ();
185
186         /* Here we check to see if we have exactly one different value of various
187            properties, and fill the controls with that value if so.
188         */
189
190         if (property == ContentProperty::POSITION) {
191
192                 set<DCPTime> check;
193                 BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
194                         check.insert (i->position ());
195                 }
196
197                 if (check.size() == 1) {
198                         _position->set (_parent->selected().front()->position(), film_video_frame_rate);
199                 } else {
200                         _position->clear ();
201                 }
202
203         } else if (
204                 property == ContentProperty::LENGTH ||
205                 property == VideoContentProperty::VIDEO_FRAME_RATE ||
206                 property == VideoContentProperty::VIDEO_FRAME_TYPE ||
207                 property == SubtitleContentProperty::SUBTITLE_VIDEO_FRAME_RATE
208                 ) {
209
210                 update_full_length ();
211
212         } else if (property == ContentProperty::TRIM_START) {
213
214                 set<ContentTime> check;
215                 BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
216                         check.insert (i->trim_start ());
217                 }
218
219                 if (check.size() == 1) {
220                         _trim_start->set (_parent->selected().front()->trim_start (), film_video_frame_rate);
221                 } else {
222                         _trim_start->clear ();
223                 }
224
225         } else if (property == ContentProperty::TRIM_END) {
226
227                 set<ContentTime> check;
228                 BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
229                         check.insert (i->trim_end ());
230                 }
231
232                 if (check.size() == 1) {
233                         _trim_end->set (_parent->selected().front()->trim_end (), film_video_frame_rate);
234                 } else {
235                         _trim_end->clear ();
236                 }
237         }
238
239         if (
240                 property == ContentProperty::LENGTH ||
241                 property == ContentProperty::TRIM_START ||
242                 property == ContentProperty::TRIM_END ||
243                 property == VideoContentProperty::VIDEO_FRAME_RATE ||
244                 property == VideoContentProperty::VIDEO_FRAME_TYPE ||
245                 property == SubtitleContentProperty::SUBTITLE_VIDEO_FRAME_RATE
246                 ) {
247
248                 update_play_length ();
249         }
250
251         if (property == VideoContentProperty::VIDEO_FRAME_RATE || property == SubtitleContentProperty::SUBTITLE_VIDEO_FRAME_RATE) {
252                 set<double> check_vc;
253                 shared_ptr<const VideoContent> vc;
254                 int count_sc = 0;
255                 shared_ptr<const SubtitleContent> sc;
256                 BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
257                         shared_ptr<const VideoContent> vt = dynamic_pointer_cast<const VideoContent> (i);
258                         if (vt) {
259                                 check_vc.insert (vt->video_frame_rate ());
260                                 vc = vt;
261                         }
262                         shared_ptr<const SubtitleContent> st = dynamic_pointer_cast<const SubtitleContent> (i);
263                         if (st) {
264                                 ++count_sc;
265                                 sc = st;
266                         }
267
268                 }
269
270                 bool const single_frame_image_content = vc && dynamic_pointer_cast<const ImageContent> (vc) && vc->number_of_paths() == 1;
271
272                 if ((check_vc.size() == 1 || count_sc == 1) && !single_frame_image_content) {
273                         if (vc) {
274                                 checked_set (_video_frame_rate, raw_convert<string> (vc->video_frame_rate (), 5));
275                         } else if (sc) {
276                                 checked_set (_video_frame_rate, raw_convert<string> (sc->subtitle_video_frame_rate (), 5));
277                         }
278                         _video_frame_rate->Enable (true);
279                 } else {
280                         checked_set (_video_frame_rate, wxT (""));
281                         _video_frame_rate->Enable (false);
282                 }
283         }
284
285         bool have_still = false;
286         BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
287                 shared_ptr<const ImageContent> ic = dynamic_pointer_cast<const ImageContent> (i);
288                 if (ic && ic->still ()) {
289                         have_still = true;
290                 }
291         }
292
293         _full_length->set_editable (have_still);
294         _play_length->set_editable (!have_still);
295         _set_video_frame_rate->Enable (false);
296 }
297
298 void
299 TimingPanel::position_changed ()
300 {
301         BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
302                 i->set_position (_position->get (_parent->film()->video_frame_rate ()));
303         }
304 }
305
306 void
307 TimingPanel::full_length_changed ()
308 {
309         BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
310                 shared_ptr<ImageContent> ic = dynamic_pointer_cast<ImageContent> (i);
311                 if (ic && ic->still ()) {
312                         int const vfr = _parent->film()->video_frame_rate ();
313                         ic->set_video_length (_full_length->get (vfr).frames_round (vfr));
314                 }
315         }
316 }
317
318 void
319 TimingPanel::trim_start_changed ()
320 {
321         BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
322                 i->set_trim_start (_trim_start->get (_parent->film()->video_frame_rate ()));
323         }
324 }
325
326
327 void
328 TimingPanel::trim_end_changed ()
329 {
330         BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
331                 i->set_trim_end (_trim_end->get (_parent->film()->video_frame_rate ()));
332         }
333 }
334
335 void
336 TimingPanel::play_length_changed ()
337 {
338         BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
339                 FrameRateChange const frc = _parent->film()->active_frame_rate_change (i->position ());
340                 i->set_trim_end (
341                         ContentTime (i->full_length() - _play_length->get (_parent->film()->video_frame_rate()), frc)
342                         - i->trim_start ()
343                         );
344         }
345 }
346
347 void
348 TimingPanel::video_frame_rate_changed ()
349 {
350         _set_video_frame_rate->Enable (true);
351 }
352
353 void
354 TimingPanel::set_video_frame_rate ()
355 {
356         BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
357                 shared_ptr<VideoContent> vc = dynamic_pointer_cast<VideoContent> (i);
358                 shared_ptr<DCPSubtitleContent> dsc = dynamic_pointer_cast<DCPSubtitleContent> (i);
359                 shared_ptr<SubRipContent> ssc = dynamic_pointer_cast<SubRipContent> (i);
360                 if (vc) {
361                         vc->set_video_frame_rate (raw_convert<double> (wx_to_std (_video_frame_rate->GetValue ())));
362                 } else if (dsc) {
363                         dsc->set_subtitle_video_frame_rate (raw_convert<double> (wx_to_std (_video_frame_rate->GetValue ())));
364                 } else if (ssc) {
365                         ssc->set_subtitle_video_frame_rate (raw_convert<double> (wx_to_std (_video_frame_rate->GetValue ())));
366                 }
367                 _set_video_frame_rate->Enable (false);
368         }
369 }
370
371 void
372 TimingPanel::content_selection_changed ()
373 {
374         bool const e = !_parent->selected().empty ();
375
376         _position->Enable (e);
377         _full_length->Enable (e);
378         _trim_start->Enable (e);
379         _trim_end->Enable (e);
380         _play_length->Enable (e);
381         _video_frame_rate->Enable (e);
382
383         film_content_changed (ContentProperty::POSITION);
384         film_content_changed (ContentProperty::LENGTH);
385         film_content_changed (ContentProperty::TRIM_START);
386         film_content_changed (ContentProperty::TRIM_END);
387         film_content_changed (VideoContentProperty::VIDEO_FRAME_RATE);
388         film_content_changed (SubtitleContentProperty::SUBTITLE_VIDEO_FRAME_RATE);
389 }
390
391 void
392 TimingPanel::film_changed (Film::Property p)
393 {
394         if (p == Film::VIDEO_FRAME_RATE) {
395                 update_full_length ();
396                 update_play_length ();
397         }
398 }
399
400 void
401 TimingPanel::trim_start_to_playhead_clicked ()
402 {
403         DCPTime const ph = _viewer->position ();
404         BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
405                 if (i->position() < ph && ph < i->end ()) {
406                         FrameRateChange const frc = _parent->film()->active_frame_rate_change (i->position ());
407                         i->set_trim_start (i->trim_start() + ContentTime (ph - i->position (), frc));
408                 }
409         }
410 }
411
412 void
413 TimingPanel::trim_end_to_playhead_clicked ()
414 {
415         DCPTime const ph = _viewer->position ();
416         BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
417                 if (i->position() < ph && ph < i->end ()) {
418                         FrameRateChange const frc = _parent->film()->active_frame_rate_change (i->position ());
419                         i->set_trim_end (ContentTime (i->position() + i->full_length() - ph, frc) - i->trim_start());
420                 }
421
422         }
423 }