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 <dcp/locale_convert.h>
37 #include <boost/foreach.hpp>
44 using boost::shared_ptr;
45 using boost::weak_ptr;
46 using boost::dynamic_pointer_cast;
47 using boost::optional;
48 using dcp::locale_convert;
50 TimingPanel::TimingPanel (ContentPanel* p, weak_ptr<FilmViewer> viewer)
51 /* horrid hack for apparent lack of context support with wxWidgets i18n code */
52 /// TRANSLATORS: translate the word "Timing" here; do not include the "Timing|" prefix
53 : ContentSubPanel (p, S_("Timing|Timing"))
56 wxSize size = TimecodeBase::size (this);
58 for (int i = 0; i < 3; ++i) {
59 _colon[i] = create_label (this, wxT(":"), false);
62 //// TRANSLATORS: this is an abbreviation for "hours"
63 _h_label = new StaticText (this, _("h"), wxDefaultPosition, size, wxALIGN_CENTRE_HORIZONTAL);
64 #ifdef DCPOMATIC_LINUX
65 /* Hack to work around failure to centre text on GTK */
66 gtk_label_set_line_wrap (GTK_LABEL(_h_label->GetHandle()), FALSE);
68 //// TRANSLATORS: this is an abbreviation for "minutes"
69 _m_label = new StaticText (this, _("m"), wxDefaultPosition, size, wxALIGN_CENTRE_HORIZONTAL);
70 #ifdef DCPOMATIC_LINUX
71 gtk_label_set_line_wrap (GTK_LABEL (_m_label->GetHandle()), FALSE);
73 //// TRANSLATORS: this is an abbreviation for "seconds"
74 _s_label = new StaticText (this, _("s"), wxDefaultPosition, size, wxALIGN_CENTRE_HORIZONTAL);
75 #ifdef DCPOMATIC_LINUX
76 gtk_label_set_line_wrap (GTK_LABEL(_s_label->GetHandle()), FALSE);
78 //// TRANSLATORS: this is an abbreviation for "frames"
79 _f_label = new StaticText (this, _("f"), wxDefaultPosition, size, wxALIGN_CENTRE_HORIZONTAL);
80 #ifdef DCPOMATIC_LINUX
81 gtk_label_set_line_wrap (GTK_LABEL(_f_label->GetHandle()), FALSE);
84 _position_label = create_label (this, _("Position"), true);
85 _position = new Timecode<DCPTime> (this);
86 _move_to_start_of_reel = new Button (this, _("Move to start of reel"));
87 _full_length_label = create_label (this, _("Full length"), true);
88 _full_length = new Timecode<DCPTime> (this);
89 _trim_start_label = create_label (this, _("Trim from start"), true);
90 _trim_start = new Timecode<ContentTime> (this);
91 _trim_start_to_playhead = new Button (this, _("Trim up to current position"));
92 _trim_end_label = create_label (this, _("Trim from end"), true);
93 _trim_end = new Timecode<ContentTime> (this);
94 _trim_end_to_playhead = new Button (this, _("Trim after current position"));
95 _play_length_label = create_label (this, _("Play length"), true);
96 _play_length = new Timecode<DCPTime> (this);
98 _video_frame_rate_label = create_label (this, _("Video frame rate"), true);
99 _video_frame_rate = new wxTextCtrl (this, wxID_ANY);
100 _set_video_frame_rate = new Button (this, _("Set"));
101 _set_video_frame_rate->Enable (false);
103 /* We can't use Wrap() here as it doesn't work with markup:
104 * http://trac.wxwidgets.org/ticket/13389
107 wxString in = _("<i>Only change this if the content's frame rate has been read incorrectly.</i>");
109 int const width = 20;
111 for (size_t i = 0; i < in.Length(); ++i) {
112 if (in[i] == ' ' && current >= width) {
121 _tip = new StaticText (this, wxT (""));
122 _tip->SetLabelMarkup (out);
124 /* Hack to stop hidden text on some versions of OS X */
125 _tip->SetMinSize (wxSize (-1, 256));
128 _position->Changed.connect (boost::bind (&TimingPanel::position_changed, this));
129 _move_to_start_of_reel->Bind (wxEVT_BUTTON, boost::bind (&TimingPanel::move_to_start_of_reel_clicked, this));
130 _full_length->Changed.connect (boost::bind (&TimingPanel::full_length_changed, this));
131 _trim_start->Changed.connect (boost::bind (&TimingPanel::trim_start_changed, this));
132 _trim_start_to_playhead->Bind (wxEVT_BUTTON, boost::bind (&TimingPanel::trim_start_to_playhead_clicked, this));
133 _trim_end->Changed.connect (boost::bind (&TimingPanel::trim_end_changed, this));
134 _trim_end_to_playhead->Bind (wxEVT_BUTTON, boost::bind (&TimingPanel::trim_end_to_playhead_clicked, this));
135 _play_length->Changed.connect (boost::bind (&TimingPanel::play_length_changed, this));
136 _video_frame_rate->Bind (wxEVT_TEXT, boost::bind (&TimingPanel::video_frame_rate_changed, this));
137 _set_video_frame_rate->Bind (wxEVT_BUTTON, boost::bind (&TimingPanel::set_video_frame_rate, this));
139 shared_ptr<FilmViewer> fv = _viewer.lock ();
140 DCPOMATIC_ASSERT (fv);
141 fv->ImageChanged.connect (boost::bind (&TimingPanel::setup_sensitivity, this));
143 setup_sensitivity ();
148 TimingPanel::add_to_grid ()
150 bool const full = Config::instance()->interface_complexity() == Config::INTERFACE_FULL;
154 wxSizer* labels = new wxBoxSizer (wxHORIZONTAL);
155 labels->Add (_h_label, 1, wxEXPAND);
156 add_label_to_sizer (labels, _colon[0], false);
157 labels->Add (_m_label, 1, wxEXPAND);
158 add_label_to_sizer (labels, _colon[1], false);
159 labels->Add (_s_label, 1, wxEXPAND);
160 add_label_to_sizer (labels, _colon[2], false);
161 labels->Add (_f_label, 1, wxEXPAND);
162 _grid->Add (labels, wxGBPosition(r, 1));
165 add_label_to_sizer (_grid, _position_label, true, wxGBPosition(r, 0));
166 _grid->Add (_position, wxGBPosition(r, 1));
169 _move_to_start_of_reel->Show (full);
170 _full_length_label->Show (full);
171 _full_length->Show (full);
172 _play_length_label->Show (full);
173 _play_length->Show (full);
174 _video_frame_rate_label->Show (full);
175 _video_frame_rate->Show (full);
176 _set_video_frame_rate->Show (full);
180 _grid->Add (_move_to_start_of_reel, wxGBPosition(r, 1));
183 add_label_to_sizer (_grid, _full_length_label, true, wxGBPosition(r, 0));
184 _grid->Add (_full_length, wxGBPosition(r, 1));
188 add_label_to_sizer (_grid, _trim_start_label, true, wxGBPosition(r, 0));
189 _grid->Add (_trim_start, wxGBPosition(r, 1));
192 _grid->Add (_trim_start_to_playhead, wxGBPosition(r, 1));
195 add_label_to_sizer (_grid, _trim_end_label, true, wxGBPosition(r, 0));
196 _grid->Add (_trim_end, wxGBPosition(r, 1));
199 _grid->Add (_trim_end_to_playhead, wxGBPosition(r, 1));
203 add_label_to_sizer (_grid, _play_length_label, true, wxGBPosition(r, 0));
204 _grid->Add (_play_length, wxGBPosition(r, 1));
208 add_label_to_sizer (_grid, _video_frame_rate_label, true, wxGBPosition(r, 0));
209 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
210 s->Add (_video_frame_rate, 1, wxEXPAND);
211 s->Add (_set_video_frame_rate, 0, wxLEFT | wxRIGHT, 8);
212 _grid->Add (s, wxGBPosition(r, 1), wxGBSpan(1, 2));
216 _grid->Add (_tip, wxGBPosition(r, 1), wxGBSpan(1, 2));
219 /* Completely speculative fix for #891 */
224 TimingPanel::update_full_length ()
227 BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
228 check.insert (i->full_length(_parent->film()));
231 if (check.size() == 1) {
232 _full_length->set (_parent->selected().front()->full_length(_parent->film()), _parent->film()->video_frame_rate());
234 _full_length->clear ();
239 TimingPanel::update_play_length ()
242 BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
243 check.insert (i->length_after_trim(_parent->film()));
246 if (check.size() == 1) {
247 _play_length->set (_parent->selected().front()->length_after_trim(_parent->film()), _parent->film()->video_frame_rate());
249 _play_length->clear ();
254 TimingPanel::film_content_changed (int property)
256 int const film_video_frame_rate = _parent->film()->video_frame_rate ();
258 /* Here we check to see if we have exactly one different value of various
259 properties, and fill the controls with that value if so.
262 if (property == ContentProperty::POSITION) {
265 BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
266 check.insert (i->position ());
269 if (check.size() == 1) {
270 _position->set (_parent->selected().front()->position(), film_video_frame_rate);
276 property == ContentProperty::LENGTH ||
277 property == ContentProperty::VIDEO_FRAME_RATE ||
278 property == VideoContentProperty::FRAME_TYPE
281 update_full_length ();
283 } else if (property == ContentProperty::TRIM_START) {
285 set<ContentTime> check;
286 BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
287 check.insert (i->trim_start ());
290 if (check.size() == 1) {
291 _trim_start->set (_parent->selected().front()->trim_start (), film_video_frame_rate);
293 _trim_start->clear ();
296 } else if (property == ContentProperty::TRIM_END) {
298 set<ContentTime> check;
299 BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
300 check.insert (i->trim_end ());
303 if (check.size() == 1) {
304 _trim_end->set (_parent->selected().front()->trim_end (), film_video_frame_rate);
311 property == ContentProperty::LENGTH ||
312 property == ContentProperty::TRIM_START ||
313 property == ContentProperty::TRIM_END ||
314 property == ContentProperty::VIDEO_FRAME_RATE ||
315 property == VideoContentProperty::FRAME_TYPE
318 update_play_length ();
321 if (property == ContentProperty::VIDEO_FRAME_RATE) {
322 set<double> check_vc;
323 shared_ptr<const Content> content;
326 BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
327 if (i->video && i->video_frame_rate()) {
328 check_vc.insert (i->video_frame_rate().get());
331 if (i->audio && i->video_frame_rate()) {
335 if (!i->text.empty() && i->video_frame_rate()) {
342 bool const single_frame_image_content = content && dynamic_pointer_cast<const ImageContent> (content) && content->number_of_paths() == 1;
344 if ((check_vc.size() == 1 || count_ac == 1 || count_sc == 1) && !single_frame_image_content) {
345 checked_set (_video_frame_rate, locale_convert<string> (content->video_frame_rate().get(), 5));
346 _video_frame_rate->Enable (true);
348 checked_set (_video_frame_rate, wxT (""));
349 _video_frame_rate->Enable (false);
353 bool have_still = false;
354 BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
355 shared_ptr<const ImageContent> ic = dynamic_pointer_cast<const ImageContent> (i);
356 if (ic && ic->still ()) {
361 _full_length->set_editable (have_still);
362 _play_length->set_editable (!have_still);
363 _set_video_frame_rate->Enable (false);
364 setup_sensitivity ();
368 TimingPanel::position_changed ()
370 DCPTime const pos = _position->get (_parent->film()->video_frame_rate ());
371 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
372 i->set_position (_parent->film(), pos);
377 TimingPanel::full_length_changed ()
379 int const vfr = _parent->film()->video_frame_rate ();
380 Frame const len = _full_length->get (vfr).frames_round (vfr);
381 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
382 shared_ptr<ImageContent> ic = dynamic_pointer_cast<ImageContent> (i);
383 if (ic && ic->still ()) {
384 ic->video->set_length (len);
390 TimingPanel::trim_start_changed ()
392 shared_ptr<FilmViewer> fv = _viewer.lock ();
397 DCPTime const ph = fv->position ();
399 fv->set_coalesce_player_changes (true);
401 shared_ptr<Content> ref;
402 optional<FrameRateChange> ref_frc;
403 optional<DCPTime> ref_ph;
405 ContentTime const trim = _trim_start->get (_parent->film()->video_frame_rate ());
407 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
408 if (i->position() <= ph && ph < i->end(_parent->film())) {
409 /* The playhead is in i. Use it as a reference to work out
410 where to put the playhead post-trim; we're trying to keep the playhead
411 at the same frame of content that we're looking at pre-trim.
414 ref_frc = _parent->film()->active_frame_rate_change (i->position ());
415 ref_ph = ph - i->position() + DCPTime (i->trim_start(), ref_frc.get());
418 i->set_trim_start (trim);
422 fv->seek (max(DCPTime(), ref_ph.get() + ref->position() - DCPTime(ref->trim_start(), ref_frc.get())), true);
425 fv->set_coalesce_player_changes (false);
429 TimingPanel::trim_end_changed ()
431 shared_ptr<FilmViewer> fv = _viewer.lock ();
436 fv->set_coalesce_player_changes (true);
438 ContentTime const trim = _trim_end->get (_parent->film()->video_frame_rate ());
439 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
440 i->set_trim_end (trim);
443 /* XXX: maybe playhead-off-the-end-of-the-film should be handled elsewhere */
444 if (fv->position() >= _parent->film()->length()) {
445 fv->seek (_parent->film()->length() - DCPTime::from_frames(1, _parent->film()->video_frame_rate()), true);
448 fv->set_coalesce_player_changes (true);
452 TimingPanel::play_length_changed ()
454 DCPTime const play_length = _play_length->get (_parent->film()->video_frame_rate());
455 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
456 FrameRateChange const frc = _parent->film()->active_frame_rate_change (i->position ());
458 ContentTime (max(DCPTime(), i->full_length(_parent->film()) - play_length), frc) - i->trim_start()
464 TimingPanel::video_frame_rate_changed ()
466 _set_video_frame_rate->Enable (true);
470 TimingPanel::set_video_frame_rate ()
473 if (_video_frame_rate->GetValue() != wxT("")) {
474 fr = locale_convert<double> (wx_to_std (_video_frame_rate->GetValue ()));
476 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
478 i->set_video_frame_rate (*fr);
480 i->unset_video_frame_rate ();
484 _set_video_frame_rate->Enable (false);
488 TimingPanel::content_selection_changed ()
490 setup_sensitivity ();
492 film_content_changed (ContentProperty::POSITION);
493 film_content_changed (ContentProperty::LENGTH);
494 film_content_changed (ContentProperty::TRIM_START);
495 film_content_changed (ContentProperty::TRIM_END);
496 film_content_changed (ContentProperty::VIDEO_FRAME_RATE);
500 TimingPanel::film_changed (Film::Property p)
502 if (p == Film::VIDEO_FRAME_RATE) {
503 update_full_length ();
504 update_play_length ();
509 TimingPanel::trim_start_to_playhead_clicked ()
511 shared_ptr<FilmViewer> fv = _viewer.lock ();
516 shared_ptr<const Film> film = _parent->film ();
517 DCPTime const ph = fv->position().floor (film->video_frame_rate ());
518 optional<DCPTime> new_ph;
520 fv->set_coalesce_player_changes (true);
522 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
523 if (i->position() < ph && ph < i->end(film)) {
524 FrameRateChange const frc = film->active_frame_rate_change (i->position());
525 i->set_trim_start (i->trim_start() + ContentTime (ph - i->position(), frc));
526 new_ph = i->position ();
531 fv->seek (new_ph.get(), true);
534 fv->set_coalesce_player_changes (false);
538 TimingPanel::trim_end_to_playhead_clicked ()
540 shared_ptr<FilmViewer> fv = _viewer.lock ();
545 shared_ptr<const Film> film = _parent->film ();
546 DCPTime const ph = fv->position().floor (film->video_frame_rate ());
547 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
548 if (i->position() < ph && ph < i->end(film)) {
549 FrameRateChange const frc = film->active_frame_rate_change (i->position ());
550 i->set_trim_end (ContentTime(i->position() + i->full_length(film) - ph - DCPTime::from_frames(1, frc.dcp), frc) - i->trim_start());
556 TimingPanel::setup_sensitivity ()
558 bool const e = !_parent->selected().empty ();
560 _position->Enable (e);
561 _move_to_start_of_reel->Enable (e);
562 _full_length->Enable (e);
563 _trim_start->Enable (e);
564 _trim_end->Enable (e);
565 _play_length->Enable (e);
566 _video_frame_rate->Enable (e);
568 shared_ptr<FilmViewer> fv = _viewer.lock ();
569 DCPOMATIC_ASSERT (fv);
570 DCPTime const ph = fv->position ();
571 bool any_over_ph = false;
572 BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
573 if (i->position() <= ph && ph < i->end(_parent->film())) {
578 _trim_start_to_playhead->Enable (any_over_ph);
579 _trim_end_to_playhead->Enable (any_over_ph);
583 TimingPanel::move_to_start_of_reel_clicked ()
585 /* Find common position of all selected content, if it exists */
587 optional<DCPTime> position;
588 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
590 position = i->position();
592 if (position.get() != i->position()) {
599 MoveToDialog* d = new MoveToDialog (this, position, _parent->film());
601 if (d->ShowModal() == wxID_OK) {
602 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected()) {
603 i->set_position (_parent->film(), d->position());