2 Copyright (C) 2012-2016 Carl Hetherington <cth@carlh.net>
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.
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.
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.
20 #include "timing_panel.h"
22 #include "film_viewer.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/audio_content.h"
31 #include "lib/text_subtitle_content.h"
32 #include <boost/foreach.hpp>
39 using boost::shared_ptr;
40 using boost::dynamic_pointer_cast;
41 using boost::optional;
43 TimingPanel::TimingPanel (ContentPanel* p, FilmViewer* viewer)
44 /* horrid hack for apparent lack of context support with wxWidgets i18n code */
45 /// TRANSLATORS: translate the word "Timing" here; do not include the "Timing|" prefix
46 : ContentSubPanel (p, S_("Timing|Timing"))
49 wxFlexGridSizer* grid = new wxFlexGridSizer (2, 4, 4);
50 _sizer->Add (grid, 0, wxALL, 8);
52 wxSize size = TimecodeBase::size (this);
54 wxSizer* labels = new wxBoxSizer (wxHORIZONTAL);
55 //// TRANSLATORS: this is an abbreviation for "hours"
56 wxStaticText* t = new wxStaticText (this, wxID_ANY, _("h"), wxDefaultPosition, size, wxALIGN_CENTRE_HORIZONTAL);
57 #ifdef DCPOMATIC_LINUX
58 /* Hack to work around failure to centre text on GTK */
59 gtk_label_set_line_wrap (GTK_LABEL (t->GetHandle()), FALSE);
61 labels->Add (t, 1, wxEXPAND);
62 add_label_to_sizer (labels, this, wxT (":"), false);
63 //// TRANSLATORS: this is an abbreviation for "minutes"
64 t = new wxStaticText (this, wxID_ANY, _("m"), wxDefaultPosition, size, wxALIGN_CENTRE_HORIZONTAL);
65 #ifdef DCPOMATIC_LINUX
66 gtk_label_set_line_wrap (GTK_LABEL (t->GetHandle()), FALSE);
68 labels->Add (t, 1, wxEXPAND);
69 add_label_to_sizer (labels, this, wxT (":"), false);
70 //// TRANSLATORS: this is an abbreviation for "seconds"
71 t = new wxStaticText (this, wxID_ANY, _("s"), wxDefaultPosition, size, wxALIGN_CENTRE_HORIZONTAL);
72 #ifdef DCPOMATIC_LINUX
73 gtk_label_set_line_wrap (GTK_LABEL (t->GetHandle()), FALSE);
75 labels->Add (t, 1, wxEXPAND);
76 add_label_to_sizer (labels, this, wxT (":"), false);
77 //// TRANSLATORS: this is an abbreviation for "frames"
78 t = new wxStaticText (this, wxID_ANY, _("f"), wxDefaultPosition, size, wxALIGN_CENTRE_HORIZONTAL);
79 #ifdef DCPOMATIC_LINUX
80 gtk_label_set_line_wrap (GTK_LABEL (t->GetHandle()), FALSE);
82 labels->Add (t, 1, wxEXPAND);
83 grid->Add (new wxStaticText (this, wxID_ANY, wxT ("")));
86 add_label_to_sizer (grid, this, _("Position"), true);
87 _position = new Timecode<DCPTime> (this);
88 grid->Add (_position);
89 add_label_to_sizer (grid, this, _("Full length"), true);
90 _full_length = new Timecode<DCPTime> (this);
91 grid->Add (_full_length);
92 add_label_to_sizer (grid, this, _("Trim from start"), true);
93 _trim_start = new Timecode<ContentTime> (this);
94 grid->Add (_trim_start);
95 _trim_start_to_playhead = new wxButton (this, wxID_ANY, _("Trim up to current position"));
97 grid->Add (_trim_start_to_playhead);
98 add_label_to_sizer (grid, this, _("Trim from end"), true);
99 _trim_end = new Timecode<ContentTime> (this);
100 grid->Add (_trim_end);
101 _trim_end_to_playhead = new wxButton (this, wxID_ANY, _("Trim after current position"));
103 grid->Add (_trim_end_to_playhead);
104 add_label_to_sizer (grid, this, _("Play length"), true);
105 _play_length = new Timecode<DCPTime> (this);
106 grid->Add (_play_length);
109 add_label_to_sizer (grid, this, _("Video frame rate"), true);
110 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
111 _video_frame_rate = new wxTextCtrl (this, wxID_ANY);
112 s->Add (_video_frame_rate, 1, wxEXPAND);
113 _set_video_frame_rate = new wxButton (this, wxID_ANY, _("Set"));
114 _set_video_frame_rate->Enable (false);
115 s->Add (_set_video_frame_rate, 0, wxLEFT | wxRIGHT, 8);
116 grid->Add (s, 1, wxEXPAND);
121 /* We can't use Wrap() here as it doesn't work with markup:
122 * http://trac.wxwidgets.org/ticket/13389
125 wxString in = _("<i>Only change this if it the content's frame rate has been read incorrectly.</i>");
127 int const width = 20;
129 for (size_t i = 0; i < in.Length(); ++i) {
130 if (in[i] == ' ' && current >= width) {
139 t = new wxStaticText (this, wxID_ANY, wxT (""));
140 t->SetLabelMarkup (out);
141 grid->Add (t, 0, wxALIGN_CENTER_VERTICAL | wxLEFT | wxRIGHT, 6);
143 _position->Changed.connect (boost::bind (&TimingPanel::position_changed, this));
144 _full_length->Changed.connect (boost::bind (&TimingPanel::full_length_changed, this));
145 _trim_start->Changed.connect (boost::bind (&TimingPanel::trim_start_changed, this));
146 _trim_start_to_playhead->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&TimingPanel::trim_start_to_playhead_clicked, this));
147 _trim_end->Changed.connect (boost::bind (&TimingPanel::trim_end_changed, this));
148 _trim_end_to_playhead->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&TimingPanel::trim_end_to_playhead_clicked, this));
149 _play_length->Changed.connect (boost::bind (&TimingPanel::play_length_changed, this));
150 _video_frame_rate->Bind (wxEVT_COMMAND_TEXT_UPDATED, boost::bind (&TimingPanel::video_frame_rate_changed, this));
151 _set_video_frame_rate->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&TimingPanel::set_video_frame_rate, this));
153 _viewer->ImageChanged.connect (boost::bind (&TimingPanel::setup_sensitivity, this));
155 setup_sensitivity ();
159 TimingPanel::update_full_length ()
162 BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
163 check.insert (i->full_length ());
166 if (check.size() == 1) {
167 _full_length->set (_parent->selected().front()->full_length (), _parent->film()->video_frame_rate ());
169 _full_length->clear ();
174 TimingPanel::update_play_length ()
177 BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
178 check.insert (i->length_after_trim ());
181 if (check.size() == 1) {
182 _play_length->set (_parent->selected().front()->length_after_trim (), _parent->film()->video_frame_rate ());
184 _play_length->clear ();
189 TimingPanel::film_content_changed (int property)
191 int const film_video_frame_rate = _parent->film()->video_frame_rate ();
193 /* Here we check to see if we have exactly one different value of various
194 properties, and fill the controls with that value if so.
197 if (property == ContentProperty::POSITION) {
200 BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
201 check.insert (i->position ());
204 if (check.size() == 1) {
205 _position->set (_parent->selected().front()->position(), film_video_frame_rate);
211 property == ContentProperty::LENGTH ||
212 property == VideoContentProperty::VIDEO_FRAME_RATE ||
213 property == VideoContentProperty::VIDEO_FRAME_TYPE ||
214 property == AudioContentProperty::AUDIO_VIDEO_FRAME_RATE ||
215 property == SubtitleContentProperty::SUBTITLE_VIDEO_FRAME_RATE
218 update_full_length ();
220 } else if (property == ContentProperty::TRIM_START) {
222 set<ContentTime> check;
223 BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
224 check.insert (i->trim_start ());
227 if (check.size() == 1) {
228 _trim_start->set (_parent->selected().front()->trim_start (), film_video_frame_rate);
230 _trim_start->clear ();
233 } else if (property == ContentProperty::TRIM_END) {
235 set<ContentTime> check;
236 BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
237 check.insert (i->trim_end ());
240 if (check.size() == 1) {
241 _trim_end->set (_parent->selected().front()->trim_end (), film_video_frame_rate);
248 property == ContentProperty::LENGTH ||
249 property == ContentProperty::TRIM_START ||
250 property == ContentProperty::TRIM_END ||
251 property == VideoContentProperty::VIDEO_FRAME_RATE ||
252 property == VideoContentProperty::VIDEO_FRAME_TYPE ||
253 property == AudioContentProperty::AUDIO_VIDEO_FRAME_RATE ||
254 property == SubtitleContentProperty::SUBTITLE_VIDEO_FRAME_RATE
257 update_play_length ();
260 if (property == VideoContentProperty::VIDEO_FRAME_RATE || property == SubtitleContentProperty::SUBTITLE_VIDEO_FRAME_RATE) {
261 set<double> check_vc;
262 shared_ptr<const VideoContent> vc;
264 shared_ptr<const AudioContent> ac;
266 shared_ptr<const SubtitleContent> sc;
267 BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
268 shared_ptr<const VideoContent> vt = dynamic_pointer_cast<const VideoContent> (i);
270 check_vc.insert (vt->video_frame_rate ());
273 shared_ptr<const AudioContent> at = dynamic_pointer_cast<const AudioContent> (i);
278 shared_ptr<const SubtitleContent> st = dynamic_pointer_cast<const SubtitleContent> (i);
286 bool const single_frame_image_content = vc && dynamic_pointer_cast<const ImageContent> (vc) && vc->number_of_paths() == 1;
288 if ((check_vc.size() == 1 || count_ac == 1 || count_sc == 1) && !single_frame_image_content) {
290 checked_set (_video_frame_rate, raw_convert<string> (vc->video_frame_rate (), 5));
292 checked_set (_video_frame_rate, raw_convert<string> (ac->audio_video_frame_rate (), 5));
294 checked_set (_video_frame_rate, raw_convert<string> (sc->subtitle_video_frame_rate (), 5));
296 _video_frame_rate->Enable (true);
298 checked_set (_video_frame_rate, wxT (""));
299 _video_frame_rate->Enable (false);
303 bool have_still = false;
304 BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
305 shared_ptr<const ImageContent> ic = dynamic_pointer_cast<const ImageContent> (i);
306 if (ic && ic->still ()) {
311 _full_length->set_editable (have_still);
312 _play_length->set_editable (!have_still);
313 _set_video_frame_rate->Enable (false);
314 setup_sensitivity ();
318 TimingPanel::position_changed ()
320 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
321 i->set_position (_position->get (_parent->film()->video_frame_rate ()));
326 TimingPanel::full_length_changed ()
328 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
329 shared_ptr<ImageContent> ic = dynamic_pointer_cast<ImageContent> (i);
330 if (ic && ic->still ()) {
331 int const vfr = _parent->film()->video_frame_rate ();
332 ic->set_video_length (_full_length->get (vfr).frames_round (vfr));
338 TimingPanel::trim_start_changed ()
340 DCPTime const ph = _viewer->position ();
342 _viewer->set_coalesce_player_changes (true);
344 shared_ptr<Content> ref;
345 optional<FrameRateChange> ref_frc;
346 optional<DCPTime> ref_ph;
347 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
348 if (i->position() <= ph && ph < i->end()) {
349 /* The playhead is in i. Use it as a reference to work out
350 where to put the playhead post-trim; we're trying to keep the playhead
351 at the same frame of content that we're looking at pre-trim.
354 ref_frc = _parent->film()->active_frame_rate_change (i->position ());
355 ref_ph = ph - i->position() + DCPTime (i->trim_start(), ref_frc.get());
358 i->set_trim_start (_trim_start->get (_parent->film()->video_frame_rate ()));
362 _viewer->set_position (max (DCPTime(), ref_ph.get() + ref->position() - DCPTime (ref->trim_start(), ref_frc.get())));
365 _viewer->set_coalesce_player_changes (false);
369 TimingPanel::trim_end_changed ()
371 _viewer->set_coalesce_player_changes (true);
373 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
374 i->set_trim_end (_trim_end->get (_parent->film()->video_frame_rate ()));
377 /* XXX: maybe playhead-off-the-end-of-the-film should be handled elsewhere */
378 if (_viewer->position() >= _parent->film()->length()) {
379 _viewer->set_position (_parent->film()->length() - DCPTime::from_frames (1, _parent->film()->video_frame_rate()));
382 _viewer->set_coalesce_player_changes (true);
386 TimingPanel::play_length_changed ()
388 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
389 FrameRateChange const frc = _parent->film()->active_frame_rate_change (i->position ());
391 ContentTime (i->full_length() - _play_length->get (_parent->film()->video_frame_rate()), frc)
398 TimingPanel::video_frame_rate_changed ()
400 _set_video_frame_rate->Enable (true);
404 TimingPanel::set_video_frame_rate ()
406 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
407 shared_ptr<VideoContent> vc = dynamic_pointer_cast<VideoContent> (i);
408 shared_ptr<AudioContent> ac = dynamic_pointer_cast<AudioContent> (i);
409 shared_ptr<DCPSubtitleContent> dsc = dynamic_pointer_cast<DCPSubtitleContent> (i);
410 shared_ptr<TextSubtitleContent> tsc = dynamic_pointer_cast<TextSubtitleContent> (i);
411 double const fr = raw_convert<double> (wx_to_std (_video_frame_rate->GetValue ()));
413 vc->set_video_frame_rate (fr);
415 /* Audio but not video, i.e. SndfileContent */
416 ac->set_audio_video_frame_rate (fr);
418 dsc->set_subtitle_video_frame_rate (fr);
420 tsc->set_subtitle_video_frame_rate (fr);
422 _set_video_frame_rate->Enable (false);
427 TimingPanel::content_selection_changed ()
429 setup_sensitivity ();
431 film_content_changed (ContentProperty::POSITION);
432 film_content_changed (ContentProperty::LENGTH);
433 film_content_changed (ContentProperty::TRIM_START);
434 film_content_changed (ContentProperty::TRIM_END);
435 film_content_changed (VideoContentProperty::VIDEO_FRAME_RATE);
436 film_content_changed (SubtitleContentProperty::SUBTITLE_VIDEO_FRAME_RATE);
437 film_content_changed (AudioContentProperty::AUDIO_VIDEO_FRAME_RATE);
441 TimingPanel::film_changed (Film::Property p)
443 if (p == Film::VIDEO_FRAME_RATE) {
444 update_full_length ();
445 update_play_length ();
450 TimingPanel::trim_start_to_playhead_clicked ()
452 DCPTime const ph = _viewer->position ();
453 optional<DCPTime> new_ph;
455 _viewer->set_coalesce_player_changes (true);
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_start (i->trim_start() + ContentTime (ph - i->position (), frc));
461 new_ph = i->position ();
466 _viewer->set_position (new_ph.get());
469 _viewer->set_coalesce_player_changes (false);
473 TimingPanel::trim_end_to_playhead_clicked ()
475 DCPTime const ph = _viewer->position ();
476 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
477 if (i->position() < ph && ph < i->end ()) {
478 FrameRateChange const frc = _parent->film()->active_frame_rate_change (i->position ());
479 i->set_trim_end (ContentTime (i->position() + i->full_length() - ph - DCPTime::from_frames (1, frc.dcp), frc) - i->trim_start());
485 TimingPanel::setup_sensitivity ()
487 bool const e = !_parent->selected().empty ();
489 _position->Enable (e);
490 _full_length->Enable (e);
491 _trim_start->Enable (e);
492 _trim_end->Enable (e);
493 _play_length->Enable (e);
494 _video_frame_rate->Enable (e);
496 DCPTime const ph = _viewer->position ();
497 bool any_over_ph = false;
498 BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
499 if (i->position() <= ph && ph < i->end()) {
504 _trim_start_to_playhead->Enable (any_over_ph);
505 _trim_end_to_playhead->Enable (any_over_ph);