Various hacks.
[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
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                 , _image (0)
45                 , _bitmap (0)
46                 , _left_crop (0)
47                 , _right_crop (0)
48                 , _top_crop (0)
49                 , _bottom_crop (0)
50         {
51         }
52
53         void paint_event (wxPaintEvent& ev)
54         {
55                 if (!_bitmap) {
56                         return;
57                 }
58
59                 wxPaintDC dc (this);
60                 dc.DrawBitmap (*_bitmap, 0, 0, false);
61         }
62
63         void size_event (wxSizeEvent &)
64         {
65                 if (!_image) {
66                         return;
67                 }
68
69                 resize ();
70         }
71
72         void resize ()
73         {
74                 int vw, vh;
75                 GetSize (&vw, &vh);
76
77                 float const target = _film->format()->ratio_as_float ();
78
79                 _cropped_image = _image->GetSubImage (
80                         wxRect (_left_crop, _top_crop, _image->GetWidth() - (_left_crop + _right_crop), _image->GetHeight() - (_top_crop + _bottom_crop))
81                         );
82
83                 if ((float (vw) / vh) > target) {
84                         /* view is longer (horizontally) than the ratio; fit height */
85                         _cropped_image.Rescale (vh * target, vh);
86                 } else {
87                         /* view is shorter (horizontally) than the ratio; fit width */
88                         _cropped_image.Rescale (vw, vw / target);
89                 }
90
91                 delete _bitmap;
92                 _bitmap = new wxBitmap (_cropped_image);
93
94                 Refresh ();
95         }
96
97         void load (string f)
98         {
99                 delete _image;
100                 _image = new wxImage (wxString (f.c_str(), wxConvUTF8));
101                 resize ();
102         }
103
104         void set_crop (int l, int r, int t, int b)
105         {
106                 _left_crop = l;
107                 _right_crop = r;
108                 _top_crop = t;
109                 _bottom_crop = b;
110                 resize ();
111         }
112
113         void clear ()
114         {
115                 delete _bitmap;
116                 _bitmap = 0;
117                 delete _image;
118                 _image = 0;
119         }
120
121         DECLARE_EVENT_TABLE ();
122
123 private:
124         Film* _film;
125         wxImage* _image;
126         wxImage _cropped_image;
127         wxBitmap* _bitmap;
128         int _left_crop;
129         int _right_crop;
130         int _top_crop;
131         int _bottom_crop;
132 };
133
134 BEGIN_EVENT_TABLE (ThumbPanel, wxPanel)
135 EVT_PAINT (ThumbPanel::paint_event)
136 EVT_SIZE (ThumbPanel::size_event)
137 END_EVENT_TABLE ()
138
139 FilmViewer::FilmViewer (Film* f, wxWindow* p)
140         : wxPanel (p)
141         , _film (f)
142 {
143         _sizer = new wxBoxSizer (wxVERTICAL);
144         SetSizer (_sizer);
145         
146         _thumb_panel = new ThumbPanel (this, f);
147         _sizer->Add (_thumb_panel, 1, wxEXPAND);
148
149         int const max = f ? f->num_thumbs() - 1 : 0;
150         _slider = new wxSlider (this, wxID_ANY, 0, 0, max);
151         _sizer->Add (_slider, 0, wxEXPAND | wxLEFT | wxRIGHT);
152         load_thumbnail (0);
153
154         _slider->Connect (wxID_ANY, wxEVT_COMMAND_SLIDER_UPDATED, wxCommandEventHandler (FilmViewer::slider_changed), 0, this);
155
156         set_film (_film);
157 }
158
159 void
160 FilmViewer::load_thumbnail (int n)
161 {
162         if (_film == 0 || _film->num_thumbs() <= n) {
163                 return;
164         }
165
166         _thumb_panel->load (_film->thumb_file(n));
167 }
168
169 void
170 FilmViewer::reload_current_thumbnail ()
171 {
172         load_thumbnail (_slider->GetValue ());
173 }
174
175 void
176 FilmViewer::slider_changed (wxCommandEvent &)
177 {
178         reload_current_thumbnail ();
179 }
180
181 void
182 FilmViewer::film_changed (Film::Property p)
183 {
184         if (p == Film::LEFT_CROP || p == Film::RIGHT_CROP || p == Film::TOP_CROP || p == Film::BOTTOM_CROP) {
185                 _thumb_panel->set_crop (_film->left_crop(), _film->right_crop(), _film->top_crop(), _film->bottom_crop ());
186         } else if (p == Film::THUMBS) {
187                 if (_film && _film->num_thumbs() > 1) {
188                         _slider->SetRange (0, _film->num_thumbs () - 1);
189                 } else {
190                         _thumb_panel->clear ();
191                         _slider->SetRange (0, 1);
192                 }
193                 
194                 _slider->SetValue (0);
195                 reload_current_thumbnail ();
196         } else if (p == Film::FORMAT) {
197                 reload_current_thumbnail ();
198         } else if (p == Film::CONTENT) {
199                 setup_visibility ();
200                 _film->examine_content ();
201                 update_thumbs ();
202         }
203 }
204
205 void
206 FilmViewer::set_film (Film* f)
207 {
208         _film = f;
209
210         if (!_film) {
211                 _thumb_panel->clear ();
212                 return;
213         }
214
215         _film->Changed.connect (sigc::mem_fun (*this, &FilmViewer::film_changed));
216
217         film_changed (Film::THUMBS);
218 }
219
220 void
221 FilmViewer::update_thumbs ()
222 {
223         if (!_film) {
224                 return;
225         }
226
227         _film->update_thumbs_pre_gui ();
228
229         shared_ptr<const FilmState> s = _film->state_copy ();
230         shared_ptr<Options> o (new Options (s->dir ("thumbs"), ".tiff", ""));
231         o->out_size = _film->size ();
232         o->apply_crop = false;
233         o->decode_audio = false;
234         o->decode_video_frequency = 128;
235         
236         shared_ptr<Job> j (new ThumbsJob (s, o, _film->log ()));
237         j->Finished.connect (sigc::mem_fun (_film, &Film::update_thumbs_post_gui));
238         JobManager::instance()->add (j);
239 }
240
241 void
242 FilmViewer::setup_visibility ()
243 {
244         if (!_film) {
245                 return;
246         }
247
248         ContentType const c = _film->content_type ();
249         _slider->Show (c == VIDEO);
250 }