Merge branch 'master' of /home/carl/git/dvdomatic
[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                 , _image (0)
46                 , _bitmap (0)
47         {
48         }
49
50         /** Handle a paint event */
51         void paint_event (wxPaintEvent& ev)
52         {
53                 if (_current_image != _pending_image) {
54                         delete _image;
55                         _image = new wxImage (std_to_wx (_pending_image));
56                         _current_image = _pending_image;
57                         setup ();
58                 }
59
60                 if (_current_crop != _pending_crop) {
61                         _current_crop = _pending_crop;
62                         setup ();
63                 }
64
65                 wxPaintDC dc (this);
66                 if (_bitmap) {
67                         dc.DrawBitmap (*_bitmap, 0, 0, false);
68                 }
69         }
70
71         /** Handle a size event */
72         void size_event (wxSizeEvent &)
73         {
74                 if (!_image) {
75                         return;
76                 }
77
78                 setup ();
79                 Refresh ();
80         }
81
82         void set (string f)
83         {
84                 _pending_image = f;
85                 Refresh ();
86         }
87
88         void set_crop (Crop c)
89         {
90                 _pending_crop = c;
91                 Refresh ();
92         }
93
94         void set_film (Film* f)
95         {
96                 _film = f;
97                 if (!_film) {
98                         clear ();
99                         Refresh ();
100                 } else {
101                         setup ();
102                         Refresh ();
103                 }
104         }
105
106         /** Clear our thumbnail image */
107         void clear ()
108         {
109                 delete _bitmap;
110                 _bitmap = 0;
111                 delete _image;
112                 _image = 0;
113         }
114
115         void refresh ()
116         {
117                 setup ();
118                 Refresh ();
119         }
120
121         DECLARE_EVENT_TABLE ();
122
123 private:
124
125         void setup ()
126         {
127                 if (!_film || !_image) {
128                         return;
129                 }
130                 
131                 int vw, vh;
132                 GetSize (&vw, &vh);
133
134                 float const target = _film->format() ? _film->format()->ratio_as_float () : 1.78;
135
136                 _cropped_image = _image->GetSubImage (
137                         wxRect (
138                                 _current_crop.left,
139                                 _current_crop.top,
140                                 _image->GetWidth() - (_current_crop.left + _current_crop.right),
141                                 _image->GetHeight() - (_current_crop.top + _current_crop.bottom)
142                                 )
143                         );
144
145                 if ((float (vw) / vh) > target) {
146                         /* view is longer (horizontally) than the ratio; fit height */
147                         _cropped_image.Rescale (vh * target, vh, wxIMAGE_QUALITY_HIGH);
148                 } else {
149                         /* view is shorter (horizontally) than the ratio; fit width */
150                         _cropped_image.Rescale (vw, vw / target, wxIMAGE_QUALITY_HIGH);
151                 }
152
153                 delete _bitmap;
154                 _bitmap = new wxBitmap (_cropped_image);
155         }
156
157         Film* _film;
158         wxImage* _image;
159         std::string _current_image;
160         std::string _pending_image;
161         wxImage _cropped_image;
162         wxBitmap* _bitmap;
163         Crop _current_crop;
164         Crop _pending_crop;
165 };
166
167 BEGIN_EVENT_TABLE (ThumbPanel, wxPanel)
168 EVT_PAINT (ThumbPanel::paint_event)
169 EVT_SIZE (ThumbPanel::size_event)
170 END_EVENT_TABLE ()
171
172 FilmViewer::FilmViewer (Film* f, wxWindow* p)
173         : wxPanel (p)
174         , _film (f)
175 {
176         _sizer = new wxBoxSizer (wxVERTICAL);
177         SetSizer (_sizer);
178         
179         _thumb_panel = new ThumbPanel (this, f);
180         _sizer->Add (_thumb_panel, 1, wxEXPAND);
181
182         int const max = f ? f->num_thumbs() - 1 : 0;
183         _slider = new wxSlider (this, wxID_ANY, 0, 0, max);
184         _sizer->Add (_slider, 0, wxEXPAND | wxLEFT | wxRIGHT);
185         set_thumbnail (0);
186
187         _slider->Connect (wxID_ANY, wxEVT_COMMAND_SLIDER_UPDATED, wxCommandEventHandler (FilmViewer::slider_changed), 0, this);
188
189         set_film (_film);
190 }
191
192 void
193 FilmViewer::set_thumbnail (int n)
194 {
195         if (_film == 0 || _film->num_thumbs() <= n) {
196                 return;
197         }
198
199         _thumb_panel->set (_film->thumb_file(n));
200 }
201
202 void
203 FilmViewer::slider_changed (wxCommandEvent &)
204 {
205         set_thumbnail (_slider->GetValue ());
206 }
207
208 void
209 FilmViewer::film_changed (Film::Property p)
210 {
211         if (p == Film::CROP) {
212                 _thumb_panel->set_crop (_film->crop ());
213         } else if (p == Film::THUMBS) {
214                 if (_film && _film->num_thumbs() > 1) {
215                         _slider->SetRange (0, _film->num_thumbs () - 1);
216                 } else {
217                         _thumb_panel->clear ();
218                         _slider->SetRange (0, 1);
219                 }
220                 
221                 _slider->SetValue (0);
222                 set_thumbnail (0);
223         } else if (p == Film::FORMAT) {
224                 _thumb_panel->refresh ();
225         } else if (p == Film::CONTENT) {
226                 setup_visibility ();
227                 _film->examine_content ();
228                 update_thumbs ();
229         }
230 }
231
232 void
233 FilmViewer::set_film (Film* f)
234 {
235         _film = f;
236         _thumb_panel->set_film (_film);
237
238         if (!_film) {
239                 return;
240         }
241
242         _film->Changed.connect (sigc::mem_fun (*this, &FilmViewer::film_changed));
243         film_changed (Film::CROP);
244         film_changed (Film::THUMBS);
245         _thumb_panel->refresh ();
246         setup_visibility ();
247 }
248
249 void
250 FilmViewer::update_thumbs ()
251 {
252         if (!_film) {
253                 return;
254         }
255
256         _film->update_thumbs_pre_gui ();
257
258         shared_ptr<const FilmState> s = _film->state_copy ();
259         shared_ptr<Options> o (new Options (s->dir ("thumbs"), ".tiff", ""));
260         o->out_size = _film->size ();
261         o->apply_crop = false;
262         o->decode_audio = false;
263         o->decode_video_frequency = 128;
264         
265         shared_ptr<Job> j (new ThumbsJob (s, o, _film->log ()));
266         j->Finished.connect (sigc::mem_fun (_film, &Film::update_thumbs_post_gui));
267         JobManager::instance()->add (j);
268 }
269
270 void
271 FilmViewer::setup_visibility ()
272 {
273         if (!_film) {
274                 return;
275         }
276
277         ContentType const c = _film->content_type ();
278         _slider->Show (c == VIDEO);
279 }