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