Re-work FilmState / Film relationship a bit; Film now inherits from FilmState and...
[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 "lib/subtitle.h"
34 #include "film_viewer.h"
35 #include "wx_util.h"
36
37 using namespace std;
38 using namespace boost;
39
40 class ThumbPanel : public wxPanel
41 {
42 public:
43         ThumbPanel (wxPanel* parent, Film* film)
44                 : wxPanel (parent)
45                 , _film (film)
46                 , _frame_rebuild_needed (false)
47                 , _composition_needed (false)
48         {}
49
50         /** Handle a paint event */
51         void paint_event (wxPaintEvent& ev)
52         {
53                 if (!_film || _film->thumbs().size() == 0) {
54                         wxPaintDC dc (this);
55                         return;
56                 }
57
58                 if (_frame_rebuild_needed) {
59                         _image.reset (new wxImage (std_to_wx (_film->thumb_file (_index))));
60
61                         _subtitle.reset ();
62                         pair<Position, string> s = _film->thumb_subtitle (_index);
63                         if (!s.second.empty ()) {
64                                 _subtitle.reset (new SubtitleView (s.first, std_to_wx (s.second)));
65                         }
66
67                         _frame_rebuild_needed = false;
68                         compose ();
69                 }
70
71                 if (_composition_needed) {
72                         compose ();
73                 }
74
75                 wxPaintDC dc (this);
76                 if (_bitmap) {
77                         dc.DrawBitmap (*_bitmap, 0, 0, false);
78                 }
79
80                 if (_film->with_subtitles() && _subtitle) {
81                         dc.DrawBitmap (*_subtitle->bitmap, _subtitle->transformed_area.x, _subtitle->transformed_area.y, true);
82                 }
83         }
84
85         /** Handle a size event */
86         void size_event (wxSizeEvent &)
87         {
88                 if (!_image) {
89                         return;
90                 }
91
92                 recompose ();
93         }
94
95         /** @param n Thumbnail index */
96         void set (int n)
97         {
98                 _index = n;
99                 _frame_rebuild_needed = true;
100                 Refresh ();
101         }
102
103         void set_film (Film* f)
104         {
105                 _film = f;
106                 if (!_film) {
107                         clear ();
108                         _frame_rebuild_needed = true;
109                         Refresh ();
110                 } else {
111                         _frame_rebuild_needed = true;
112                         Refresh ();
113                 }
114         }
115
116         /** Clear our thumbnail image */
117         void clear ()
118         {
119                 _bitmap.reset ();
120                 _image.reset ();
121                 _subtitle.reset ();
122         }
123
124         void recompose ()
125         {
126                 _composition_needed = true;
127                 Refresh ();
128         }
129
130         DECLARE_EVENT_TABLE ();
131
132 private:
133
134         void compose ()
135         {
136                 _composition_needed = false;
137                 
138                 if (!_film || !_image) {
139                         return;
140                 }
141
142                 /* Size of the view */
143                 int vw, vh;
144                 GetSize (&vw, &vh);
145
146                 Crop const fc = _film->crop ();
147
148                 /* Cropped rectangle */
149                 Rect cropped_area (
150                         fc.left,
151                         fc.top,
152                         _image->GetWidth() - (fc.left + fc.right),
153                         _image->GetHeight() - (fc.top + fc.bottom)
154                         );
155
156                 /* Target ratio */
157                 float const target = _film->format() ? _film->format()->ratio_as_float (_film) : 1.78;
158
159                 _transformed_image = _image->GetSubImage (wxRect (cropped_area.x, cropped_area.y, cropped_area.width, cropped_area.height));
160
161                 float x_scale = 1;
162                 float y_scale = 1;
163
164                 if ((float (vw) / vh) > target) {
165                         /* view is longer (horizontally) than the ratio; fit height */
166                         _transformed_image.Rescale (vh * target, vh, wxIMAGE_QUALITY_HIGH);
167                         x_scale = vh * target / cropped_area.width;
168                         y_scale = float (vh) / cropped_area.height;
169                 } else {
170                         /* view is shorter (horizontally) than the ratio; fit width */
171                         _transformed_image.Rescale (vw, vw / target, wxIMAGE_QUALITY_HIGH);
172                         x_scale = float (vw) / cropped_area.width;
173                         y_scale = (vw / target) / cropped_area.height;
174                 }
175
176                 _bitmap.reset (new wxBitmap (_transformed_image));
177
178                 if (_subtitle) {
179
180                         _subtitle->transformed_area = subtitle_transformed_area (
181                                 x_scale, y_scale, _subtitle->base_area, _film->subtitle_offset(), _film->subtitle_scale()
182                                 );
183
184                         _subtitle->transformed_image = _subtitle->base_image;
185                         _subtitle->transformed_image.Rescale (_subtitle->transformed_area.width, _subtitle->transformed_area.height, wxIMAGE_QUALITY_HIGH);
186                         _subtitle->transformed_area.x -= _film->crop().left;
187                         _subtitle->transformed_area.y -= _film->crop().top;
188                         _subtitle->bitmap.reset (new wxBitmap (_subtitle->transformed_image));
189                 }
190         }
191
192         Film* _film;
193         shared_ptr<wxImage> _image;
194         wxImage _transformed_image;
195         /** currently-displayed thumbnail index */
196         int _index;
197         shared_ptr<wxBitmap> _bitmap;
198         bool _frame_rebuild_needed;
199         bool _composition_needed;
200
201         struct SubtitleView
202         {
203                 SubtitleView (Position p, wxString const & i)
204                         : base_image (i)
205                 {
206                         base_area.x = p.x;
207                         base_area.y = p.y;
208                         base_area.width = base_image.GetWidth ();
209                         base_area.height = base_image.GetHeight ();
210                 }
211
212                 Rect base_area;
213                 Rect transformed_area;
214                 wxImage base_image;
215                 wxImage transformed_image;
216                 shared_ptr<wxBitmap> bitmap;
217         };
218
219         shared_ptr<SubtitleView> _subtitle;
220 };
221
222 BEGIN_EVENT_TABLE (ThumbPanel, wxPanel)
223 EVT_PAINT (ThumbPanel::paint_event)
224 EVT_SIZE (ThumbPanel::size_event)
225 END_EVENT_TABLE ()
226
227 FilmViewer::FilmViewer (Film* f, wxWindow* p)
228         : wxPanel (p)
229         , _film (0)
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 (1LU, 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 (FilmState::Property p)
265 {
266         switch (p) {
267         case FilmState::THUMBS:
268                 if (_film && _film->thumbs().size() > 1) {
269                         _slider->SetRange (0, _film->thumbs().size() - 1);
270                 } else {
271                         _thumb_panel->clear ();
272                         _slider->SetRange (0, 1);
273                 }
274                 
275                 _slider->SetValue (0);
276                 set_thumbnail (0);
277                 break;
278         case FilmState::CONTENT:
279                 setup_visibility ();
280                 _film->examine_content ();
281                 update_thumbs ();
282                 break;
283         case FilmState::CROP:
284         case FilmState::FORMAT:
285         case FilmState::WITH_SUBTITLES:
286         case FilmState::SUBTITLE_OFFSET:
287         case FilmState::SUBTITLE_SCALE:
288                 _thumb_panel->recompose ();
289                 break;
290         default:
291                 break;
292         }
293 }
294
295 void
296 FilmViewer::set_film (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 (sigc::mem_fun (*this, &FilmViewer::film_changed));
310         film_changed (Film::CROP);
311         film_changed (Film::THUMBS);
312         setup_visibility ();
313 }
314
315 void
316 FilmViewer::update_thumbs ()
317 {
318         if (!_film) {
319                 return;
320         }
321
322         _film->update_thumbs_pre_gui ();
323
324         shared_ptr<const FilmState> s = _film->state_copy ();
325         shared_ptr<Options> o (new Options (s->dir ("thumbs"), ".png", ""));
326         o->out_size = _film->size ();
327         o->apply_crop = false;
328         o->decode_audio = false;
329         o->decode_video_frequency = 128;
330         o->decode_subtitles = true;
331         
332         shared_ptr<Job> j (new ThumbsJob (s, o, _film->log(), shared_ptr<Job> ()));
333         j->Finished.connect (sigc::mem_fun (_film, &Film::update_thumbs_post_gui));
334         JobManager::instance()->add (j);
335 }
336
337 void
338 FilmViewer::setup_visibility ()
339 {
340         if (!_film) {
341                 return;
342         }
343
344         ContentType const c = _film->content_type ();
345         _slider->Show (c == VIDEO);
346 }