Partial merge of examine content and thumbnail jobs.
[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/film_state.h"
31 #include "lib/options.h"
32 #include "lib/subtitle.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 (!_film || _film->thumbs().size() == 0) {
53                         wxPaintDC dc (this);
54                         return;
55                 }
56
57                 if (_frame_rebuild_needed) {
58                         _image.reset (new wxImage (std_to_wx (_film->thumb_file (_index))));
59
60                         _subtitle.reset ();
61                         pair<Position, string> s = _film->thumb_subtitle (_index);
62                         if (!s.second.empty ()) {
63                                 _subtitle.reset (new SubtitleView (s.first, std_to_wx (s.second)));
64                         }
65
66                         _frame_rebuild_needed = false;
67                         compose ();
68                 }
69
70                 if (_composition_needed) {
71                         compose ();
72                 }
73
74                 wxPaintDC dc (this);
75                 if (_bitmap) {
76                         dc.DrawBitmap (*_bitmap, 0, 0, false);
77                 }
78
79                 if (_film->with_subtitles() && _subtitle) {
80                         dc.DrawBitmap (*_subtitle->bitmap, _subtitle->transformed_area.x, _subtitle->transformed_area.y, true);
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                 _subtitle.reset ();
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                 _composition_needed = false;
136                 
137                 if (!_film || !_image) {
138                         return;
139                 }
140
141                 /* Size of the view */
142                 int vw, vh;
143                 GetSize (&vw, &vh);
144
145                 Crop const fc = _film->crop ();
146
147                 /* Cropped rectangle */
148                 Rect cropped_area (
149                         fc.left,
150                         fc.top,
151                         _image->GetWidth() - (fc.left + fc.right),
152                         _image->GetHeight() - (fc.top + fc.bottom)
153                         );
154
155                 /* Target ratio */
156                 float const target = _film->format() ? _film->format()->ratio_as_float (_film) : 1.78;
157
158                 _transformed_image = _image->GetSubImage (wxRect (cropped_area.x, cropped_area.y, cropped_area.width, cropped_area.height));
159
160                 float x_scale = 1;
161                 float y_scale = 1;
162
163                 if ((float (vw) / vh) > target) {
164                         /* view is longer (horizontally) than the ratio; fit height */
165                         _transformed_image.Rescale (vh * target, vh, wxIMAGE_QUALITY_HIGH);
166                         x_scale = vh * target / cropped_area.width;
167                         y_scale = float (vh) / cropped_area.height;
168                 } else {
169                         /* view is shorter (horizontally) than the ratio; fit width */
170                         _transformed_image.Rescale (vw, vw / target, wxIMAGE_QUALITY_HIGH);
171                         x_scale = float (vw) / cropped_area.width;
172                         y_scale = (vw / target) / cropped_area.height;
173                 }
174
175                 _bitmap.reset (new wxBitmap (_transformed_image));
176
177                 if (_subtitle) {
178
179                         _subtitle->transformed_area = subtitle_transformed_area (
180                                 x_scale, y_scale, _subtitle->base_area, _film->subtitle_offset(), _film->subtitle_scale()
181                                 );
182
183                         _subtitle->transformed_image = _subtitle->base_image;
184                         _subtitle->transformed_image.Rescale (_subtitle->transformed_area.width, _subtitle->transformed_area.height, wxIMAGE_QUALITY_HIGH);
185                         _subtitle->transformed_area.x -= _film->crop().left;
186                         _subtitle->transformed_area.y -= _film->crop().top;
187                         _subtitle->bitmap.reset (new wxBitmap (_subtitle->transformed_image));
188                 }
189         }
190
191         Film* _film;
192         shared_ptr<wxImage> _image;
193         wxImage _transformed_image;
194         /** currently-displayed thumbnail index */
195         int _index;
196         shared_ptr<wxBitmap> _bitmap;
197         bool _frame_rebuild_needed;
198         bool _composition_needed;
199
200         struct SubtitleView
201         {
202                 SubtitleView (Position p, wxString const & i)
203                         : base_image (i)
204                 {
205                         base_area.x = p.x;
206                         base_area.y = p.y;
207                         base_area.width = base_image.GetWidth ();
208                         base_area.height = base_image.GetHeight ();
209                 }
210
211                 Rect base_area;
212                 Rect transformed_area;
213                 wxImage base_image;
214                 wxImage transformed_image;
215                 shared_ptr<wxBitmap> bitmap;
216         };
217
218         shared_ptr<SubtitleView> _subtitle;
219 };
220
221 BEGIN_EVENT_TABLE (ThumbPanel, wxPanel)
222 EVT_PAINT (ThumbPanel::paint_event)
223 EVT_SIZE (ThumbPanel::size_event)
224 END_EVENT_TABLE ()
225
226 FilmViewer::FilmViewer (Film* f, wxWindow* p)
227         : wxPanel (p)
228         , _film (0)
229 {
230         _sizer = new wxBoxSizer (wxVERTICAL);
231         SetSizer (_sizer);
232         
233         _thumb_panel = new ThumbPanel (this, f);
234         _sizer->Add (_thumb_panel, 1, wxEXPAND);
235
236         int const m = max ((size_t) 1, f ? f->thumbs().size() - 1 : 0);
237         _slider = new wxSlider (this, wxID_ANY, 0, 0, m);
238         _sizer->Add (_slider, 0, wxEXPAND | wxLEFT | wxRIGHT);
239         set_thumbnail (0);
240
241         _slider->Connect (wxID_ANY, wxEVT_COMMAND_SLIDER_UPDATED, wxCommandEventHandler (FilmViewer::slider_changed), 0, this);
242
243         set_film (_film);
244 }
245
246 void
247 FilmViewer::set_thumbnail (int n)
248 {
249         if (_film == 0 || int (_film->thumbs().size()) <= n) {
250                 return;
251         }
252
253         _thumb_panel->set (n);
254 }
255
256 void
257 FilmViewer::slider_changed (wxCommandEvent &)
258 {
259         set_thumbnail (_slider->GetValue ());
260 }
261
262 void
263 FilmViewer::film_changed (FilmState::Property p)
264 {
265         switch (p) {
266         case FilmState::THUMBS:
267                 if (_film && _film->thumbs().size() > 1) {
268                         _slider->SetRange (0, _film->thumbs().size() - 1);
269                 } else {
270                         _thumb_panel->clear ();
271                         _slider->SetRange (0, 1);
272                 }
273                 
274                 _slider->SetValue (0);
275                 set_thumbnail (0);
276                 break;
277         case FilmState::CONTENT:
278                 setup_visibility ();
279                 break;
280         case FilmState::CROP:
281         case FilmState::FORMAT:
282         case FilmState::WITH_SUBTITLES:
283         case FilmState::SUBTITLE_OFFSET:
284         case FilmState::SUBTITLE_SCALE:
285                 _thumb_panel->recompose ();
286                 break;
287         default:
288                 break;
289         }
290 }
291
292 void
293 FilmViewer::set_film (Film* f)
294 {
295         if (_film == f) {
296                 return;
297         }
298         
299         _film = f;
300         _thumb_panel->set_film (_film);
301
302         if (!_film) {
303                 return;
304         }
305
306         _film->Changed.connect (sigc::mem_fun (*this, &FilmViewer::film_changed));
307         film_changed (Film::CROP);
308         film_changed (Film::THUMBS);
309         setup_visibility ();
310 }
311
312 void
313 FilmViewer::setup_visibility ()
314 {
315         if (!_film) {
316                 return;
317         }
318
319         ContentType const c = _film->content_type ();
320         _slider->Show (c == VIDEO);
321 }