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/raw_convert.h"
30 #include "lib/subtitle_content.h"
31 #include "lib/dcp_subtitle_content.h"
32 #include "lib/audio_content.h"
33 #include "lib/text_subtitle_content.h"
34 #include "lib/video_content.h"
35 #include <boost/foreach.hpp>
42 using boost::shared_ptr;
43 using boost::dynamic_pointer_cast;
44 using boost::optional;
46 TimingPanel::TimingPanel (ContentPanel* p, FilmViewer* viewer)
47 /* horrid hack for apparent lack of context support with wxWidgets i18n code */
48 /// TRANSLATORS: translate the word "Timing" here; do not include the "Timing|" prefix
49 : ContentSubPanel (p, S_("Timing|Timing"))
52 wxFlexGridSizer* grid = new wxFlexGridSizer (2, 4, 4);
53 _sizer->Add (grid, 0, wxALL, 8);
55 wxSize size = TimecodeBase::size (this);
57 wxSizer* labels = new wxBoxSizer (wxHORIZONTAL);
58 //// TRANSLATORS: this is an abbreviation for "hours"
59 wxStaticText* t = new wxStaticText (this, wxID_ANY, _("h"), wxDefaultPosition, size, wxALIGN_CENTRE_HORIZONTAL);
60 #ifdef DCPOMATIC_LINUX
61 /* Hack to work around failure to centre text on GTK */
62 gtk_label_set_line_wrap (GTK_LABEL (t->GetHandle()), FALSE);
64 labels->Add (t, 1, wxEXPAND);
65 add_label_to_sizer (labels, this, wxT (":"), false);
66 //// TRANSLATORS: this is an abbreviation for "minutes"
67 t = new wxStaticText (this, wxID_ANY, _("m"), wxDefaultPosition, size, wxALIGN_CENTRE_HORIZONTAL);
68 #ifdef DCPOMATIC_LINUX
69 gtk_label_set_line_wrap (GTK_LABEL (t->GetHandle()), FALSE);
71 labels->Add (t, 1, wxEXPAND);
72 add_label_to_sizer (labels, this, wxT (":"), false);
73 //// TRANSLATORS: this is an abbreviation for "seconds"
74 t = new wxStaticText (this, wxID_ANY, _("s"), wxDefaultPosition, size, wxALIGN_CENTRE_HORIZONTAL);
75 #ifdef DCPOMATIC_LINUX
76 gtk_label_set_line_wrap (GTK_LABEL (t->GetHandle()), FALSE);
78 labels->Add (t, 1, wxEXPAND);
79 add_label_to_sizer (labels, this, wxT (":"), false);
80 //// TRANSLATORS: this is an abbreviation for "frames"
81 t = new wxStaticText (this, wxID_ANY, _("f"), wxDefaultPosition, size, wxALIGN_CENTRE_HORIZONTAL);
82 #ifdef DCPOMATIC_LINUX
83 gtk_label_set_line_wrap (GTK_LABEL (t->GetHandle()), FALSE);
85 labels->Add (t, 1, wxEXPAND);
86 grid->Add (new wxStaticText (this, wxID_ANY, wxT ("")));
89 add_label_to_sizer (grid, this, _("Position"), true);
90 _position = new Timecode<DCPTime> (this);
91 grid->Add (_position);
92 _move_to_start_of_reel = new wxButton (this, wxID_ANY, _("Move to start of reel"));
94 grid->Add (_move_to_start_of_reel);
95 add_label_to_sizer (grid, this, _("Full length"), true);
96 _full_length = new Timecode<DCPTime> (this);
97 grid->Add (_full_length);
98 add_label_to_sizer (grid, this, _("Trim from start"), true);
99 _trim_start = new Timecode<ContentTime> (this);
100 grid->Add (_trim_start);
101 _trim_start_to_playhead = new wxButton (this, wxID_ANY, _("Trim up to current position"));
103 grid->Add (_trim_start_to_playhead);
104 add_label_to_sizer (grid, this, _("Trim from end"), true);
105 _trim_end = new Timecode<ContentTime> (this);
106 grid->Add (_trim_end);
107 _trim_end_to_playhead = new wxButton (this, wxID_ANY, _("Trim after current position"));
109 grid->Add (_trim_end_to_playhead);
110 add_label_to_sizer (grid, this, _("Play length"), true);
111 _play_length = new Timecode<DCPTime> (this);
112 grid->Add (_play_length);
115 add_label_to_sizer (grid, this, _("Video frame rate"), true);
116 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
117 _video_frame_rate = new wxTextCtrl (this, wxID_ANY);
118 s->Add (_video_frame_rate, 1, wxEXPAND);
119 _set_video_frame_rate = new wxButton (this, wxID_ANY, _("Set"));
120 _set_video_frame_rate->Enable (false);
121 s->Add (_set_video_frame_rate, 0, wxLEFT | wxRIGHT, 8);
122 grid->Add (s, 1, wxEXPAND);
127 /* We can't use Wrap() here as it doesn't work with markup:
128 * http://trac.wxwidgets.org/ticket/13389
131 wxString in = _("<i>Only change this if it the content's frame rate has been read incorrectly.</i>");
133 int const width = 20;
135 for (size_t i = 0; i < in.Length(); ++i) {
136 if (in[i] == ' ' && current >= width) {
145 t = new wxStaticText (this, wxID_ANY, wxT (""));
146 t->SetLabelMarkup (out);
147 grid->Add (t, 0, wxALIGN_CENTER_VERTICAL | wxLEFT | wxRIGHT, 6);
149 _position->Changed.connect (boost::bind (&TimingPanel::position_changed, this));
150 _move_to_start_of_reel->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&TimingPanel::move_to_start_of_reel_clicked, this));
151 _full_length->Changed.connect (boost::bind (&TimingPanel::full_length_changed, this));
152 _trim_start->Changed.connect (boost::bind (&TimingPanel::trim_start_changed, this));
153 _trim_start_to_playhead->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&TimingPanel::trim_start_to_playhead_clicked, this));
154 _trim_end->Changed.connect (boost::bind (&TimingPanel::trim_end_changed, this));
155 _trim_end_to_playhead->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&TimingPanel::trim_end_to_playhead_clicked, this));
156 _play_length->Changed.connect (boost::bind (&TimingPanel::play_length_changed, this));
157 _video_frame_rate->Bind (wxEVT_COMMAND_TEXT_UPDATED, boost::bind (&TimingPanel::video_frame_rate_changed, this));
158 _set_video_frame_rate->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&TimingPanel::set_video_frame_rate, this));
160 _viewer->ImageChanged.connect (boost::bind (&TimingPanel::setup_sensitivity, this));
162 setup_sensitivity ();
166 TimingPanel::update_full_length ()
169 BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
170 check.insert (i->full_length ());
173 if (check.size() == 1) {
174 _full_length->set (_parent->selected().front()->full_length (), _parent->film()->video_frame_rate ());
176 _full_length->clear ();
181 TimingPanel::update_play_length ()
184 BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
185 check.insert (i->length_after_trim ());
188 if (check.size() == 1) {
189 _play_length->set (_parent->selected().front()->length_after_trim (), _parent->film()->video_frame_rate ());
191 _play_length->clear ();
196 TimingPanel::film_content_changed (int property)
198 int const film_video_frame_rate = _parent->film()->video_frame_rate ();
200 /* Here we check to see if we have exactly one different value of various
201 properties, and fill the controls with that value if so.
204 if (property == ContentProperty::POSITION) {
207 BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
208 check.insert (i->position ());
211 if (check.size() == 1) {
212 _position->set (_parent->selected().front()->position(), film_video_frame_rate);
218 property == ContentProperty::LENGTH ||
219 property == ContentProperty::VIDEO_FRAME_RATE ||
220 property == VideoContentProperty::FRAME_TYPE
223 update_full_length ();
225 } else if (property == ContentProperty::TRIM_START) {
227 set<ContentTime> check;
228 BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
229 check.insert (i->trim_start ());
232 if (check.size() == 1) {
233 _trim_start->set (_parent->selected().front()->trim_start (), film_video_frame_rate);
235 _trim_start->clear ();
238 } else if (property == ContentProperty::TRIM_END) {
240 set<ContentTime> check;
241 BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
242 check.insert (i->trim_end ());
245 if (check.size() == 1) {
246 _trim_end->set (_parent->selected().front()->trim_end (), film_video_frame_rate);
253 property == ContentProperty::LENGTH ||
254 property == ContentProperty::TRIM_START ||
255 property == ContentProperty::TRIM_END ||
256 property == ContentProperty::VIDEO_FRAME_RATE ||
257 property == VideoContentProperty::FRAME_TYPE
260 update_play_length ();
263 if (property == ContentProperty::VIDEO_FRAME_RATE) {
264 set<double> check_vc;
265 shared_ptr<const Content> content;
268 BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
269 if (i->video && i->video_frame_rate()) {
270 check_vc.insert (i->video_frame_rate().get());
273 if (i->audio && i->video_frame_rate()) {
277 if (i->subtitle && i->video_frame_rate()) {
284 bool const single_frame_image_content = content && dynamic_pointer_cast<const ImageContent> (content) && content->number_of_paths() == 1;
286 if ((check_vc.size() == 1 || count_ac == 1 || count_sc == 1) && !single_frame_image_content) {
287 checked_set (_video_frame_rate, raw_convert<string> (content->video_frame_rate().get(), 5));
288 _video_frame_rate->Enable (true);
290 checked_set (_video_frame_rate, wxT (""));
291 _video_frame_rate->Enable (false);
295 bool have_still = false;
296 BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
297 shared_ptr<const ImageContent> ic = dynamic_pointer_cast<const ImageContent> (i);
298 if (ic && ic->still ()) {
303 _full_length->set_editable (have_still);
304 _play_length->set_editable (!have_still);
305 _set_video_frame_rate->Enable (false);
306 setup_sensitivity ();
310 TimingPanel::position_changed ()
312 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
313 i->set_position (_position->get (_parent->film()->video_frame_rate ()));
318 TimingPanel::full_length_changed ()
320 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
321 shared_ptr<ImageContent> ic = dynamic_pointer_cast<ImageContent> (i);
322 if (ic && ic->still ()) {
323 int const vfr = _parent->film()->video_frame_rate ();
324 ic->video->set_length (_full_length->get (vfr).frames_round (vfr));
330 TimingPanel::trim_start_changed ()
332 DCPTime const ph = _viewer->position ();
334 _viewer->set_coalesce_player_changes (true);
336 shared_ptr<Content> ref;
337 optional<FrameRateChange> ref_frc;
338 optional<DCPTime> ref_ph;
339 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
340 if (i->position() <= ph && ph < i->end()) {
341 /* The playhead is in i. Use it as a reference to work out
342 where to put the playhead post-trim; we're trying to keep the playhead
343 at the same frame of content that we're looking at pre-trim.
346 ref_frc = _parent->film()->active_frame_rate_change (i->position ());
347 ref_ph = ph - i->position() + DCPTime (i->trim_start(), ref_frc.get());
350 i->set_trim_start (_trim_start->get (_parent->film()->video_frame_rate ()));
354 _viewer->set_position (max (DCPTime(), ref_ph.get() + ref->position() - DCPTime (ref->trim_start(), ref_frc.get())));
357 _viewer->set_coalesce_player_changes (false);
361 TimingPanel::trim_end_changed ()
363 _viewer->set_coalesce_player_changes (true);
365 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
366 i->set_trim_end (_trim_end->get (_parent->film()->video_frame_rate ()));
369 /* XXX: maybe playhead-off-the-end-of-the-film should be handled elsewhere */
370 if (_viewer->position() >= _parent->film()->length()) {
371 _viewer->set_position (_parent->film()->length() - DCPTime::from_frames (1, _parent->film()->video_frame_rate()));
374 _viewer->set_coalesce_player_changes (true);
378 TimingPanel::play_length_changed ()
380 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
381 FrameRateChange const frc = _parent->film()->active_frame_rate_change (i->position ());
383 ContentTime (i->full_length() - _play_length->get (_parent->film()->video_frame_rate()), frc)
390 TimingPanel::video_frame_rate_changed ()
392 _set_video_frame_rate->Enable (true);
396 TimingPanel::set_video_frame_rate ()
398 double const fr = raw_convert<double> (wx_to_std (_video_frame_rate->GetValue ()));
399 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
400 i->set_video_frame_rate (fr);
403 _set_video_frame_rate->Enable (false);
407 TimingPanel::content_selection_changed ()
409 setup_sensitivity ();
411 film_content_changed (ContentProperty::POSITION);
412 film_content_changed (ContentProperty::LENGTH);
413 film_content_changed (ContentProperty::TRIM_START);
414 film_content_changed (ContentProperty::TRIM_END);
415 film_content_changed (ContentProperty::VIDEO_FRAME_RATE);
419 TimingPanel::film_changed (Film::Property p)
421 if (p == Film::VIDEO_FRAME_RATE) {
422 update_full_length ();
423 update_play_length ();
428 TimingPanel::trim_start_to_playhead_clicked ()
430 DCPTime const ph = _viewer->position ();
431 optional<DCPTime> new_ph;
433 _viewer->set_coalesce_player_changes (true);
435 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
436 if (i->position() < ph && ph < i->end ()) {
437 FrameRateChange const frc = _parent->film()->active_frame_rate_change (i->position ());
438 i->set_trim_start (i->trim_start() + ContentTime (ph - i->position (), frc));
439 new_ph = i->position ();
444 _viewer->set_position (new_ph.get());
447 _viewer->set_coalesce_player_changes (false);
451 TimingPanel::trim_end_to_playhead_clicked ()
453 DCPTime const ph = _viewer->position ();
454 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
455 if (i->position() < ph && ph < i->end ()) {
456 FrameRateChange const frc = _parent->film()->active_frame_rate_change (i->position ());
457 i->set_trim_end (ContentTime (i->position() + i->full_length() - ph - DCPTime::from_frames (1, frc.dcp), frc) - i->trim_start());
463 TimingPanel::setup_sensitivity ()
465 bool const e = !_parent->selected().empty ();
467 _position->Enable (e);
468 _full_length->Enable (e);
469 _trim_start->Enable (e);
470 _trim_end->Enable (e);
471 _play_length->Enable (e);
472 _video_frame_rate->Enable (e);
474 DCPTime const ph = _viewer->position ();
475 bool any_over_ph = false;
476 BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
477 if (i->position() <= ph && ph < i->end()) {
482 _trim_start_to_playhead->Enable (any_over_ph);
483 _trim_end_to_playhead->Enable (any_over_ph);
487 TimingPanel::move_to_start_of_reel_clicked ()
489 /* Find common position of all selected content, if it exists */
491 optional<DCPTime> position;
492 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
494 position = i->position();
496 if (position.get() != i->position()) {
503 MoveToDialog* d = new MoveToDialog (this, position, _parent->film());
505 if (d->ShowModal() == wxID_OK) {
506 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
507 i->set_position (d->position ());