Update new panels when they are created.
[dcpomatic.git] / src / wx / video_panel.cc
1 /*
2     Copyright (C) 2012-2016 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
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.
10
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.
15
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/>.
18
19 */
20
21 #include "filter_dialog.h"
22 #include "video_panel.h"
23 #include "wx_util.h"
24 #include "content_colour_conversion_dialog.h"
25 #include "content_widget.h"
26 #include "content_panel.h"
27 #include "lib/filter.h"
28 #include "lib/ffmpeg_content.h"
29 #include "lib/colour_conversion.h"
30 #include "lib/config.h"
31 #include "lib/util.h"
32 #include "lib/ratio.h"
33 #include "lib/frame_rate_change.h"
34 #include "lib/dcp_content.h"
35 #include "lib/video_content.h"
36 #include <wx/spinctrl.h>
37 #include <boost/foreach.hpp>
38 #include <set>
39 #include <iostream>
40
41 using std::vector;
42 using std::string;
43 using std::pair;
44 using std::cout;
45 using std::list;
46 using std::set;
47 using boost::shared_ptr;
48 using boost::dynamic_pointer_cast;
49 using boost::bind;
50 using boost::optional;
51
52 static VideoContentScale
53 index_to_scale (int n)
54 {
55         vector<VideoContentScale> scales = VideoContentScale::all ();
56         DCPOMATIC_ASSERT (n >= 0);
57         DCPOMATIC_ASSERT (n < int (scales.size ()));
58         return scales[n];
59 }
60
61 static int
62 scale_to_index (VideoContentScale scale)
63 {
64         vector<VideoContentScale> scales = VideoContentScale::all ();
65         for (size_t i = 0; i < scales.size(); ++i) {
66                 if (scales[i] == scale) {
67                         return i;
68                 }
69         }
70
71         DCPOMATIC_ASSERT (false);
72 }
73
74 VideoPanel::VideoPanel (ContentPanel* p)
75         : ContentSubPanel (p, _("Video"))
76 {
77         wxBoxSizer* reference_sizer = new wxBoxSizer (wxVERTICAL);
78
79         _reference = new wxCheckBox (this, wxID_ANY, _("Use this DCP's video as OV and make VF"));
80         reference_sizer->Add (_reference, 0, wxLEFT | wxRIGHT | wxTOP, DCPOMATIC_SIZER_GAP);
81
82         _reference_note = new wxStaticText (this, wxID_ANY, _(""));
83         _reference_note->Wrap (200);
84         reference_sizer->Add (_reference_note, 0, wxLEFT | wxRIGHT, DCPOMATIC_SIZER_GAP);
85         wxFont font = _reference_note->GetFont();
86         font.SetStyle(wxFONTSTYLE_ITALIC);
87         font.SetPointSize(font.GetPointSize() - 1);
88         _reference_note->SetFont(font);
89
90         _sizer->Add (reference_sizer);
91
92         wxGridBagSizer* grid = new wxGridBagSizer (DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
93         _sizer->Add (grid, 0, wxALL, 8);
94
95         int r = 0;
96
97         add_label_to_sizer (grid, this, _("Type"), true, wxGBPosition (r, 0));
98         _frame_type = new ContentChoice<VideoContent, VideoFrameType> (
99                 this,
100                 new wxChoice (this, wxID_ANY),
101                 VideoContentProperty::FRAME_TYPE,
102                 &Content::video,
103                 boost::mem_fn (&VideoContent::frame_type),
104                 boost::mem_fn (&VideoContent::set_frame_type),
105                 &caster<int, VideoFrameType>,
106                 &caster<VideoFrameType, int>
107                 );
108         _frame_type->add (grid, wxGBPosition (r, 1), wxGBSpan (1, 2));
109         ++r;
110
111         add_label_to_sizer (grid, this, _("Crop"), true, wxGBPosition (r, 0));
112
113         int cr = 0;
114         wxGridBagSizer* crop = new wxGridBagSizer (DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
115
116         add_label_to_sizer (crop, this, _("Left"), true, wxGBPosition (cr, 0));
117         _left_crop = new ContentSpinCtrl<VideoContent> (
118                 this,
119                 new wxSpinCtrl (this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize (64, -1)),
120                 VideoContentProperty::CROP,
121                 &Content::video,
122                 boost::mem_fn (&VideoContent::left_crop),
123                 boost::mem_fn (&VideoContent::set_left_crop)
124                 );
125         _left_crop->add (crop, wxGBPosition (cr, 1));
126
127         add_label_to_sizer (crop, this, _("Right"), true, wxGBPosition (cr, 2));
128         _right_crop = new ContentSpinCtrl<VideoContent> (
129                 this,
130                 new wxSpinCtrl (this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize (64, -1)),
131                 VideoContentProperty::CROP,
132                 &Content::video,
133                 boost::mem_fn (&VideoContent::right_crop),
134                 boost::mem_fn (&VideoContent::set_right_crop)
135                 );
136         _right_crop->add (crop, wxGBPosition (cr, 3));
137
138         ++cr;
139
140         add_label_to_sizer (crop, this, _("Top"), true, wxGBPosition (cr, 0));
141         _top_crop = new ContentSpinCtrl<VideoContent> (
142                 this,
143                 new wxSpinCtrl (this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize (64, -1)),
144                 VideoContentProperty::CROP,
145                 &Content::video,
146                 boost::mem_fn (&VideoContent::top_crop),
147                 boost::mem_fn (&VideoContent::set_top_crop)
148                 );
149         _top_crop->add (crop, wxGBPosition (cr, 1));
150
151         add_label_to_sizer (crop, this, _("Bottom"), true, wxGBPosition (cr, 2));
152         _bottom_crop = new ContentSpinCtrl<VideoContent> (
153                 this,
154                 new wxSpinCtrl (this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize (64, -1)),
155                 VideoContentProperty::CROP,
156                 &Content::video,
157                 boost::mem_fn (&VideoContent::bottom_crop),
158                 boost::mem_fn (&VideoContent::set_bottom_crop)
159                 );
160         _bottom_crop->add (crop, wxGBPosition (cr, 3));
161
162         grid->Add (crop, wxGBPosition (r, 1), wxGBSpan (2, 3));
163         r += 2;
164
165         add_label_to_sizer (grid, this, _("Fade in"), true, wxGBPosition (r, 0));
166         _fade_in = new Timecode<ContentTime> (this);
167         grid->Add (_fade_in, wxGBPosition (r, 1), wxGBSpan (1, 3));
168         ++r;
169
170         add_label_to_sizer (grid, this, _("Fade out"), true, wxGBPosition (r, 0));
171         _fade_out = new Timecode<ContentTime> (this);
172         grid->Add (_fade_out, wxGBPosition (r, 1), wxGBSpan (1, 3));
173         ++r;
174
175         add_label_to_sizer (grid, this, _("Scale to"), true, wxGBPosition (r, 0));
176         _scale = new ContentChoice<VideoContent, VideoContentScale> (
177                 this,
178                 new wxChoice (this, wxID_ANY),
179                 VideoContentProperty::SCALE,
180                 &Content::video,
181                 boost::mem_fn (&VideoContent::scale),
182                 boost::mem_fn (&VideoContent::set_scale),
183                 &index_to_scale,
184                 &scale_to_index
185                 );
186         _scale->add (grid, wxGBPosition (r, 1), wxGBSpan (1, 2));
187         ++r;
188
189         wxClientDC dc (this);
190         wxSize size = dc.GetTextExtent (wxT ("A quite long name"));
191         size.SetHeight (-1);
192
193         add_label_to_sizer (grid, this, _("Filters"), true, wxGBPosition (r, 0));
194         {
195                 wxSizer* s = new wxBoxSizer (wxHORIZONTAL);
196
197                 _filters = new wxStaticText (this, wxID_ANY, _("None"), wxDefaultPosition, size);
198                 s->Add (_filters, 1, wxALIGN_CENTER_VERTICAL | wxTOP | wxBOTTOM | wxRIGHT, 6);
199                 _filters_button = new wxButton (this, wxID_ANY, _("Edit..."));
200                 s->Add (_filters_button, 0, wxALIGN_CENTER_VERTICAL);
201
202                 grid->Add (s, wxGBPosition (r, 1), wxDefaultSpan, wxALIGN_CENTER_VERTICAL);
203         }
204         ++r;
205
206         add_label_to_sizer (grid, this, _("Colour conversion"), true, wxGBPosition (r, 0));
207         {
208                 wxSizer* s = new wxBoxSizer (wxHORIZONTAL);
209
210                 _colour_conversion = new wxChoice (this, wxID_ANY, wxDefaultPosition, size);
211                 _colour_conversion->Append (_("None"));
212                 BOOST_FOREACH (PresetColourConversion const & i, PresetColourConversion::all()) {
213                         _colour_conversion->Append (std_to_wx (i.name));
214                 }
215
216                 /// TRANSLATORS: translate the word "Custom" here; do not include the "Colour|" prefix
217                 _colour_conversion->Append (S_("Colour|Custom"));
218                 s->Add (_colour_conversion, 1, wxALIGN_CENTER_VERTICAL | wxTOP | wxBOTTOM | wxRIGHT, 6);
219
220                 _edit_colour_conversion_button = new wxButton (this, wxID_ANY, _("Edit..."));
221                 s->Add (_edit_colour_conversion_button, 0, wxALIGN_CENTER_VERTICAL);
222
223                 grid->Add (s, wxGBPosition (r, 1), wxDefaultSpan, wxALIGN_CENTER_VERTICAL);
224         }
225         ++r;
226
227         _description = new wxStaticText (this, wxID_ANY, wxT ("\n \n \n \n \n"), wxDefaultPosition, wxDefaultSize);
228         grid->Add (_description, wxGBPosition (r, 0), wxGBSpan (1, 4), wxEXPAND | wxALIGN_CENTER_VERTICAL, 6);
229         _description->SetFont(font);
230         ++r;
231
232         _left_crop->wrapped()->SetRange (0, 1024);
233         _top_crop->wrapped()->SetRange (0, 1024);
234         _right_crop->wrapped()->SetRange (0, 1024);
235         _bottom_crop->wrapped()->SetRange (0, 1024);
236
237         _scale->wrapped()->Clear ();
238         BOOST_FOREACH (VideoContentScale const & i, VideoContentScale::all ()) {
239                 _scale->wrapped()->Append (std_to_wx (i.name ()));
240         }
241
242         _frame_type->wrapped()->Append (_("2D"));
243         _frame_type->wrapped()->Append (_("3D"));
244         _frame_type->wrapped()->Append (_("3D left/right"));
245         _frame_type->wrapped()->Append (_("3D top/bottom"));
246         _frame_type->wrapped()->Append (_("3D alternate"));
247         _frame_type->wrapped()->Append (_("3D left only"));
248         _frame_type->wrapped()->Append (_("3D right only"));
249
250         content_selection_changed ();
251
252         _fade_in->Changed.connect (boost::bind (&VideoPanel::fade_in_changed, this));
253         _fade_out->Changed.connect (boost::bind (&VideoPanel::fade_out_changed, this));
254
255         _reference->Bind                     (wxEVT_CHECKBOX, boost::bind (&VideoPanel::reference_clicked, this));
256         _filters_button->Bind                (wxEVT_BUTTON,   boost::bind (&VideoPanel::edit_filters_clicked, this));
257         _colour_conversion->Bind             (wxEVT_CHOICE,   boost::bind (&VideoPanel::colour_conversion_changed, this));
258         _edit_colour_conversion_button->Bind (wxEVT_BUTTON,   boost::bind (&VideoPanel::edit_colour_conversion_clicked, this));
259 }
260
261 void
262 VideoPanel::film_changed (Film::Property property)
263 {
264         switch (property) {
265         case Film::VIDEO_FRAME_RATE:
266         case Film::CONTAINER:
267                 setup_description ();
268                 setup_sensitivity ();
269                 break;
270         case Film::RESOLUTION:
271                 setup_description ();
272                 break;
273         case Film::REEL_TYPE:
274         case Film::INTEROP:
275                 setup_sensitivity ();
276                 break;
277         default:
278                 break;
279         }
280 }
281
282 void
283 VideoPanel::film_content_changed (int property)
284 {
285         ContentList vc = _parent->selected_video ();
286         shared_ptr<Content> vcs;
287         shared_ptr<FFmpegContent> fcs;
288         if (!vc.empty ()) {
289                 vcs = vc.front ();
290                 fcs = dynamic_pointer_cast<FFmpegContent> (vcs);
291         }
292
293         if (property == ContentProperty::VIDEO_FRAME_RATE ||
294             property == VideoContentProperty::FRAME_TYPE ||
295             property == VideoContentProperty::CROP ||
296             property == VideoContentProperty::SCALE) {
297                 setup_description ();
298         } else if (property == VideoContentProperty::COLOUR_CONVERSION) {
299                 if (vcs && vcs->video->colour_conversion ()) {
300                         optional<size_t> preset = vcs->video->colour_conversion().get().preset ();
301                         vector<PresetColourConversion> cc = PresetColourConversion::all ();
302                         if (preset) {
303                                 checked_set (_colour_conversion, preset.get() + 1);
304                         } else {
305                                 checked_set (_colour_conversion, cc.size() + 1);
306                         }
307                 } else {
308                         checked_set (_colour_conversion, 0);
309                 }
310
311                 setup_sensitivity ();
312
313         } else if (property == FFmpegContentProperty::FILTERS) {
314                 if (fcs) {
315                         string p = Filter::ffmpeg_string (fcs->filters ());
316                         if (p.empty ()) {
317                                 checked_set (_filters, _("None"));
318                         } else {
319                                 if (p.length() > 25) {
320                                         p = p.substr (0, 25) + "...";
321                                 }
322                                 checked_set (_filters, p);
323                         }
324                 }
325         } else if (property == VideoContentProperty::FADE_IN) {
326                 set<Frame> check;
327                 BOOST_FOREACH (shared_ptr<const Content> i, vc) {
328                         check.insert (i->video->fade_in ());
329                 }
330
331                 if (check.size() == 1) {
332                         _fade_in->set (
333                                 ContentTime::from_frames (vc.front()->video->fade_in (), vc.front()->active_video_frame_rate ()),
334                                 vc.front()->active_video_frame_rate ()
335                                 );
336                 } else {
337                         _fade_in->clear ();
338                 }
339         } else if (property == VideoContentProperty::FADE_OUT) {
340                 set<Frame> check;
341                 BOOST_FOREACH (shared_ptr<const Content> i, vc) {
342                         check.insert (i->video->fade_out ());
343                 }
344
345                 if (check.size() == 1) {
346                         _fade_out->set (
347                                 ContentTime::from_frames (vc.front()->video->fade_out (), vc.front()->active_video_frame_rate ()),
348                                 vc.front()->active_video_frame_rate ()
349                                 );
350                 } else {
351                         _fade_out->clear ();
352                 }
353         } else if (property == DCPContentProperty::REFERENCE_VIDEO) {
354                 if (vc.size() == 1) {
355                         shared_ptr<DCPContent> dcp = dynamic_pointer_cast<DCPContent> (vc.front ());
356                         checked_set (_reference, dcp ? dcp->reference_video () : false);
357                 } else {
358                         checked_set (_reference, false);
359                 }
360
361                 setup_sensitivity ();
362         }
363 }
364
365 /** Called when the `Edit filters' button has been clicked */
366 void
367 VideoPanel::edit_filters_clicked ()
368 {
369         FFmpegContentList c = _parent->selected_ffmpeg ();
370         if (c.size() != 1) {
371                 return;
372         }
373
374         FilterDialog* d = new FilterDialog (this, c.front()->filters());
375         d->ActiveChanged.connect (bind (&FFmpegContent::set_filters, c.front(), _1));
376         d->ShowModal ();
377         d->Destroy ();
378 }
379
380 void
381 VideoPanel::setup_description ()
382 {
383         ContentList vc = _parent->selected_video ();
384         if (vc.empty ()) {
385                 checked_set (_description, wxT (""));
386                 return;
387         } else if (vc.size() > 1) {
388                 checked_set (_description, _("Multiple content selected"));
389                 return;
390         }
391
392         string d = vc.front()->video->processing_description ();
393         size_t lines = count (d.begin(), d.end(), '\n');
394
395         for (int i = lines; i < 6; ++i) {
396                 d += "\n ";
397         }
398
399         checked_set (_description, d);
400         _sizer->Layout ();
401 }
402
403 void
404 VideoPanel::colour_conversion_changed ()
405 {
406         ContentList vc = _parent->selected_video ();
407         if (vc.size() != 1) {
408                 return;
409         }
410
411         int const s = _colour_conversion->GetSelection ();
412         vector<PresetColourConversion> all = PresetColourConversion::all ();
413
414         if (s == 0) {
415                 vc.front()->video->unset_colour_conversion ();
416         } else if (s == int (all.size() + 1)) {
417                 edit_colour_conversion_clicked ();
418         } else {
419                 vc.front()->video->set_colour_conversion (all[s - 1].conversion);
420         }
421 }
422
423 void
424 VideoPanel::edit_colour_conversion_clicked ()
425 {
426         ContentList vc = _parent->selected_video ();
427         if (vc.size() != 1) {
428                 return;
429         }
430
431         ContentColourConversionDialog* d = new ContentColourConversionDialog (this, vc.front()->video->yuv ());
432         d->set (vc.front()->video->colour_conversion().get_value_or (PresetColourConversion::all().front().conversion));
433         if (d->ShowModal() == wxID_OK) {
434                 vc.front()->video->set_colour_conversion (d->get ());
435         } else {
436                 /* Reset the colour conversion choice */
437                 film_content_changed (VideoContentProperty::COLOUR_CONVERSION);
438         }
439         d->Destroy ();
440 }
441
442 void
443 VideoPanel::content_selection_changed ()
444 {
445         ContentList video_sel = _parent->selected_video ();
446
447         _frame_type->set_content (video_sel);
448         _left_crop->set_content (video_sel);
449         _right_crop->set_content (video_sel);
450         _top_crop->set_content (video_sel);
451         _bottom_crop->set_content (video_sel);
452         _scale->set_content (video_sel);
453
454         film_content_changed (ContentProperty::VIDEO_FRAME_RATE);
455         film_content_changed (VideoContentProperty::CROP);
456         film_content_changed (VideoContentProperty::COLOUR_CONVERSION);
457         film_content_changed (VideoContentProperty::FADE_IN);
458         film_content_changed (VideoContentProperty::FADE_OUT);
459         film_content_changed (FFmpegContentProperty::FILTERS);
460         film_content_changed (DCPContentProperty::REFERENCE_VIDEO);
461
462         setup_sensitivity ();
463 }
464
465 void
466 VideoPanel::setup_sensitivity ()
467 {
468         ContentList sel = _parent->selected ();
469
470         shared_ptr<DCPContent> dcp;
471         if (sel.size() == 1) {
472                 dcp = dynamic_pointer_cast<DCPContent> (sel.front ());
473         }
474
475         string why_not;
476         bool const can_reference = dcp && dcp->can_reference_video (why_not);
477         setup_refer_button (_reference, _reference_note, dcp, can_reference, why_not);
478
479         if (_reference->GetValue ()) {
480                 _frame_type->wrapped()->Enable (false);
481                 _left_crop->wrapped()->Enable (false);
482                 _right_crop->wrapped()->Enable (false);
483                 _top_crop->wrapped()->Enable (false);
484                 _bottom_crop->wrapped()->Enable (false);
485                 _fade_in->Enable (false);
486                 _fade_out->Enable (false);
487                 _scale->wrapped()->Enable (false);
488                 _description->Enable (false);
489                 _filters->Enable (false);
490                 _filters_button->Enable (false);
491                 _colour_conversion->Enable (false);
492         } else {
493                 ContentList video_sel = _parent->selected_video ();
494                 FFmpegContentList ffmpeg_sel = _parent->selected_ffmpeg ();
495                 bool const single = video_sel.size() == 1;
496
497                 _frame_type->wrapped()->Enable (true);
498                 _left_crop->wrapped()->Enable (true);
499                 _right_crop->wrapped()->Enable (true);
500                 _top_crop->wrapped()->Enable (true);
501                 _bottom_crop->wrapped()->Enable (true);
502                 _fade_in->Enable (!video_sel.empty ());
503                 _fade_out->Enable (!video_sel.empty ());
504                 _scale->wrapped()->Enable (true);
505                 _description->Enable (true);
506                 _filters->Enable (true);
507                 _filters_button->Enable (single && !ffmpeg_sel.empty ());
508                 _colour_conversion->Enable (single && !video_sel.empty ());
509         }
510
511         ContentList vc = _parent->selected_video ();
512         shared_ptr<Content> vcs;
513         if (!vc.empty ()) {
514                 vcs = vc.front ();
515         }
516
517         if (vcs && vcs->video->colour_conversion ()) {
518                 _edit_colour_conversion_button->Enable (!vcs->video->colour_conversion().get().preset());
519         } else {
520                 _edit_colour_conversion_button->Enable (false);
521         }
522 }
523
524 void
525 VideoPanel::fade_in_changed ()
526 {
527         BOOST_FOREACH (shared_ptr<Content> i, _parent->selected_video ()) {
528                 int const vfr = _parent->film()->video_frame_rate ();
529                 i->video->set_fade_in (_fade_in->get (vfr).frames_round (vfr));
530         }
531 }
532
533 void
534 VideoPanel::fade_out_changed ()
535 {
536         BOOST_FOREACH (shared_ptr<Content> i, _parent->selected_video ()) {
537                 int const vfr = _parent->film()->video_frame_rate ();
538                 i->video->set_fade_out (_fade_out->get (vfr).frames_round (vfr));
539         }
540 }
541
542 void
543 VideoPanel::reference_clicked ()
544 {
545         ContentList c = _parent->selected ();
546         if (c.size() != 1) {
547                 return;
548         }
549
550         shared_ptr<DCPContent> d = dynamic_pointer_cast<DCPContent> (c.front ());
551         if (!d) {
552                 return;
553         }
554
555         d->set_reference_video (_reference->GetValue ());
556 }