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