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)
40 , _update_button ("Update")
42 _scroller.add (_image);
44 Gtk::HBox* controls = manage (new Gtk::HBox);
45 controls->set_spacing (6);
46 controls->pack_start (_update_button, false, false);
47 controls->pack_start (_position_slider);
49 _vbox.pack_start (_scroller, true, true);
50 _vbox.pack_start (*controls, false, false);
51 _vbox.set_border_width (12);
53 _update_button.signal_clicked().connect (sigc::mem_fun (*this, &FilmViewer::update_thumbs));
55 _position_slider.set_digits (0);
56 _position_slider.signal_format_value().connect (sigc::mem_fun (*this, &FilmViewer::format_position_slider_value));
57 _position_slider.signal_value_changed().connect (sigc::mem_fun (*this, &FilmViewer::position_slider_changed));
59 _scroller.signal_size_allocate().connect (sigc::mem_fun (*this, &FilmViewer::scroller_size_allocate));
65 FilmViewer::load_thumbnail (int n)
67 if (_film == 0 || _film->num_thumbs() <= n) {
71 int const left = _film->left_crop ();
72 int const right = _film->right_crop ();
73 int const top = _film->top_crop ();
74 int const bottom = _film->bottom_crop ();
76 _pixbuf = Gdk::Pixbuf::create_from_file (_film->thumb_file (n));
78 int const cw = _film->size().width - left - right;
79 int const ch = _film->size().height - top - bottom;
80 _cropped_pixbuf = Gdk::Pixbuf::create_subpixbuf (_pixbuf, left, top, cw, ch);
81 update_scaled_pixbuf ();
82 _image.set (_scaled_pixbuf);
86 FilmViewer::reload_current_thumbnail ()
88 load_thumbnail (_position_slider.get_value ());
92 FilmViewer::position_slider_changed ()
94 reload_current_thumbnail ();
98 FilmViewer::format_position_slider_value (double v) const
102 if (_film && int (v) < _film->num_thumbs ()) {
103 int const f = _film->thumb_frame (int (v));
104 s << f << " " << seconds_to_hms (f / _film->frames_per_second ());
113 FilmViewer::film_changed (Film::Property p)
115 if (p == Film::LEFT_CROP || p == Film::RIGHT_CROP || p == Film::TOP_CROP || p == Film::BOTTOM_CROP) {
116 reload_current_thumbnail ();
117 } else if (p == Film::THUMBS) {
118 if (_film && _film->num_thumbs() > 1) {
119 _position_slider.set_range (0, _film->num_thumbs () - 1);
122 _position_slider.set_range (0, 1);
125 _position_slider.set_value (0);
126 reload_current_thumbnail ();
127 } else if (p == Film::FORMAT) {
128 reload_current_thumbnail ();
129 } else if (p == Film::CONTENT) {
136 FilmViewer::set_film (Film* f)
140 _update_button.set_sensitive (_film != 0);
147 _film->Changed.connect (sigc::mem_fun (*this, &FilmViewer::film_changed));
149 film_changed (Film::THUMBS);
153 FilmViewer::scaled_pixbuf_size () const
156 return make_pair (0, 0);
159 int const cw = _film->size().width - _film->left_crop() - _film->right_crop();
160 int const ch = _film->size().height - _film->top_crop() - _film->bottom_crop();
163 if (_film->format()) {
164 ratio = _film->format()->ratio_as_float() * ch / cw;
167 Gtk::Allocation const a = _scroller.get_allocation ();
168 float const zoom = min (float (a.get_width()) / (cw * ratio), float (a.get_height()) / cw);
169 return make_pair (cw * zoom * ratio, ch * zoom);
173 FilmViewer::update_scaled_pixbuf ()
175 pair<int, int> const s = scaled_pixbuf_size ();
177 if (s.first > 0 && s.second > 0 && _cropped_pixbuf) {
178 _scaled_pixbuf = _cropped_pixbuf->scale_simple (s.first, s.second, Gdk::INTERP_HYPER);
179 _image.set (_scaled_pixbuf);
184 FilmViewer::update_thumbs ()
190 _film->update_thumbs_pre_gui ();
192 shared_ptr<const FilmState> s = _film->state_copy ();
193 shared_ptr<Options> o (new Options (s->dir ("thumbs"), ".tiff", ""));
194 o->out_size = _film->size ();
195 o->apply_crop = false;
196 o->decode_audio = false;
197 o->decode_video_frequency = 128;
199 shared_ptr<Job> j (new ThumbsJob (s, o, _film->log ()));
200 j->Finished.connect (sigc::mem_fun (_film, &Film::update_thumbs_post_gui));
201 JobManager::instance()->add (j);
205 FilmViewer::scroller_size_allocate (Gtk::Allocation a)
207 if (a.get_width() != _last_scroller_allocation.get_width() || a.get_height() != _last_scroller_allocation.get_height()) {
208 update_scaled_pixbuf ();
211 _last_scroller_allocation = a;
215 FilmViewer::setup_visibility ()
221 ContentType const c = _film->content_type ();
222 _update_button.property_visible() = (c == VIDEO);
223 _position_slider.property_visible() = (c == VIDEO);