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