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