No-op: remove all trailing whitespace.
[dcpomatic.git] / src / wx / video_panel.cc
1 /*
2     Copyright (C) 2012-2015 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include "lib/filter.h"
21 #include "lib/ffmpeg_content.h"
22 #include "lib/colour_conversion.h"
23 #include "lib/config.h"
24 #include "lib/util.h"
25 #include "lib/ratio.h"
26 #include "lib/frame_rate_change.h"
27 #include "filter_dialog.h"
28 #include "video_panel.h"
29 #include "wx_util.h"
30 #include "content_colour_conversion_dialog.h"
31 #include "content_widget.h"
32 #include "content_panel.h"
33 #include <wx/spinctrl.h>
34 #include <boost/foreach.hpp>
35 #include <set>
36
37 using std::vector;
38 using std::string;
39 using std::pair;
40 using std::cout;
41 using std::list;
42 using std::set;
43 using boost::shared_ptr;
44 using boost::dynamic_pointer_cast;
45 using boost::bind;
46 using boost::optional;
47
48 static VideoContentScale
49 index_to_scale (int n)
50 {
51         vector<VideoContentScale> scales = VideoContentScale::all ();
52         DCPOMATIC_ASSERT (n >= 0);
53         DCPOMATIC_ASSERT (n < int (scales.size ()));
54         return scales[n];
55 }
56
57 static int
58 scale_to_index (VideoContentScale scale)
59 {
60         vector<VideoContentScale> scales = VideoContentScale::all ();
61         for (size_t i = 0; i < scales.size(); ++i) {
62                 if (scales[i] == scale) {
63                         return i;
64                 }
65         }
66
67         DCPOMATIC_ASSERT (false);
68 }
69
70 VideoPanel::VideoPanel (ContentPanel* p)
71         : ContentSubPanel (p, _("Video"))
72 {
73         wxGridBagSizer* grid = new wxGridBagSizer (DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
74         _sizer->Add (grid, 0, wxALL, 8);
75
76         int r = 0;
77
78         add_label_to_grid_bag_sizer (grid, this, _("Type"), true, wxGBPosition (r, 0));
79         _frame_type = new ContentChoice<VideoContent, VideoFrameType> (
80                 this,
81                 new wxChoice (this, wxID_ANY),
82                 VideoContentProperty::VIDEO_FRAME_TYPE,
83                 boost::mem_fn (&VideoContent::video_frame_type),
84                 boost::mem_fn (&VideoContent::set_video_frame_type),
85                 &caster<int, VideoFrameType>,
86                 &caster<VideoFrameType, int>
87                 );
88         _frame_type->add (grid, wxGBPosition (r, 1), wxGBSpan (1, 2));
89         ++r;
90
91         add_label_to_grid_bag_sizer (grid, this, _("Crop"), true, wxGBPosition (r, 0));
92
93         int cr = 0;
94         wxGridBagSizer* crop = new wxGridBagSizer (DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
95
96         add_label_to_grid_bag_sizer (crop, this, _("Left"), true, wxGBPosition (cr, 0));
97         _left_crop = new ContentSpinCtrl<VideoContent> (
98                 this,
99                 new wxSpinCtrl (this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize (64, -1)),
100                 VideoContentProperty::VIDEO_CROP,
101                 boost::mem_fn (&VideoContent::left_crop),
102                 boost::mem_fn (&VideoContent::set_left_crop)
103                 );
104         _left_crop->add (crop, wxGBPosition (cr, 1));
105
106         add_label_to_grid_bag_sizer (crop, this, _("Right"), true, wxGBPosition (cr, 2));
107         _right_crop = new ContentSpinCtrl<VideoContent> (
108                 this,
109                 new wxSpinCtrl (this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize (64, -1)),
110                 VideoContentProperty::VIDEO_CROP,
111                 boost::mem_fn (&VideoContent::right_crop),
112                 boost::mem_fn (&VideoContent::set_right_crop)
113                 );
114         _right_crop->add (crop, wxGBPosition (cr, 3));
115
116         ++cr;
117
118         add_label_to_grid_bag_sizer (crop, this, _("Top"), true, wxGBPosition (cr, 0));
119         _top_crop = new ContentSpinCtrl<VideoContent> (
120                 this,
121                 new wxSpinCtrl (this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize (64, -1)),
122                 VideoContentProperty::VIDEO_CROP,
123                 boost::mem_fn (&VideoContent::top_crop),
124                 boost::mem_fn (&VideoContent::set_top_crop)
125                 );
126         _top_crop->add (crop, wxGBPosition (cr, 1));
127
128         add_label_to_grid_bag_sizer (crop, this, _("Bottom"), true, wxGBPosition (cr, 2));
129         _bottom_crop = new ContentSpinCtrl<VideoContent> (
130                 this,
131                 new wxSpinCtrl (this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize (64, -1)),
132                 VideoContentProperty::VIDEO_CROP,
133                 boost::mem_fn (&VideoContent::bottom_crop),
134                 boost::mem_fn (&VideoContent::set_bottom_crop)
135                 );
136         _bottom_crop->add (crop, wxGBPosition (cr, 3));
137
138         grid->Add (crop, wxGBPosition (r, 1), wxGBSpan (2, 3));
139         r += 2;
140
141         add_label_to_grid_bag_sizer (grid, this, _("Fade in"), true, wxGBPosition (r, 0));
142         _fade_in = new Timecode<ContentTime> (this);
143         grid->Add (_fade_in, wxGBPosition (r, 1), wxGBSpan (1, 3));
144         ++r;
145
146         add_label_to_grid_bag_sizer (grid, this, _("Fade out"), true, wxGBPosition (r, 0));
147         _fade_out = new Timecode<ContentTime> (this);
148         grid->Add (_fade_out, wxGBPosition (r, 1), wxGBSpan (1, 3));
149         ++r;
150
151         add_label_to_grid_bag_sizer (grid, this, _("Scale to"), true, wxGBPosition (r, 0));
152         _scale = new ContentChoice<VideoContent, VideoContentScale> (
153                 this,
154                 new wxChoice (this, wxID_ANY),
155                 VideoContentProperty::VIDEO_SCALE,
156                 boost::mem_fn (&VideoContent::scale),
157                 boost::mem_fn (&VideoContent::set_scale),
158                 &index_to_scale,
159                 &scale_to_index
160                 );
161         _scale->add (grid, wxGBPosition (r, 1), wxGBSpan (1, 2));
162         ++r;
163
164         wxClientDC dc (this);
165         wxSize size = dc.GetTextExtent (wxT ("A quite long name"));
166         size.SetHeight (-1);
167
168         add_label_to_grid_bag_sizer (grid, this, _("Filters"), true, wxGBPosition (r, 0));
169         {
170                 wxSizer* s = new wxBoxSizer (wxHORIZONTAL);
171
172                 _filters = new wxStaticText (this, wxID_ANY, _("None"), wxDefaultPosition, size);
173                 s->Add (_filters, 1, wxEXPAND | wxALIGN_CENTER_VERTICAL | wxTOP | wxBOTTOM | wxRIGHT, 6);
174                 _filters_button = new wxButton (this, wxID_ANY, _("Edit..."));
175                 s->Add (_filters_button, 0, wxALIGN_CENTER_VERTICAL);
176
177                 grid->Add (s, wxGBPosition (r, 1), wxDefaultSpan, wxALIGN_CENTER_VERTICAL);
178         }
179         ++r;
180
181         add_label_to_grid_bag_sizer (grid, this, _("Colour conversion"), true, wxGBPosition (r, 0));
182         {
183                 wxSizer* s = new wxBoxSizer (wxHORIZONTAL);
184
185                 _colour_conversion = new wxChoice (this, wxID_ANY, wxDefaultPosition, size);
186                 _colour_conversion->Append (_("None"));
187                 BOOST_FOREACH (PresetColourConversion const & i, PresetColourConversion::all()) {
188                         _colour_conversion->Append (std_to_wx (i.name));
189                 }
190                 _colour_conversion->Append (_("Custom"));
191                 s->Add (_colour_conversion, 1, wxEXPAND | wxALIGN_CENTER_VERTICAL | wxTOP | wxBOTTOM | wxRIGHT, 6);
192
193                 _edit_colour_conversion_button = new wxButton (this, wxID_ANY, _("Edit..."));
194                 s->Add (_edit_colour_conversion_button, 0, wxALIGN_CENTER_VERTICAL);
195
196                 grid->Add (s, wxGBPosition (r, 1), wxDefaultSpan, wxALIGN_CENTER_VERTICAL);
197         }
198         ++r;
199
200         _description = new wxStaticText (this, wxID_ANY, wxT ("\n \n \n \n \n"), wxDefaultPosition, wxDefaultSize);
201         grid->Add (_description, wxGBPosition (r, 0), wxGBSpan (1, 4), wxEXPAND | wxALIGN_CENTER_VERTICAL | wxALL, 6);
202         wxFont font = _description->GetFont();
203         font.SetStyle(wxFONTSTYLE_ITALIC);
204         font.SetPointSize(font.GetPointSize() - 1);
205         _description->SetFont(font);
206         ++r;
207
208         _left_crop->wrapped()->SetRange (0, 1024);
209         _top_crop->wrapped()->SetRange (0, 1024);
210         _right_crop->wrapped()->SetRange (0, 1024);
211         _bottom_crop->wrapped()->SetRange (0, 1024);
212
213         vector<VideoContentScale> scales = VideoContentScale::all ();
214         _scale->wrapped()->Clear ();
215         for (vector<VideoContentScale>::iterator i = scales.begin(); i != scales.end(); ++i) {
216                 _scale->wrapped()->Append (std_to_wx (i->name ()));
217         }
218
219         _frame_type->wrapped()->Append (_("2D"));
220         _frame_type->wrapped()->Append (_("3D left/right"));
221         _frame_type->wrapped()->Append (_("3D top/bottom"));
222         _frame_type->wrapped()->Append (_("3D alternate"));
223         _frame_type->wrapped()->Append (_("3D left only"));
224         _frame_type->wrapped()->Append (_("3D right only"));
225
226         _fade_in->Changed.connect (boost::bind (&VideoPanel::fade_in_changed, this));
227         _fade_out->Changed.connect (boost::bind (&VideoPanel::fade_out_changed, this));
228
229         _filters_button->Bind                (wxEVT_COMMAND_BUTTON_CLICKED,  boost::bind (&VideoPanel::edit_filters_clicked, this));
230         _colour_conversion->Bind             (wxEVT_COMMAND_CHOICE_SELECTED, boost::bind (&VideoPanel::colour_conversion_changed, this));
231         _edit_colour_conversion_button->Bind (wxEVT_COMMAND_BUTTON_CLICKED,  boost::bind (&VideoPanel::edit_colour_conversion_clicked, this));
232 }
233
234 void
235 VideoPanel::film_changed (Film::Property property)
236 {
237         switch (property) {
238         case Film::CONTAINER:
239         case Film::VIDEO_FRAME_RATE:
240         case Film::RESOLUTION:
241                 setup_description ();
242                 break;
243         default:
244                 break;
245         }
246 }
247
248 void
249 VideoPanel::film_content_changed (int property)
250 {
251         VideoContentList vc = _parent->selected_video ();
252         shared_ptr<VideoContent> vcs;
253         shared_ptr<FFmpegContent> fcs;
254         if (!vc.empty ()) {
255                 vcs = vc.front ();
256                 fcs = dynamic_pointer_cast<FFmpegContent> (vcs);
257         }
258
259         if (property == VideoContentProperty::VIDEO_FRAME_TYPE) {
260                 setup_description ();
261         } else if (property == VideoContentProperty::VIDEO_CROP) {
262                 setup_description ();
263         } else if (property == VideoContentProperty::VIDEO_SCALE) {
264                 setup_description ();
265         } else if (property == VideoContentProperty::VIDEO_FRAME_RATE) {
266                 setup_description ();
267         } else if (property == VideoContentProperty::COLOUR_CONVERSION) {
268                 if (!vcs) {
269                         checked_set (_colour_conversion, 0);
270                         _edit_colour_conversion_button->Enable (false);
271                 } else if (vcs->colour_conversion ()) {
272                         optional<size_t> preset = vcs->colour_conversion().get().preset ();
273                         vector<PresetColourConversion> cc = PresetColourConversion::all ();
274                         if (preset) {
275                                 checked_set (_colour_conversion, preset.get() + 1);
276                                 _edit_colour_conversion_button->Enable (false);
277                         } else {
278                                 checked_set (_colour_conversion, cc.size() + 1);
279                                 _edit_colour_conversion_button->Enable (true);
280                         }
281                 } else {
282                         checked_set (_colour_conversion, 0);
283                         _edit_colour_conversion_button->Enable (false);
284                 }
285         } else if (property == FFmpegContentProperty::FILTERS) {
286                 if (fcs) {
287                         string p = Filter::ffmpeg_string (fcs->filters ());
288                         if (p.empty ()) {
289                                 checked_set (_filters, _("None"));
290                         } else {
291                                 if (p.length() > 25) {
292                                         p = p.substr (0, 25) + "...";
293                                 }
294                                 checked_set (_filters, p);
295                         }
296                 }
297         } else if (property == VideoContentProperty::VIDEO_FADE_IN) {
298                 set<Frame> check;
299                 for (VideoContentList::const_iterator i = vc.begin (); i != vc.end(); ++i) {
300                         check.insert ((*i)->fade_in ());
301                 }
302
303                 if (check.size() == 1) {
304                         _fade_in->set (ContentTime::from_frames (vc.front()->fade_in (), vc.front()->video_frame_rate ()), vc.front()->video_frame_rate ());
305                 } else {
306                         _fade_in->clear ();
307                 }
308         } else if (property == VideoContentProperty::VIDEO_FADE_OUT) {
309                 set<Frame> check;
310                 for (VideoContentList::const_iterator i = vc.begin (); i != vc.end(); ++i) {
311                         check.insert ((*i)->fade_out ());
312                 }
313
314                 if (check.size() == 1) {
315                         _fade_out->set (ContentTime::from_frames (vc.front()->fade_out (), vc.front()->video_frame_rate ()), vc.front()->video_frame_rate ());
316                 } else {
317                         _fade_out->clear ();
318                 }
319         }
320 }
321
322 /** Called when the `Edit filters' button has been clicked */
323 void
324 VideoPanel::edit_filters_clicked ()
325 {
326         FFmpegContentList c = _parent->selected_ffmpeg ();
327         if (c.size() != 1) {
328                 return;
329         }
330
331         FilterDialog* d = new FilterDialog (this, c.front()->filters());
332         d->ActiveChanged.connect (bind (&FFmpegContent::set_filters, c.front(), _1));
333         d->ShowModal ();
334         d->Destroy ();
335 }
336
337 void
338 VideoPanel::setup_description ()
339 {
340         VideoContentList vc = _parent->selected_video ();
341         if (vc.empty ()) {
342                 checked_set (_description, wxT (""));
343                 return;
344         } else if (vc.size() > 1) {
345                 checked_set (_description, _("Multiple content selected"));
346                 return;
347         }
348
349         string d = vc.front()->processing_description ();
350         size_t lines = count (d.begin(), d.end(), '\n');
351
352         for (int i = lines; i < 6; ++i) {
353                 d += "\n ";
354         }
355
356         checked_set (_description, d);
357         _sizer->Layout ();
358 }
359
360 void
361 VideoPanel::colour_conversion_changed ()
362 {
363         VideoContentList vc = _parent->selected_video ();
364         if (vc.size() != 1) {
365                 return;
366         }
367
368         int const s = _colour_conversion->GetSelection ();
369         vector<PresetColourConversion> all = PresetColourConversion::all ();
370
371         if (s == 0) {
372                 vc.front()->unset_colour_conversion ();
373         } else if (s == int (all.size() + 1)) {
374                 edit_colour_conversion_clicked ();
375         } else {
376                 vc.front()->set_colour_conversion (all[s - 1].conversion);
377         }
378 }
379
380 void
381 VideoPanel::edit_colour_conversion_clicked ()
382 {
383         VideoContentList vc = _parent->selected_video ();
384         if (vc.size() != 1) {
385                 return;
386         }
387
388         ContentColourConversionDialog* d = new ContentColourConversionDialog (this);
389         d->set (vc.front()->colour_conversion().get_value_or (PresetColourConversion::all().front ().conversion));
390         d->ShowModal ();
391         vc.front()->set_colour_conversion (d->get ());
392         d->Destroy ();
393 }
394
395 void
396 VideoPanel::content_selection_changed ()
397 {
398         VideoContentList video_sel = _parent->selected_video ();
399         FFmpegContentList ffmpeg_sel = _parent->selected_ffmpeg ();
400
401         bool const single = video_sel.size() == 1;
402
403         _frame_type->set_content (video_sel);
404         _left_crop->set_content (video_sel);
405         _right_crop->set_content (video_sel);
406         _top_crop->set_content (video_sel);
407         _bottom_crop->set_content (video_sel);
408         _fade_in->Enable (!video_sel.empty ());
409         _fade_out->Enable (!video_sel.empty ());
410         _scale->set_content (video_sel);
411
412         _filters_button->Enable (single && !ffmpeg_sel.empty ());
413
414         film_content_changed (VideoContentProperty::VIDEO_CROP);
415         film_content_changed (VideoContentProperty::VIDEO_FRAME_RATE);
416         film_content_changed (VideoContentProperty::COLOUR_CONVERSION);
417         film_content_changed (VideoContentProperty::VIDEO_FADE_IN);
418         film_content_changed (VideoContentProperty::VIDEO_FADE_OUT);
419         film_content_changed (FFmpegContentProperty::FILTERS);
420 }
421
422 void
423 VideoPanel::fade_in_changed ()
424 {
425         VideoContentList vc = _parent->selected_video ();
426         for (VideoContentList::const_iterator i = vc.begin(); i != vc.end(); ++i) {
427                 int const vfr = _parent->film()->video_frame_rate ();
428                 (*i)->set_fade_in (_fade_in->get (vfr).frames (vfr));
429         }
430 }
431
432 void
433 VideoPanel::fade_out_changed ()
434 {
435         VideoContentList vc = _parent->selected_video ();
436         for (VideoContentList::const_iterator i = vc.begin(); i != vc.end(); ++i) {
437                 int const vfr = _parent->film()->video_frame_rate ();
438                 (*i)->set_fade_out (_fade_out->get (vfr).frames (vfr));
439         }
440 }