More include fixes.
[dcpomatic.git] / src / wx / video_panel.cc
1 /*
2     Copyright (C) 2012-2014 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 <set>
35
36 using std::vector;
37 using std::string;
38 using std::pair;
39 using std::cout;
40 using std::list;
41 using std::set;
42 using boost::shared_ptr;
43 using boost::dynamic_pointer_cast;
44 using boost::bind;
45 using boost::optional;
46
47 static VideoContentScale
48 index_to_scale (int n)
49 {
50         vector<VideoContentScale> scales = VideoContentScale::all ();
51         assert (n >= 0);
52         assert (n < int (scales.size ()));
53         return scales[n];
54 }
55
56 static int
57 scale_to_index (VideoContentScale scale)
58 {
59         vector<VideoContentScale> scales = VideoContentScale::all ();
60         for (size_t i = 0; i < scales.size(); ++i) {
61                 if (scales[i] == scale) {
62                         return i;
63                 }
64         }
65
66         assert (false);
67 }
68
69 VideoPanel::VideoPanel (ContentPanel* p)
70         : ContentSubPanel (p, _("Video"))
71 {
72         wxGridBagSizer* grid = new wxGridBagSizer (DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
73         _sizer->Add (grid, 0, wxALL, 8);
74
75         int r = 0;
76
77         add_label_to_grid_bag_sizer (grid, this, _("Type"), true, wxGBPosition (r, 0));
78         _frame_type = new ContentChoice<VideoContent, VideoFrameType> (
79                 this,
80                 new wxChoice (this, wxID_ANY),
81                 VideoContentProperty::VIDEO_FRAME_TYPE,
82                 boost::mem_fn (&VideoContent::video_frame_type),
83                 boost::mem_fn (&VideoContent::set_video_frame_type),
84                 &caster<int, VideoFrameType>,
85                 &caster<VideoFrameType, int>
86                 );
87         _frame_type->add (grid, wxGBPosition (r, 1), wxGBSpan (1, 2));
88         ++r;
89
90         int cr = 0;
91         wxGridBagSizer* crop = new wxGridBagSizer (DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
92
93         add_label_to_grid_bag_sizer (crop, this, _("Left crop"), true, wxGBPosition (cr, 0));
94         _left_crop = new ContentSpinCtrl<VideoContent> (
95                 this,
96                 new wxSpinCtrl (this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize (64, -1)),
97                 VideoContentProperty::VIDEO_CROP,
98                 boost::mem_fn (&VideoContent::left_crop),
99                 boost::mem_fn (&VideoContent::set_left_crop)
100                 );
101         _left_crop->add (crop, wxGBPosition (cr, 1));
102
103         add_label_to_grid_bag_sizer (crop, this, _("Right crop"), true, wxGBPosition (cr, 2));
104         _right_crop = new ContentSpinCtrl<VideoContent> (
105                 this,
106                 new wxSpinCtrl (this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize (64, -1)),
107                 VideoContentProperty::VIDEO_CROP,
108                 boost::mem_fn (&VideoContent::right_crop),
109                 boost::mem_fn (&VideoContent::set_right_crop)
110                 );
111         _right_crop->add (crop, wxGBPosition (cr, 3));
112
113         ++cr;
114         
115         add_label_to_grid_bag_sizer (crop, this, _("Top crop"), true, wxGBPosition (cr, 0));
116         _top_crop = new ContentSpinCtrl<VideoContent> (
117                 this,
118                 new wxSpinCtrl (this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize (64, -1)),
119                 VideoContentProperty::VIDEO_CROP,
120                 boost::mem_fn (&VideoContent::top_crop),
121                 boost::mem_fn (&VideoContent::set_top_crop)
122                 );
123         _top_crop->add (crop, wxGBPosition (cr, 1));
124
125         add_label_to_grid_bag_sizer (crop, this, _("Bottom crop"), true, wxGBPosition (cr, 2));
126         _bottom_crop = new ContentSpinCtrl<VideoContent> (
127                 this,
128                 new wxSpinCtrl (this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize (64, -1)),
129                 VideoContentProperty::VIDEO_CROP,
130                 boost::mem_fn (&VideoContent::bottom_crop),
131                 boost::mem_fn (&VideoContent::set_bottom_crop)
132                 );
133         _bottom_crop->add (crop, wxGBPosition (cr, 3));
134
135         grid->Add (crop, wxGBPosition (r, 0), wxGBSpan (2, 3));
136         r += 2;
137
138         add_label_to_grid_bag_sizer (grid, this, _("Fade in"), true, wxGBPosition (r, 0));
139         _fade_in = new Timecode<ContentTime> (this);
140         grid->Add (_fade_in, wxGBPosition (r, 1), wxGBSpan (1, 3));
141         ++r;
142
143         add_label_to_grid_bag_sizer (grid, this, _("Fade out"), true, wxGBPosition (r, 0));
144         _fade_out = new Timecode<ContentTime> (this);
145         grid->Add (_fade_out, wxGBPosition (r, 1), wxGBSpan (1, 3));
146         ++r;
147         
148         add_label_to_grid_bag_sizer (grid, this, _("Scale to"), true, wxGBPosition (r, 0));
149         _scale = new ContentChoice<VideoContent, VideoContentScale> (
150                 this,
151                 new wxChoice (this, wxID_ANY),
152                 VideoContentProperty::VIDEO_SCALE,
153                 boost::mem_fn (&VideoContent::scale),
154                 boost::mem_fn (&VideoContent::set_scale),
155                 &index_to_scale,
156                 &scale_to_index
157                 );
158         _scale->add (grid, wxGBPosition (r, 1), wxGBSpan (1, 2));
159         ++r;
160
161         wxClientDC dc (this);
162         wxSize size = dc.GetTextExtent (wxT ("A quite long name"));
163         size.SetHeight (-1);
164         
165         add_label_to_grid_bag_sizer (grid, this, _("Filters"), true, wxGBPosition (r, 0));
166         _filters = new wxStaticText (this, wxID_ANY, _("None"), wxDefaultPosition, size);
167         grid->Add (_filters, wxGBPosition (r, 1), wxGBSpan (1, 2), wxALIGN_CENTER_VERTICAL);
168         _filters_button = new wxButton (this, wxID_ANY, _("Edit..."));
169         grid->Add (_filters_button, wxGBPosition (r, 3), wxDefaultSpan, wxALIGN_CENTER_VERTICAL);
170         ++r;
171         
172         _enable_colour_conversion = new wxCheckBox (this, wxID_ANY, _("Colour conversion"));
173         grid->Add (_enable_colour_conversion, wxGBPosition (r, 0), wxGBSpan (1, 2), wxALIGN_CENTER_VERTICAL);
174         _colour_conversion = new wxStaticText (this, wxID_ANY, wxT (""), wxDefaultPosition, size);
175         grid->Add (_colour_conversion, wxGBPosition (r, 2), wxDefaultSpan, wxALIGN_CENTER_VERTICAL);
176         _colour_conversion_button = new wxButton (this, wxID_ANY, _("Edit..."));
177         grid->Add (_colour_conversion_button, wxGBPosition (r, 3), wxDefaultSpan, wxALIGN_CENTER_VERTICAL);
178         ++r;
179
180         _description = new wxStaticText (this, wxID_ANY, wxT ("\n \n \n \n \n"), wxDefaultPosition, wxDefaultSize);
181         grid->Add (_description, wxGBPosition (r, 0), wxGBSpan (1, 4), wxEXPAND | wxALIGN_CENTER_VERTICAL | wxALL, 6);
182         wxFont font = _description->GetFont();
183         font.SetStyle(wxFONTSTYLE_ITALIC);
184         font.SetPointSize(font.GetPointSize() - 1);
185         _description->SetFont(font);
186         ++r;
187
188         _left_crop->wrapped()->SetRange (0, 1024);
189         _top_crop->wrapped()->SetRange (0, 1024);
190         _right_crop->wrapped()->SetRange (0, 1024);
191         _bottom_crop->wrapped()->SetRange (0, 1024);
192
193         vector<VideoContentScale> scales = VideoContentScale::all ();
194         _scale->wrapped()->Clear ();
195         for (vector<VideoContentScale>::iterator i = scales.begin(); i != scales.end(); ++i) {
196                 _scale->wrapped()->Append (std_to_wx (i->name ()));
197         }
198
199         _frame_type->wrapped()->Append (_("2D"));
200         _frame_type->wrapped()->Append (_("3D left/right"));
201         _frame_type->wrapped()->Append (_("3D top/bottom"));
202         _frame_type->wrapped()->Append (_("3D alternate"));
203         _frame_type->wrapped()->Append (_("3D left only"));
204         _frame_type->wrapped()->Append (_("3D right only"));
205
206         _fade_in->Changed.connect (boost::bind (&VideoPanel::fade_in_changed, this));
207         _fade_out->Changed.connect (boost::bind (&VideoPanel::fade_out_changed, this));
208         
209         _filters_button->Bind           (wxEVT_COMMAND_BUTTON_CLICKED,   boost::bind (&VideoPanel::edit_filters_clicked, this));
210         _enable_colour_conversion->Bind (wxEVT_COMMAND_CHECKBOX_CLICKED, boost::bind (&VideoPanel::enable_colour_conversion_clicked, this));
211         _colour_conversion_button->Bind (wxEVT_COMMAND_BUTTON_CLICKED,   boost::bind (&VideoPanel::edit_colour_conversion_clicked, this));
212 }
213
214 void
215 VideoPanel::film_changed (Film::Property property)
216 {
217         switch (property) {
218         case Film::CONTAINER:
219         case Film::VIDEO_FRAME_RATE:
220         case Film::RESOLUTION:
221                 setup_description ();
222                 break;
223         default:
224                 break;
225         }
226 }
227
228 void
229 VideoPanel::film_content_changed (int property)
230 {
231         VideoContentList vc = _parent->selected_video ();
232         shared_ptr<VideoContent> vcs;
233         shared_ptr<FFmpegContent> fcs;
234         if (!vc.empty ()) {
235                 vcs = vc.front ();
236                 fcs = dynamic_pointer_cast<FFmpegContent> (vcs);
237         }
238         
239         if (property == VideoContentProperty::VIDEO_FRAME_TYPE) {
240                 setup_description ();
241         } else if (property == VideoContentProperty::VIDEO_CROP) {
242                 setup_description ();
243         } else if (property == VideoContentProperty::VIDEO_SCALE) {
244                 setup_description ();
245         } else if (property == VideoContentProperty::VIDEO_FRAME_RATE) {
246                 setup_description ();
247         } else if (property == VideoContentProperty::COLOUR_CONVERSION) {
248                 if (!vcs) {
249                         _colour_conversion->SetLabel (wxT (""));
250                 } else if (vcs->colour_conversion ()) {
251                         optional<size_t> preset = vcs->colour_conversion().get().preset ();
252                         vector<PresetColourConversion> cc = Config::instance()->colour_conversions ();
253                         _colour_conversion->SetLabel (preset ? std_to_wx (cc[preset.get()].name) : _("Custom"));
254                         _enable_colour_conversion->SetValue (true);
255                         _colour_conversion->Enable (true);
256                         _colour_conversion_button->Enable (true);
257                 } else {
258                         _colour_conversion->SetLabel (_("None"));
259                         _enable_colour_conversion->SetValue (false);
260                         _colour_conversion->Enable (false);
261                         _colour_conversion_button->Enable (false);
262                 }
263         } else if (property == FFmpegContentProperty::FILTERS) {
264                 if (fcs) {
265                         string const p = Filter::ffmpeg_string (fcs->filters ());
266                         if (p.empty ()) {
267                                 _filters->SetLabel (_("None"));
268                         } else {
269                                 _filters->SetLabel (std_to_wx (p));
270                         }
271                 }
272         } else if (property == VideoContentProperty::VIDEO_FADE_IN) {
273                 set<ContentTime> check;
274                 for (VideoContentList::const_iterator i = vc.begin (); i != vc.end(); ++i) {
275                         check.insert ((*i)->fade_in ());
276                 }
277                 
278                 if (check.size() == 1) {
279                         _fade_in->set (vc.front()->fade_in (), vc.front()->video_frame_rate ());
280                 } else {
281                         _fade_in->clear ();
282                 }
283         } else if (property == VideoContentProperty::VIDEO_FADE_OUT) {
284                 set<ContentTime> check;
285                 for (VideoContentList::const_iterator i = vc.begin (); i != vc.end(); ++i) {
286                         check.insert ((*i)->fade_out ());
287                 }
288                 
289                 if (check.size() == 1) {
290                         _fade_out->set (vc.front()->fade_out (), vc.front()->video_frame_rate ());
291                 } else {
292                         _fade_out->clear ();
293                 }
294         }
295 }
296
297 /** Called when the `Edit filters' button has been clicked */
298 void
299 VideoPanel::edit_filters_clicked ()
300 {
301         FFmpegContentList c = _parent->selected_ffmpeg ();
302         if (c.size() != 1) {
303                 return;
304         }
305
306         FilterDialog* d = new FilterDialog (this, c.front()->filters());
307         d->ActiveChanged.connect (bind (&FFmpegContent::set_filters, c.front(), _1));
308         d->ShowModal ();
309         d->Destroy ();
310 }
311
312 void
313 VideoPanel::setup_description ()
314 {
315         VideoContentList vc = _parent->selected_video ();
316         if (vc.empty ()) {
317                 _description->SetLabel ("");
318                 return;
319         } else if (vc.size() > 1) {
320                 _description->SetLabel (_("Multiple content selected"));
321                 return;
322         }
323
324         string d = vc.front()->processing_description ();
325         size_t lines = count (d.begin(), d.end(), '\n');
326
327         for (int i = lines; i < 6; ++i) {
328                 d += "\n ";
329         }
330
331         _description->SetLabel (std_to_wx (d));
332         _sizer->Layout ();
333 }
334
335 void
336 VideoPanel::edit_colour_conversion_clicked ()
337 {
338         VideoContentList vc = _parent->selected_video ();
339         if (vc.size() != 1) {
340                 return;
341         }
342
343         if (!vc.front()->colour_conversion ()) {
344                 return;
345         }
346
347         ColourConversion conversion = vc.front()->colour_conversion().get ();
348         ContentColourConversionDialog* d = new ContentColourConversionDialog (this);
349         d->set (conversion);
350         d->ShowModal ();
351
352         vc.front()->set_colour_conversion (d->get ());
353         d->Destroy ();
354 }
355
356 void
357 VideoPanel::content_selection_changed ()
358 {
359         VideoContentList video_sel = _parent->selected_video ();
360         FFmpegContentList ffmpeg_sel = _parent->selected_ffmpeg ();
361         
362         bool const single = video_sel.size() == 1;
363
364         _left_crop->set_content (video_sel);
365         _right_crop->set_content (video_sel);
366         _top_crop->set_content (video_sel);
367         _bottom_crop->set_content (video_sel);
368         _frame_type->set_content (video_sel);
369         _scale->set_content (video_sel);
370
371         _filters_button->Enable (single && !ffmpeg_sel.empty ());
372         _colour_conversion_button->Enable (single);
373
374         film_content_changed (VideoContentProperty::VIDEO_CROP);
375         film_content_changed (VideoContentProperty::VIDEO_FRAME_RATE);
376         film_content_changed (VideoContentProperty::COLOUR_CONVERSION);
377         film_content_changed (VideoContentProperty::VIDEO_FADE_IN);
378         film_content_changed (VideoContentProperty::VIDEO_FADE_OUT);
379         film_content_changed (FFmpegContentProperty::FILTERS);
380 }
381
382 void
383 VideoPanel::fade_in_changed ()
384 {
385         VideoContentList vc = _parent->selected_video ();
386         for (VideoContentList::const_iterator i = vc.begin(); i != vc.end(); ++i) {
387                 (*i)->set_fade_in (_fade_in->get (_parent->film()->video_frame_rate ()));
388         }
389 }
390
391 void
392 VideoPanel::fade_out_changed ()
393 {
394         VideoContentList vc = _parent->selected_video ();
395         for (VideoContentList::const_iterator i = vc.begin(); i != vc.end(); ++i) {
396                 (*i)->set_fade_out (_fade_out->get (_parent->film()->video_frame_rate ()));
397         }
398 }
399
400 void
401 VideoPanel::enable_colour_conversion_clicked ()
402 {
403         VideoContentList vc = _parent->selected_video ();
404         if (vc.size() != 1) {
405                 return;
406         }
407
408         if (_enable_colour_conversion->GetValue()) {
409                 vc.front()->set_default_colour_conversion ();
410         } else {
411                 vc.front()->unset_colour_conversion ();
412         }
413 }