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 "lib/dcp_content.h"
37 #include "lib/ffmpeg_content.h"
38 #include <dcp/locale_convert.h>
39 #include "lib/warnings.h"
40 #if defined(__WXGTK20__) && !defined(__WXGTK3__)
41 #define TIMING_PANEL_ALIGNMENT_HACK 1
42 DCPOMATIC_DISABLE_WARNINGS
44 DCPOMATIC_ENABLE_WARNINGS
52 using std::shared_ptr;
54 using std::dynamic_pointer_cast;
55 using boost::optional;
56 #if BOOST_VERSION >= 106100
57 using namespace boost::placeholders;
59 using dcp::locale_convert;
60 using namespace dcpomatic;
62 TimingPanel::TimingPanel (ContentPanel* p, weak_ptr<FilmViewer> viewer)
63 /* horrid hack for apparent lack of context support with wxWidgets i18n code */
64 /// TRANSLATORS: translate the word "Timing" here; do not include the "Timing|" prefix
65 : ContentSubPanel (p, S_("Timing|Timing"))
67 , _film_content_changed_suspender (boost::bind(&TimingPanel::film_content_changed, this, _1))
74 TimingPanel::create ()
76 wxSize size = TimecodeBase::size (this);
78 for (int i = 0; i < 3; ++i) {
79 _colon[i] = create_label (this, wxT(":"), false);
82 //// TRANSLATORS: this is an abbreviation for "hours"
83 _h_label = new StaticText (this, _("h"), wxDefaultPosition, size, wxALIGN_CENTRE_HORIZONTAL);
84 #ifdef TIMING_PANEL_ALIGNMENT_HACK
85 /* Hack to work around failure to centre text on GTK */
86 gtk_label_set_line_wrap (GTK_LABEL(_h_label->GetHandle()), FALSE);
88 //// TRANSLATORS: this is an abbreviation for "minutes"
89 _m_label = new StaticText (this, _("m"), wxDefaultPosition, size, wxALIGN_CENTRE_HORIZONTAL);
90 #ifdef TIMING_PANEL_ALIGNMENT_HACK
91 gtk_label_set_line_wrap (GTK_LABEL (_m_label->GetHandle()), FALSE);
93 //// TRANSLATORS: this is an abbreviation for "seconds"
94 _s_label = new StaticText (this, _("s"), wxDefaultPosition, size, wxALIGN_CENTRE_HORIZONTAL);
95 #ifdef TIMING_PANEL_ALIGNMENT_HACK
96 gtk_label_set_line_wrap (GTK_LABEL(_s_label->GetHandle()), FALSE);
98 //// TRANSLATORS: this is an abbreviation for "frames"
99 _f_label = new StaticText (this, _("f"), wxDefaultPosition, size, wxALIGN_CENTRE_HORIZONTAL);
100 #ifdef TIMING_PANEL_ALIGNMENT_HACK
101 gtk_label_set_line_wrap (GTK_LABEL(_f_label->GetHandle()), FALSE);
104 _position_label = create_label (this, _("Position"), true);
105 _position = new Timecode<DCPTime> (this);
106 _move_to_start_of_reel = new Button (this, _("Move to start of reel"));
107 _full_length_label = create_label (this, _("Full length"), true);
108 _full_length = new Timecode<DCPTime> (this);
109 _trim_start_label = create_label (this, _("Trim from start"), true);
110 _trim_start = new Timecode<ContentTime> (this);
111 _trim_start_to_playhead = new Button (this, _("Trim up to current position"));
112 _trim_end_label = create_label (this, _("Trim from end"), true);
113 _trim_end = new Timecode<ContentTime> (this);
114 _trim_end_to_playhead = new Button (this, _("Trim from current position to end"));
115 _play_length_label = create_label (this, _("Play length"), true);
116 _play_length = new Timecode<DCPTime> (this);
118 _position->Changed.connect (boost::bind (&TimingPanel::position_changed, this));
119 _move_to_start_of_reel->Bind (wxEVT_BUTTON, boost::bind (&TimingPanel::move_to_start_of_reel_clicked, this));
120 _full_length->Changed.connect (boost::bind (&TimingPanel::full_length_changed, this));
121 _trim_start->Changed.connect (boost::bind (&TimingPanel::trim_start_changed, this));
122 _trim_start_to_playhead->Bind (wxEVT_BUTTON, boost::bind (&TimingPanel::trim_start_to_playhead_clicked, this));
123 _trim_end->Changed.connect (boost::bind (&TimingPanel::trim_end_changed, this));
124 _trim_end_to_playhead->Bind (wxEVT_BUTTON, boost::bind (&TimingPanel::trim_end_to_playhead_clicked, this));
125 _play_length->Changed.connect (boost::bind (&TimingPanel::play_length_changed, this));
127 shared_ptr<FilmViewer> fv = _viewer.lock ();
128 DCPOMATIC_ASSERT (fv);
129 fv->ImageChanged.connect (boost::bind (&TimingPanel::setup_sensitivity, this));
131 setup_sensitivity ();
138 TimingPanel::add_to_grid ()
142 wxSizer* labels = new wxBoxSizer (wxHORIZONTAL);
143 labels->Add (_h_label, 1, wxEXPAND);
144 add_label_to_sizer (labels, _colon[0], false);
145 labels->Add (_m_label, 1, wxEXPAND);
146 add_label_to_sizer (labels, _colon[1], false);
147 labels->Add (_s_label, 1, wxEXPAND);
148 add_label_to_sizer (labels, _colon[2], false);
149 labels->Add (_f_label, 1, wxEXPAND);
150 _grid->Add (labels, wxGBPosition(r, 1));
153 add_label_to_sizer (_grid, _position_label, true, wxGBPosition(r, 0));
154 _grid->Add (_position, wxGBPosition(r, 1));
157 _grid->Add (_move_to_start_of_reel, wxGBPosition(r, 1));
160 add_label_to_sizer (_grid, _full_length_label, true, wxGBPosition(r, 0));
161 _grid->Add (_full_length, wxGBPosition(r, 1));
164 add_label_to_sizer (_grid, _trim_start_label, true, wxGBPosition(r, 0));
165 _grid->Add (_trim_start, wxGBPosition(r, 1));
168 _grid->Add (_trim_start_to_playhead, wxGBPosition(r, 1));
171 add_label_to_sizer (_grid, _trim_end_label, true, wxGBPosition(r, 0));
172 _grid->Add (_trim_end, wxGBPosition(r, 1));
175 _grid->Add (_trim_end_to_playhead, wxGBPosition(r, 1));
178 add_label_to_sizer (_grid, _play_length_label, true, wxGBPosition(r, 0));
179 _grid->Add (_play_length, wxGBPosition(r, 1));
182 /* Completely speculative fix for #891 */
187 TimingPanel::update_full_length ()
190 for (auto i: _parent->selected()) {
191 check.insert (i->full_length(_parent->film()));
194 if (check.size() == 1) {
195 _full_length->set (_parent->selected().front()->full_length(_parent->film()), _parent->film()->video_frame_rate());
197 _full_length->clear ();
202 TimingPanel::update_play_length ()
205 for (auto i: _parent->selected()) {
206 check.insert (i->length_after_trim(_parent->film()));
209 if (check.size() == 1) {
210 _play_length->set (_parent->selected().front()->length_after_trim(_parent->film()), _parent->film()->video_frame_rate());
212 _play_length->clear ();
217 TimingPanel::film_content_changed (int property)
219 if (_film_content_changed_suspender.check(property)) {
223 int const film_video_frame_rate = _parent->film()->video_frame_rate ();
225 /* Here we check to see if we have exactly one different value of various
226 properties, and fill the controls with that value if so.
229 if (property == ContentProperty::POSITION) {
232 for (auto i: _parent->selected()) {
233 check.insert (i->position ());
236 if (check.size() == 1) {
237 _position->set (_parent->selected().front()->position(), film_video_frame_rate);
243 property == ContentProperty::LENGTH ||
244 property == ContentProperty::VIDEO_FRAME_RATE ||
245 property == VideoContentProperty::FRAME_TYPE
248 update_full_length ();
250 } else if (property == ContentProperty::TRIM_START) {
252 set<ContentTime> check;
253 for (auto i: _parent->selected()) {
254 check.insert (i->trim_start ());
257 if (check.size() == 1) {
258 _trim_start->set (_parent->selected().front()->trim_start (), film_video_frame_rate);
260 _trim_start->clear ();
263 } else if (property == ContentProperty::TRIM_END) {
265 set<ContentTime> check;
266 for (auto i: _parent->selected()) {
267 check.insert (i->trim_end ());
270 if (check.size() == 1) {
271 _trim_end->set (_parent->selected().front()->trim_end (), film_video_frame_rate);
278 property == ContentProperty::LENGTH ||
279 property == ContentProperty::TRIM_START ||
280 property == ContentProperty::TRIM_END ||
281 property == ContentProperty::VIDEO_FRAME_RATE ||
282 property == VideoContentProperty::FRAME_TYPE
285 update_play_length ();
288 if (property == ContentProperty::VIDEO_FRAME_RATE) {
289 set<double> check_vc;
290 shared_ptr<const Content> content;
293 for (auto i: _parent->selected()) {
294 if (i->video && i->video_frame_rate()) {
295 check_vc.insert (i->video_frame_rate().get());
298 if (i->audio && i->video_frame_rate()) {
302 if (!i->text.empty() && i->video_frame_rate()) {
310 bool have_still = false;
311 for (auto i: _parent->selected()) {
312 shared_ptr<const ImageContent> ic = dynamic_pointer_cast<const ImageContent> (i);
313 if (ic && ic->still ()) {
318 _full_length->set_editable (have_still);
319 _play_length->set_editable (!have_still);
320 setup_sensitivity ();
324 TimingPanel::position_changed ()
326 DCPTime const pos = _position->get (_parent->film()->video_frame_rate ());
327 for (auto i: _parent->selected()) {
328 i->set_position (_parent->film(), pos);
333 TimingPanel::full_length_changed ()
335 int const vfr = _parent->film()->video_frame_rate ();
336 Frame const len = _full_length->get (vfr).frames_round (vfr);
337 for (auto i: _parent->selected()) {
338 shared_ptr<ImageContent> ic = dynamic_pointer_cast<ImageContent> (i);
339 if (ic && ic->still ()) {
340 ic->video->set_length (len);
346 TimingPanel::trim_start_changed ()
348 shared_ptr<FilmViewer> fv = _viewer.lock ();
353 DCPTime const ph = fv->position ();
355 fv->set_coalesce_player_changes (true);
357 shared_ptr<Content> ref;
358 optional<FrameRateChange> ref_frc;
359 optional<DCPTime> ref_ph;
361 Suspender::Block bl = _film_content_changed_suspender.block ();
362 for (auto i: _parent->selected()) {
363 if (i->position() <= ph && ph < i->end(_parent->film())) {
364 /* The playhead is in i. Use it as a reference to work out
365 where to put the playhead post-trim; we're trying to keep the playhead
366 at the same frame of content that we're looking at pre-trim.
369 ref_frc = _parent->film()->active_frame_rate_change (i->position ());
370 ref_ph = ph - i->position() + DCPTime (i->trim_start(), ref_frc.get());
373 ContentTime const trim = _trim_start->get (i->video_frame_rate().get_value_or(_parent->film()->video_frame_rate()));
374 i->set_trim_start (trim);
378 fv->seek (max(DCPTime(), ref_ph.get() + ref->position() - DCPTime(ref->trim_start(), ref_frc.get())), true);
381 fv->set_coalesce_player_changes (false);
385 TimingPanel::trim_end_changed ()
387 shared_ptr<FilmViewer> fv = _viewer.lock ();
392 fv->set_coalesce_player_changes (true);
394 Suspender::Block bl = _film_content_changed_suspender.block ();
395 for (auto i: _parent->selected()) {
396 ContentTime const trim = _trim_end->get (i->video_frame_rate().get_value_or(_parent->film()->video_frame_rate()));
397 i->set_trim_end (trim);
400 /* XXX: maybe playhead-off-the-end-of-the-film should be handled elsewhere */
401 if (fv->position() >= _parent->film()->length()) {
402 fv->seek (_parent->film()->length() - DCPTime::from_frames(1, _parent->film()->video_frame_rate()), true);
405 fv->set_coalesce_player_changes (false);
409 TimingPanel::play_length_changed ()
411 DCPTime const play_length = _play_length->get (_parent->film()->video_frame_rate());
412 Suspender::Block bl = _film_content_changed_suspender.block ();
413 for (auto i: _parent->selected()) {
414 FrameRateChange const frc = _parent->film()->active_frame_rate_change (i->position ());
416 ContentTime (max(DCPTime(), i->full_length(_parent->film()) - play_length), frc) - i->trim_start()
423 TimingPanel::content_selection_changed ()
425 setup_sensitivity ();
427 film_content_changed (ContentProperty::POSITION);
428 film_content_changed (ContentProperty::LENGTH);
429 film_content_changed (ContentProperty::TRIM_START);
430 film_content_changed (ContentProperty::TRIM_END);
431 film_content_changed (ContentProperty::VIDEO_FRAME_RATE);
435 TimingPanel::film_changed (Film::Property p)
437 if (p == Film::Property::VIDEO_FRAME_RATE) {
438 update_full_length ();
439 update_play_length ();
444 TimingPanel::trim_start_to_playhead_clicked ()
446 shared_ptr<FilmViewer> fv = _viewer.lock ();
451 shared_ptr<const Film> film = _parent->film ();
452 DCPTime const ph = fv->position().floor (film->video_frame_rate ());
453 optional<DCPTime> new_ph;
455 fv->set_coalesce_player_changes (true);
457 for (auto i: _parent->selected()) {
458 if (i->position() < ph && ph < i->end(film)) {
459 FrameRateChange const frc = film->active_frame_rate_change (i->position());
460 i->set_trim_start (i->trim_start() + ContentTime (ph - i->position(), frc));
461 new_ph = i->position ();
465 fv->set_coalesce_player_changes (false);
468 fv->seek (new_ph.get(), true);
473 TimingPanel::trim_end_to_playhead_clicked ()
475 shared_ptr<FilmViewer> fv = _viewer.lock ();
480 shared_ptr<const Film> film = _parent->film ();
481 DCPTime const ph = fv->position().floor (film->video_frame_rate ());
482 for (auto i: _parent->selected()) {
483 if (i->position() < ph && ph < i->end(film)) {
484 FrameRateChange const frc = film->active_frame_rate_change (i->position ());
485 i->set_trim_end (ContentTime(i->position() + i->full_length(film) - ph, frc) - i->trim_start());
491 TimingPanel::setup_sensitivity ()
493 bool const e = !_parent->selected().empty ();
495 _position->Enable (e);
496 _move_to_start_of_reel->Enable (e);
497 _full_length->Enable (e);
498 _trim_start->Enable (e);
499 _trim_end->Enable (e);
500 _play_length->Enable (e);
502 shared_ptr<FilmViewer> fv = _viewer.lock ();
503 DCPOMATIC_ASSERT (fv);
504 DCPTime const ph = fv->position ();
505 bool any_over_ph = false;
506 for (auto i: _parent->selected()) {
507 if (i->position() <= ph && ph < i->end(_parent->film())) {
512 _trim_start_to_playhead->Enable (any_over_ph);
513 _trim_end_to_playhead->Enable (any_over_ph);
517 TimingPanel::move_to_start_of_reel_clicked ()
519 /* Find common position of all selected content, if it exists */
521 optional<DCPTime> position;
522 for (auto i: _parent->selected()) {
524 position = i->position();
526 if (position.get() != i->position()) {
533 MoveToDialog* d = new MoveToDialog (this, position, _parent->film());
535 if (d->ShowModal() == wxID_OK) {
536 for (auto i: _parent->selected()) {
537 i->set_position (_parent->film(), d->position());