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/subtitle_content.h"
30 #include "lib/dcp_subtitle_content.h"
31 #include "lib/audio_content.h"
32 #include "lib/text_subtitle_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, FilmViewer* 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 wxFlexGridSizer* grid = new wxFlexGridSizer (2, 4, 4);
54 _sizer->Add (grid, 0, wxALL, 8);
56 wxSize size = TimecodeBase::size (this);
58 wxSizer* labels = new wxBoxSizer (wxHORIZONTAL);
59 //// TRANSLATORS: this is an abbreviation for "hours"
60 wxStaticText* t = 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 (t->GetHandle()), FALSE);
65 labels->Add (t, 1, wxEXPAND);
66 add_label_to_sizer (labels, this, wxT (":"), false);
67 //// TRANSLATORS: this is an abbreviation for "minutes"
68 t = new wxStaticText (this, wxID_ANY, _("m"), wxDefaultPosition, size, wxALIGN_CENTRE_HORIZONTAL);
69 #ifdef DCPOMATIC_LINUX
70 gtk_label_set_line_wrap (GTK_LABEL (t->GetHandle()), FALSE);
72 labels->Add (t, 1, wxEXPAND);
73 add_label_to_sizer (labels, this, wxT (":"), false);
74 //// TRANSLATORS: this is an abbreviation for "seconds"
75 t = new wxStaticText (this, wxID_ANY, _("s"), wxDefaultPosition, size, wxALIGN_CENTRE_HORIZONTAL);
76 #ifdef DCPOMATIC_LINUX
77 gtk_label_set_line_wrap (GTK_LABEL (t->GetHandle()), FALSE);
79 labels->Add (t, 1, wxEXPAND);
80 add_label_to_sizer (labels, this, wxT (":"), false);
81 //// TRANSLATORS: this is an abbreviation for "frames"
82 t = new wxStaticText (this, wxID_ANY, _("f"), wxDefaultPosition, size, wxALIGN_CENTRE_HORIZONTAL);
83 #ifdef DCPOMATIC_LINUX
84 gtk_label_set_line_wrap (GTK_LABEL (t->GetHandle()), FALSE);
86 labels->Add (t, 1, wxEXPAND);
87 grid->Add (new wxStaticText (this, wxID_ANY, wxT ("")));
90 add_label_to_sizer (grid, this, _("Position"), true);
91 _position = new Timecode<DCPTime> (this);
92 grid->Add (_position);
93 _move_to_start_of_reel = new wxButton (this, wxID_ANY, _("Move to start of reel"));
95 grid->Add (_move_to_start_of_reel);
96 add_label_to_sizer (grid, this, _("Full length"), true);
97 _full_length = new Timecode<DCPTime> (this);
98 grid->Add (_full_length);
99 add_label_to_sizer (grid, this, _("Trim from start"), true);
100 _trim_start = new Timecode<ContentTime> (this);
101 grid->Add (_trim_start);
102 _trim_start_to_playhead = new wxButton (this, wxID_ANY, _("Trim up to current position"));
104 grid->Add (_trim_start_to_playhead);
105 add_label_to_sizer (grid, this, _("Trim from end"), true);
106 _trim_end = new Timecode<ContentTime> (this);
107 grid->Add (_trim_end);
108 _trim_end_to_playhead = new wxButton (this, wxID_ANY, _("Trim after current position"));
110 grid->Add (_trim_end_to_playhead);
111 add_label_to_sizer (grid, this, _("Play length"), true);
112 _play_length = new Timecode<DCPTime> (this);
113 grid->Add (_play_length);
116 add_label_to_sizer (grid, this, _("Video frame rate"), true);
117 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
118 _video_frame_rate = new wxTextCtrl (this, wxID_ANY);
119 s->Add (_video_frame_rate, 1, wxEXPAND);
120 _set_video_frame_rate = new wxButton (this, wxID_ANY, _("Set"));
121 _set_video_frame_rate->Enable (false);
122 s->Add (_set_video_frame_rate, 0, wxLEFT | wxRIGHT, 8);
123 grid->Add (s, 1, wxEXPAND);
128 /* We can't use Wrap() here as it doesn't work with markup:
129 * http://trac.wxwidgets.org/ticket/13389
132 wxString in = _("<i>Only change this if the content's frame rate has been read incorrectly.</i>");
134 int const width = 20;
136 for (size_t i = 0; i < in.Length(); ++i) {
137 if (in[i] == ' ' && current >= width) {
146 t = new wxStaticText (this, wxID_ANY, wxT (""));
147 t->SetLabelMarkup (out);
149 /* Hack to stop hidden text on some versions of OS X */
150 t->SetMinSize (wxSize (-1, 256));
152 grid->Add (t, 0, wxALIGN_CENTER_VERTICAL | wxLEFT | wxRIGHT, 6);
154 /* Completely speculative fix for #891 */
157 _position->Changed.connect (boost::bind (&TimingPanel::position_changed, this));
158 _move_to_start_of_reel->Bind (wxEVT_BUTTON, boost::bind (&TimingPanel::move_to_start_of_reel_clicked, this));
159 _full_length->Changed.connect (boost::bind (&TimingPanel::full_length_changed, this));
160 _trim_start->Changed.connect (boost::bind (&TimingPanel::trim_start_changed, this));
161 _trim_start_to_playhead->Bind (wxEVT_BUTTON, boost::bind (&TimingPanel::trim_start_to_playhead_clicked, this));
162 _trim_end->Changed.connect (boost::bind (&TimingPanel::trim_end_changed, this));
163 _trim_end_to_playhead->Bind (wxEVT_BUTTON, boost::bind (&TimingPanel::trim_end_to_playhead_clicked, this));
164 _play_length->Changed.connect (boost::bind (&TimingPanel::play_length_changed, this));
165 _video_frame_rate->Bind (wxEVT_TEXT, boost::bind (&TimingPanel::video_frame_rate_changed, this));
166 _set_video_frame_rate->Bind (wxEVT_BUTTON, boost::bind (&TimingPanel::set_video_frame_rate, this));
168 _viewer->ImageChanged.connect (boost::bind (&TimingPanel::setup_sensitivity, this));
170 setup_sensitivity ();
174 TimingPanel::update_full_length ()
177 BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
178 check.insert (i->full_length ());
181 if (check.size() == 1) {
182 _full_length->set (_parent->selected().front()->full_length (), _parent->film()->video_frame_rate ());
184 _full_length->clear ();
189 TimingPanel::update_play_length ()
192 BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
193 check.insert (i->length_after_trim ());
196 if (check.size() == 1) {
197 _play_length->set (_parent->selected().front()->length_after_trim (), _parent->film()->video_frame_rate ());
199 _play_length->clear ();
204 TimingPanel::film_content_changed (int property)
206 int const film_video_frame_rate = _parent->film()->video_frame_rate ();
208 /* Here we check to see if we have exactly one different value of various
209 properties, and fill the controls with that value if so.
212 if (property == ContentProperty::POSITION) {
215 BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
216 check.insert (i->position ());
219 if (check.size() == 1) {
220 _position->set (_parent->selected().front()->position(), film_video_frame_rate);
226 property == ContentProperty::LENGTH ||
227 property == ContentProperty::VIDEO_FRAME_RATE ||
228 property == VideoContentProperty::FRAME_TYPE
231 update_full_length ();
233 } else if (property == ContentProperty::TRIM_START) {
235 set<ContentTime> check;
236 BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
237 check.insert (i->trim_start ());
240 if (check.size() == 1) {
241 _trim_start->set (_parent->selected().front()->trim_start (), film_video_frame_rate);
243 _trim_start->clear ();
246 } else if (property == ContentProperty::TRIM_END) {
248 set<ContentTime> check;
249 BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
250 check.insert (i->trim_end ());
253 if (check.size() == 1) {
254 _trim_end->set (_parent->selected().front()->trim_end (), film_video_frame_rate);
261 property == ContentProperty::LENGTH ||
262 property == ContentProperty::TRIM_START ||
263 property == ContentProperty::TRIM_END ||
264 property == ContentProperty::VIDEO_FRAME_RATE ||
265 property == VideoContentProperty::FRAME_TYPE
268 update_play_length ();
271 if (property == ContentProperty::VIDEO_FRAME_RATE) {
272 set<double> check_vc;
273 shared_ptr<const Content> content;
276 BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
277 if (i->video && i->video_frame_rate()) {
278 check_vc.insert (i->video_frame_rate().get());
281 if (i->audio && i->video_frame_rate()) {
285 if (i->subtitle && i->video_frame_rate()) {
292 bool const single_frame_image_content = content && dynamic_pointer_cast<const ImageContent> (content) && content->number_of_paths() == 1;
294 if ((check_vc.size() == 1 || count_ac == 1 || count_sc == 1) && !single_frame_image_content) {
295 checked_set (_video_frame_rate, locale_convert<string> (content->video_frame_rate().get(), 5));
296 _video_frame_rate->Enable (true);
298 checked_set (_video_frame_rate, wxT (""));
299 _video_frame_rate->Enable (false);
303 bool have_still = false;
304 BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
305 shared_ptr<const ImageContent> ic = dynamic_pointer_cast<const ImageContent> (i);
306 if (ic && ic->still ()) {
311 _full_length->set_editable (have_still);
312 _play_length->set_editable (!have_still);
313 _set_video_frame_rate->Enable (false);
314 setup_sensitivity ();
318 TimingPanel::position_changed ()
320 DCPTime const pos = _position->get (_parent->film()->video_frame_rate ());
321 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
322 i->set_position (pos);
327 TimingPanel::full_length_changed ()
329 int const vfr = _parent->film()->video_frame_rate ();
330 Frame const len = _full_length->get (vfr).frames_round (vfr);
331 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
332 shared_ptr<ImageContent> ic = dynamic_pointer_cast<ImageContent> (i);
333 if (ic && ic->still ()) {
334 ic->video->set_length (len);
340 TimingPanel::trim_start_changed ()
342 DCPTime const ph = _viewer->position ();
344 _viewer->set_coalesce_player_changes (true);
346 shared_ptr<Content> ref;
347 optional<FrameRateChange> ref_frc;
348 optional<DCPTime> ref_ph;
350 ContentTime const trim = _trim_start->get (_parent->film()->video_frame_rate ());
352 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
353 if (i->position() <= ph && ph < i->end()) {
354 /* The playhead is in i. Use it as a reference to work out
355 where to put the playhead post-trim; we're trying to keep the playhead
356 at the same frame of content that we're looking at pre-trim.
359 ref_frc = _parent->film()->active_frame_rate_change (i->position ());
360 ref_ph = ph - i->position() + DCPTime (i->trim_start(), ref_frc.get());
363 i->set_trim_start (trim);
367 _viewer->set_position (max (DCPTime(), ref_ph.get() + ref->position() - DCPTime (ref->trim_start(), ref_frc.get())));
370 _viewer->set_coalesce_player_changes (false);
374 TimingPanel::trim_end_changed ()
376 _viewer->set_coalesce_player_changes (true);
378 ContentTime const trim = _trim_end->get (_parent->film()->video_frame_rate ());
379 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
380 i->set_trim_end (trim);
383 /* XXX: maybe playhead-off-the-end-of-the-film should be handled elsewhere */
384 if (_viewer->position() >= _parent->film()->length()) {
385 _viewer->set_position (_parent->film()->length() - DCPTime::from_frames (1, _parent->film()->video_frame_rate()));
388 _viewer->set_coalesce_player_changes (true);
392 TimingPanel::play_length_changed ()
394 DCPTime const play_length = _play_length->get (_parent->film()->video_frame_rate());
395 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
396 FrameRateChange const frc = _parent->film()->active_frame_rate_change (i->position ());
398 ContentTime (i->full_length() - play_length, frc) - i->trim_start ()
404 TimingPanel::video_frame_rate_changed ()
406 _set_video_frame_rate->Enable (true);
410 TimingPanel::set_video_frame_rate ()
412 double const fr = locale_convert<double> (wx_to_std (_video_frame_rate->GetValue ()));
413 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
414 i->set_video_frame_rate (fr);
417 _set_video_frame_rate->Enable (false);
421 TimingPanel::content_selection_changed ()
423 setup_sensitivity ();
425 film_content_changed (ContentProperty::POSITION);
426 film_content_changed (ContentProperty::LENGTH);
427 film_content_changed (ContentProperty::TRIM_START);
428 film_content_changed (ContentProperty::TRIM_END);
429 film_content_changed (ContentProperty::VIDEO_FRAME_RATE);
433 TimingPanel::film_changed (Film::Property p)
435 if (p == Film::VIDEO_FRAME_RATE) {
436 update_full_length ();
437 update_play_length ();
442 TimingPanel::trim_start_to_playhead_clicked ()
444 shared_ptr<const Film> film = _parent->film ();
445 DCPTime const ph = _viewer->position().floor (film->video_frame_rate ());
446 optional<DCPTime> new_ph;
448 _viewer->set_coalesce_player_changes (true);
450 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
451 if (i->position() < ph && ph < i->end ()) {
452 FrameRateChange const frc = film->active_frame_rate_change (i->position ());
453 i->set_trim_start (i->trim_start() + ContentTime (ph - i->position (), frc));
454 new_ph = i->position ();
459 _viewer->set_position (new_ph.get());
462 _viewer->set_coalesce_player_changes (false);
466 TimingPanel::trim_end_to_playhead_clicked ()
468 shared_ptr<const Film> film = _parent->film ();
469 DCPTime const ph = _viewer->position().floor (film->video_frame_rate ());
470 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
471 if (i->position() < ph && ph < i->end ()) {
472 FrameRateChange const frc = film->active_frame_rate_change (i->position ());
473 i->set_trim_end (ContentTime (i->position() + i->full_length() - ph - DCPTime::from_frames (1, frc.dcp), frc) - i->trim_start());
479 TimingPanel::setup_sensitivity ()
481 bool const e = !_parent->selected().empty ();
483 _position->Enable (e);
484 _move_to_start_of_reel->Enable (e);
485 _full_length->Enable (e);
486 _trim_start->Enable (e);
487 _trim_end->Enable (e);
488 _play_length->Enable (e);
489 _video_frame_rate->Enable (e);
491 DCPTime const ph = _viewer->position ();
492 bool any_over_ph = false;
493 BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
494 if (i->position() <= ph && ph < i->end()) {
499 _trim_start_to_playhead->Enable (any_over_ph);
500 _trim_end_to_playhead->Enable (any_over_ph);
504 TimingPanel::move_to_start_of_reel_clicked ()
506 /* Find common position of all selected content, if it exists */
508 optional<DCPTime> position;
509 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
511 position = i->position();
513 if (position.get() != i->position()) {
520 MoveToDialog* d = new MoveToDialog (this, position, _parent->film());
522 if (d->ShowModal() == wxID_OK) {
523 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
524 i->set_position (d->position ());