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