2 Copyright (C) 2012-2016 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 "lib/content.h"
28 #include "lib/image_content.h"
29 #include "lib/text_content.h"
30 #include "lib/dcp_subtitle_content.h"
31 #include "lib/audio_content.h"
32 #include "lib/string_text_file_content.h"
33 #include "lib/video_content.h"
34 #include <dcp/locale_convert.h>
35 #include <boost/foreach.hpp>
42 using boost::shared_ptr;
43 using boost::weak_ptr;
44 using boost::dynamic_pointer_cast;
45 using boost::optional;
46 using dcp::locale_convert;
48 TimingPanel::TimingPanel (ContentPanel* p, weak_ptr<FilmViewer> viewer)
49 /* horrid hack for apparent lack of context support with wxWidgets i18n code */
50 /// TRANSLATORS: translate the word "Timing" here; do not include the "Timing|" prefix
51 : ContentSubPanel (p, S_("Timing|Timing"))
54 wxSize size = TimecodeBase::size (this);
56 for (int i = 0; i < 3; ++i) {
57 _colon[i] = create_label (this, wxT(":"), false);
60 //// TRANSLATORS: this is an abbreviation for "hours"
61 _h_label = new wxStaticText (this, wxID_ANY, _("h"), wxDefaultPosition, size, wxALIGN_CENTRE_HORIZONTAL);
62 #ifdef DCPOMATIC_LINUX
63 /* Hack to work around failure to centre text on GTK */
64 gtk_label_set_line_wrap (GTK_LABEL(_h_label->GetHandle()), FALSE);
66 //// TRANSLATORS: this is an abbreviation for "minutes"
67 _m_label = new wxStaticText (this, wxID_ANY, _("m"), wxDefaultPosition, size, wxALIGN_CENTRE_HORIZONTAL);
68 #ifdef DCPOMATIC_LINUX
69 gtk_label_set_line_wrap (GTK_LABEL (_m_label->GetHandle()), FALSE);
71 //// TRANSLATORS: this is an abbreviation for "seconds"
72 _s_label = new wxStaticText (this, wxID_ANY, _("s"), wxDefaultPosition, size, wxALIGN_CENTRE_HORIZONTAL);
73 #ifdef DCPOMATIC_LINUX
74 gtk_label_set_line_wrap (GTK_LABEL(_s_label->GetHandle()), FALSE);
76 //// TRANSLATORS: this is an abbreviation for "frames"
77 _f_label = new wxStaticText (this, wxID_ANY, _("f"), wxDefaultPosition, size, wxALIGN_CENTRE_HORIZONTAL);
78 #ifdef DCPOMATIC_LINUX
79 gtk_label_set_line_wrap (GTK_LABEL(_f_label->GetHandle()), FALSE);
82 _position_label = create_label (this, _("Position"), true);
83 _position = new Timecode<DCPTime> (this);
84 _move_to_start_of_reel = new wxButton (this, wxID_ANY, _("Move to start of reel"));
85 _full_length_label = create_label (this, _("Full length"), true);
86 _full_length = new Timecode<DCPTime> (this);
87 _trim_start_label = create_label (this, _("Trim from start"), true);
88 _trim_start = new Timecode<ContentTime> (this);
89 _trim_start_to_playhead = new wxButton (this, wxID_ANY, _("Trim up to current position"));
90 _trim_end_label = create_label (this, _("Trim from end"), true);
91 _trim_end = new Timecode<ContentTime> (this);
92 _trim_end_to_playhead = new wxButton (this, wxID_ANY, _("Trim after current position"));
93 _play_length_label = create_label (this, _("Play length"), true);
94 _play_length = new Timecode<DCPTime> (this);
96 _video_frame_rate_label = create_label (this, _("Video frame rate"), true);
97 _video_frame_rate = new wxTextCtrl (this, wxID_ANY);
98 _set_video_frame_rate = new wxButton (this, wxID_ANY, _("Set"));
99 _set_video_frame_rate->Enable (false);
101 /* We can't use Wrap() here as it doesn't work with markup:
102 * http://trac.wxwidgets.org/ticket/13389
105 wxString in = _("<i>Only change this if the content's frame rate has been read incorrectly.</i>");
107 int const width = 20;
109 for (size_t i = 0; i < in.Length(); ++i) {
110 if (in[i] == ' ' && current >= width) {
119 _tip = new wxStaticText (this, wxID_ANY, wxT (""));
120 _tip->SetLabelMarkup (out);
122 /* Hack to stop hidden text on some versions of OS X */
123 _tip->SetMinSize (wxSize (-1, 256));
126 _position->Changed.connect (boost::bind (&TimingPanel::position_changed, this));
127 _move_to_start_of_reel->Bind (wxEVT_BUTTON, boost::bind (&TimingPanel::move_to_start_of_reel_clicked, this));
128 _full_length->Changed.connect (boost::bind (&TimingPanel::full_length_changed, this));
129 _trim_start->Changed.connect (boost::bind (&TimingPanel::trim_start_changed, this));
130 _trim_start_to_playhead->Bind (wxEVT_BUTTON, boost::bind (&TimingPanel::trim_start_to_playhead_clicked, this));
131 _trim_end->Changed.connect (boost::bind (&TimingPanel::trim_end_changed, this));
132 _trim_end_to_playhead->Bind (wxEVT_BUTTON, boost::bind (&TimingPanel::trim_end_to_playhead_clicked, this));
133 _play_length->Changed.connect (boost::bind (&TimingPanel::play_length_changed, this));
134 _video_frame_rate->Bind (wxEVT_TEXT, boost::bind (&TimingPanel::video_frame_rate_changed, this));
135 _set_video_frame_rate->Bind (wxEVT_BUTTON, boost::bind (&TimingPanel::set_video_frame_rate, this));
137 shared_ptr<FilmViewer> fv = _viewer.lock ();
138 DCPOMATIC_ASSERT (fv);
139 fv->ImageChanged.connect (boost::bind (&TimingPanel::setup_sensitivity, this));
141 setup_sensitivity ();
146 TimingPanel::add_to_grid ()
148 bool const full = Config::instance()->interface_complexity() == Config::INTERFACE_FULL;
152 wxSizer* labels = new wxBoxSizer (wxHORIZONTAL);
153 labels->Add (_h_label, 1, wxEXPAND);
154 add_label_to_sizer (labels, _colon[0], false);
155 labels->Add (_m_label, 1, wxEXPAND);
156 add_label_to_sizer (labels, _colon[1], false);
157 labels->Add (_s_label, 1, wxEXPAND);
158 add_label_to_sizer (labels, _colon[2], false);
159 labels->Add (_f_label, 1, wxEXPAND);
160 _grid->Add (labels, wxGBPosition(r, 1));
163 add_label_to_sizer (_grid, _position_label, true, wxGBPosition(r, 0));
164 _grid->Add (_position, wxGBPosition(r, 1));
167 _move_to_start_of_reel->Show (full);
168 _full_length_label->Show (full);
169 _full_length->Show (full);
170 _trim_start_label->Show (full);
171 _trim_start->Show (full);
172 _trim_start_to_playhead->Show (full);
173 _trim_end_label->Show (full);
174 _trim_end->Show (full);
175 _trim_end_to_playhead->Show (full);
176 _play_length_label->Show (full);
177 _play_length->Show (full);
178 _video_frame_rate_label->Show (full);
179 _video_frame_rate->Show (full);
180 _set_video_frame_rate->Show (full);
184 _grid->Add (_move_to_start_of_reel, wxGBPosition(r, 1));
187 add_label_to_sizer (_grid, _full_length_label, true, wxGBPosition(r, 0));
188 _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));
205 add_label_to_sizer (_grid, _play_length_label, true, wxGBPosition(r, 0));
206 _grid->Add (_play_length, wxGBPosition(r, 1));
210 add_label_to_sizer (_grid, _video_frame_rate_label, true, wxGBPosition(r, 0));
211 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
212 s->Add (_video_frame_rate, 1, wxEXPAND);
213 s->Add (_set_video_frame_rate, 0, wxLEFT | wxRIGHT, 8);
214 _grid->Add (s, wxGBPosition(r, 1), wxGBSpan(1, 2));
218 _grid->Add (_tip, wxGBPosition(r, 1), wxGBSpan(1, 2));
221 /* Completely speculative fix for #891 */
226 TimingPanel::update_full_length ()
229 BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
230 check.insert (i->full_length ());
233 if (check.size() == 1) {
234 _full_length->set (_parent->selected().front()->full_length (), _parent->film()->video_frame_rate ());
236 _full_length->clear ();
241 TimingPanel::update_play_length ()
244 BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
245 check.insert (i->length_after_trim ());
248 if (check.size() == 1) {
249 _play_length->set (_parent->selected().front()->length_after_trim (), _parent->film()->video_frame_rate ());
251 _play_length->clear ();
256 TimingPanel::film_content_changed (int property)
258 int const film_video_frame_rate = _parent->film()->video_frame_rate ();
260 /* Here we check to see if we have exactly one different value of various
261 properties, and fill the controls with that value if so.
264 if (property == ContentProperty::POSITION) {
267 BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
268 check.insert (i->position ());
271 if (check.size() == 1) {
272 _position->set (_parent->selected().front()->position(), film_video_frame_rate);
278 property == ContentProperty::LENGTH ||
279 property == ContentProperty::VIDEO_FRAME_RATE ||
280 property == VideoContentProperty::FRAME_TYPE
283 update_full_length ();
285 } else if (property == ContentProperty::TRIM_START) {
287 set<ContentTime> check;
288 BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
289 check.insert (i->trim_start ());
292 if (check.size() == 1) {
293 _trim_start->set (_parent->selected().front()->trim_start (), film_video_frame_rate);
295 _trim_start->clear ();
298 } else if (property == ContentProperty::TRIM_END) {
300 set<ContentTime> check;
301 BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
302 check.insert (i->trim_end ());
305 if (check.size() == 1) {
306 _trim_end->set (_parent->selected().front()->trim_end (), film_video_frame_rate);
313 property == ContentProperty::LENGTH ||
314 property == ContentProperty::TRIM_START ||
315 property == ContentProperty::TRIM_END ||
316 property == ContentProperty::VIDEO_FRAME_RATE ||
317 property == VideoContentProperty::FRAME_TYPE
320 update_play_length ();
323 if (property == ContentProperty::VIDEO_FRAME_RATE) {
324 set<double> check_vc;
325 shared_ptr<const Content> content;
328 BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
329 if (i->video && i->video_frame_rate()) {
330 check_vc.insert (i->video_frame_rate().get());
333 if (i->audio && i->video_frame_rate()) {
337 if (!i->text.empty() && i->video_frame_rate()) {
344 bool const single_frame_image_content = content && dynamic_pointer_cast<const ImageContent> (content) && content->number_of_paths() == 1;
346 if ((check_vc.size() == 1 || count_ac == 1 || count_sc == 1) && !single_frame_image_content) {
347 checked_set (_video_frame_rate, locale_convert<string> (content->video_frame_rate().get(), 5));
348 _video_frame_rate->Enable (true);
350 checked_set (_video_frame_rate, wxT (""));
351 _video_frame_rate->Enable (false);
355 bool have_still = false;
356 BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
357 shared_ptr<const ImageContent> ic = dynamic_pointer_cast<const ImageContent> (i);
358 if (ic && ic->still ()) {
363 _full_length->set_editable (have_still);
364 _play_length->set_editable (!have_still);
365 _set_video_frame_rate->Enable (false);
366 setup_sensitivity ();
370 TimingPanel::position_changed ()
372 DCPTime const pos = _position->get (_parent->film()->video_frame_rate ());
373 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
374 i->set_position (pos);
379 TimingPanel::full_length_changed ()
381 int const vfr = _parent->film()->video_frame_rate ();
382 Frame const len = _full_length->get (vfr).frames_round (vfr);
383 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
384 shared_ptr<ImageContent> ic = dynamic_pointer_cast<ImageContent> (i);
385 if (ic && ic->still ()) {
386 ic->video->set_length (len);
392 TimingPanel::trim_start_changed ()
394 shared_ptr<FilmViewer> fv = _viewer.lock ();
399 DCPTime const ph = fv->position ();
401 fv->set_coalesce_player_changes (true);
403 shared_ptr<Content> ref;
404 optional<FrameRateChange> ref_frc;
405 optional<DCPTime> ref_ph;
407 ContentTime const trim = _trim_start->get (_parent->film()->video_frame_rate ());
409 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
410 if (i->position() <= ph && ph < i->end()) {
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 i->set_trim_start (trim);
424 fv->seek (max(DCPTime(), ref_ph.get() + ref->position() - DCPTime(ref->trim_start(), ref_frc.get())), true);
427 fv->set_coalesce_player_changes (false);
431 TimingPanel::trim_end_changed ()
433 shared_ptr<FilmViewer> fv = _viewer.lock ();
438 fv->set_coalesce_player_changes (true);
440 ContentTime const trim = _trim_end->get (_parent->film()->video_frame_rate ());
441 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
442 i->set_trim_end (trim);
445 /* XXX: maybe playhead-off-the-end-of-the-film should be handled elsewhere */
446 if (fv->position() >= _parent->film()->length()) {
447 fv->seek (_parent->film()->length() - DCPTime::from_frames(1, _parent->film()->video_frame_rate()), true);
450 fv->set_coalesce_player_changes (true);
454 TimingPanel::play_length_changed ()
456 DCPTime const play_length = _play_length->get (_parent->film()->video_frame_rate());
457 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
458 FrameRateChange const frc = _parent->film()->active_frame_rate_change (i->position ());
460 ContentTime (max(DCPTime(), i->full_length() - play_length), frc) - i->trim_start ()
466 TimingPanel::video_frame_rate_changed ()
468 _set_video_frame_rate->Enable (true);
472 TimingPanel::set_video_frame_rate ()
475 if (_video_frame_rate->GetValue() != wxT("")) {
476 fr = locale_convert<double> (wx_to_std (_video_frame_rate->GetValue ()));
478 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
480 i->set_video_frame_rate (*fr);
482 i->unset_video_frame_rate ();
486 _set_video_frame_rate->Enable (false);
490 TimingPanel::content_selection_changed ()
492 setup_sensitivity ();
494 film_content_changed (ContentProperty::POSITION);
495 film_content_changed (ContentProperty::LENGTH);
496 film_content_changed (ContentProperty::TRIM_START);
497 film_content_changed (ContentProperty::TRIM_END);
498 film_content_changed (ContentProperty::VIDEO_FRAME_RATE);
502 TimingPanel::film_changed (Film::Property p)
504 if (p == Film::VIDEO_FRAME_RATE) {
505 update_full_length ();
506 update_play_length ();
511 TimingPanel::trim_start_to_playhead_clicked ()
513 shared_ptr<FilmViewer> fv = _viewer.lock ();
518 shared_ptr<const Film> film = _parent->film ();
519 DCPTime const ph = fv->position().floor (film->video_frame_rate ());
520 optional<DCPTime> new_ph;
522 fv->set_coalesce_player_changes (true);
524 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
525 if (i->position() < ph && ph < i->end ()) {
526 FrameRateChange const frc = film->active_frame_rate_change (i->position ());
527 i->set_trim_start (i->trim_start() + ContentTime (ph - i->position (), frc));
528 new_ph = i->position ();
533 fv->seek (new_ph.get(), true);
536 fv->set_coalesce_player_changes (false);
540 TimingPanel::trim_end_to_playhead_clicked ()
542 shared_ptr<FilmViewer> fv = _viewer.lock ();
547 shared_ptr<const Film> film = _parent->film ();
548 DCPTime const ph = fv->position().floor (film->video_frame_rate ());
549 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
550 if (i->position() < ph && ph < i->end ()) {
551 FrameRateChange const frc = film->active_frame_rate_change (i->position ());
552 i->set_trim_end (ContentTime (i->position() + i->full_length() - ph - DCPTime::from_frames (1, frc.dcp), frc) - i->trim_start());
558 TimingPanel::setup_sensitivity ()
560 bool const e = !_parent->selected().empty ();
562 _position->Enable (e);
563 _move_to_start_of_reel->Enable (e);
564 _full_length->Enable (e);
565 _trim_start->Enable (e);
566 _trim_end->Enable (e);
567 _play_length->Enable (e);
568 _video_frame_rate->Enable (e);
570 shared_ptr<FilmViewer> fv = _viewer.lock ();
571 DCPOMATIC_ASSERT (fv);
572 DCPTime const ph = fv->position ();
573 bool any_over_ph = false;
574 BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
575 if (i->position() <= ph && ph < i->end()) {
580 _trim_start_to_playhead->Enable (any_over_ph);
581 _trim_end_to_playhead->Enable (any_over_ph);
585 TimingPanel::move_to_start_of_reel_clicked ()
587 /* Find common position of all selected content, if it exists */
589 optional<DCPTime> position;
590 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
592 position = i->position();
594 if (position.get() != i->position()) {
601 MoveToDialog* d = new MoveToDialog (this, position, _parent->film());
603 if (d->ShowModal() == wxID_OK) {
604 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
605 i->set_position (d->position ());