Improve scaling description layout.
[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 "filter_dialog.h"
24 #include "video_panel.h"
25 #include "wx_util.h"
26 #include "film_editor.h"
27
28 using std::vector;
29 using std::string;
30 using std::pair;
31 using boost::shared_ptr;
32 using boost::dynamic_pointer_cast;
33
34 VideoPanel::VideoPanel (FilmEditor* e)
35         : FilmEditorPanel (e, _("Video"))
36 {
37         wxGridBagSizer* grid = new wxGridBagSizer (DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
38         _sizer->Add (grid, 0, wxALL, 8);
39
40         int r = 0;
41         add_label_to_grid_bag_sizer (grid, this, _("Left crop"), true, wxGBPosition (r, 0));
42         _left_crop = new wxSpinCtrl (this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize (64, -1));
43         grid->Add (_left_crop, wxGBPosition (r, 1));
44         ++r;
45
46         add_label_to_grid_bag_sizer (grid, this, _("Right crop"), true, wxGBPosition (r, 0));
47         _right_crop = new wxSpinCtrl (this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize (64, -1));
48         grid->Add (_right_crop, wxGBPosition (r, 1));
49         ++r;
50         
51         add_label_to_grid_bag_sizer (grid, this, _("Top crop"), true, wxGBPosition (r, 0));
52         _top_crop = new wxSpinCtrl (this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize (64, -1));
53         grid->Add (_top_crop, wxGBPosition (r, 1));
54         ++r;
55         
56         add_label_to_grid_bag_sizer (grid, this, _("Bottom crop"), true, wxGBPosition (r, 0));
57         _bottom_crop = new wxSpinCtrl (this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize (64, -1));
58         grid->Add (_bottom_crop, wxGBPosition (r, 1));
59         ++r;
60
61         add_label_to_grid_bag_sizer (grid, this, _("Scale to"), true, wxGBPosition (r, 0));
62         _ratio = new wxChoice (this, wxID_ANY);
63         grid->Add (_ratio, wxGBPosition (r, 1));
64         ++r;
65
66         _scaling_description = new wxStaticText (this, wxID_ANY, wxT ("\n \n \n \n"), wxDefaultPosition, wxDefaultSize);
67         grid->Add (_scaling_description, wxGBPosition (r, 0), wxGBSpan (1, 2), wxEXPAND | wxALIGN_CENTER_VERTICAL | wxALL, 6);
68         wxFont font = _scaling_description->GetFont();
69         font.SetStyle(wxFONTSTYLE_ITALIC);
70         font.SetPointSize(font.GetPointSize() - 1);
71         _scaling_description->SetFont(font);
72         ++r;
73
74         {
75                 add_label_to_grid_bag_sizer (grid, this, _("Filters"), true, wxGBPosition (r, 0));
76                 wxSizer* s = new wxBoxSizer (wxHORIZONTAL);
77                 _filters = new wxStaticText (this, wxID_ANY, _("None"));
78                 s->Add (_filters, 1, wxEXPAND | wxALIGN_CENTER_VERTICAL | wxTOP | wxBOTTOM | wxRIGHT, 6);
79                 _filters_button = new wxButton (this, wxID_ANY, _("Edit..."));
80                 s->Add (_filters_button, 0, wxALIGN_CENTER_VERTICAL);
81                 grid->Add (s, wxGBPosition (r, 1), wxDefaultSpan, wxALIGN_CENTER_VERTICAL);
82         }
83         ++r;
84
85         _left_crop->SetRange (0, 1024);
86         _top_crop->SetRange (0, 1024);
87         _right_crop->SetRange (0, 1024);
88         _bottom_crop->SetRange (0, 1024);
89
90         vector<Ratio const *> ratios = Ratio::all ();
91         _ratio->Clear ();
92         for (vector<Ratio const *>::iterator i = ratios.begin(); i != ratios.end(); ++i) {
93                 _ratio->Append (std_to_wx ((*i)->nickname ()));
94         }
95
96         _ratio->Connect          (wxID_ANY, wxEVT_COMMAND_CHOICE_SELECTED,  wxCommandEventHandler (VideoPanel::ratio_changed), 0, this);
97         _left_crop->Connect      (wxID_ANY, wxEVT_COMMAND_SPINCTRL_UPDATED, wxCommandEventHandler (VideoPanel::left_crop_changed), 0, this);
98         _right_crop->Connect     (wxID_ANY, wxEVT_COMMAND_SPINCTRL_UPDATED, wxCommandEventHandler (VideoPanel::right_crop_changed), 0, this);
99         _top_crop->Connect       (wxID_ANY, wxEVT_COMMAND_SPINCTRL_UPDATED, wxCommandEventHandler (VideoPanel::top_crop_changed), 0, this);
100         _bottom_crop->Connect    (wxID_ANY, wxEVT_COMMAND_SPINCTRL_UPDATED, wxCommandEventHandler (VideoPanel::bottom_crop_changed), 0, this);
101         _filters_button->Connect (wxID_ANY, wxEVT_COMMAND_BUTTON_CLICKED,   wxCommandEventHandler (VideoPanel::edit_filters_clicked), 0, this);
102 }
103
104
105 /** Called when the left crop widget has been changed */
106 void
107 VideoPanel::left_crop_changed (wxCommandEvent &)
108 {
109         shared_ptr<VideoContent> c = _editor->selected_video_content ();
110         if (!c) {
111                 return;
112         }
113
114         c->set_left_crop (_left_crop->GetValue ());
115 }
116
117 /** Called when the right crop widget has been changed */
118 void
119 VideoPanel::right_crop_changed (wxCommandEvent &)
120 {
121         shared_ptr<VideoContent> c = _editor->selected_video_content ();
122         if (!c) {
123                 return;
124         }
125
126         c->set_right_crop (_right_crop->GetValue ());
127 }
128
129 /** Called when the top crop widget has been changed */
130 void
131 VideoPanel::top_crop_changed (wxCommandEvent &)
132 {
133         shared_ptr<VideoContent> c = _editor->selected_video_content ();
134         if (!c) {
135                 return;
136         }
137
138         c->set_top_crop (_top_crop->GetValue ());
139 }
140
141 /** Called when the bottom crop value has been changed */
142 void
143 VideoPanel::bottom_crop_changed (wxCommandEvent &)
144 {
145         shared_ptr<VideoContent> c = _editor->selected_video_content ();
146         if (!c) {
147                 return;
148         }
149
150         c->set_bottom_crop (_bottom_crop->GetValue ());
151 }
152
153 void
154 VideoPanel::film_changed (Film::Property property)
155 {
156         switch (property) {
157         case Film::CONTAINER:
158                 setup_scaling_description ();
159                 break;
160         default:
161                 break;
162         }
163 }
164
165 void
166 VideoPanel::film_content_changed (shared_ptr<Content> c, int property)
167 {
168         shared_ptr<VideoContent> vc = dynamic_pointer_cast<VideoContent> (c);
169         shared_ptr<FFmpegContent> fc = dynamic_pointer_cast<FFmpegContent> (c);
170         
171         if (property == VideoContentProperty::VIDEO_CROP) {
172                 checked_set (_left_crop,   vc ? vc->crop().left : 0);
173                 checked_set (_right_crop,  vc ? vc->crop().right        : 0);
174                 checked_set (_top_crop,    vc ? vc->crop().top  : 0);
175                 checked_set (_bottom_crop, vc ? vc->crop().bottom : 0);
176                 setup_scaling_description ();
177         } else if (property == VideoContentProperty::VIDEO_RATIO) {
178                 if (vc) {
179                         int n = 0;
180                         vector<Ratio const *> ratios = Ratio::all ();
181                         vector<Ratio const *>::iterator i = ratios.begin ();
182                         while (i != ratios.end() && *i != vc->ratio()) {
183                                 ++i;
184                                 ++n;
185                         }
186
187                         if (i == ratios.end()) {
188                                 checked_set (_ratio, -1);
189                         } else {
190                                 checked_set (_ratio, n);
191                         }
192                 } else {
193                         checked_set (_ratio, -1);
194                 }
195                 setup_scaling_description ();
196         } else if (property == FFmpegContentProperty::FILTERS) {
197                 if (fc) {
198                         pair<string, string> p = Filter::ffmpeg_strings (fc->filters ());
199                         if (p.first.empty () && p.second.empty ()) {
200                                 _filters->SetLabel (_("None"));
201                         } else {
202                                 string const b = p.first + " " + p.second;
203                                 _filters->SetLabel (std_to_wx (b));
204                         }
205                 }
206         }
207 }
208
209 /** Called when the `Edit filters' button has been clicked */
210 void
211 VideoPanel::edit_filters_clicked (wxCommandEvent &)
212 {
213         shared_ptr<Content> c = _editor->selected_content ();
214         if (!c) {
215                 return;
216         }
217
218         shared_ptr<FFmpegContent> fc = dynamic_pointer_cast<FFmpegContent> (c);
219         if (!fc) {
220                 return;
221         }
222         
223         FilterDialog* d = new FilterDialog (this, fc->filters());
224         d->ActiveChanged.connect (bind (&FFmpegContent::set_filters, fc, _1));
225         d->ShowModal ();
226         d->Destroy ();
227 }
228
229 void
230 VideoPanel::setup_scaling_description ()
231 {
232         shared_ptr<VideoContent> vc = _editor->selected_video_content ();
233         if (!vc) {
234                 _scaling_description->SetLabel ("");
235                 return;
236         }
237
238         wxString d;
239
240         int lines = 0;
241
242         if (vc->video_size().width && vc->video_size().height) {
243                 d << wxString::Format (
244                         _("Original video is %dx%d (%.2f:1)\n"),
245                         vc->video_size().width, vc->video_size().height,
246                         float (vc->video_size().width) / vc->video_size().height
247                         );
248                 ++lines;
249         }
250
251         Crop const crop = vc->crop ();
252         if ((crop.left || crop.right || crop.top || crop.bottom) && vc->video_size() != libdcp::Size (0, 0)) {
253                 libdcp::Size cropped = vc->video_size ();
254                 cropped.width -= crop.left + crop.right;
255                 cropped.height -= crop.top + crop.bottom;
256                 d << wxString::Format (
257                         _("Cropped to %dx%d (%.2f:1)\n"),
258                         cropped.width, cropped.height,
259                         float (cropped.width) / cropped.height
260                         );
261                 ++lines;
262         }
263
264         Ratio const * ratio = vc->ratio ();
265         if (ratio) {
266                 libdcp::Size container_size = _editor->film()->container()->size (_editor->film()->full_frame ());
267                 
268                 libdcp::Size const scaled = ratio->size (container_size);
269                 d << wxString::Format (
270                         _("Scaled to %dx%d (%.2f:1)\n"),
271                         scaled.width, scaled.height,
272                         float (scaled.width) / scaled.height
273                         );
274                 ++lines;
275
276                 if (scaled != container_size) {
277                         d << wxString::Format (
278                                 _("Padded with black to %dx%d (%.2f:1)\n"),
279                                 container_size.width, container_size.height,
280                                 float (container_size.width) / container_size.height
281                                 );
282                         ++lines;
283                 }
284         }
285
286         for (int i = lines; i < 4; ++i) {
287                 d << wxT ("\n ");
288         }
289
290         _scaling_description->SetLabel (d);
291         _sizer->Layout ();
292 }
293
294
295 void
296 VideoPanel::ratio_changed (wxCommandEvent &)
297 {
298         if (!_editor->film ()) {
299                 return;
300         }
301
302         shared_ptr<VideoContent> vc = _editor->selected_video_content ();
303         
304         int const n = _ratio->GetSelection ();
305         if (n >= 0) {
306                 vector<Ratio const *> ratios = Ratio::all ();
307                 assert (n < int (ratios.size()));
308                 vc->set_ratio (ratios[n]);
309         }
310 }