2 Copyright (C) 2012 Carl Hetherington <cth@carlh.net>
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.
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.
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.
20 /** @file src/film_viewer.cc
21 * @brief A wx widget to view `thumbnails' of a Film.
27 #include "lib/format.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"
37 using namespace boost;
39 class ThumbPanel : public wxPanel
42 ThumbPanel (wxPanel* parent, Film* film)
45 , _frame_rebuild_needed (false)
46 , _composition_needed (false)
49 /** Handle a paint event */
50 void paint_event (wxPaintEvent& ev)
57 if (_frame_rebuild_needed) {
58 _image.reset (new wxImage (std_to_wx (_film->thumb_file (_index))));
61 list<pair<Position, string> > s = _film->thumb_subtitles (_index);
62 for (list<pair<Position, string> >::iterator i = s.begin(); i != s.end(); ++i) {
63 _subtitles.push_back (SubtitleView (i->first, std_to_wx (i->second)));
66 _frame_rebuild_needed = false;
69 _composition_needed = false;
72 if (_composition_needed) {
74 _composition_needed = false;
79 dc.DrawBitmap (*_bitmap, 0, 0, false);
82 if (_film->with_subtitles ()) {
83 for (list<SubtitleView>::iterator i = _subtitles.begin(); i != _subtitles.end(); ++i) {
84 dc.DrawBitmap (*i->bitmap, i->cropped_position.x, i->cropped_position.y, true);
89 /** Handle a size event */
90 void size_event (wxSizeEvent &)
99 /** @param n Thumbnail index */
103 _frame_rebuild_needed = true;
107 void set_film (Film* f)
112 _frame_rebuild_needed = true;
115 _frame_rebuild_needed = true;
120 /** Clear our thumbnail image */
130 _composition_needed = true;
134 DECLARE_EVENT_TABLE ();
140 if (!_film || !_image) {
144 /* Size of the view */
148 /* Cropped rectangle */
152 _image->GetWidth() - (_film->crop().left + _film->crop().right),
153 _image->GetHeight() - (_film->crop().top + _film->crop().bottom)
157 float const target = _film->format() ? _film->format()->ratio_as_float (_film) : 1.78;
159 _cropped_image = _image->GetSubImage (wxRect (cropped.x, cropped.y, cropped.w, cropped.h));
164 if ((float (vw) / vh) > target) {
165 /* view is longer (horizontally) than the ratio; fit height */
166 _cropped_image.Rescale (vh * target, vh, wxIMAGE_QUALITY_HIGH);
167 x_scale = vh * target / cropped.w;
168 y_scale = float (vh) / cropped.h;
170 /* view is shorter (horizontally) than the ratio; fit width */
171 _cropped_image.Rescale (vw, vw / target, wxIMAGE_QUALITY_HIGH);
172 x_scale = float (vw) / cropped.w;
173 y_scale = (vw / target) / cropped.h;
176 _bitmap.reset (new wxBitmap (_cropped_image));
178 for (list<SubtitleView>::iterator i = _subtitles.begin(); i != _subtitles.end(); ++i) {
180 /* Area of the subtitle graphic within the (uncropped) picture frame */
181 Rectangle sub_rect (i->position.x, i->position.y + _film->subtitle_offset(), i->image.GetWidth(), i->image.GetHeight());
182 /* Hence the subtitle graphic after it has been cropped */
183 Rectangle cropped_sub_rect = sub_rect.intersection (cropped);
185 /* Get the cropped version of the subtitle image */
186 i->cropped_image = i->image.GetSubImage (
188 cropped_sub_rect.x - sub_rect.x,
189 cropped_sub_rect.y - sub_rect.y,
195 i->cropped_image.Rescale (cropped_sub_rect.w * x_scale, cropped_sub_rect.h * y_scale, wxIMAGE_QUALITY_HIGH);
197 i->cropped_position = Position (
198 cropped_sub_rect.x * x_scale,
199 (cropped_sub_rect.y - _film->crop().top) * y_scale
202 i->bitmap.reset (new wxBitmap (i->cropped_image));
207 shared_ptr<wxImage> _image;
208 wxImage _cropped_image;
209 /** currently-displayed thumbnail index */
211 shared_ptr<wxBitmap> _bitmap;
212 bool _frame_rebuild_needed;
213 bool _composition_needed;
217 SubtitleView (Position p, wxString const & i)
224 Position cropped_position;
225 wxImage cropped_image;
226 shared_ptr<wxBitmap> bitmap;
229 list<SubtitleView> _subtitles;
232 BEGIN_EVENT_TABLE (ThumbPanel, wxPanel)
233 EVT_PAINT (ThumbPanel::paint_event)
234 EVT_SIZE (ThumbPanel::size_event)
237 FilmViewer::FilmViewer (Film* f, wxWindow* p)
241 _sizer = new wxBoxSizer (wxVERTICAL);
244 _thumb_panel = new ThumbPanel (this, f);
245 _sizer->Add (_thumb_panel, 1, wxEXPAND);
247 int const max = f ? f->num_thumbs() - 1 : 0;
248 _slider = new wxSlider (this, wxID_ANY, 0, 0, max);
249 _sizer->Add (_slider, 0, wxEXPAND | wxLEFT | wxRIGHT);
252 _slider->Connect (wxID_ANY, wxEVT_COMMAND_SLIDER_UPDATED, wxCommandEventHandler (FilmViewer::slider_changed), 0, this);
258 FilmViewer::set_thumbnail (int n)
260 if (_film == 0 || _film->num_thumbs() <= n) {
264 _thumb_panel->set (n);
268 FilmViewer::slider_changed (wxCommandEvent &)
270 set_thumbnail (_slider->GetValue ());
274 FilmViewer::film_changed (Film::Property p)
278 if (_film && _film->num_thumbs() > 1) {
279 _slider->SetRange (0, _film->num_thumbs () - 1);
281 _thumb_panel->clear ();
282 _slider->SetRange (0, 1);
285 _slider->SetValue (0);
290 _film->examine_content ();
295 case Film::WITH_SUBTITLES:
296 case Film::SUBTITLE_OFFSET:
297 case Film::SUBTITLE_SCALE:
298 _thumb_panel->recompose ();
306 FilmViewer::set_film (Film* f)
313 _thumb_panel->set_film (_film);
319 _film->Changed.connect (sigc::mem_fun (*this, &FilmViewer::film_changed));
320 film_changed (Film::CROP);
321 film_changed (Film::THUMBS);
326 FilmViewer::update_thumbs ()
332 _film->update_thumbs_pre_gui ();
334 shared_ptr<const FilmState> s = _film->state_copy ();
335 shared_ptr<Options> o (new Options (s->dir ("thumbs"), ".png", ""));
336 o->out_size = _film->size ();
337 o->apply_crop = false;
338 o->decode_audio = false;
339 o->decode_video_frequency = 128;
341 shared_ptr<Job> j (new ThumbsJob (s, o, _film->log(), shared_ptr<Job> ()));
342 j->Finished.connect (sigc::mem_fun (_film, &Film::update_thumbs_post_gui));
343 JobManager::instance()->add (j);
347 FilmViewer::setup_visibility ()
353 ContentType const c = _film->content_type ();
354 _slider->Show (c == VIDEO);