2 Copyright (C) 2018-2021 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/>.
23 #include "film_viewer.h"
25 #include "playhead_to_timecode_dialog.h"
26 #include "playhead_to_frame_dialog.h"
27 #include "content_view.h"
28 #include "static_text.h"
29 #include "check_box.h"
30 #include "dcpomatic_button.h"
31 #include "lib/job_manager.h"
32 #include "lib/player_video.h"
33 #include "lib/dcp_content.h"
35 #include "lib/examine_content_job.h"
36 #include "lib/content_factory.h"
37 #include "lib/cross.h"
41 #include <dcp/reel_picture_asset.h>
43 #include <wx/tglbtn.h>
44 #include <wx/listctrl.h>
45 #include <wx/progdlg.h>
53 using boost::optional;
54 using std::shared_ptr;
56 using std::dynamic_pointer_cast;
57 #if BOOST_VERSION >= 106100
58 using namespace boost::placeholders;
60 using namespace dcpomatic;
63 Controls::Controls (wxWindow* parent, shared_ptr<FilmViewer> viewer, bool editor_controls)
65 , _slider (new wxSlider(this, wxID_ANY, 0, 0, 4096))
67 , _slider_being_moved (false)
68 , _outline_content (0)
70 , _jump_to_selected (0)
71 , _rewind_button (new Button(this, wxT("|<")))
72 , _back_button (new Button(this, wxT("<")))
73 , _forward_button (new Button(this, wxT(">")))
74 , _frame_number (new StaticText(this, wxT("")))
75 , _timecode (new StaticText(this, wxT("")))
78 _v_sizer = new wxBoxSizer (wxVERTICAL);
81 auto view_options = new wxBoxSizer (wxHORIZONTAL);
82 if (editor_controls) {
83 _outline_content = new CheckBox (this, _("Outline content"));
84 view_options->Add (_outline_content, 0, wxRIGHT | wxALIGN_CENTER_VERTICAL, DCPOMATIC_SIZER_GAP);
85 _eye = new wxChoice (this, wxID_ANY);
86 _eye->Append (_("Left"));
87 _eye->Append (_("Right"));
88 _eye->SetSelection (0);
89 view_options->Add (_eye, 0, wxLEFT | wxRIGHT | wxALIGN_CENTER_VERTICAL, DCPOMATIC_SIZER_GAP);
90 _jump_to_selected = new CheckBox (this, _("Jump to selected content"));
91 view_options->Add (_jump_to_selected, 0, wxLEFT | wxRIGHT | wxALIGN_CENTER_VERTICAL, DCPOMATIC_SIZER_GAP);
94 _v_sizer->Add (view_options, 0, wxALL, DCPOMATIC_SIZER_GAP);
96 auto h_sizer = new wxBoxSizer (wxHORIZONTAL);
98 auto time_sizer = new wxBoxSizer (wxVERTICAL);
99 time_sizer->Add (_frame_number, 0, wxEXPAND);
100 time_sizer->Add (_timecode, 0, wxEXPAND);
102 h_sizer->Add (_rewind_button, 0, wxALL | wxALIGN_CENTER_VERTICAL, 2);
103 h_sizer->Add (time_sizer, 0, wxEXPAND);
104 h_sizer->Add (_back_button, 0, wxALL | wxALIGN_CENTER_VERTICAL, 2);
105 h_sizer->Add (_forward_button, 0, wxALL | wxALIGN_CENTER_VERTICAL, 2);
107 _button_sizer = new wxBoxSizer (wxHORIZONTAL);
108 h_sizer->Add (_button_sizer, 0, wxEXPAND);
110 h_sizer->Add (_slider, 1, wxEXPAND);
112 _v_sizer->Add (h_sizer, 0, wxEXPAND | wxALL, 6);
115 _frame_number->SetMinSize (wxSize(100, -1));
116 _rewind_button->SetMinSize (wxSize(48, -1));
117 _back_button->SetMinSize (wxSize(48, -1));
118 _forward_button->SetMinSize (wxSize(48, -1));
120 _frame_number->SetMinSize (wxSize (84, -1));
121 _rewind_button->SetMinSize (wxSize (32, -1));
122 _back_button->SetMinSize (wxSize (32, -1));
123 _forward_button->SetMinSize (wxSize (32, -1));
127 _eye->Bind (wxEVT_CHOICE, boost::bind (&Controls::eye_changed, this));
129 if (_outline_content) {
130 _outline_content->Bind (wxEVT_CHECKBOX, boost::bind (&Controls::outline_content_changed, this));
133 _slider->Bind (wxEVT_SCROLL_THUMBTRACK, boost::bind(&Controls::slider_moved, this, false));
134 _slider->Bind (wxEVT_SCROLL_PAGEUP, boost::bind(&Controls::slider_moved, this, true));
135 _slider->Bind (wxEVT_SCROLL_PAGEDOWN, boost::bind(&Controls::slider_moved, this, true));
136 _slider->Bind (wxEVT_SCROLL_THUMBRELEASE, boost::bind(&Controls::slider_released, this));
137 _rewind_button->Bind (wxEVT_LEFT_DOWN, boost::bind(&Controls::rewind_clicked, this, _1));
138 _back_button->Bind (wxEVT_LEFT_DOWN, boost::bind(&Controls::back_clicked, this, _1));
139 _forward_button->Bind (wxEVT_LEFT_DOWN, boost::bind(&Controls::forward_clicked, this, _1));
140 _frame_number->Bind (wxEVT_LEFT_DOWN, boost::bind(&Controls::frame_number_clicked, this));
141 _timecode->Bind (wxEVT_LEFT_DOWN, boost::bind(&Controls::timecode_clicked, this));
142 if (_jump_to_selected) {
143 _jump_to_selected->Bind (wxEVT_CHECKBOX, boost::bind (&Controls::jump_to_selected_clicked, this));
144 _jump_to_selected->SetValue (Config::instance()->jump_to_selected ());
147 _viewer->Started.connect (boost::bind(&Controls::started, this));
148 _viewer->Stopped.connect (boost::bind(&Controls::stopped, this));
150 Bind (wxEVT_TIMER, boost::bind(&Controls::update_position, this));
151 _timer.Start (80, wxTIMER_CONTINUOUS);
153 set_film (_viewer->film());
155 setup_sensitivity ();
157 JobManager::instance()->ActiveJobsChanged.connect (
158 bind (&Controls::active_jobs_changed, this, _2)
161 _config_changed_connection = Config::instance()->Changed.connect (bind(&Controls::config_changed, this, _1));
162 config_changed (Config::OTHER);
166 Controls::config_changed (int)
168 setup_sensitivity ();
175 setup_sensitivity ();
182 setup_sensitivity ();
187 Controls::update_position ()
189 if (!_slider_being_moved && !_viewer->pending_idle_get()) {
190 update_position_label ();
191 update_position_slider ();
197 Controls::eye_changed ()
199 _viewer->set_eyes (_eye->GetSelection() == 0 ? Eyes::LEFT : Eyes::RIGHT);
204 Controls::outline_content_changed ()
206 _viewer->set_outline_content (_outline_content->GetValue());
210 /** @param page true if this was a PAGEUP/PAGEDOWN event for which we won't receive a THUMBRELEASE */
212 Controls::slider_moved (bool page)
218 if (!page && !_slider_being_moved) {
219 /* This is the first event of a drag; stop playback for the duration of the drag */
221 _slider_being_moved = true;
224 DCPTime t (_slider->GetValue() * _film->length().get() / 4096);
225 t = t.round (_film->video_frame_rate());
226 /* Ensure that we hit the end of the film at the end of the slider. In particular, we
227 need to do an accurate seek in case there isn't a keyframe near the end.
229 bool accurate = false;
230 if (t >= _film->length ()) {
231 t = _film->length() - _viewer->one_video_frame();
234 _viewer->seek (t, accurate);
235 update_position_label ();
239 "playback-seeked %s", t.timecode(_film->video_frame_rate()).c_str()
246 Controls::slider_released ()
248 /* Restart after a drag */
250 _slider_being_moved = false;
255 Controls::update_position_slider ()
258 _slider->SetValue (0);
262 auto const len = _film->length ();
265 int const new_slider_position = 4096 * _viewer->position().get() / len.get();
266 if (new_slider_position != _slider->GetValue()) {
267 _slider->SetValue (new_slider_position);
274 Controls::update_position_label ()
277 checked_set (_frame_number, wxT("0"));
278 checked_set (_timecode, wxT("0:0:0.0"));
282 double const fps = _film->video_frame_rate ();
283 /* Count frame number from 1 ... not sure if this is the best idea */
284 checked_set (_frame_number, wxString::Format (wxT("%ld"), lrint (_viewer->position().seconds() * fps) + 1));
285 checked_set (_timecode, time_to_timecode (_viewer->position(), fps));
290 Controls::active_jobs_changed (optional<string> j)
293 setup_sensitivity ();
298 Controls::nudge_amount (wxKeyboardState& ev)
300 auto amount = _viewer->one_video_frame ();
302 if (ev.ShiftDown() && !ev.ControlDown()) {
303 amount = DCPTime::from_seconds (1);
304 } else if (!ev.ShiftDown() && ev.ControlDown()) {
305 amount = DCPTime::from_seconds (10);
306 } else if (ev.ShiftDown() && ev.ControlDown()) {
307 amount = DCPTime::from_seconds (60);
315 Controls::rewind_clicked (wxMouseEvent& ev)
317 _viewer->seek (DCPTime(), true);
323 Controls::back_frame ()
325 _viewer->seek_by (-_viewer->one_video_frame(), true);
330 Controls::forward_frame ()
332 _viewer->seek_by (_viewer->one_video_frame(), true);
337 Controls::back_clicked (wxKeyboardState& ev)
339 _viewer->seek_by (-nudge_amount(ev), true);
344 Controls::forward_clicked (wxKeyboardState& ev)
346 _viewer->seek_by (nudge_amount(ev), true);
351 Controls::setup_sensitivity ()
353 /* examine content is the only job which stops the viewer working */
354 bool const active_job = _active_job && *_active_job != "examine_content";
355 bool const c = _film && !_film->content().empty() && !active_job;
358 _rewind_button->Enable (c);
359 _back_button->Enable (c);
360 _forward_button->Enable (c);
361 if (_outline_content) {
362 _outline_content->Enable (c);
364 _frame_number->Enable (c);
365 _timecode->Enable (c);
366 if (_jump_to_selected) {
367 _jump_to_selected->Enable (c);
371 _eye->Enable (c && _film->three_d ());
377 Controls::timecode_clicked ()
379 auto dialog = new PlayheadToTimecodeDialog (this, _viewer->position(), _film->video_frame_rate());
380 if (dialog->ShowModal() == wxID_OK) {
381 _viewer->seek (dialog->get(), true);
388 Controls::frame_number_clicked ()
390 auto dialog = new PlayheadToFrameDialog (this, _viewer->position(), _film->video_frame_rate());
391 if (dialog->ShowModal() == wxID_OK) {
392 _viewer->seek (dialog->get(), true);
399 Controls::jump_to_selected_clicked ()
401 Config::instance()->set_jump_to_selected (_jump_to_selected->GetValue ());
406 Controls::set_film (shared_ptr<Film> film)
415 _film_change_connection = _film->Change.connect (boost::bind(&Controls::film_change, this, _1, _2));
418 setup_sensitivity ();
420 update_position_slider ();
421 update_position_label ();
426 Controls::film () const
433 Controls::film_change (ChangeType type, Film::Property p)
435 if (type == ChangeType::DONE) {
436 if (p == Film::Property::CONTENT) {
437 setup_sensitivity ();
438 update_position_label ();
439 update_position_slider ();
440 } else if (p == Film::Property::THREE_D) {
441 setup_sensitivity ();
448 Controls::seek (int slider)
450 _slider->SetValue (slider);
451 slider_moved (false);