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/>.
22 #include "content_panel.h"
23 #include "dcpomatic_button.h"
24 #include "film_viewer.h"
25 #include "move_to_dialog.h"
26 #include "static_text.h"
28 #include "timing_panel.h"
30 #include "lib/audio_content.h"
31 #include "lib/content.h"
32 #include "lib/dcp_content.h"
33 #include "lib/dcp_subtitle_content.h"
34 #include "lib/ffmpeg_content.h"
36 #include "lib/image_content.h"
37 #include "lib/string_text_file_content.h"
38 #include "lib/text_content.h"
39 #include "lib/video_content.h"
40 #include <dcp/locale_convert.h>
41 #include <dcp/warnings.h>
42 #if defined(__WXGTK20__) && !defined(__WXGTK3__)
43 #define TIMING_PANEL_ALIGNMENT_HACK 1
44 LIBDCP_DISABLE_WARNINGS
46 LIBDCP_ENABLE_WARNINGS
53 using std::shared_ptr;
55 using std::dynamic_pointer_cast;
56 using boost::optional;
57 #if BOOST_VERSION >= 106100
58 using namespace boost::placeholders;
60 using dcp::locale_convert;
61 using namespace dcpomatic;
64 TimingPanel::TimingPanel (ContentPanel* p, FilmViewer& viewer)
65 /* horrid hack for apparent lack of context support with wxWidgets i18n code */
66 /// TRANSLATORS: translate the word "Timing" here; do not include the "Timing|" prefix
67 : ContentSubPanel (p, S_("Timing|Timing"))
69 , _film_content_changed_suspender (boost::bind(&TimingPanel::film_content_changed, this, _1))
76 TimingPanel::create ()
78 wxSize size = TimecodeBase::size (this);
80 for (int i = 0; i < 3; ++i) {
81 _colon[i] = create_label (this, wxT(":"), false);
84 //// TRANSLATORS: this is an abbreviation for "hours"
85 _label.push_back(new StaticText(this, _("h"), wxDefaultPosition, size, wxALIGN_CENTRE_HORIZONTAL));
86 //// TRANSLATORS: this is an abbreviation for "minutes"
87 _label.push_back(new StaticText(this, _("m"), wxDefaultPosition, size, wxALIGN_CENTRE_HORIZONTAL));
88 //// TRANSLATORS: this is an abbreviation for "seconds"
89 _label.push_back(new StaticText (this, _("s"), wxDefaultPosition, size, wxALIGN_CENTRE_HORIZONTAL));
90 //// TRANSLATORS: this is an abbreviation for "frames"
91 _label.push_back(new StaticText (this, _("f"), wxDefaultPosition, size, wxALIGN_CENTRE_HORIZONTAL));
93 if (GetLayoutDirection() == wxLayout_RightToLeft) {
94 std::reverse(_label.begin(), _label.end());
97 #ifdef TIMING_PANEL_ALIGNMENT_HACK
98 for (auto label: _label) {
99 /* Hack to work around failure to centre text on GTK */
100 gtk_label_set_line_wrap(GTK_LABEL(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 _viewer.ImageChanged.connect(boost::bind(&TimingPanel::setup_sensitivity, this));
129 setup_sensitivity ();
136 TimingPanel::add_to_grid ()
140 auto labels = new wxBoxSizer(wxHORIZONTAL);
142 for (auto label: _label) {
143 labels->Add(label, 1, wxEXPAND);
145 add_label_to_sizer(labels, _colon[index++], false);
148 _grid->Add (labels, wxGBPosition(r, 1));
151 add_label_to_sizer (_grid, _position_label, true, wxGBPosition(r, 0));
152 _grid->Add (_position, wxGBPosition(r, 1));
155 _grid->Add (_move_to_start_of_reel, wxGBPosition(r, 1));
158 add_label_to_sizer (_grid, _full_length_label, true, wxGBPosition(r, 0));
159 _grid->Add (_full_length, wxGBPosition(r, 1));
162 add_label_to_sizer (_grid, _trim_start_label, true, wxGBPosition(r, 0));
163 _grid->Add (_trim_start, wxGBPosition(r, 1));
166 _grid->Add (_trim_start_to_playhead, wxGBPosition(r, 1));
169 add_label_to_sizer (_grid, _trim_end_label, true, wxGBPosition(r, 0));
170 _grid->Add (_trim_end, wxGBPosition(r, 1));
173 _grid->Add (_trim_end_to_playhead, wxGBPosition(r, 1));
176 add_label_to_sizer (_grid, _play_length_label, true, wxGBPosition(r, 0));
177 _grid->Add (_play_length, wxGBPosition(r, 1));
180 /* Completely speculative fix for #891 */
185 TimingPanel::update_full_length ()
188 for (auto i: _parent->selected()) {
189 check.insert (i->full_length(_parent->film()));
192 if (check.size() == 1) {
193 _full_length->set (_parent->selected().front()->full_length(_parent->film()), _parent->film()->video_frame_rate());
195 _full_length->clear ();
200 TimingPanel::update_play_length ()
203 for (auto i: _parent->selected()) {
204 check.insert (i->length_after_trim(_parent->film()));
207 if (check.size() == 1) {
208 _play_length->set (_parent->selected().front()->length_after_trim(_parent->film()), _parent->film()->video_frame_rate());
210 _play_length->clear ();
215 TimingPanel::film_content_changed (int property)
217 if (_film_content_changed_suspender.check(property)) {
221 int const film_video_frame_rate = _parent->film()->video_frame_rate ();
223 /* Here we check to see if we have exactly one different value of various
224 properties, and fill the controls with that value if so.
227 if (property == ContentProperty::POSITION) {
230 for (auto i: _parent->selected()) {
231 check.insert (i->position ());
234 if (check.size() == 1) {
235 _position->set (_parent->selected().front()->position(), film_video_frame_rate);
241 property == ContentProperty::LENGTH ||
242 property == ContentProperty::VIDEO_FRAME_RATE ||
243 property == VideoContentProperty::FRAME_TYPE
246 update_full_length ();
248 } else if (property == ContentProperty::TRIM_START) {
250 set<ContentTime> check;
251 for (auto i: _parent->selected()) {
252 check.insert (i->trim_start ());
255 if (check.size() == 1) {
256 _trim_start->set (_parent->selected().front()->trim_start (), film_video_frame_rate);
258 _trim_start->clear ();
261 } else if (property == ContentProperty::TRIM_END) {
263 set<ContentTime> check;
264 for (auto i: _parent->selected()) {
265 check.insert (i->trim_end ());
268 if (check.size() == 1) {
269 _trim_end->set (_parent->selected().front()->trim_end (), film_video_frame_rate);
276 property == ContentProperty::LENGTH ||
277 property == ContentProperty::TRIM_START ||
278 property == ContentProperty::TRIM_END ||
279 property == ContentProperty::VIDEO_FRAME_RATE ||
280 property == VideoContentProperty::FRAME_TYPE
283 update_play_length ();
286 if (property == ContentProperty::VIDEO_FRAME_RATE) {
287 set<double> check_vc;
288 shared_ptr<const Content> content;
289 for (auto i: _parent->selected()) {
290 if (i->video && i->video_frame_rate()) {
291 check_vc.insert (i->video_frame_rate().get());
294 if (i->audio && i->video_frame_rate()) {
297 if (!i->text.empty() && i->video_frame_rate()) {
304 bool have_still = false;
305 for (auto i: _parent->selected()) {
306 shared_ptr<const ImageContent> ic = dynamic_pointer_cast<const ImageContent> (i);
307 if (ic && ic->still ()) {
312 _full_length->set_editable (have_still);
313 _play_length->set_editable (!have_still);
314 setup_sensitivity ();
318 TimingPanel::position_changed ()
320 DCPTime const pos = _position->get (_parent->film()->video_frame_rate ());
321 for (auto i: _parent->selected()) {
322 i->set_position (_parent->film(), 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 for (auto 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 Suspender::Block bl = _film_content_changed_suspender.block ();
351 for (auto i: _parent->selected()) {
352 if (i->position() <= ph && ph < i->end(_parent->film())) {
353 /* The playhead is in i. Use it as a reference to work out
354 where to put the playhead post-trim; we're trying to keep the playhead
355 at the same frame of content that we're looking at pre-trim.
358 ref_frc = _parent->film()->active_frame_rate_change (i->position ());
359 ref_ph = ph - i->position() + DCPTime (i->trim_start(), ref_frc.get());
362 ContentTime const trim = _trim_start->get (i->video_frame_rate().get_value_or(_parent->film()->video_frame_rate()));
363 i->set_trim_start(_parent->film(), trim);
367 _viewer.seek(max(DCPTime(), ref_ph.get() + ref->position() - DCPTime(ref->trim_start(), ref_frc.get())), true);
370 _viewer.set_coalesce_player_changes(false);
374 TimingPanel::trim_end_changed ()
376 _viewer.set_coalesce_player_changes(true);
378 Suspender::Block bl = _film_content_changed_suspender.block ();
379 for (auto i: _parent->selected()) {
380 ContentTime const trim = _trim_end->get (i->video_frame_rate().get_value_or(_parent->film()->video_frame_rate()));
381 i->set_trim_end (trim);
384 /* XXX: maybe playhead-off-the-end-of-the-film should be handled elsewhere */
385 if (_viewer.position() >= _parent->film()->length()) {
386 _viewer.seek(_parent->film()->length() - DCPTime::from_frames(1, _parent->film()->video_frame_rate()), true);
389 _viewer.set_coalesce_player_changes(false);
393 TimingPanel::play_length_changed ()
395 DCPTime const play_length = _play_length->get (_parent->film()->video_frame_rate());
396 Suspender::Block bl = _film_content_changed_suspender.block ();
397 for (auto i: _parent->selected()) {
398 FrameRateChange const frc = _parent->film()->active_frame_rate_change (i->position ());
399 auto dcp = max(DCPTime(), i->full_length(_parent->film()) - play_length);
400 i->set_trim_end (max(ContentTime(), ContentTime(dcp, frc) - i->trim_start()));
406 TimingPanel::content_selection_changed ()
408 setup_sensitivity ();
410 film_content_changed (ContentProperty::POSITION);
411 film_content_changed (ContentProperty::LENGTH);
412 film_content_changed (ContentProperty::TRIM_START);
413 film_content_changed (ContentProperty::TRIM_END);
414 film_content_changed (ContentProperty::VIDEO_FRAME_RATE);
418 TimingPanel::film_changed(FilmProperty p)
420 if (p == FilmProperty::VIDEO_FRAME_RATE) {
421 update_full_length ();
422 update_play_length ();
427 TimingPanel::trim_start_to_playhead_clicked ()
429 auto film = _parent->film ();
430 DCPTime const ph = _viewer.position().floor(film->video_frame_rate());
431 optional<DCPTime> new_ph;
433 _viewer.set_coalesce_player_changes(true);
435 for (auto i: _parent->selected()) {
436 if (i->position() < ph && ph < i->end(film)) {
437 FrameRateChange const frc = film->active_frame_rate_change (i->position());
438 i->set_trim_start(film, i->trim_start() + ContentTime(ph - i->position(), frc));
439 new_ph = i->position ();
443 _viewer.set_coalesce_player_changes(false);
446 _viewer.seek(new_ph.get(), true);
451 TimingPanel::trim_end_to_playhead_clicked ()
453 auto film = _parent->film ();
454 auto const ph = _viewer.position().floor(film->video_frame_rate());
455 for (auto i: _parent->selected()) {
456 if (i->position() < ph && ph < i->end(film)) {
457 FrameRateChange const frc = film->active_frame_rate_change (i->position ());
458 i->set_trim_end (ContentTime(i->position() + i->full_length(film) - ph, frc) - i->trim_start());
464 TimingPanel::setup_sensitivity ()
466 bool const e = !_parent->selected().empty ();
468 _position->Enable (e);
469 _move_to_start_of_reel->Enable (e);
470 _full_length->Enable (e);
471 _trim_start->Enable (e);
472 _trim_end->Enable (e);
473 _play_length->Enable (e);
475 auto const ph = _viewer.position();
476 bool any_over_ph = false;
477 for (auto i: _parent->selected()) {
478 if (i->position() <= ph && ph < i->end(_parent->film())) {
483 _trim_start_to_playhead->Enable (any_over_ph);
484 _trim_end_to_playhead->Enable (any_over_ph);
488 TimingPanel::move_to_start_of_reel_clicked ()
490 /* Find common position of all selected content, if it exists */
492 optional<DCPTime> position;
493 for (auto i: _parent->selected()) {
495 position = i->position();
497 if (position.get() != i->position()) {
504 auto d = make_wx<MoveToDialog>(this, position, _parent->film());
506 if (d->ShowModal() == wxID_OK) {
507 for (auto i: _parent->selected()) {
508 i->set_position (_parent->film(), d->position());