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"
36 using namespace boost;
38 class ThumbPanel : public wxPanel
41 ThumbPanel (wxPanel* parent, Film* film)
53 void paint_event (wxPaintEvent& ev)
60 dc.DrawBitmap (*_bitmap, 0, 0, false);
63 void size_event (wxSizeEvent &)
77 float const target = _film->format()->ratio_as_float ();
79 _cropped_image = _image->GetSubImage (
80 wxRect (_left_crop, _top_crop, _image->GetWidth() - (_left_crop + _right_crop), _image->GetHeight() - (_top_crop + _bottom_crop))
83 if ((float (vw) / vh) > target) {
84 /* view is longer (horizontally) than the ratio; fit height */
85 _cropped_image.Rescale (vh * target, vh);
87 /* view is shorter (horizontally) than the ratio; fit width */
88 _cropped_image.Rescale (vw, vw / target);
92 _bitmap = new wxBitmap (_cropped_image);
100 _image = new wxImage (wxString (f.c_str(), wxConvUTF8));
104 void set_crop (int l, int r, int t, int b)
121 DECLARE_EVENT_TABLE ();
126 wxImage _cropped_image;
134 BEGIN_EVENT_TABLE (ThumbPanel, wxPanel)
135 EVT_PAINT (ThumbPanel::paint_event)
136 EVT_SIZE (ThumbPanel::size_event)
139 FilmViewer::FilmViewer (Film* f, wxWindow* p)
143 _sizer = new wxBoxSizer (wxVERTICAL);
146 _thumb_panel = new ThumbPanel (this, f);
147 _sizer->Add (_thumb_panel, 1, wxEXPAND);
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);
154 _slider->Connect (wxID_ANY, wxEVT_COMMAND_SLIDER_UPDATED, wxCommandEventHandler (FilmViewer::slider_changed), 0, this);
160 FilmViewer::load_thumbnail (int n)
162 if (_film == 0 || _film->num_thumbs() <= n) {
166 _thumb_panel->load (_film->thumb_file(n));
170 FilmViewer::reload_current_thumbnail ()
172 load_thumbnail (_slider->GetValue ());
176 FilmViewer::slider_changed (wxCommandEvent &)
178 reload_current_thumbnail ();
182 FilmViewer::film_changed (Film::Property p)
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);
190 _thumb_panel->clear ();
191 _slider->SetRange (0, 1);
194 _slider->SetValue (0);
195 reload_current_thumbnail ();
196 } else if (p == Film::FORMAT) {
197 reload_current_thumbnail ();
198 } else if (p == Film::CONTENT) {
200 _film->examine_content ();
206 FilmViewer::set_film (Film* f)
211 _thumb_panel->clear ();
215 _film->Changed.connect (sigc::mem_fun (*this, &FilmViewer::film_changed));
217 film_changed (Film::THUMBS);
221 FilmViewer::update_thumbs ()
227 _film->update_thumbs_pre_gui ();
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;
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);
242 FilmViewer::setup_visibility ()
248 ContentType const c = _film->content_type ();
249 _slider->Show (c == VIDEO);