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)
52 if (_frame_rebuild_needed) {
53 _image.reset (new wxImage (std_to_wx (_film->thumb_file (_index))));
56 list<pair<Position, string> > s = _film->thumb_subtitles (_index);
57 for (list<pair<Position, string> >::iterator i = s.begin(); i != s.end(); ++i) {
58 _subtitles.push_back (SubtitleView (i->first, std_to_wx (i->second)));
61 _frame_rebuild_needed = false;
64 _composition_needed = false;
67 if (_composition_needed) {
69 _composition_needed = false;
74 dc.DrawBitmap (*_bitmap, 0, 0, false);
77 if (_film && _film->with_subtitles ()) {
78 for (list<SubtitleView>::iterator i = _subtitles.begin(); i != _subtitles.end(); ++i) {
79 dc.DrawBitmap (*i->bitmap, i->cropped_position.x, i->cropped_position.y, true);
84 /** Handle a size event */
85 void size_event (wxSizeEvent &)
94 /** @param n Thumbnail index */
98 _frame_rebuild_needed = true;
102 void set_film (Film* f)
107 _frame_rebuild_needed = true;
110 _frame_rebuild_needed = true;
115 /** Clear our thumbnail image */
125 _composition_needed = true;
129 DECLARE_EVENT_TABLE ();
135 if (!_film || !_image) {
139 /* Size of the view */
143 /* Cropped rectangle */
147 _image->GetWidth() - (_film->crop().left + _film->crop().right),
148 _image->GetHeight() - (_film->crop().top + _film->crop().bottom)
152 float const target = _film->format() ? _film->format()->ratio_as_float (_film) : 1.78;
154 _cropped_image = _image->GetSubImage (wxRect (cropped.x, cropped.y, cropped.w, cropped.h));
159 if ((float (vw) / vh) > target) {
160 /* view is longer (horizontally) than the ratio; fit height */
161 _cropped_image.Rescale (vh * target, vh, wxIMAGE_QUALITY_HIGH);
162 x_scale = vh * target / cropped.w;
163 y_scale = float (vh) / cropped.h;
165 /* view is shorter (horizontally) than the ratio; fit width */
166 _cropped_image.Rescale (vw, vw / target, wxIMAGE_QUALITY_HIGH);
167 x_scale = float (vw) / cropped.w;
168 y_scale = (vw / target) / cropped.h;
171 _bitmap.reset (new wxBitmap (_cropped_image));
173 for (list<SubtitleView>::iterator i = _subtitles.begin(); i != _subtitles.end(); ++i) {
175 /* Area of the subtitle graphic within the (uncropped) picture frame */
176 Rectangle sub_rect (i->position.x, i->position.y + _film->subtitle_offset(), i->image.GetWidth(), i->image.GetHeight());
177 /* Hence the subtitle graphic after it has been cropped */
178 Rectangle cropped_sub_rect = sub_rect.intersection (cropped);
180 /* Get the cropped version of the subtitle image */
181 i->cropped_image = i->image.GetSubImage (
183 cropped_sub_rect.x - sub_rect.x,
184 cropped_sub_rect.y - sub_rect.y,
190 i->cropped_image.Rescale (cropped_sub_rect.w * x_scale, cropped_sub_rect.h * y_scale, wxIMAGE_QUALITY_HIGH);
192 i->cropped_position = Position (
193 cropped_sub_rect.x * x_scale,
194 (cropped_sub_rect.y - _film->crop().top) * y_scale
197 i->bitmap.reset (new wxBitmap (i->cropped_image));
202 shared_ptr<wxImage> _image;
203 wxImage _cropped_image;
204 /** currently-displayed thumbnail index */
206 shared_ptr<wxBitmap> _bitmap;
207 bool _frame_rebuild_needed;
208 bool _composition_needed;
212 SubtitleView (Position p, wxString const & i)
219 Position cropped_position;
220 wxImage cropped_image;
221 shared_ptr<wxBitmap> bitmap;
224 list<SubtitleView> _subtitles;
227 BEGIN_EVENT_TABLE (ThumbPanel, wxPanel)
228 EVT_PAINT (ThumbPanel::paint_event)
229 EVT_SIZE (ThumbPanel::size_event)
232 FilmViewer::FilmViewer (Film* f, wxWindow* p)
236 _sizer = new wxBoxSizer (wxVERTICAL);
239 _thumb_panel = new ThumbPanel (this, f);
240 _sizer->Add (_thumb_panel, 1, wxEXPAND);
242 int const max = f ? f->num_thumbs() - 1 : 0;
243 _slider = new wxSlider (this, wxID_ANY, 0, 0, max);
244 _sizer->Add (_slider, 0, wxEXPAND | wxLEFT | wxRIGHT);
247 _slider->Connect (wxID_ANY, wxEVT_COMMAND_SLIDER_UPDATED, wxCommandEventHandler (FilmViewer::slider_changed), 0, this);
253 FilmViewer::set_thumbnail (int n)
255 if (_film == 0 || _film->num_thumbs() <= n) {
259 _thumb_panel->set (n);
263 FilmViewer::slider_changed (wxCommandEvent &)
265 set_thumbnail (_slider->GetValue ());
269 FilmViewer::film_changed (Film::Property p)
273 if (_film && _film->num_thumbs() > 1) {
274 _slider->SetRange (0, _film->num_thumbs () - 1);
276 _thumb_panel->clear ();
277 _slider->SetRange (0, 1);
280 _slider->SetValue (0);
285 _film->examine_content ();
290 case Film::WITH_SUBTITLES:
291 case Film::SUBTITLE_OFFSET:
292 case Film::SUBTITLE_SCALE:
293 _thumb_panel->recompose ();
301 FilmViewer::set_film (Film* f)
308 _thumb_panel->set_film (_film);
314 _film->Changed.connect (sigc::mem_fun (*this, &FilmViewer::film_changed));
315 film_changed (Film::CROP);
316 film_changed (Film::THUMBS);
321 FilmViewer::update_thumbs ()
327 _film->update_thumbs_pre_gui ();
329 shared_ptr<const FilmState> s = _film->state_copy ();
330 shared_ptr<Options> o (new Options (s->dir ("thumbs"), ".png", ""));
331 o->out_size = _film->size ();
332 o->apply_crop = false;
333 o->decode_audio = false;
334 o->decode_video_frequency = 128;
336 shared_ptr<Job> j (new ThumbsJob (s, o, _film->log(), shared_ptr<Job> ()));
337 j->Finished.connect (sigc::mem_fun (_film, &Film::update_thumbs_post_gui));
338 JobManager::instance()->add (j);
342 FilmViewer::setup_visibility ()
348 ContentType const c = _film->content_type ();
349 _slider->Show (c == VIDEO);