2 Copyright (C) 2012-2018 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
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.
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.
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/>.
21 #include "timing_panel.h"
23 #include "film_viewer.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>
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 using namespace dcpomatic;
53 TimingPanel::TimingPanel (ContentPanel* p, weak_ptr<FilmViewer> viewer)
54 /* horrid hack for apparent lack of context support with wxWidgets i18n code */
55 /// TRANSLATORS: translate the word "Timing" here; do not include the "Timing|" prefix
56 : ContentSubPanel (p, S_("Timing|Timing"))
59 wxSize size = TimecodeBase::size (this);
61 for (int i = 0; i < 3; ++i) {
62 _colon[i] = create_label (this, wxT(":"), false);
65 //// TRANSLATORS: this is an abbreviation for "hours"
66 _h_label = new StaticText (this, _("h"), wxDefaultPosition, size, wxALIGN_CENTRE_HORIZONTAL);
67 #ifdef DCPOMATIC_LINUX
68 /* Hack to work around failure to centre text on GTK */
69 gtk_label_set_line_wrap (GTK_LABEL(_h_label->GetHandle()), FALSE);
71 //// TRANSLATORS: this is an abbreviation for "minutes"
72 _m_label = new StaticText (this, _("m"), wxDefaultPosition, size, wxALIGN_CENTRE_HORIZONTAL);
73 #ifdef DCPOMATIC_LINUX
74 gtk_label_set_line_wrap (GTK_LABEL (_m_label->GetHandle()), FALSE);
76 //// TRANSLATORS: this is an abbreviation for "seconds"
77 _s_label = new StaticText (this, _("s"), wxDefaultPosition, size, wxALIGN_CENTRE_HORIZONTAL);
78 #ifdef DCPOMATIC_LINUX
79 gtk_label_set_line_wrap (GTK_LABEL(_s_label->GetHandle()), FALSE);
81 //// TRANSLATORS: this is an abbreviation for "frames"
82 _f_label = new StaticText (this, _("f"), wxDefaultPosition, size, wxALIGN_CENTRE_HORIZONTAL);
83 #ifdef DCPOMATIC_LINUX
84 gtk_label_set_line_wrap (GTK_LABEL(_f_label->GetHandle()), FALSE);
87 _position_label = create_label (this, _("Position"), true);
88 _position = new Timecode<DCPTime> (this);
89 _move_to_start_of_reel = new Button (this, _("Move to start of reel"));
90 _full_length_label = create_label (this, _("Full length"), true);
91 _full_length = new Timecode<DCPTime> (this);
92 _trim_start_label = create_label (this, _("Trim from start"), true);
93 _trim_start = new Timecode<ContentTime> (this);
94 _trim_start_to_playhead = new Button (this, _("Trim up to current position"));
95 _trim_end_label = create_label (this, _("Trim from end"), true);
96 _trim_end = new Timecode<ContentTime> (this);
97 _trim_end_to_playhead = new Button (this, _("Trim after current position"));
98 _play_length_label = create_label (this, _("Play length"), true);
99 _play_length = new Timecode<DCPTime> (this);
101 _video_frame_rate_label = create_label (this, _("Video frame rate"), true);
102 _video_frame_rate = new wxTextCtrl (this, wxID_ANY);
103 _set_video_frame_rate = new Button (this, _("Set"));
104 _set_video_frame_rate->Enable (false);
106 /* We can't use Wrap() here as it doesn't work with markup:
107 * http://trac.wxwidgets.org/ticket/13389
110 wxString in = _("<i>Only change this if the content's frame rate has been read incorrectly.</i>");
112 int const width = 20;
114 for (size_t i = 0; i < in.Length(); ++i) {
115 if (in[i] == ' ' && current >= width) {
124 _tip = new StaticText (this, wxT (""));
125 _tip->SetLabelMarkup (out);
127 /* Hack to stop hidden text on some versions of OS X */
128 _tip->SetMinSize (wxSize (-1, 256));
131 _position->Changed.connect (boost::bind (&TimingPanel::position_changed, this));
132 _move_to_start_of_reel->Bind (wxEVT_BUTTON, boost::bind (&TimingPanel::move_to_start_of_reel_clicked, this));
133 _full_length->Changed.connect (boost::bind (&TimingPanel::full_length_changed, this));
134 _trim_start->Changed.connect (boost::bind (&TimingPanel::trim_start_changed, this));
135 _trim_start_to_playhead->Bind (wxEVT_BUTTON, boost::bind (&TimingPanel::trim_start_to_playhead_clicked, this));
136 _trim_end->Changed.connect (boost::bind (&TimingPanel::trim_end_changed, this));
137 _trim_end_to_playhead->Bind (wxEVT_BUTTON, boost::bind (&TimingPanel::trim_end_to_playhead_clicked, this));
138 _play_length->Changed.connect (boost::bind (&TimingPanel::play_length_changed, this));
139 _video_frame_rate->Bind (wxEVT_TEXT, boost::bind (&TimingPanel::video_frame_rate_changed, this));
140 _set_video_frame_rate->Bind (wxEVT_BUTTON, boost::bind (&TimingPanel::set_video_frame_rate, this));
142 shared_ptr<FilmViewer> fv = _viewer.lock ();
143 DCPOMATIC_ASSERT (fv);
144 fv->ImageChanged.connect (boost::bind (&TimingPanel::setup_sensitivity, this));
146 setup_sensitivity ();
151 TimingPanel::add_to_grid ()
153 bool const full = Config::instance()->interface_complexity() == Config::INTERFACE_FULL;
157 wxSizer* labels = new wxBoxSizer (wxHORIZONTAL);
158 labels->Add (_h_label, 1, wxEXPAND);
159 add_label_to_sizer (labels, _colon[0], false);
160 labels->Add (_m_label, 1, wxEXPAND);
161 add_label_to_sizer (labels, _colon[1], false);
162 labels->Add (_s_label, 1, wxEXPAND);
163 add_label_to_sizer (labels, _colon[2], false);
164 labels->Add (_f_label, 1, wxEXPAND);
165 _grid->Add (labels, wxGBPosition(r, 1));
168 add_label_to_sizer (_grid, _position_label, true, wxGBPosition(r, 0));
169 _grid->Add (_position, wxGBPosition(r, 1));
172 _move_to_start_of_reel->Show (full);
173 _full_length_label->Show (full);
174 _full_length->Show (full);
175 _play_length_label->Show (full);
176 _play_length->Show (full);
177 _video_frame_rate_label->Show (full);
178 _video_frame_rate->Show (full);
179 _set_video_frame_rate->Show (full);
183 _grid->Add (_move_to_start_of_reel, wxGBPosition(r, 1));
186 add_label_to_sizer (_grid, _full_length_label, true, wxGBPosition(r, 0));
187 _grid->Add (_full_length, wxGBPosition(r, 1));
191 add_label_to_sizer (_grid, _trim_start_label, true, wxGBPosition(r, 0));
192 _grid->Add (_trim_start, wxGBPosition(r, 1));
195 _grid->Add (_trim_start_to_playhead, wxGBPosition(r, 1));
198 add_label_to_sizer (_grid, _trim_end_label, true, wxGBPosition(r, 0));
199 _grid->Add (_trim_end, wxGBPosition(r, 1));
202 _grid->Add (_trim_end_to_playhead, wxGBPosition(r, 1));
206 add_label_to_sizer (_grid, _play_length_label, true, wxGBPosition(r, 0));
207 _grid->Add (_play_length, wxGBPosition(r, 1));
211 add_label_to_sizer (_grid, _video_frame_rate_label, true, wxGBPosition(r, 0));
212 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
213 s->Add (_video_frame_rate, 1, wxEXPAND);
214 s->Add (_set_video_frame_rate, 0, wxLEFT | wxRIGHT, 8);
215 _grid->Add (s, wxGBPosition(r, 1), wxGBSpan(1, 2));
219 _grid->Add (_tip, wxGBPosition(r, 1), wxGBSpan(1, 2));
222 /* Completely speculative fix for #891 */
227 TimingPanel::update_full_length ()
230 BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
231 check.insert (i->full_length(_parent->film()));
234 if (check.size() == 1) {
235 _full_length->set (_parent->selected().front()->full_length(_parent->film()), _parent->film()->video_frame_rate());
237 _full_length->clear ();
242 TimingPanel::update_play_length ()
245 BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
246 check.insert (i->length_after_trim(_parent->film()));
249 if (check.size() == 1) {
250 _play_length->set (_parent->selected().front()->length_after_trim(_parent->film()), _parent->film()->video_frame_rate());
252 _play_length->clear ();
257 TimingPanel::film_content_changed (int property)
259 int const film_video_frame_rate = _parent->film()->video_frame_rate ();
261 /* Here we check to see if we have exactly one different value of various
262 properties, and fill the controls with that value if so.
265 if (property == ContentProperty::POSITION) {
268 BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
269 check.insert (i->position ());
272 if (check.size() == 1) {
273 _position->set (_parent->selected().front()->position(), film_video_frame_rate);
279 property == ContentProperty::LENGTH ||
280 property == ContentProperty::VIDEO_FRAME_RATE ||
281 property == VideoContentProperty::FRAME_TYPE
284 update_full_length ();
286 } else if (property == ContentProperty::TRIM_START) {
288 set<ContentTime> check;
289 BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
290 check.insert (i->trim_start ());
293 if (check.size() == 1) {
294 _trim_start->set (_parent->selected().front()->trim_start (), film_video_frame_rate);
296 _trim_start->clear ();
299 } else if (property == ContentProperty::TRIM_END) {
301 set<ContentTime> check;
302 BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
303 check.insert (i->trim_end ());
306 if (check.size() == 1) {
307 _trim_end->set (_parent->selected().front()->trim_end (), film_video_frame_rate);
314 property == ContentProperty::LENGTH ||
315 property == ContentProperty::TRIM_START ||
316 property == ContentProperty::TRIM_END ||
317 property == ContentProperty::VIDEO_FRAME_RATE ||
318 property == VideoContentProperty::FRAME_TYPE
321 update_play_length ();
324 if (property == ContentProperty::VIDEO_FRAME_RATE) {
325 set<double> check_vc;
326 shared_ptr<const Content> content;
329 BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
330 if (i->video && i->video_frame_rate()) {
331 check_vc.insert (i->video_frame_rate().get());
334 if (i->audio && i->video_frame_rate()) {
338 if (!i->text.empty() && i->video_frame_rate()) {
345 bool const single_frame_image_content = content && dynamic_pointer_cast<const ImageContent> (content) && content->number_of_paths() == 1;
347 if ((check_vc.size() == 1 || count_ac == 1 || count_sc == 1) && !single_frame_image_content) {
348 checked_set (_video_frame_rate, locale_convert<string> (content->video_frame_rate().get(), 5));
349 _video_frame_rate->Enable (true);
351 checked_set (_video_frame_rate, wxT (""));
352 _video_frame_rate->Enable (false);
356 bool have_still = false;
357 BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
358 shared_ptr<const ImageContent> ic = dynamic_pointer_cast<const ImageContent> (i);
359 if (ic && ic->still ()) {
364 _full_length->set_editable (have_still);
365 _play_length->set_editable (!have_still);
366 _set_video_frame_rate->Enable (false);
367 setup_sensitivity ();
371 TimingPanel::position_changed ()
373 DCPTime const pos = _position->get (_parent->film()->video_frame_rate ());
374 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
375 i->set_position (_parent->film(), pos);
380 TimingPanel::full_length_changed ()
382 int const vfr = _parent->film()->video_frame_rate ();
383 Frame const len = _full_length->get (vfr).frames_round (vfr);
384 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
385 shared_ptr<ImageContent> ic = dynamic_pointer_cast<ImageContent> (i);
386 if (ic && ic->still ()) {
387 ic->video->set_length (len);
393 TimingPanel::trim_start_changed ()
395 shared_ptr<FilmViewer> fv = _viewer.lock ();
400 DCPTime const ph = fv->position ();
402 fv->set_coalesce_player_changes (true);
404 shared_ptr<Content> ref;
405 optional<FrameRateChange> ref_frc;
406 optional<DCPTime> ref_ph;
409 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
410 if (i->position() <= ph && ph < i->end(_parent->film())) {
411 /* The playhead is in i. Use it as a reference to work out
412 where to put the playhead post-trim; we're trying to keep the playhead
413 at the same frame of content that we're looking at pre-trim.
416 ref_frc = _parent->film()->active_frame_rate_change (i->position ());
417 ref_ph = ph - i->position() + DCPTime (i->trim_start(), ref_frc.get());
420 ContentTime const trim = _trim_start->get (i->video_frame_rate().get_value_or(_parent->film()->video_frame_rate()));
421 i->set_trim_start (trim);
425 fv->seek (max(DCPTime(), ref_ph.get() + ref->position() - DCPTime(ref->trim_start(), ref_frc.get())), true);
428 fv->set_coalesce_player_changes (false);
432 TimingPanel::trim_end_changed ()
434 shared_ptr<FilmViewer> fv = _viewer.lock ();
439 fv->set_coalesce_player_changes (true);
441 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
442 ContentTime const trim = _trim_end->get (i->video_frame_rate().get_value_or(_parent->film()->video_frame_rate()));
443 i->set_trim_end (trim);
446 /* XXX: maybe playhead-off-the-end-of-the-film should be handled elsewhere */
447 if (fv->position() >= _parent->film()->length()) {
448 fv->seek (_parent->film()->length() - DCPTime::from_frames(1, _parent->film()->video_frame_rate()), true);
451 fv->set_coalesce_player_changes (false);
455 TimingPanel::play_length_changed ()
457 DCPTime const play_length = _play_length->get (_parent->film()->video_frame_rate());
458 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
459 FrameRateChange const frc = _parent->film()->active_frame_rate_change (i->position ());
461 ContentTime (max(DCPTime(), i->full_length(_parent->film()) - play_length), frc) - i->trim_start()
467 TimingPanel::video_frame_rate_changed ()
470 if (_video_frame_rate->GetValue() == wxT("")) {
471 /* No frame rate has been entered; if the user clicks "set" now it would unset the video
472 frame rate in the selected content. This can't be allowed for some content types.
474 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected()) {
476 dynamic_pointer_cast<DCPContent>(i) ||
477 dynamic_pointer_cast<FFmpegContent>(i)
484 _set_video_frame_rate->Enable (enable);
488 TimingPanel::set_video_frame_rate ()
491 if (_video_frame_rate->GetValue() != wxT("")) {
492 fr = locale_convert<double> (wx_to_std (_video_frame_rate->GetValue ()));
494 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
496 i->set_video_frame_rate (*fr);
498 i->unset_video_frame_rate ();
502 _set_video_frame_rate->Enable (false);
506 TimingPanel::content_selection_changed ()
508 setup_sensitivity ();
510 film_content_changed (ContentProperty::POSITION);
511 film_content_changed (ContentProperty::LENGTH);
512 film_content_changed (ContentProperty::TRIM_START);
513 film_content_changed (ContentProperty::TRIM_END);
514 film_content_changed (ContentProperty::VIDEO_FRAME_RATE);
518 TimingPanel::film_changed (Film::Property p)
520 if (p == Film::VIDEO_FRAME_RATE) {
521 update_full_length ();
522 update_play_length ();
527 TimingPanel::trim_start_to_playhead_clicked ()
529 shared_ptr<FilmViewer> fv = _viewer.lock ();
534 shared_ptr<const Film> film = _parent->film ();
535 DCPTime const ph = fv->position().floor (film->video_frame_rate ());
536 optional<DCPTime> new_ph;
538 fv->set_coalesce_player_changes (true);
540 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
541 if (i->position() < ph && ph < i->end(film)) {
542 FrameRateChange const frc = film->active_frame_rate_change (i->position());
543 i->set_trim_start (i->trim_start() + ContentTime (ph - i->position(), frc));
544 new_ph = i->position ();
549 fv->seek (new_ph.get(), true);
552 fv->set_coalesce_player_changes (false);
556 TimingPanel::trim_end_to_playhead_clicked ()
558 shared_ptr<FilmViewer> fv = _viewer.lock ();
563 shared_ptr<const Film> film = _parent->film ();
564 DCPTime const ph = fv->position().floor (film->video_frame_rate ());
565 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
566 if (i->position() < ph && ph < i->end(film)) {
567 FrameRateChange const frc = film->active_frame_rate_change (i->position ());
568 i->set_trim_end (ContentTime(i->position() + i->full_length(film) - ph - DCPTime::from_frames(1, frc.dcp), frc) - i->trim_start());
574 TimingPanel::setup_sensitivity ()
576 bool const e = !_parent->selected().empty ();
578 _position->Enable (e);
579 _move_to_start_of_reel->Enable (e);
580 _full_length->Enable (e);
581 _trim_start->Enable (e);
582 _trim_end->Enable (e);
583 _play_length->Enable (e);
584 _video_frame_rate->Enable (e);
586 shared_ptr<FilmViewer> fv = _viewer.lock ();
587 DCPOMATIC_ASSERT (fv);
588 DCPTime const ph = fv->position ();
589 bool any_over_ph = false;
590 BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
591 if (i->position() <= ph && ph < i->end(_parent->film())) {
596 _trim_start_to_playhead->Enable (any_over_ph);
597 _trim_end_to_playhead->Enable (any_over_ph);
601 TimingPanel::move_to_start_of_reel_clicked ()
603 /* Find common position of all selected content, if it exists */
605 optional<DCPTime> position;
606 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
608 position = i->position();
610 if (position.get() != i->position()) {
617 MoveToDialog* d = new MoveToDialog (this, position, _parent->film());
619 if (d->ShowModal() == wxID_OK) {
620 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected()) {
621 i->set_position (_parent->film(), d->position());