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 "lib/subtitle.h"
34 #include "film_viewer.h"
38 using namespace boost;
40 class ThumbPanel : public wxPanel
43 ThumbPanel (wxPanel* parent, Film* film)
46 , _frame_rebuild_needed (false)
47 , _composition_needed (false)
50 /** Handle a paint event */
51 void paint_event (wxPaintEvent& ev)
53 if (!_film || _film->num_thumbs() == 0) {
58 if (_frame_rebuild_needed) {
59 _image.reset (new wxImage (std_to_wx (_film->thumb_file (_index))));
62 list<pair<Position, string> > s = _film->thumb_subtitles (_index);
63 for (list<pair<Position, string> >::iterator i = s.begin(); i != s.end(); ++i) {
64 _subtitles.push_back (SubtitleView (i->first, std_to_wx (i->second)));
67 _frame_rebuild_needed = false;
70 _composition_needed = false;
73 if (_composition_needed) {
75 _composition_needed = false;
80 dc.DrawBitmap (*_bitmap, 0, 0, false);
83 if (_film->with_subtitles ()) {
84 for (list<SubtitleView>::iterator i = _subtitles.begin(); i != _subtitles.end(); ++i) {
85 dc.DrawBitmap (*i->bitmap, i->transformed_area.x, i->transformed_area.y, true);
90 /** Handle a size event */
91 void size_event (wxSizeEvent &)
100 /** @param n Thumbnail index */
104 _frame_rebuild_needed = true;
108 void set_film (Film* f)
113 _frame_rebuild_needed = true;
116 _frame_rebuild_needed = true;
121 /** Clear our thumbnail image */
131 _composition_needed = true;
135 DECLARE_EVENT_TABLE ();
141 if (!_film || !_image) {
145 /* Size of the view */
149 /* Cropped rectangle */
150 Rectangle cropped_area (
153 _image->GetWidth() - (_film->crop().left + _film->crop().right),
154 _image->GetHeight() - (_film->crop().top + _film->crop().bottom)
158 float const target = _film->format() ? _film->format()->ratio_as_float (_film) : 1.78;
160 _transformed_image = _image->GetSubImage (wxRect (cropped_area.x, cropped_area.y, cropped_area.w, cropped_area.h));
165 if ((float (vw) / vh) > target) {
166 /* view is longer (horizontally) than the ratio; fit height */
167 _transformed_image.Rescale (vh * target, vh, wxIMAGE_QUALITY_HIGH);
168 x_scale = vh * target / cropped_area.w;
169 y_scale = float (vh) / cropped_area.h;
171 /* view is shorter (horizontally) than the ratio; fit width */
172 _transformed_image.Rescale (vw, vw / target, wxIMAGE_QUALITY_HIGH);
173 x_scale = float (vw) / cropped_area.w;
174 y_scale = (vw / target) / cropped_area.h;
177 _bitmap.reset (new wxBitmap (_transformed_image));
179 for (list<SubtitleView>::iterator i = _subtitles.begin(); i != _subtitles.end(); ++i) {
181 i->transformed_area = transformed_subtitle_area (
182 x_scale, y_scale, i->base_area, _film->subtitle_offset(), _film->subtitle_scale()
185 i->transformed_image = i->base_image;
186 i->transformed_image.Rescale (i->transformed_area.w, i->transformed_area.h, wxIMAGE_QUALITY_HIGH);
187 i->transformed_area.x -= _film->crop().left;
188 i->transformed_area.y -= _film->crop().top;
189 i->bitmap.reset (new wxBitmap (i->transformed_image));
194 shared_ptr<wxImage> _image;
195 wxImage _transformed_image;
196 /** currently-displayed thumbnail index */
198 shared_ptr<wxBitmap> _bitmap;
199 bool _frame_rebuild_needed;
200 bool _composition_needed;
204 SubtitleView (Position p, wxString const & i)
209 base_area.w = base_image.GetWidth ();
210 base_area.h = base_image.GetHeight ();
214 Rectangle transformed_area;
216 wxImage transformed_image;
217 shared_ptr<wxBitmap> bitmap;
220 list<SubtitleView> _subtitles;
223 BEGIN_EVENT_TABLE (ThumbPanel, wxPanel)
224 EVT_PAINT (ThumbPanel::paint_event)
225 EVT_SIZE (ThumbPanel::size_event)
228 FilmViewer::FilmViewer (Film* f, wxWindow* p)
232 _sizer = new wxBoxSizer (wxVERTICAL);
235 _thumb_panel = new ThumbPanel (this, f);
236 _sizer->Add (_thumb_panel, 1, wxEXPAND);
238 int const max = f ? f->num_thumbs() - 1 : 0;
239 _slider = new wxSlider (this, wxID_ANY, 0, 0, max);
240 _sizer->Add (_slider, 0, wxEXPAND | wxLEFT | wxRIGHT);
243 _slider->Connect (wxID_ANY, wxEVT_COMMAND_SLIDER_UPDATED, wxCommandEventHandler (FilmViewer::slider_changed), 0, this);
249 FilmViewer::set_thumbnail (int n)
251 if (_film == 0 || _film->num_thumbs() <= n) {
255 _thumb_panel->set (n);
259 FilmViewer::slider_changed (wxCommandEvent &)
261 set_thumbnail (_slider->GetValue ());
265 FilmViewer::film_changed (Film::Property p)
269 if (_film && _film->num_thumbs() > 1) {
270 _slider->SetRange (0, _film->num_thumbs () - 1);
272 _thumb_panel->clear ();
273 _slider->SetRange (0, 1);
276 _slider->SetValue (0);
281 _film->examine_content ();
286 case Film::WITH_SUBTITLES:
287 case Film::SUBTITLE_OFFSET:
288 case Film::SUBTITLE_SCALE:
289 _thumb_panel->recompose ();
297 FilmViewer::set_film (Film* f)
304 _thumb_panel->set_film (_film);
310 _film->Changed.connect (sigc::mem_fun (*this, &FilmViewer::film_changed));
311 film_changed (Film::CROP);
312 film_changed (Film::THUMBS);
317 FilmViewer::update_thumbs ()
323 _film->update_thumbs_pre_gui ();
325 shared_ptr<const FilmState> s = _film->state_copy ();
326 shared_ptr<Options> o (new Options (s->dir ("thumbs"), ".png", ""));
327 o->out_size = _film->size ();
328 o->apply_crop = false;
329 o->decode_audio = false;
330 o->decode_video_frequency = 128;
331 o->decode_subtitles = true;
333 shared_ptr<Job> j (new ThumbsJob (s, o, _film->log(), shared_ptr<Job> ()));
334 j->Finished.connect (sigc::mem_fun (_film, &Film::update_thumbs_post_gui));
335 JobManager::instance()->add (j);
339 FilmViewer::setup_visibility ()
345 ContentType const c = _film->content_type ();
346 _slider->Show (c == VIDEO);