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 GTK 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 FilmViewer::FilmViewer (Film* f)
41 _scroller.add (_image);
43 Gtk::HBox* controls = manage (new Gtk::HBox);
44 controls->set_spacing (6);
45 controls->pack_start (_position_slider);
47 _vbox.pack_start (_scroller, true, true);
48 _vbox.pack_start (*controls, false, false);
49 _vbox.set_border_width (12);
51 _position_slider.set_digits (0);
52 _position_slider.signal_format_value().connect (sigc::mem_fun (*this, &FilmViewer::format_position_slider_value));
53 _position_slider.signal_value_changed().connect (sigc::mem_fun (*this, &FilmViewer::position_slider_changed));
55 _scroller.signal_size_allocate().connect (sigc::mem_fun (*this, &FilmViewer::scroller_size_allocate));
61 FilmViewer::load_thumbnail (int n)
63 if (_film == 0 || _film->num_thumbs() <= n) {
67 int const left = _film->left_crop ();
68 int const right = _film->right_crop ();
69 int const top = _film->top_crop ();
70 int const bottom = _film->bottom_crop ();
72 _pixbuf = Gdk::Pixbuf::create_from_file (_film->thumb_file (n));
74 int const cw = _film->size().width - left - right;
75 int const ch = _film->size().height - top - bottom;
76 _cropped_pixbuf = Gdk::Pixbuf::create_subpixbuf (_pixbuf, left, top, cw, ch);
77 update_scaled_pixbuf ();
78 _image.set (_scaled_pixbuf);
82 FilmViewer::reload_current_thumbnail ()
84 load_thumbnail (_position_slider.get_value ());
88 FilmViewer::position_slider_changed ()
90 reload_current_thumbnail ();
94 FilmViewer::format_position_slider_value (double v) const
98 if (_film && int (v) < _film->num_thumbs ()) {
99 int const f = _film->thumb_frame (int (v));
100 s << f << " " << seconds_to_hms (f / _film->frames_per_second ());
109 FilmViewer::film_changed (Film::Property p)
111 if (p == Film::LEFT_CROP || p == Film::RIGHT_CROP || p == Film::TOP_CROP || p == Film::BOTTOM_CROP) {
112 reload_current_thumbnail ();
113 } else if (p == Film::THUMBS) {
114 if (_film && _film->num_thumbs() > 1) {
115 _position_slider.set_range (0, _film->num_thumbs () - 1);
118 _position_slider.set_range (0, 1);
121 _position_slider.set_value (0);
122 reload_current_thumbnail ();
123 } else if (p == Film::FORMAT) {
124 reload_current_thumbnail ();
125 } else if (p == Film::CONTENT) {
127 _film->examine_content ();
133 FilmViewer::set_film (Film* f)
142 _film->Changed.connect (sigc::mem_fun (*this, &FilmViewer::film_changed));
144 film_changed (Film::THUMBS);
148 FilmViewer::scaled_pixbuf_size () const
151 return make_pair (0, 0);
154 int const cw = _film->size().width - _film->left_crop() - _film->right_crop();
155 int const ch = _film->size().height - _film->top_crop() - _film->bottom_crop();
158 if (_film->format()) {
159 ratio = _film->format()->ratio_as_float() * ch / cw;
162 Gtk::Allocation const a = _scroller.get_allocation ();
163 float const zoom = min (float (a.get_width()) / (cw * ratio), float (a.get_height()) / cw);
164 return make_pair (cw * zoom * ratio, ch * zoom);
168 FilmViewer::update_scaled_pixbuf ()
170 pair<int, int> const s = scaled_pixbuf_size ();
172 if (s.first > 0 && s.second > 0 && _cropped_pixbuf) {
173 _scaled_pixbuf = _cropped_pixbuf->scale_simple (s.first, s.second, Gdk::INTERP_HYPER);
174 _image.set (_scaled_pixbuf);
179 FilmViewer::update_thumbs ()
185 _film->update_thumbs_pre_gui ();
187 shared_ptr<const FilmState> s = _film->state_copy ();
188 shared_ptr<Options> o (new Options (s->dir ("thumbs"), ".tiff", ""));
189 o->out_size = _film->size ();
190 o->apply_crop = false;
191 o->decode_audio = false;
192 o->decode_video_frequency = 128;
194 shared_ptr<Job> j (new ThumbsJob (s, o, _film->log ()));
195 j->Finished.connect (sigc::mem_fun (_film, &Film::update_thumbs_post_gui));
196 JobManager::instance()->add (j);
200 FilmViewer::scroller_size_allocate (Gtk::Allocation a)
202 if (a.get_width() != _last_scroller_allocation.get_width() || a.get_height() != _last_scroller_allocation.get_height()) {
203 update_scaled_pixbuf ();
206 _last_scroller_allocation = a;
210 FilmViewer::setup_visibility ()
216 ContentType const c = _film->content_type ();
217 _position_slider.property_visible() = (c == VIDEO);