Try to neaten things up; first wxChoice.
[dcpomatic.git] / src / wx / video_panel.cc
1 /*
2     Copyright (C) 2012-2013 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/ratio.h"
22 #include "lib/filter.h"
23 #include "lib/ffmpeg_content.h"
24 #include "lib/colour_conversion.h"
25 #include "lib/config.h"
26 #include "lib/util.h"
27 #include "filter_dialog.h"
28 #include "video_panel.h"
29 #include "wx_util.h"
30 #include "film_editor.h"
31 #include "content_colour_conversion_dialog.h"
32 #include "content_widget.h"
33
34 using std::vector;
35 using std::string;
36 using std::pair;
37 using std::cout;
38 using std::list;
39 using boost::shared_ptr;
40 using boost::dynamic_pointer_cast;
41 using boost::bind;
42 using boost::optional;
43
44 VideoPanel::VideoPanel (FilmEditor* e)
45         : FilmEditorPanel (e, _("Video"))
46 {
47         wxGridBagSizer* grid = new wxGridBagSizer (DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
48         _sizer->Add (grid, 0, wxALL, 8);
49
50         int r = 0;
51
52         add_label_to_grid_bag_sizer (grid, this, _("Type"), true, wxGBPosition (r, 0));
53         _frame_type = new ContentChoice<VideoContent, VideoFrameType> (
54                 this,
55                 new wxChoice (this, wxID_ANY),
56                 VideoContentProperty::VIDEO_FRAME_TYPE,
57                 boost::mem_fn (&VideoContent::video_frame_type),
58                 boost::mem_fn (&VideoContent::set_video_frame_type)
59                 );
60         _frame_type->add (grid, wxGBPosition (r, 1));
61         ++r;
62         
63         add_label_to_grid_bag_sizer (grid, this, _("Left crop"), true, wxGBPosition (r, 0));
64         _left_crop = new ContentSpinCtrl<VideoContent> (
65                 this,
66                 new wxSpinCtrl (this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize (64, -1)),
67                 VideoContentProperty::VIDEO_CROP,
68                 boost::mem_fn (&VideoContent::left_crop),
69                 boost::mem_fn (&VideoContent::set_left_crop)
70                 );
71         _left_crop->add (grid, wxGBPosition (r, 1));
72         ++r;
73
74         add_label_to_grid_bag_sizer (grid, this, _("Right crop"), true, wxGBPosition (r, 0));
75         _right_crop = new ContentSpinCtrl<VideoContent> (
76                 this,
77                 new wxSpinCtrl (this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize (64, -1)),
78                 VideoContentProperty::VIDEO_CROP,
79                 boost::mem_fn (&VideoContent::right_crop),
80                 boost::mem_fn (&VideoContent::set_right_crop)
81                 );
82         _right_crop->add (grid, wxGBPosition (r, 1));
83         ++r;
84         
85         add_label_to_grid_bag_sizer (grid, this, _("Top crop"), true, wxGBPosition (r, 0));
86         _top_crop = new ContentSpinCtrl<VideoContent> (
87                 this,
88                 new wxSpinCtrl (this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize (64, -1)),
89                 VideoContentProperty::VIDEO_CROP,
90                 boost::mem_fn (&VideoContent::top_crop),
91                 boost::mem_fn (&VideoContent::set_top_crop)
92                 );
93         _top_crop->add (grid, wxGBPosition (r,1 ));
94         ++r;
95         
96         add_label_to_grid_bag_sizer (grid, this, _("Bottom crop"), true, wxGBPosition (r, 0));
97         _bottom_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::bottom_crop),
102                 boost::mem_fn (&VideoContent::set_bottom_crop)
103                 );
104         _bottom_crop->add (grid, wxGBPosition (r, 1));
105         ++r;
106
107         add_label_to_grid_bag_sizer (grid, this, _("Scale to"), true, wxGBPosition (r, 0));
108         _ratio = new wxChoice (this, wxID_ANY);
109         grid->Add (_ratio, wxGBPosition (r, 1));
110         ++r;
111
112         {
113                 add_label_to_grid_bag_sizer (grid, this, _("Filters"), true, wxGBPosition (r, 0));
114                 wxSizer* s = new wxBoxSizer (wxHORIZONTAL);
115
116                 wxClientDC dc (this);
117                 wxSize size = dc.GetTextExtent (wxT ("A quite long name"));
118                 size.SetHeight (-1);
119                 
120                 _filters = new wxStaticText (this, wxID_ANY, _("None"), wxDefaultPosition, size);
121                 s->Add (_filters, 1, wxEXPAND | wxALIGN_CENTER_VERTICAL | wxTOP | wxBOTTOM | wxRIGHT, 6);
122                 _filters_button = new wxButton (this, wxID_ANY, _("Edit..."));
123                 s->Add (_filters_button, 0, wxALIGN_CENTER_VERTICAL);
124                 grid->Add (s, wxGBPosition (r, 1), wxDefaultSpan, wxALIGN_CENTER_VERTICAL);
125         }
126         ++r;
127         
128         {
129                 add_label_to_grid_bag_sizer (grid, this, _("Colour conversion"), true, wxGBPosition (r, 0));
130                 wxSizer* s = new wxBoxSizer (wxHORIZONTAL);
131
132                 wxClientDC dc (this);
133                 wxSize size = dc.GetTextExtent (wxT ("A quite long name"));
134                 size.SetHeight (-1);
135                 
136                 _colour_conversion = new wxStaticText (this, wxID_ANY, wxT (""), wxDefaultPosition, size);
137
138                 s->Add (_colour_conversion, 1, wxEXPAND | wxALIGN_CENTER_VERTICAL | wxTOP | wxBOTTOM | wxRIGHT, 6);
139                 _colour_conversion_button = new wxButton (this, wxID_ANY, _("Edit..."));
140                 s->Add (_colour_conversion_button, 0, wxALIGN_CENTER_VERTICAL);
141                 grid->Add (s, wxGBPosition (r, 1), wxDefaultSpan, wxALIGN_CENTER_VERTICAL);
142         }
143         ++r;
144
145         _description = new wxStaticText (this, wxID_ANY, wxT ("\n \n \n \n \n"), wxDefaultPosition, wxDefaultSize);
146         grid->Add (_description, wxGBPosition (r, 0), wxGBSpan (1, 2), wxEXPAND | wxALIGN_CENTER_VERTICAL | wxALL, 6);
147         wxFont font = _description->GetFont();
148         font.SetStyle(wxFONTSTYLE_ITALIC);
149         font.SetPointSize(font.GetPointSize() - 1);
150         _description->SetFont(font);
151         ++r;
152
153         _left_crop->wrapped()->SetRange (0, 1024);
154         _top_crop->wrapped()->SetRange (0, 1024);
155         _right_crop->wrapped()->SetRange (0, 1024);
156         _bottom_crop->wrapped()->SetRange (0, 1024);
157
158         vector<Ratio const *> ratios = Ratio::all ();
159         _ratio->Clear ();
160         for (vector<Ratio const *>::iterator i = ratios.begin(); i != ratios.end(); ++i) {
161                 _ratio->Append (std_to_wx ((*i)->nickname ()));
162         }
163         _ratio->Append (_("No stretch"));
164
165         _frame_type->wrapped()->Append (_("2D"));
166         _frame_type->wrapped()->Append (_("3D left/right"));
167
168         _ratio->Bind                    (wxEVT_COMMAND_CHOICE_SELECTED,  boost::bind (&VideoPanel::ratio_changed, this));
169         _filters_button->Bind           (wxEVT_COMMAND_BUTTON_CLICKED,   boost::bind (&VideoPanel::edit_filters_clicked, this));
170         _colour_conversion_button->Bind (wxEVT_COMMAND_BUTTON_CLICKED,   boost::bind (&VideoPanel::edit_colour_conversion_clicked, this));
171 }
172
173 void
174 VideoPanel::film_changed (Film::Property property)
175 {
176         switch (property) {
177         case Film::CONTAINER:
178         case Film::VIDEO_FRAME_RATE:
179                 setup_description ();
180                 break;
181         default:
182                 break;
183         }
184 }
185
186 void
187 VideoPanel::film_content_changed (int property)
188 {
189         VideoContentList vc = _editor->selected_video_content ();
190         shared_ptr<VideoContent> vcs;
191         shared_ptr<FFmpegContent> fcs;
192         if (!vc.empty ()) {
193                 vcs = vc.front ();
194                 fcs = dynamic_pointer_cast<FFmpegContent> (vcs);
195         }
196         
197         if (property == VideoContentProperty::VIDEO_FRAME_TYPE) {
198                 setup_description ();
199         } else if (property == VideoContentProperty::VIDEO_CROP) {
200                 setup_description ();
201         } else if (property == VideoContentProperty::VIDEO_RATIO) {
202                 if (vcs) {
203                         int n = 0;
204                         vector<Ratio const *> ratios = Ratio::all ();
205                         vector<Ratio const *>::iterator i = ratios.begin ();
206                         while (i != ratios.end() && *i != vcs->ratio()) {
207                                 ++i;
208                                 ++n;
209                         }
210
211                         if (i == ratios.end()) {
212                                 checked_set (_ratio, ratios.size ());
213                         } else {
214                                 checked_set (_ratio, n);
215                         }
216                 } else {
217                         checked_set (_ratio, -1);
218                 }
219                 setup_description ();
220         } else if (property == VideoContentProperty::VIDEO_FRAME_RATE) {
221                 setup_description ();
222         } else if (property == VideoContentProperty::COLOUR_CONVERSION) {
223                 optional<size_t> preset = vcs ? vcs->colour_conversion().preset () : optional<size_t> ();
224                 vector<PresetColourConversion> cc = Config::instance()->colour_conversions ();
225                 _colour_conversion->SetLabel (preset ? std_to_wx (cc[preset.get()].name) : _("Custom"));
226         } else if (property == FFmpegContentProperty::FILTERS) {
227                 if (fcs) {
228                         pair<string, string> p = Filter::ffmpeg_strings (fcs->filters ());
229                         if (p.first.empty () && p.second.empty ()) {
230                                 _filters->SetLabel (_("None"));
231                         } else {
232                                 string const b = p.first + " " + p.second;
233                                 _filters->SetLabel (std_to_wx (b));
234                         }
235                 }
236         }
237 }
238
239 /** Called when the `Edit filters' button has been clicked */
240 void
241 VideoPanel::edit_filters_clicked ()
242 {
243         FFmpegContentList c = _editor->selected_ffmpeg_content ();
244         if (c.size() != 1) {
245                 return;
246         }
247
248         FilterDialog* d = new FilterDialog (this, c.front()->filters());
249         d->ActiveChanged.connect (bind (&FFmpegContent::set_filters, c.front(), _1));
250         d->ShowModal ();
251         d->Destroy ();
252 }
253
254 void
255 VideoPanel::setup_description ()
256 {
257         FFmpegContentList vc = _editor->selected_ffmpeg_content ();
258         if (vc.empty ()) {
259                 _description->SetLabel ("");
260                 return;
261         } else if (vc.size() > 1) {
262                 _description->SetLabel (_("Multiple content selected"));
263                 return;
264         }
265
266         shared_ptr<FFmpegContent> vcs = vc.front ();
267
268         wxString d;
269
270         int lines = 0;
271
272         if (vcs->video_size().width && vcs->video_size().height) {
273                 d << wxString::Format (
274                         _("Content video is %dx%d (%.2f:1)\n"),
275                         vcs->video_size_after_3d_split().width,
276                         vcs->video_size_after_3d_split().height,
277                         vcs->video_size_after_3d_split().ratio ()
278                         );
279                 ++lines;
280         }
281
282         Crop const crop = vcs->crop ();
283         if ((crop.left || crop.right || crop.top || crop.bottom) && vcs->video_size() != libdcp::Size (0, 0)) {
284                 libdcp::Size cropped = vcs->video_size_after_crop ();
285                 d << wxString::Format (
286                         _("Cropped to %dx%d (%.2f:1)\n"),
287                         cropped.width, cropped.height,
288                         cropped.ratio ()
289                         );
290                 ++lines;
291         }
292
293         Ratio const * ratio = vcs->ratio ();
294         libdcp::Size container_size = fit_ratio_within (_editor->film()->container()->ratio (), _editor->film()->full_frame ());
295         float const ratio_value = ratio ? ratio->ratio() : vcs->video_size_after_crop().ratio ();
296
297         /* We have a specified ratio to scale to */
298         libdcp::Size const scaled = fit_ratio_within (ratio_value, container_size);
299         
300         d << wxString::Format (
301                 _("Scaled to %dx%d (%.2f:1)\n"),
302                 scaled.width, scaled.height,
303                 scaled.ratio ()
304                 );
305         ++lines;
306         
307         if (scaled != container_size) {
308                 d << wxString::Format (
309                         _("Padded with black to %dx%d (%.2f:1)\n"),
310                         container_size.width, container_size.height,
311                         container_size.ratio ()
312                         );
313                 ++lines;
314         }
315
316         d << wxString::Format (_("Content frame rate %.4f\n"), vcs->video_frame_rate ());
317         ++lines;
318         FrameRateConversion frc (vcs->video_frame_rate(), _editor->film()->video_frame_rate ());
319         d << frc.description << "\n";
320         ++lines;
321
322         for (int i = lines; i < 6; ++i) {
323                 d << wxT ("\n ");
324         }
325
326         _description->SetLabel (d);
327         _sizer->Layout ();
328 }
329
330
331 void
332 VideoPanel::ratio_changed ()
333 {
334         if (!_editor->film ()) {
335                 return;
336         }
337
338         VideoContentList vc = _editor->selected_video_content ();
339         if (vc.size() != 1) {
340                 return;
341         }
342         
343         int const n = _ratio->GetSelection ();
344         if (n >= 0) {
345                 vector<Ratio const *> ratios = Ratio::all ();
346                 if (n < int (ratios.size ())) {
347                         vc.front()->set_ratio (ratios[n]);
348                 } else {
349                         vc.front()->set_ratio (0);
350                 }
351         }
352 }
353
354 void
355 VideoPanel::edit_colour_conversion_clicked ()
356 {
357         VideoContentList vc = _editor->selected_video_content ();
358         if (vc.size() != 1) {
359                 return;
360         }
361
362         ColourConversion conversion = vc.front()->colour_conversion ();
363         ContentColourConversionDialog* d = new ContentColourConversionDialog (this);
364         d->set (conversion);
365         d->ShowModal ();
366
367         vc.front()->set_colour_conversion (d->get ());
368         d->Destroy ();
369 }
370
371 void
372 VideoPanel::content_selection_changed ()
373 {
374         VideoContentList sel = _editor->selected_video_content ();
375         bool const single = sel.size() == 1;
376
377         _left_crop->set_content (sel);
378         _right_crop->set_content (sel);
379         _top_crop->set_content (sel);
380         _bottom_crop->set_content (sel);
381         _frame_type->set_content (sel);
382
383         /* Things that are only allowed with single selections */
384         _ratio->Enable (single);
385         _filters_button->Enable (single);
386         _colour_conversion_button->Enable (single);
387
388         film_content_changed (VideoContentProperty::VIDEO_CROP);
389         film_content_changed (VideoContentProperty::VIDEO_RATIO);
390         film_content_changed (VideoContentProperty::VIDEO_FRAME_RATE);
391         film_content_changed (VideoContentProperty::COLOUR_CONVERSION);
392         film_content_changed (FFmpegContentProperty::FILTERS);
393 }