1d50c2f8898c702dde89100298b9d3676abe96a0
[dcpomatic.git] / src / gtk / film_viewer.cc
1 /*
2     Copyright (C) 2012 Carl Hetherington <cth@carlh.net>
3
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.
8
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.
13
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.
17
18 */
19
20 /** @file  src/film_viewer.cc
21  *  @brief A GTK widget to view `thumbnails' of a Film.
22  */
23
24 #include <iostream>
25 #include <iomanip>
26 #include "lib/film.h"
27 #include "lib/format.h"
28 #include "lib/util.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"
34
35 using namespace std;
36 using namespace boost;
37
38 FilmViewer::FilmViewer (Film* f)
39         : _film (f)
40 {
41         _scroller.add (_image);
42
43         Gtk::HBox* controls = manage (new Gtk::HBox);
44         controls->set_spacing (6);
45         controls->pack_start (_position_slider);
46         
47         _vbox.pack_start (_scroller, true, true);
48         _vbox.pack_start (*controls, false, false);
49         _vbox.set_border_width (12);
50
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));
54
55         _scroller.signal_size_allocate().connect (sigc::mem_fun (*this, &FilmViewer::scroller_size_allocate));
56
57         set_film (_film);
58 }
59
60 void
61 FilmViewer::load_thumbnail (int n)
62 {
63         if (_film == 0 || _film->num_thumbs() <= n) {
64                 return;
65         }
66
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 ();
71
72         _pixbuf = Gdk::Pixbuf::create_from_file (_film->thumb_file (n));
73
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);
79 }
80
81 void
82 FilmViewer::reload_current_thumbnail ()
83 {
84         load_thumbnail (_position_slider.get_value ());
85 }
86
87 void
88 FilmViewer::position_slider_changed ()
89 {
90         reload_current_thumbnail ();
91 }
92
93 string
94 FilmViewer::format_position_slider_value (double v) const
95 {
96         stringstream s;
97
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 ());
101         } else {
102                 s << "-";
103         }
104         
105         return s.str ();
106 }
107
108 void
109 FilmViewer::film_changed (Film::Property p)
110 {
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);
116                 } else {
117                         _image.clear ();
118                         _position_slider.set_range (0, 1);
119                 }
120                 
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) {
126                 setup_visibility ();
127                 _film->examine_content ();
128                 update_thumbs ();
129         }
130 }
131
132 void
133 FilmViewer::set_film (Film* f)
134 {
135         _film = f;
136
137         if (!_film) {
138                 _image.clear ();
139                 return;
140         }
141
142         _film->Changed.connect (sigc::mem_fun (*this, &FilmViewer::film_changed));
143
144         film_changed (Film::THUMBS);
145 }
146
147 pair<int, int>
148 FilmViewer::scaled_pixbuf_size () const
149 {
150         if (_film == 0) {
151                 return make_pair (0, 0);
152         }
153         
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();
156
157         float ratio = 1;
158         if (_film->format()) {
159                 ratio = _film->format()->ratio_as_float() * ch / cw;
160         }
161
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);
165 }
166         
167 void
168 FilmViewer::update_scaled_pixbuf ()
169 {
170         pair<int, int> const s = scaled_pixbuf_size ();
171
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);
175         }
176 }
177
178 void
179 FilmViewer::update_thumbs ()
180 {
181         if (!_film) {
182                 return;
183         }
184
185         _film->update_thumbs_pre_gui ();
186
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;
193         
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);
197 }
198
199 void
200 FilmViewer::scroller_size_allocate (Gtk::Allocation a)
201 {
202         if (a.get_width() != _last_scroller_allocation.get_width() || a.get_height() != _last_scroller_allocation.get_height()) {
203                 update_scaled_pixbuf ();
204         }
205         
206         _last_scroller_allocation = a;
207 }
208
209 void
210 FilmViewer::setup_visibility ()
211 {
212         if (!_film) {
213                 return;
214         }
215
216         ContentType const c = _film->content_type ();
217         _position_slider.property_visible() = (c == VIDEO);
218 }