85632075c0d4c90ea02a2dcfa186d7c379080960
[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/thumbs_job.h"
30 #include "lib/job_manager.h"
31 #include "lib/film_state.h"
32 #include "lib/options.h"
33 #include "film_viewer.h"
34 #include "wx_util.h"
35
36 using namespace std;
37 using namespace boost;
38
39 class ThumbPanel : public wxPanel
40 {
41 public:
42         ThumbPanel (wxPanel* parent, Film* film)
43                 : wxPanel (parent)
44                 , _film (film)
45                 , _current_index (-1)
46                 , _pending_index (-1)
47                 , _current_subtitle_offset (0)
48                 , _pending_subtitle_offset (0)
49         {
50         }
51
52         /** Handle a paint event */
53         void paint_event (wxPaintEvent& ev)
54         {
55                 if (_current_index != _pending_index) {
56                         _image.reset (new wxImage (std_to_wx (_film->thumb_file (_pending_index))));
57                         _current_index = _pending_index;
58
59                         _subtitles.clear ();
60
61                         list<pair<Position, string> > s = _film->thumb_subtitles (_pending_index);
62                         for (list<pair<Position, string> >::iterator i = s.begin(); i != s.end(); ++i) {
63                                 _subtitles.push_back (SubtitleView (i->first, std_to_wx (i->second)));
64                         }
65
66                         setup ();
67                 }
68
69                 if (_current_crop != _pending_crop) {
70                         _current_crop = _pending_crop;
71                         setup ();
72                 }
73
74                 if (_current_subtitle_offset != _pending_subtitle_offset) {
75                         _current_subtitle_offset = _pending_subtitle_offset;
76                         setup ();
77                 }
78
79                 wxPaintDC dc (this);
80                 if (_bitmap) {
81                         dc.DrawBitmap (*_bitmap, 0, 0, false);
82                 }
83
84                 if (_film->with_subtitles ()) {
85                         for (list<SubtitleView>::iterator i = _subtitles.begin(); i != _subtitles.end(); ++i) {
86                                 dc.DrawBitmap (*i->bitmap, i->cropped_position.x, i->cropped_position.y, true);
87                         }
88                 }
89         }
90
91         /** Handle a size event */
92         void size_event (wxSizeEvent &)
93         {
94                 if (!_image) {
95                         return;
96                 }
97
98                 setup ();
99                 Refresh ();
100         }
101
102         /** @param n Thumbnail index */
103         void set (int n)
104         {
105                 _pending_index = n;
106                 Refresh ();
107         }
108
109         void set_crop (Crop c)
110         {
111                 _pending_crop = c;
112                 Refresh ();
113         }
114
115         void set_subtitle_offset (int o)
116         {
117                 _pending_subtitle_offset = o;
118                 Refresh ();
119         }
120
121         void set_film (Film* f)
122         {
123                 _film = f;
124                 if (!_film) {
125                         clear ();
126                         Refresh ();
127                 } else {
128                         setup ();
129                         Refresh ();
130                 }
131         }
132
133         /** Clear our thumbnail image */
134         void clear ()
135         {
136                 _bitmap.reset ();
137                 _image.reset ();
138                 _subtitles.clear ();
139         }
140
141         void refresh ()
142         {
143                 setup ();
144                 Refresh ();
145         }
146
147         DECLARE_EVENT_TABLE ();
148
149 private:
150
151         void setup ()
152         {
153                 if (!_film || !_image) {
154                         return;
155                 }
156
157                 /* Size of the view */
158                 int vw, vh;
159                 GetSize (&vw, &vh);
160
161                 /* Cropped rectangle */
162                 Rectangle cropped (
163                         _current_crop.left,
164                         _current_crop.top,
165                         _image->GetWidth() - (_current_crop.left + _current_crop.right),
166                         _image->GetHeight() - (_current_crop.top + _current_crop.bottom)
167                         );
168
169                 /* Target ratio */
170                 float const target = _film->format() ? _film->format()->ratio_as_float (_film) : 1.78;
171
172                 _cropped_image = _image->GetSubImage (wxRect (cropped.x, cropped.y, cropped.w, cropped.h));
173
174                 float x_scale = 1;
175                 float y_scale = 1;
176
177                 if ((float (vw) / vh) > target) {
178                         /* view is longer (horizontally) than the ratio; fit height */
179                         _cropped_image.Rescale (vh * target, vh, wxIMAGE_QUALITY_HIGH);
180                         x_scale = vh * target / cropped.w;
181                         y_scale = float (vh) / cropped.h;
182                 } else {
183                         /* view is shorter (horizontally) than the ratio; fit width */
184                         _cropped_image.Rescale (vw, vw / target, wxIMAGE_QUALITY_HIGH);
185                         x_scale = float (vw) / cropped.w;
186                         y_scale = (vw / target) / cropped.h;
187                 }
188
189                 _bitmap.reset (new wxBitmap (_cropped_image));
190
191                 for (list<SubtitleView>::iterator i = _subtitles.begin(); i != _subtitles.end(); ++i) {
192
193                         /* Area of the subtitle graphic within the (uncropped) picture frame */
194                         Rectangle sub_rect (i->position.x, i->position.y + _current_subtitle_offset, i->image.GetWidth(), i->image.GetHeight());
195                         /* Hence the subtitle graphic after it has been cropped */
196                         Rectangle cropped_sub_rect = sub_rect.intersection (cropped);
197
198                         /* Get the cropped version of the subtitle image */
199                         i->cropped_image = i->image.GetSubImage (
200                                 wxRect (
201                                         cropped_sub_rect.x - sub_rect.x,
202                                         cropped_sub_rect.y - sub_rect.y,
203                                         cropped_sub_rect.w,
204                                         cropped_sub_rect.h
205                                         )
206                                 );
207
208                         i->cropped_image.Rescale (cropped_sub_rect.w * x_scale, cropped_sub_rect.h * y_scale, wxIMAGE_QUALITY_HIGH);
209
210                         i->cropped_position = Position (
211                                 cropped_sub_rect.x * x_scale,
212                                 (cropped_sub_rect.y - _current_crop.top) * y_scale
213                                 );
214
215                         i->bitmap.reset (new wxBitmap (i->cropped_image));
216                 }
217         }
218
219         Film* _film;
220         shared_ptr<wxImage> _image;
221         wxImage _cropped_image;
222         /** currently-displayed thumbnail index */
223         int _current_index;
224         int _pending_index;
225         shared_ptr<wxBitmap> _bitmap;
226         Crop _current_crop;
227         Crop _pending_crop;
228         int _current_subtitle_offset;
229         int _pending_subtitle_offset;
230
231         struct SubtitleView
232         {
233                 SubtitleView (Position p, wxString const & i)
234                         : position (p)
235                         , image (i)
236                 {}
237                               
238                 Position position;
239                 wxImage image;
240                 Position cropped_position;
241                 wxImage cropped_image;
242                 shared_ptr<wxBitmap> bitmap;
243         };
244
245         list<SubtitleView> _subtitles;
246 };
247
248 BEGIN_EVENT_TABLE (ThumbPanel, wxPanel)
249 EVT_PAINT (ThumbPanel::paint_event)
250 EVT_SIZE (ThumbPanel::size_event)
251 END_EVENT_TABLE ()
252
253 FilmViewer::FilmViewer (Film* f, wxWindow* p)
254         : wxPanel (p)
255         , _film (0)
256 {
257         _sizer = new wxBoxSizer (wxVERTICAL);
258         SetSizer (_sizer);
259         
260         _thumb_panel = new ThumbPanel (this, f);
261         _sizer->Add (_thumb_panel, 1, wxEXPAND);
262
263         int const max = f ? f->num_thumbs() - 1 : 0;
264         _slider = new wxSlider (this, wxID_ANY, 0, 0, max);
265         _sizer->Add (_slider, 0, wxEXPAND | wxLEFT | wxRIGHT);
266         set_thumbnail (0);
267
268         _slider->Connect (wxID_ANY, wxEVT_COMMAND_SLIDER_UPDATED, wxCommandEventHandler (FilmViewer::slider_changed), 0, this);
269
270         set_film (_film);
271 }
272
273 void
274 FilmViewer::set_thumbnail (int n)
275 {
276         if (_film == 0 || _film->num_thumbs() <= n) {
277                 return;
278         }
279
280         _thumb_panel->set (n);
281 }
282
283 void
284 FilmViewer::slider_changed (wxCommandEvent &)
285 {
286         set_thumbnail (_slider->GetValue ());
287 }
288
289 void
290 FilmViewer::film_changed (Film::Property p)
291 {
292         switch (p) {
293         case Film::CROP:
294                 _thumb_panel->set_crop (_film->crop ());
295                 break;
296         case Film::THUMBS:
297                 if (_film && _film->num_thumbs() > 1) {
298                         _slider->SetRange (0, _film->num_thumbs () - 1);
299                 } else {
300                         _thumb_panel->clear ();
301                         _slider->SetRange (0, 1);
302                 }
303                 
304                 _slider->SetValue (0);
305                 set_thumbnail (0);
306                 break;
307         case Film::FORMAT:
308                 _thumb_panel->refresh ();
309                 break;
310         case Film::CONTENT:
311                 setup_visibility ();
312                 _film->examine_content ();
313                 update_thumbs ();
314                 break;
315         case Film::WITH_SUBTITLES:
316                 _thumb_panel->Refresh ();
317                 break;
318         case Film::SUBTITLE_OFFSET:
319                 _thumb_panel->set_subtitle_offset (_film->subtitle_offset ());
320                 break;
321         default:
322                 break;
323         }
324 }
325
326 void
327 FilmViewer::set_film (Film* f)
328 {
329         if (_film == f) {
330                 return;
331         }
332         
333         _film = f;
334         _thumb_panel->set_film (_film);
335
336         if (!_film) {
337                 return;
338         }
339
340         _film->Changed.connect (sigc::mem_fun (*this, &FilmViewer::film_changed));
341         film_changed (Film::CROP);
342         film_changed (Film::THUMBS);
343         _thumb_panel->refresh ();
344         setup_visibility ();
345 }
346
347 void
348 FilmViewer::update_thumbs ()
349 {
350         if (!_film) {
351                 return;
352         }
353
354         _film->update_thumbs_pre_gui ();
355
356         shared_ptr<const FilmState> s = _film->state_copy ();
357         shared_ptr<Options> o (new Options (s->dir ("thumbs"), ".png", ""));
358         o->out_size = _film->size ();
359         o->apply_crop = false;
360         o->decode_audio = false;
361         o->decode_video_frequency = 128;
362         
363         shared_ptr<Job> j (new ThumbsJob (s, o, _film->log(), shared_ptr<Job> ()));
364         j->Finished.connect (sigc::mem_fun (_film, &Film::update_thumbs_post_gui));
365         JobManager::instance()->add (j);
366 }
367
368 void
369 FilmViewer::setup_visibility ()
370 {
371         if (!_film) {
372                 return;
373         }
374
375         ContentType const c = _film->content_type ();
376         _slider->Show (c == VIDEO);
377 }