2 Copyright (C) 2018-2019 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 "film_viewer.h"
24 #include "playhead_to_timecode_dialog.h"
25 #include "playhead_to_frame_dialog.h"
26 #include "content_view.h"
27 #include "static_text.h"
28 #include "check_box.h"
29 #include "dcpomatic_button.h"
30 #include "lib/job_manager.h"
31 #include "lib/player_video.h"
32 #include "lib/dcp_content.h"
34 #include "lib/examine_content_job.h"
35 #include "lib/content_factory.h"
36 #include "lib/cross.h"
40 #include <dcp/reel_picture_asset.h>
42 #include <wx/tglbtn.h>
43 #include <wx/listctrl.h>
44 #include <wx/progdlg.h>
51 using boost::optional;
52 using boost::shared_ptr;
53 using boost::weak_ptr;
54 using boost::dynamic_pointer_cast;
55 using namespace dcpomatic;
57 Controls::Controls (wxWindow* parent, shared_ptr<FilmViewer> viewer, bool editor_controls)
59 , _slider (new wxSlider (this, wxID_ANY, 0, 0, 4096))
61 , _slider_being_moved (false)
62 , _outline_content (0)
64 , _jump_to_selected (0)
65 , _rewind_button (new Button (this, wxT("|<")))
66 , _back_button (new Button (this, wxT("<")))
67 , _forward_button (new Button (this, wxT(">")))
68 , _frame_number (new StaticText (this, wxT("")))
69 , _timecode (new StaticText (this, wxT("")))
72 _v_sizer = new wxBoxSizer (wxVERTICAL);
75 wxBoxSizer* view_options = new wxBoxSizer (wxHORIZONTAL);
76 if (editor_controls) {
77 _outline_content = new CheckBox (this, _("Outline content"));
78 view_options->Add (_outline_content, 0, wxRIGHT | wxALIGN_CENTER_VERTICAL, DCPOMATIC_SIZER_GAP);
79 _eye = new wxChoice (this, wxID_ANY);
80 _eye->Append (_("Left"));
81 _eye->Append (_("Right"));
82 _eye->SetSelection (0);
83 view_options->Add (_eye, 0, wxLEFT | wxRIGHT | wxALIGN_CENTER_VERTICAL, DCPOMATIC_SIZER_GAP);
84 _jump_to_selected = new CheckBox (this, _("Jump to selected content"));
85 view_options->Add (_jump_to_selected, 0, wxLEFT | wxRIGHT | wxALIGN_CENTER_VERTICAL, DCPOMATIC_SIZER_GAP);
88 _v_sizer->Add (view_options, 0, wxALL, DCPOMATIC_SIZER_GAP);
90 wxBoxSizer* h_sizer = new wxBoxSizer (wxHORIZONTAL);
92 wxBoxSizer* time_sizer = new wxBoxSizer (wxVERTICAL);
93 time_sizer->Add (_frame_number, 0, wxEXPAND);
94 time_sizer->Add (_timecode, 0, wxEXPAND);
96 h_sizer->Add (_rewind_button, 0, wxALL, 2);
97 h_sizer->Add (time_sizer, 0, wxEXPAND);
98 h_sizer->Add (_back_button, 0, wxALL, 2);
99 h_sizer->Add (_forward_button, 0, wxALL, 2);
101 _button_sizer = new wxBoxSizer (wxHORIZONTAL);
102 h_sizer->Add (_button_sizer, 0, wxEXPAND);
104 h_sizer->Add (_slider, 1, wxEXPAND);
106 _v_sizer->Add (h_sizer, 0, wxEXPAND | wxALL, 6);
108 _frame_number->SetMinSize (wxSize (84, -1));
109 _rewind_button->SetMinSize (wxSize (32, -1));
110 _back_button->SetMinSize (wxSize (32, -1));
111 _forward_button->SetMinSize (wxSize (32, -1));
114 _eye->Bind (wxEVT_CHOICE, boost::bind (&Controls::eye_changed, this));
116 if (_outline_content) {
117 _outline_content->Bind (wxEVT_CHECKBOX, boost::bind (&Controls::outline_content_changed, this));
120 _slider->Bind (wxEVT_SCROLL_THUMBTRACK, boost::bind(&Controls::slider_moved, this, false));
121 _slider->Bind (wxEVT_SCROLL_PAGEUP, boost::bind(&Controls::slider_moved, this, true));
122 _slider->Bind (wxEVT_SCROLL_PAGEDOWN, boost::bind(&Controls::slider_moved, this, true));
123 _slider->Bind (wxEVT_SCROLL_THUMBRELEASE, boost::bind(&Controls::slider_released, this));
124 _rewind_button->Bind (wxEVT_LEFT_DOWN, boost::bind(&Controls::rewind_clicked, this, _1));
125 _back_button->Bind (wxEVT_LEFT_DOWN, boost::bind(&Controls::back_clicked, this, _1));
126 _forward_button->Bind (wxEVT_LEFT_DOWN, boost::bind(&Controls::forward_clicked, this, _1));
127 _frame_number->Bind (wxEVT_LEFT_DOWN, boost::bind(&Controls::frame_number_clicked, this));
128 _timecode->Bind (wxEVT_LEFT_DOWN, boost::bind(&Controls::timecode_clicked, this));
129 if (_jump_to_selected) {
130 _jump_to_selected->Bind (wxEVT_CHECKBOX, boost::bind (&Controls::jump_to_selected_clicked, this));
131 _jump_to_selected->SetValue (Config::instance()->jump_to_selected ());
134 _viewer->Started.connect (boost::bind(&Controls::started, this));
135 _viewer->Stopped.connect (boost::bind(&Controls::stopped, this));
137 Bind (wxEVT_TIMER, boost::bind(&Controls::update_position, this));
138 _timer.Start (80, wxTIMER_CONTINUOUS);
140 set_film (_viewer->film());
142 setup_sensitivity ();
144 JobManager::instance()->ActiveJobsChanged.connect (
145 bind (&Controls::active_jobs_changed, this, _2)
148 _config_changed_connection = Config::instance()->Changed.connect (bind(&Controls::config_changed, this, _1));
149 config_changed (Config::OTHER);
153 Controls::config_changed (int)
155 setup_sensitivity ();
161 setup_sensitivity ();
167 setup_sensitivity ();
171 Controls::update_position ()
173 if (!_slider_being_moved && !_viewer->pending_idle_get()) {
174 update_position_label ();
175 update_position_slider ();
180 Controls::eye_changed ()
182 _viewer->set_eyes (_eye->GetSelection() == 0 ? EYES_LEFT : EYES_RIGHT);
186 Controls::outline_content_changed ()
188 _viewer->set_outline_content (_outline_content->GetValue());
191 /** @param page true if this was a PAGEUP/PAGEDOWN event for which we won't receive a THUMBRELEASE */
193 Controls::slider_moved (bool page)
199 if (!page && !_slider_being_moved) {
200 /* This is the first event of a drag; stop playback for the duration of the drag */
202 _slider_being_moved = true;
205 DCPTime t (_slider->GetValue() * _film->length().get() / 4096);
206 t = t.round (_film->video_frame_rate());
207 /* Ensure that we hit the end of the film at the end of the slider. In particular, we
208 need to do an accurate seek in case there isn't a keyframe near the end.
210 bool accurate = false;
211 if (t >= _film->length ()) {
212 t = _film->length() - _viewer->one_video_frame();
215 _viewer->seek (t, accurate);
216 update_position_label ();
220 "playback-seeked %s", t.timecode(_film->video_frame_rate()).c_str()
226 Controls::slider_released ()
228 /* Restart after a drag */
230 _slider_being_moved = false;
234 Controls::update_position_slider ()
237 _slider->SetValue (0);
241 DCPTime const len = _film->length ();
244 int const new_slider_position = 4096 * _viewer->position().get() / len.get();
245 if (new_slider_position != _slider->GetValue()) {
246 _slider->SetValue (new_slider_position);
252 Controls::update_position_label ()
255 _frame_number->SetLabel ("0");
256 _timecode->SetLabel ("0:0:0.0");
260 double const fps = _film->video_frame_rate ();
261 /* Count frame number from 1 ... not sure if this is the best idea */
262 _frame_number->SetLabel (wxString::Format (wxT("%ld"), lrint (_viewer->position().seconds() * fps) + 1));
263 _timecode->SetLabel (time_to_timecode (_viewer->position(), fps));
267 Controls::active_jobs_changed (optional<string> j)
270 setup_sensitivity ();
274 Controls::nudge_amount (wxKeyboardState& ev)
276 DCPTime amount = _viewer->one_video_frame ();
278 if (ev.ShiftDown() && !ev.ControlDown()) {
279 amount = DCPTime::from_seconds (1);
280 } else if (!ev.ShiftDown() && ev.ControlDown()) {
281 amount = DCPTime::from_seconds (10);
282 } else if (ev.ShiftDown() && ev.ControlDown()) {
283 amount = DCPTime::from_seconds (60);
290 Controls::rewind_clicked (wxMouseEvent& ev)
292 _viewer->seek (DCPTime(), true);
297 Controls::back_frame ()
299 _viewer->seek_by (-_viewer->one_video_frame(), true);
303 Controls::forward_frame ()
305 _viewer->seek_by (_viewer->one_video_frame(), true);
309 Controls::back_clicked (wxKeyboardState& ev)
311 _viewer->seek_by (-nudge_amount(ev), true);
315 Controls::forward_clicked (wxKeyboardState& ev)
317 _viewer->seek_by (nudge_amount(ev), true);
321 Controls::setup_sensitivity ()
323 /* examine content is the only job which stops the viewer working */
324 bool const active_job = _active_job && *_active_job != "examine_content";
325 bool const c = _film && !_film->content().empty() && !active_job;
328 _rewind_button->Enable (c);
329 _back_button->Enable (c);
330 _forward_button->Enable (c);
331 if (_outline_content) {
332 _outline_content->Enable (c);
334 _frame_number->Enable (c);
335 _timecode->Enable (c);
336 if (_jump_to_selected) {
337 _jump_to_selected->Enable (c);
341 _eye->Enable (c && _film->three_d ());
346 Controls::timecode_clicked ()
348 PlayheadToTimecodeDialog* dialog = new PlayheadToTimecodeDialog (this, _viewer->position(), _film->video_frame_rate());
349 if (dialog->ShowModal() == wxID_OK) {
350 _viewer->seek (dialog->get(), true);
356 Controls::frame_number_clicked ()
358 PlayheadToFrameDialog* dialog = new PlayheadToFrameDialog (this, _viewer->position(), _film->video_frame_rate());
359 if (dialog->ShowModal() == wxID_OK) {
360 _viewer->seek (dialog->get(), true);
366 Controls::jump_to_selected_clicked ()
368 Config::instance()->set_jump_to_selected (_jump_to_selected->GetValue ());
372 Controls::set_film (shared_ptr<Film> film)
381 _film_change_connection = _film->Change.connect (boost::bind(&Controls::film_change, this, _1, _2));
384 setup_sensitivity ();
386 update_position_slider ();
387 update_position_label ();
391 Controls::film () const
397 Controls::film_change (ChangeType type, Film::Property p)
399 if (type == CHANGE_TYPE_DONE) {
400 if (p == Film::CONTENT) {
401 setup_sensitivity ();
402 update_position_label ();
403 update_position_slider ();
404 } else if (p == Film::THREE_D) {
405 setup_sensitivity ();
411 Controls::seek (int slider)
413 _slider->SetValue (slider);
414 slider_moved (false);