Revert "Skipping hacks."
[dcpomatic.git] / src / wx / film_viewer.cc
1 /*
2     Copyright (C) 2012 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 /** @file  src/film_viewer.cc
21  *  @brief A wx widget to view `thumbnails' of a Film.
22  */
23
24 #include <iostream>
25 #include <iomanip>
26 #include "lib/film.h"
27 #include "lib/format.h"
28 #include "lib/util.h"
29 #include "lib/job_manager.h"
30 #include "lib/options.h"
31 #include "lib/subtitle.h"
32 #include "film_viewer.h"
33 #include "wx_util.h"
34
35 using std::string;
36 using std::pair;
37 using std::max;
38 using boost::shared_ptr;
39
40 class ThumbPanel : public wxPanel
41 {
42 public:
43         ThumbPanel (wxPanel* parent, shared_ptr<Film> film)
44                 : wxPanel (parent)
45                 , _film (film)
46                 , _index (0)
47                 , _frame_rebuild_needed (false)
48                 , _composition_needed (false)
49         {}
50
51         /** Handle a paint event */
52         void paint_event (wxPaintEvent& ev)
53         {
54                 if (!_film || _film->thumbs().size() == 0) {
55                         wxPaintDC dc (this);
56                         return;
57                 }
58
59                 if (_frame_rebuild_needed) {
60                         _image.reset (new wxImage (std_to_wx (_film->thumb_file (_index))));
61
62                         _subtitle.reset ();
63                         pair<Position, string> s = _film->thumb_subtitle (_index);
64                         if (!s.second.empty ()) {
65                                 _subtitle.reset (new SubtitleView (s.first, std_to_wx (s.second)));
66                         }
67
68                         _frame_rebuild_needed = false;
69                         compose ();
70                 }
71
72                 if (_composition_needed) {
73                         compose ();
74                 }
75
76                 wxPaintDC dc (this);
77                 if (_bitmap) {
78                         dc.DrawBitmap (*_bitmap, 0, 0, false);
79                 }
80
81                 if (_film->with_subtitles() && _subtitle) {
82                         dc.DrawBitmap (*_subtitle->bitmap, _subtitle->transformed_area.x, _subtitle->transformed_area.y, true);
83                 }
84         }
85
86         /** Handle a size event */
87         void size_event (wxSizeEvent &)
88         {
89                 if (!_image) {
90                         return;
91                 }
92
93                 recompose ();
94         }
95
96         /** @param n Thumbnail index */
97         void set (int n)
98         {
99                 _index = n;
100                 _frame_rebuild_needed = true;
101                 Refresh ();
102         }
103
104         void set_film (shared_ptr<Film> f)
105         {
106                 _film = f;
107                 if (!_film) {
108                         clear ();
109                         _frame_rebuild_needed = true;
110                         Refresh ();
111                 } else {
112                         _frame_rebuild_needed = true;
113                         Refresh ();
114                 }
115         }
116
117         /** Clear our thumbnail image */
118         void clear ()
119         {
120                 _bitmap.reset ();
121                 _image.reset ();
122                 _subtitle.reset ();
123         }
124
125         void recompose ()
126         {
127                 _composition_needed = true;
128                 Refresh ();
129         }
130
131         DECLARE_EVENT_TABLE ();
132
133 private:
134
135         void compose ()
136         {
137                 _composition_needed = false;
138                 
139                 if (!_film || !_image) {
140                         return;
141                 }
142
143                 /* Size of the view */
144                 int vw, vh;
145                 GetSize (&vw, &vh);
146
147                 Crop const fc = _film->crop ();
148
149                 /* Cropped rectangle */
150                 Rect cropped_area (
151                         fc.left,
152                         fc.top,
153                         _image->GetWidth() - (fc.left + fc.right),
154                         _image->GetHeight() - (fc.top + fc.bottom)
155                         );
156
157                 /* Target ratio */
158                 float const target = _film->format() ? _film->format()->ratio_as_float (_film) : 1.78;
159
160                 _transformed_image = _image->GetSubImage (wxRect (cropped_area.x, cropped_area.y, cropped_area.width, cropped_area.height));
161
162                 float x_scale = 1;
163                 float y_scale = 1;
164
165                 if ((float (vw) / vh) > target) {
166                         /* view is longer (horizontally) than the ratio; fit height */
167                         _transformed_image.Rescale (vh * target, vh, wxIMAGE_QUALITY_HIGH);
168                         x_scale = vh * target / cropped_area.width;
169                         y_scale = float (vh) / cropped_area.height;
170                 } else {
171                         /* view is shorter (horizontally) than the ratio; fit width */
172                         _transformed_image.Rescale (vw, vw / target, wxIMAGE_QUALITY_HIGH);
173                         x_scale = float (vw) / cropped_area.width;
174                         y_scale = (vw / target) / cropped_area.height;
175                 }
176
177                 _bitmap.reset (new wxBitmap (_transformed_image));
178
179                 if (_subtitle) {
180
181                         _subtitle->transformed_area = subtitle_transformed_area (
182                                 x_scale, y_scale, _subtitle->base_area, _film->subtitle_offset(), _film->subtitle_scale()
183                                 );
184
185                         _subtitle->transformed_image = _subtitle->base_image;
186                         _subtitle->transformed_image.Rescale (_subtitle->transformed_area.width, _subtitle->transformed_area.height, wxIMAGE_QUALITY_HIGH);
187                         _subtitle->transformed_area.x -= rint (_film->crop().left * x_scale);
188                         _subtitle->transformed_area.y -= rint (_film->crop().top * y_scale);
189                         _subtitle->bitmap.reset (new wxBitmap (_subtitle->transformed_image));
190                 }
191         }
192
193         shared_ptr<Film> _film;
194         shared_ptr<wxImage> _image;
195         wxImage _transformed_image;
196         /** currently-displayed thumbnail index */
197         int _index;
198         shared_ptr<wxBitmap> _bitmap;
199         bool _frame_rebuild_needed;
200         bool _composition_needed;
201
202         struct SubtitleView
203         {
204                 SubtitleView (Position p, wxString const & i)
205                         : base_image (i)
206                 {
207                         base_area.x = p.x;
208                         base_area.y = p.y;
209                         base_area.width = base_image.GetWidth ();
210                         base_area.height = base_image.GetHeight ();
211                 }
212
213                 Rect base_area;
214                 Rect transformed_area;
215                 wxImage base_image;
216                 wxImage transformed_image;
217                 shared_ptr<wxBitmap> bitmap;
218         };
219
220         shared_ptr<SubtitleView> _subtitle;
221 };
222
223 BEGIN_EVENT_TABLE (ThumbPanel, wxPanel)
224 EVT_PAINT (ThumbPanel::paint_event)
225 EVT_SIZE (ThumbPanel::size_event)
226 END_EVENT_TABLE ()
227
228 FilmViewer::FilmViewer (shared_ptr<Film> f, wxWindow* p)
229         : wxPanel (p)
230 {
231         _sizer = new wxBoxSizer (wxVERTICAL);
232         SetSizer (_sizer);
233         
234         _thumb_panel = new ThumbPanel (this, f);
235         _sizer->Add (_thumb_panel, 1, wxEXPAND);
236
237         int const m = max ((size_t) 1, f ? f->thumbs().size() - 1 : 0);
238         _slider = new wxSlider (this, wxID_ANY, 0, 0, m);
239         _sizer->Add (_slider, 0, wxEXPAND | wxLEFT | wxRIGHT);
240         set_thumbnail (0);
241
242         _slider->Connect (wxID_ANY, wxEVT_COMMAND_SLIDER_UPDATED, wxCommandEventHandler (FilmViewer::slider_changed), 0, this);
243
244         set_film (_film);
245 }
246
247 void
248 FilmViewer::set_thumbnail (int n)
249 {
250         if (_film == 0 || int (_film->thumbs().size()) <= n) {
251                 return;
252         }
253
254         _thumb_panel->set (n);
255 }
256
257 void
258 FilmViewer::slider_changed (wxCommandEvent &)
259 {
260         set_thumbnail (_slider->GetValue ());
261 }
262
263 void
264 FilmViewer::film_changed (Film::Property p)
265 {
266         ensure_ui_thread ();
267         
268         switch (p) {
269         case Film::THUMBS:
270                 if (_film && _film->thumbs().size() > 1) {
271                         _slider->SetRange (0, _film->thumbs().size() - 1);
272                 } else {
273                         _thumb_panel->clear ();
274                         _slider->SetRange (0, 1);
275                 }
276                 
277                 _slider->SetValue (0);
278                 set_thumbnail (0);
279                 break;
280         case Film::CONTENT:
281                 setup_visibility ();
282                 break;
283         case Film::CROP:
284         case Film::FORMAT:
285         case Film::WITH_SUBTITLES:
286         case Film::SUBTITLE_OFFSET:
287         case Film::SUBTITLE_SCALE:
288                 _thumb_panel->recompose ();
289                 break;
290         default:
291                 break;
292         }
293 }
294
295 void
296 FilmViewer::set_film (shared_ptr<Film> f)
297 {
298         if (_film == f) {
299                 return;
300         }
301         
302         _film = f;
303         _thumb_panel->set_film (_film);
304
305         if (!_film) {
306                 return;
307         }
308
309         _film->Changed.connect (bind (&FilmViewer::film_changed, this, _1));
310         film_changed (Film::CROP);
311         film_changed (Film::THUMBS);
312         setup_visibility ();
313 }
314
315 void
316 FilmViewer::setup_visibility ()
317 {
318         if (!_film) {
319                 return;
320         }
321
322         ContentType const c = _film->content_type ();
323         _slider->Show (c == VIDEO);
324 }