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 "control_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::dynamic_pointer_cast;
44 using boost::optional;
45 using dcp::locale_convert;
47 TimingPanel::TimingPanel (ContentPanel* p, ControlFilmViewer* viewer)
48 /* horrid hack for apparent lack of context support with wxWidgets i18n code */
49 /// TRANSLATORS: translate the word "Timing" here; do not include the "Timing|" prefix
50 : ContentSubPanel (p, S_("Timing|Timing"))
53 wxSize size = TimecodeBase::size (this);
55 for (int i = 0; i < 3; ++i) {
56 _colon[i] = create_label (this, wxT(":"), false);
59 //// TRANSLATORS: this is an abbreviation for "hours"
60 _h_label = new wxStaticText (this, wxID_ANY, _("h"), wxDefaultPosition, size, wxALIGN_CENTRE_HORIZONTAL);
61 #ifdef DCPOMATIC_LINUX
62 /* Hack to work around failure to centre text on GTK */
63 gtk_label_set_line_wrap (GTK_LABEL(_h_label->GetHandle()), FALSE);
65 //// TRANSLATORS: this is an abbreviation for "minutes"
66 _m_label = new wxStaticText (this, wxID_ANY, _("m"), wxDefaultPosition, size, wxALIGN_CENTRE_HORIZONTAL);
67 #ifdef DCPOMATIC_LINUX
68 gtk_label_set_line_wrap (GTK_LABEL (_m_label->GetHandle()), FALSE);
70 //// TRANSLATORS: this is an abbreviation for "seconds"
71 _s_label = new wxStaticText (this, wxID_ANY, _("s"), wxDefaultPosition, size, wxALIGN_CENTRE_HORIZONTAL);
72 #ifdef DCPOMATIC_LINUX
73 gtk_label_set_line_wrap (GTK_LABEL(_s_label->GetHandle()), FALSE);
75 //// TRANSLATORS: this is an abbreviation for "frames"
76 _f_label = new wxStaticText (this, wxID_ANY, _("f"), wxDefaultPosition, size, wxALIGN_CENTRE_HORIZONTAL);
77 #ifdef DCPOMATIC_LINUX
78 gtk_label_set_line_wrap (GTK_LABEL(_f_label->GetHandle()), FALSE);
81 _position_label = create_label (this, _("Position"), true);
82 _position = new Timecode<DCPTime> (this);
83 _move_to_start_of_reel = new wxButton (this, wxID_ANY, _("Move to start of reel"));
84 _full_length_label = create_label (this, _("Full length"), true);
85 _full_length = new Timecode<DCPTime> (this);
86 _trim_start_label = create_label (this, _("Trim from start"), true);
87 _trim_start = new Timecode<ContentTime> (this);
88 _trim_start_to_playhead = new wxButton (this, wxID_ANY, _("Trim up to current position"));
89 _trim_end_label = create_label (this, _("Trim from end"), true);
90 _trim_end = new Timecode<ContentTime> (this);
91 _trim_end_to_playhead = new wxButton (this, wxID_ANY, _("Trim after current position"));
92 _play_length_label = create_label (this, _("Play length"), true);
93 _play_length = new Timecode<DCPTime> (this);
95 _video_frame_rate_label = create_label (this, _("Video frame rate"), true);
96 _video_frame_rate = new wxTextCtrl (this, wxID_ANY);
97 _set_video_frame_rate = new wxButton (this, wxID_ANY, _("Set"));
98 _set_video_frame_rate->Enable (false);
100 /* We can't use Wrap() here as it doesn't work with markup:
101 * http://trac.wxwidgets.org/ticket/13389
104 wxString in = _("<i>Only change this if the content's frame rate has been read incorrectly.</i>");
106 int const width = 20;
108 for (size_t i = 0; i < in.Length(); ++i) {
109 if (in[i] == ' ' && current >= width) {
118 _tip = new wxStaticText (this, wxID_ANY, wxT (""));
119 _tip->SetLabelMarkup (out);
121 /* Hack to stop hidden text on some versions of OS X */
122 _tip->SetMinSize (wxSize (-1, 256));
125 _position->Changed.connect (boost::bind (&TimingPanel::position_changed, this));
126 _move_to_start_of_reel->Bind (wxEVT_BUTTON, boost::bind (&TimingPanel::move_to_start_of_reel_clicked, this));
127 _full_length->Changed.connect (boost::bind (&TimingPanel::full_length_changed, this));
128 _trim_start->Changed.connect (boost::bind (&TimingPanel::trim_start_changed, this));
129 _trim_start_to_playhead->Bind (wxEVT_BUTTON, boost::bind (&TimingPanel::trim_start_to_playhead_clicked, this));
130 _trim_end->Changed.connect (boost::bind (&TimingPanel::trim_end_changed, this));
131 _trim_end_to_playhead->Bind (wxEVT_BUTTON, boost::bind (&TimingPanel::trim_end_to_playhead_clicked, this));
132 _play_length->Changed.connect (boost::bind (&TimingPanel::play_length_changed, this));
133 _video_frame_rate->Bind (wxEVT_TEXT, boost::bind (&TimingPanel::video_frame_rate_changed, this));
134 _set_video_frame_rate->Bind (wxEVT_BUTTON, boost::bind (&TimingPanel::set_video_frame_rate, this));
136 _viewer->ImageChanged.connect (boost::bind (&TimingPanel::setup_sensitivity, this));
138 setup_sensitivity ();
143 TimingPanel::add_to_grid ()
145 bool const full = Config::instance()->interface_complexity() == Config::INTERFACE_FULL;
149 wxSizer* labels = new wxBoxSizer (wxHORIZONTAL);
150 labels->Add (_h_label, 1, wxEXPAND);
151 add_label_to_sizer (labels, _colon[0], false);
152 labels->Add (_m_label, 1, wxEXPAND);
153 add_label_to_sizer (labels, _colon[1], false);
154 labels->Add (_s_label, 1, wxEXPAND);
155 add_label_to_sizer (labels, _colon[2], false);
156 labels->Add (_f_label, 1, wxEXPAND);
157 _grid->Add (labels, wxGBPosition(r, 1));
160 add_label_to_sizer (_grid, _position_label, true, wxGBPosition(r, 0));
161 _grid->Add (_position, wxGBPosition(r, 1));
164 _move_to_start_of_reel->Show (full);
165 _full_length_label->Show (full);
166 _full_length->Show (full);
167 _trim_start_label->Show (full);
168 _trim_start->Show (full);
169 _trim_start_to_playhead->Show (full);
170 _trim_end_label->Show (full);
171 _trim_end->Show (full);
172 _trim_end_to_playhead->Show (full);
173 _play_length_label->Show (full);
174 _play_length->Show (full);
175 _video_frame_rate_label->Show (full);
176 _video_frame_rate->Show (full);
177 _set_video_frame_rate->Show (full);
181 _grid->Add (_move_to_start_of_reel, wxGBPosition(r, 1));
184 add_label_to_sizer (_grid, _full_length_label, true, wxGBPosition(r, 0));
185 _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));
202 add_label_to_sizer (_grid, _play_length_label, true, wxGBPosition(r, 0));
203 _grid->Add (_play_length, wxGBPosition(r, 1));
207 add_label_to_sizer (_grid, _video_frame_rate_label, true, wxGBPosition(r, 0));
208 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
209 s->Add (_video_frame_rate, 1, wxEXPAND);
210 s->Add (_set_video_frame_rate, 0, wxLEFT | wxRIGHT, 8);
211 _grid->Add (s, wxGBPosition(r, 1), wxGBSpan(1, 2));
215 _grid->Add (_tip, wxGBPosition(r, 1), wxGBSpan(1, 2));
218 /* Completely speculative fix for #891 */
223 TimingPanel::update_full_length ()
226 BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
227 check.insert (i->full_length ());
230 if (check.size() == 1) {
231 _full_length->set (_parent->selected().front()->full_length (), _parent->film()->video_frame_rate ());
233 _full_length->clear ();
238 TimingPanel::update_play_length ()
241 BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
242 check.insert (i->length_after_trim ());
245 if (check.size() == 1) {
246 _play_length->set (_parent->selected().front()->length_after_trim (), _parent->film()->video_frame_rate ());
248 _play_length->clear ();
253 TimingPanel::film_content_changed (int property)
255 int const film_video_frame_rate = _parent->film()->video_frame_rate ();
257 /* Here we check to see if we have exactly one different value of various
258 properties, and fill the controls with that value if so.
261 if (property == ContentProperty::POSITION) {
264 BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
265 check.insert (i->position ());
268 if (check.size() == 1) {
269 _position->set (_parent->selected().front()->position(), film_video_frame_rate);
275 property == ContentProperty::LENGTH ||
276 property == ContentProperty::VIDEO_FRAME_RATE ||
277 property == VideoContentProperty::FRAME_TYPE
280 update_full_length ();
282 } else if (property == ContentProperty::TRIM_START) {
284 set<ContentTime> check;
285 BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
286 check.insert (i->trim_start ());
289 if (check.size() == 1) {
290 _trim_start->set (_parent->selected().front()->trim_start (), film_video_frame_rate);
292 _trim_start->clear ();
295 } else if (property == ContentProperty::TRIM_END) {
297 set<ContentTime> check;
298 BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
299 check.insert (i->trim_end ());
302 if (check.size() == 1) {
303 _trim_end->set (_parent->selected().front()->trim_end (), film_video_frame_rate);
310 property == ContentProperty::LENGTH ||
311 property == ContentProperty::TRIM_START ||
312 property == ContentProperty::TRIM_END ||
313 property == ContentProperty::VIDEO_FRAME_RATE ||
314 property == VideoContentProperty::FRAME_TYPE
317 update_play_length ();
320 if (property == ContentProperty::VIDEO_FRAME_RATE) {
321 set<double> check_vc;
322 shared_ptr<const Content> content;
325 BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
326 if (i->video && i->video_frame_rate()) {
327 check_vc.insert (i->video_frame_rate().get());
330 if (i->audio && i->video_frame_rate()) {
334 if (!i->text.empty() && i->video_frame_rate()) {
341 bool const single_frame_image_content = content && dynamic_pointer_cast<const ImageContent> (content) && content->number_of_paths() == 1;
343 if ((check_vc.size() == 1 || count_ac == 1 || count_sc == 1) && !single_frame_image_content) {
344 checked_set (_video_frame_rate, locale_convert<string> (content->video_frame_rate().get(), 5));
345 _video_frame_rate->Enable (true);
347 checked_set (_video_frame_rate, wxT (""));
348 _video_frame_rate->Enable (false);
352 bool have_still = false;
353 BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
354 shared_ptr<const ImageContent> ic = dynamic_pointer_cast<const ImageContent> (i);
355 if (ic && ic->still ()) {
360 _full_length->set_editable (have_still);
361 _play_length->set_editable (!have_still);
362 _set_video_frame_rate->Enable (false);
363 setup_sensitivity ();
367 TimingPanel::position_changed ()
369 DCPTime const pos = _position->get (_parent->film()->video_frame_rate ());
370 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
371 i->set_position (pos);
376 TimingPanel::full_length_changed ()
378 int const vfr = _parent->film()->video_frame_rate ();
379 Frame const len = _full_length->get (vfr).frames_round (vfr);
380 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
381 shared_ptr<ImageContent> ic = dynamic_pointer_cast<ImageContent> (i);
382 if (ic && ic->still ()) {
383 ic->video->set_length (len);
389 TimingPanel::trim_start_changed ()
391 DCPTime const ph = _viewer->position ();
393 _viewer->set_coalesce_player_changes (true);
395 shared_ptr<Content> ref;
396 optional<FrameRateChange> ref_frc;
397 optional<DCPTime> ref_ph;
399 ContentTime const trim = _trim_start->get (_parent->film()->video_frame_rate ());
401 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
402 if (i->position() <= ph && ph < i->end()) {
403 /* The playhead is in i. Use it as a reference to work out
404 where to put the playhead post-trim; we're trying to keep the playhead
405 at the same frame of content that we're looking at pre-trim.
408 ref_frc = _parent->film()->active_frame_rate_change (i->position ());
409 ref_ph = ph - i->position() + DCPTime (i->trim_start(), ref_frc.get());
412 i->set_trim_start (trim);
416 _viewer->set_position (max (DCPTime(), ref_ph.get() + ref->position() - DCPTime (ref->trim_start(), ref_frc.get())));
419 _viewer->set_coalesce_player_changes (false);
423 TimingPanel::trim_end_changed ()
425 _viewer->set_coalesce_player_changes (true);
427 ContentTime const trim = _trim_end->get (_parent->film()->video_frame_rate ());
428 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
429 i->set_trim_end (trim);
432 /* XXX: maybe playhead-off-the-end-of-the-film should be handled elsewhere */
433 if (_viewer->position() >= _parent->film()->length()) {
434 _viewer->set_position (_parent->film()->length() - DCPTime::from_frames (1, _parent->film()->video_frame_rate()));
437 _viewer->set_coalesce_player_changes (true);
441 TimingPanel::play_length_changed ()
443 DCPTime const play_length = _play_length->get (_parent->film()->video_frame_rate());
444 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
445 FrameRateChange const frc = _parent->film()->active_frame_rate_change (i->position ());
447 ContentTime (max(DCPTime(), i->full_length() - play_length), frc) - i->trim_start ()
453 TimingPanel::video_frame_rate_changed ()
455 _set_video_frame_rate->Enable (true);
459 TimingPanel::set_video_frame_rate ()
462 if (_video_frame_rate->GetValue() != wxT("")) {
463 fr = locale_convert<double> (wx_to_std (_video_frame_rate->GetValue ()));
465 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
467 i->set_video_frame_rate (*fr);
469 i->unset_video_frame_rate ();
473 _set_video_frame_rate->Enable (false);
477 TimingPanel::content_selection_changed ()
479 setup_sensitivity ();
481 film_content_changed (ContentProperty::POSITION);
482 film_content_changed (ContentProperty::LENGTH);
483 film_content_changed (ContentProperty::TRIM_START);
484 film_content_changed (ContentProperty::TRIM_END);
485 film_content_changed (ContentProperty::VIDEO_FRAME_RATE);
489 TimingPanel::film_changed (Film::Property p)
491 if (p == Film::VIDEO_FRAME_RATE) {
492 update_full_length ();
493 update_play_length ();
498 TimingPanel::trim_start_to_playhead_clicked ()
500 shared_ptr<const Film> film = _parent->film ();
501 DCPTime const ph = _viewer->position().floor (film->video_frame_rate ());
502 optional<DCPTime> new_ph;
504 _viewer->set_coalesce_player_changes (true);
506 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
507 if (i->position() < ph && ph < i->end ()) {
508 FrameRateChange const frc = film->active_frame_rate_change (i->position ());
509 i->set_trim_start (i->trim_start() + ContentTime (ph - i->position (), frc));
510 new_ph = i->position ();
515 _viewer->set_position (new_ph.get());
518 _viewer->set_coalesce_player_changes (false);
522 TimingPanel::trim_end_to_playhead_clicked ()
524 shared_ptr<const Film> film = _parent->film ();
525 DCPTime const ph = _viewer->position().floor (film->video_frame_rate ());
526 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
527 if (i->position() < ph && ph < i->end ()) {
528 FrameRateChange const frc = film->active_frame_rate_change (i->position ());
529 i->set_trim_end (ContentTime (i->position() + i->full_length() - ph - DCPTime::from_frames (1, frc.dcp), frc) - i->trim_start());
535 TimingPanel::setup_sensitivity ()
537 bool const e = !_parent->selected().empty ();
539 _position->Enable (e);
540 _move_to_start_of_reel->Enable (e);
541 _full_length->Enable (e);
542 _trim_start->Enable (e);
543 _trim_end->Enable (e);
544 _play_length->Enable (e);
545 _video_frame_rate->Enable (e);
547 DCPTime const ph = _viewer->position ();
548 bool any_over_ph = false;
549 BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
550 if (i->position() <= ph && ph < i->end()) {
555 _trim_start_to_playhead->Enable (any_over_ph);
556 _trim_end_to_playhead->Enable (any_over_ph);
560 TimingPanel::move_to_start_of_reel_clicked ()
562 /* Find common position of all selected content, if it exists */
564 optional<DCPTime> position;
565 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
567 position = i->position();
569 if (position.get() != i->position()) {
576 MoveToDialog* d = new MoveToDialog (this, position, _parent->film());
578 if (d->ShowModal() == wxID_OK) {
579 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
580 i->set_position (d->position ());