Move things round a bit.
[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         , _update_button ("Update")
41 {
42         _scroller.add (_image);
43
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);
48         
49         _vbox.pack_start (_scroller, true, true);
50         _vbox.pack_start (*controls, false, false);
51         _vbox.set_border_width (12);
52
53         _update_button.signal_clicked().connect (sigc::mem_fun (*this, &FilmViewer::update_thumbs));
54
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));
58
59         _scroller.signal_size_allocate().connect (sigc::mem_fun (*this, &FilmViewer::scroller_size_allocate));
60
61         set_film (_film);
62 }
63
64 void
65 FilmViewer::load_thumbnail (int n)
66 {
67         if (_film == 0 || _film->num_thumbs() <= n) {
68                 return;
69         }
70
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 ();
75
76         _pixbuf = Gdk::Pixbuf::create_from_file (_film->thumb_file (n));
77
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);
83 }
84
85 void
86 FilmViewer::reload_current_thumbnail ()
87 {
88         load_thumbnail (_position_slider.get_value ());
89 }
90
91 void
92 FilmViewer::position_slider_changed ()
93 {
94         reload_current_thumbnail ();
95 }
96
97 string
98 FilmViewer::format_position_slider_value (double v) const
99 {
100         stringstream s;
101
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 ());
105         } else {
106                 s << "-";
107         }
108         
109         return s.str ();
110 }
111
112 void
113 FilmViewer::film_changed (Film::Property p)
114 {
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);
120                 } else {
121                         _image.clear ();
122                         _position_slider.set_range (0, 1);
123                 }
124                 
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) {
130                 setup_visibility ();
131                 update_thumbs ();
132         }
133 }
134
135 void
136 FilmViewer::set_film (Film* f)
137 {
138         _film = f;
139
140         _update_button.set_sensitive (_film != 0);
141         
142         if (!_film) {
143                 _image.clear ();
144                 return;
145         }
146
147         _film->Changed.connect (sigc::mem_fun (*this, &FilmViewer::film_changed));
148
149         film_changed (Film::THUMBS);
150 }
151
152 pair<int, int>
153 FilmViewer::scaled_pixbuf_size () const
154 {
155         if (_film == 0) {
156                 return make_pair (0, 0);
157         }
158         
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();
161
162         float ratio = 1;
163         if (_film->format()) {
164                 ratio = _film->format()->ratio_as_float() * ch / cw;
165         }
166
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);
170 }
171         
172 void
173 FilmViewer::update_scaled_pixbuf ()
174 {
175         pair<int, int> const s = scaled_pixbuf_size ();
176
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);
180         }
181 }
182
183 void
184 FilmViewer::update_thumbs ()
185 {
186         if (!_film) {
187                 return;
188         }
189
190         _film->update_thumbs_pre_gui ();
191
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;
198         
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);
202 }
203
204 void
205 FilmViewer::scroller_size_allocate (Gtk::Allocation a)
206 {
207         if (a.get_width() != _last_scroller_allocation.get_width() || a.get_height() != _last_scroller_allocation.get_height()) {
208                 update_scaled_pixbuf ();
209         }
210         
211         _last_scroller_allocation = a;
212 }
213
214 void
215 FilmViewer::setup_visibility ()
216 {
217         if (!_film) {
218                 return;
219         }
220
221         ContentType const c = _film->content_type ();
222         _update_button.property_visible() = (c == VIDEO);
223         _position_slider.property_visible() = (c == VIDEO);
224 }