Express trims using ContentTime so that they do not change
[dcpomatic.git] / src / wx / timing_panel.cc
1 /*
2     Copyright (C) 2012-2015 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 #include "lib/content.h"
21 #include "lib/image_content.h"
22 #include "lib/raw_convert.h"
23 #include "timing_panel.h"
24 #include "wx_util.h"
25 #include "film_viewer.h"
26 #include "timecode.h"
27 #include "content_panel.h"
28 #include <boost/foreach.hpp>
29 #include <set>
30
31 using std::cout;
32 using std::string;
33 using std::set;
34 using boost::shared_ptr;
35 using boost::dynamic_pointer_cast;
36
37 TimingPanel::TimingPanel (ContentPanel* p, FilmViewer* viewer)
38         /* horrid hack for apparent lack of context support with wxWidgets i18n code */
39         : ContentSubPanel (p, S_("Timing|Timing"))
40         , _viewer (viewer)
41 {
42         wxFlexGridSizer* grid = new wxFlexGridSizer (2, 4, 4);
43         _sizer->Add (grid, 0, wxALL, 8);
44
45         wxSize size = TimecodeBase::size (this);
46
47         wxSizer* labels = new wxBoxSizer (wxHORIZONTAL);
48         //// TRANSLATORS: this is an abbreviation for "hours"
49         wxStaticText* t = new wxStaticText (this, wxID_ANY, _("h"), wxDefaultPosition, size, wxALIGN_CENTRE_HORIZONTAL);
50 #ifdef DCPOMATIC_LINUX
51         /* Hack to work around failure to centre text on GTK */
52         gtk_label_set_line_wrap (GTK_LABEL (t->GetHandle()), FALSE);
53 #endif
54         labels->Add (t, 1, wxEXPAND);
55         add_label_to_sizer (labels, this, wxT (":"), false);
56         //// TRANSLATORS: this is an abbreviation for "minutes"
57         t = new wxStaticText (this, wxID_ANY, _("m"), wxDefaultPosition, size, wxALIGN_CENTRE_HORIZONTAL);
58 #ifdef DCPOMATIC_LINUX
59         gtk_label_set_line_wrap (GTK_LABEL (t->GetHandle()), FALSE);
60 #endif
61         labels->Add (t, 1, wxEXPAND);
62         add_label_to_sizer (labels, this, wxT (":"), false);
63         //// TRANSLATORS: this is an abbreviation for "seconds"
64         t = new wxStaticText (this, wxID_ANY, _("s"), wxDefaultPosition, size, wxALIGN_CENTRE_HORIZONTAL);
65 #ifdef DCPOMATIC_LINUX
66         gtk_label_set_line_wrap (GTK_LABEL (t->GetHandle()), FALSE);
67 #endif
68         labels->Add (t, 1, wxEXPAND);
69         add_label_to_sizer (labels, this, wxT (":"), false);
70         //// TRANSLATORS: this is an abbreviation for "frames"
71         t = new wxStaticText (this, wxID_ANY, _("f"), wxDefaultPosition, size, wxALIGN_CENTRE_HORIZONTAL);
72 #ifdef DCPOMATIC_LINUX
73         gtk_label_set_line_wrap (GTK_LABEL (t->GetHandle()), FALSE);
74 #endif
75         labels->Add (t, 1, wxEXPAND);
76         grid->Add (new wxStaticText (this, wxID_ANY, wxT ("")));
77         grid->Add (labels);
78
79         add_label_to_sizer (grid, this, _("Position"), true);
80         _position = new Timecode<DCPTime> (this);
81         grid->Add (_position);
82         add_label_to_sizer (grid, this, _("Full length"), true);
83         _full_length = new Timecode<DCPTime> (this);
84         grid->Add (_full_length);
85         add_label_to_sizer (grid, this, _("Trim from start"), true);
86         _trim_start = new Timecode<ContentTime> (this);
87         grid->Add (_trim_start);
88         _trim_start_to_playhead = new wxButton (this, wxID_ANY, _("Trim up to current position"));
89         grid->AddSpacer (0);
90         grid->Add (_trim_start_to_playhead);
91         add_label_to_sizer (grid, this, _("Trim from end"), true);
92         _trim_end = new Timecode<ContentTime> (this);
93         grid->Add (_trim_end);
94         _trim_end_to_playhead = new wxButton (this, wxID_ANY, _("Trim after current position"));
95         grid->AddSpacer (0);
96         grid->Add (_trim_end_to_playhead);
97         add_label_to_sizer (grid, this, _("Play length"), true);
98         _play_length = new Timecode<DCPTime> (this);
99         grid->Add (_play_length);
100
101         {
102                 add_label_to_sizer (grid, this, _("Video frame rate"), true);
103                 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
104                 _video_frame_rate = new wxTextCtrl (this, wxID_ANY);
105                 s->Add (_video_frame_rate, 1, wxEXPAND);
106                 _set_video_frame_rate = new wxButton (this, wxID_ANY, _("Set"));
107                 _set_video_frame_rate->Enable (false);
108                 s->Add (_set_video_frame_rate, 0, wxLEFT | wxRIGHT, 8);
109                 grid->Add (s, 1, wxEXPAND);
110         }
111
112         grid->AddSpacer (0);
113
114         /* We can't use Wrap() here as it doesn't work with markup:
115          * http://trac.wxwidgets.org/ticket/13389
116          */
117
118         wxString in = _("<i>Only change this if it the content's frame rate has been read incorrectly.</i>");
119         wxString out;
120         int const width = 20;
121         int current = 0;
122         for (size_t i = 0; i < in.Length(); ++i) {
123                 if (in[i] == ' ' && current >= width) {
124                         out += '\n';
125                         current = 0;
126                 } else {
127                         out += in[i];
128                         ++current;
129                 }
130         }
131
132         t = new wxStaticText (this, wxID_ANY, wxT (""));
133         t->SetLabelMarkup (out);
134         grid->Add (t, 0, wxALIGN_CENTER_VERTICAL | wxLEFT | wxRIGHT, 6);
135
136         _position->Changed.connect    (boost::bind (&TimingPanel::position_changed, this));
137         _full_length->Changed.connect (boost::bind (&TimingPanel::full_length_changed, this));
138         _trim_start->Changed.connect  (boost::bind (&TimingPanel::trim_start_changed, this));
139         _trim_start_to_playhead->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&TimingPanel::trim_start_to_playhead_clicked, this));
140         _trim_end->Changed.connect    (boost::bind (&TimingPanel::trim_end_changed, this));
141         _trim_end_to_playhead->Bind   (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&TimingPanel::trim_end_to_playhead_clicked, this));
142         _play_length->Changed.connect (boost::bind (&TimingPanel::play_length_changed, this));
143         _video_frame_rate->Bind       (wxEVT_COMMAND_TEXT_UPDATED, boost::bind (&TimingPanel::video_frame_rate_changed, this));
144         _set_video_frame_rate->Bind   (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&TimingPanel::set_video_frame_rate, this));
145 }
146
147 void
148 TimingPanel::update_full_length ()
149 {
150         ContentList cl = _parent->selected ();
151
152         set<DCPTime> check;
153         for (ContentList::const_iterator i = cl.begin (); i != cl.end(); ++i) {
154                 check.insert ((*i)->full_length ());
155         }
156
157         if (check.size() == 1) {
158                 _full_length->set (cl.front()->full_length (), _parent->film()->video_frame_rate ());
159         } else {
160                 _full_length->clear ();
161         }
162 }
163
164 void
165 TimingPanel::update_play_length ()
166 {
167         ContentList cl = _parent->selected ();
168
169         set<DCPTime> check;
170         for (ContentList::const_iterator i = cl.begin (); i != cl.end(); ++i) {
171                 check.insert ((*i)->length_after_trim ());
172         }
173
174         if (check.size() == 1) {
175                 _play_length->set (cl.front()->length_after_trim (), _parent->film()->video_frame_rate ());
176         } else {
177                 _play_length->clear ();
178         }
179 }
180
181 void
182 TimingPanel::film_content_changed (int property)
183 {
184         ContentList cl = _parent->selected ();
185         int const film_video_frame_rate = _parent->film()->video_frame_rate ();
186
187         /* Here we check to see if we have exactly one different value of various
188            properties, and fill the controls with that value if so.
189         */
190
191         if (property == ContentProperty::POSITION) {
192
193                 set<DCPTime> check;
194                 for (ContentList::const_iterator i = cl.begin (); i != cl.end(); ++i) {
195                         check.insert ((*i)->position ());
196                 }
197
198                 if (check.size() == 1) {
199                         _position->set (cl.front()->position(), film_video_frame_rate);
200                 } else {
201                         _position->clear ();
202                 }
203
204         } else if (
205                 property == ContentProperty::LENGTH ||
206                 property == VideoContentProperty::VIDEO_FRAME_RATE ||
207                 property == VideoContentProperty::VIDEO_FRAME_TYPE
208                 ) {
209
210                 update_full_length ();
211
212         } else if (property == ContentProperty::TRIM_START) {
213
214                 set<ContentTime> check;
215                 for (ContentList::const_iterator i = cl.begin (); i != cl.end(); ++i) {
216                         check.insert ((*i)->trim_start ());
217                 }
218
219                 if (check.size() == 1) {
220                         _trim_start->set (cl.front()->trim_start (), film_video_frame_rate);
221                 } else {
222                         _trim_start->clear ();
223                 }
224
225         } else if (property == ContentProperty::TRIM_END) {
226
227                 set<ContentTime> check;
228                 for (ContentList::const_iterator i = cl.begin (); i != cl.end(); ++i) {
229                         check.insert ((*i)->trim_end ());
230                 }
231
232                 if (check.size() == 1) {
233                         _trim_end->set (cl.front()->trim_end (), film_video_frame_rate);
234                 } else {
235                         _trim_end->clear ();
236                 }
237         }
238
239         if (
240                 property == ContentProperty::LENGTH ||
241                 property == ContentProperty::TRIM_START ||
242                 property == ContentProperty::TRIM_END ||
243                 property == VideoContentProperty::VIDEO_FRAME_RATE ||
244                 property == VideoContentProperty::VIDEO_FRAME_TYPE
245                 ) {
246
247                 update_play_length ();
248         }
249
250         if (property == VideoContentProperty::VIDEO_FRAME_RATE) {
251                 set<float> check;
252                 shared_ptr<VideoContent> vc;
253                 for (ContentList::const_iterator i = cl.begin (); i != cl.end(); ++i) {
254                         shared_ptr<VideoContent> t = dynamic_pointer_cast<VideoContent> (*i);
255                         if (t) {
256                                 check.insert (t->video_frame_rate ());
257                                 vc = t;
258                         }
259                 }
260                 if (check.size() == 1) {
261                         checked_set (_video_frame_rate, raw_convert<string> (vc->video_frame_rate (), 5));
262                         _video_frame_rate->Enable (true);
263                 } else {
264                         checked_set (_video_frame_rate, wxT (""));
265                         _video_frame_rate->Enable (false);
266                 }
267         }
268
269         bool have_still = false;
270         for (ContentList::const_iterator i = cl.begin (); i != cl.end(); ++i) {
271                 shared_ptr<ImageContent> ic = dynamic_pointer_cast<ImageContent> (*i);
272                 if (ic && ic->still ()) {
273                         have_still = true;
274                 }
275         }
276
277         _full_length->set_editable (have_still);
278         _play_length->set_editable (!have_still);
279         _set_video_frame_rate->Enable (false);
280 }
281
282 void
283 TimingPanel::position_changed ()
284 {
285         ContentList c = _parent->selected ();
286         for (ContentList::iterator i = c.begin(); i != c.end(); ++i) {
287                 (*i)->set_position (_position->get (_parent->film()->video_frame_rate ()));
288         }
289 }
290
291 void
292 TimingPanel::full_length_changed ()
293 {
294         ContentList c = _parent->selected ();
295         for (ContentList::iterator i = c.begin(); i != c.end(); ++i) {
296                 shared_ptr<ImageContent> ic = dynamic_pointer_cast<ImageContent> (*i);
297                 if (ic && ic->still ()) {
298                         int const vfr = _parent->film()->video_frame_rate ();
299                         ic->set_video_length (_full_length->get (vfr).frames (vfr));
300                 }
301         }
302 }
303
304 void
305 TimingPanel::trim_start_changed ()
306 {
307         ContentList c = _parent->selected ();
308         for (ContentList::iterator i = c.begin(); i != c.end(); ++i) {
309                 (*i)->set_trim_start (_trim_start->get (_parent->film()->video_frame_rate ()));
310         }
311 }
312
313
314 void
315 TimingPanel::trim_end_changed ()
316 {
317         ContentList c = _parent->selected ();
318         for (ContentList::iterator i = c.begin(); i != c.end(); ++i) {
319                 (*i)->set_trim_end (_trim_end->get (_parent->film()->video_frame_rate ()));
320         }
321 }
322
323 void
324 TimingPanel::play_length_changed ()
325 {
326         ContentList c = _parent->selected ();
327         for (ContentList::iterator i = c.begin(); i != c.end(); ++i) {
328                 FrameRateChange const frc = _parent->film()->active_frame_rate_change ((*i)->position ());
329                 (*i)->set_trim_end (
330                         ContentTime ((*i)->full_length() - _play_length->get (_parent->film()->video_frame_rate()), frc)
331                         - (*i)->trim_start ()
332                         );
333         }
334 }
335
336 void
337 TimingPanel::video_frame_rate_changed ()
338 {
339         _set_video_frame_rate->Enable (true);
340 }
341
342 void
343 TimingPanel::set_video_frame_rate ()
344 {
345         ContentList c = _parent->selected ();
346         for (ContentList::iterator i = c.begin(); i != c.end(); ++i) {
347                 shared_ptr<VideoContent> vc = dynamic_pointer_cast<VideoContent> (*i);
348                 if (vc) {
349                         vc->set_video_frame_rate (raw_convert<float> (wx_to_std (_video_frame_rate->GetValue ())));
350                 }
351                 _set_video_frame_rate->Enable (false);
352         }
353 }
354
355 void
356 TimingPanel::content_selection_changed ()
357 {
358         bool const e = !_parent->selected().empty ();
359
360         _position->Enable (e);
361         _full_length->Enable (e);
362         _trim_start->Enable (e);
363         _trim_end->Enable (e);
364         _play_length->Enable (e);
365         _video_frame_rate->Enable (e);
366
367         film_content_changed (ContentProperty::POSITION);
368         film_content_changed (ContentProperty::LENGTH);
369         film_content_changed (ContentProperty::TRIM_START);
370         film_content_changed (ContentProperty::TRIM_END);
371         film_content_changed (VideoContentProperty::VIDEO_FRAME_RATE);
372 }
373
374 void
375 TimingPanel::film_changed (Film::Property p)
376 {
377         if (p == Film::VIDEO_FRAME_RATE) {
378                 update_full_length ();
379                 update_play_length ();
380         }
381 }
382
383 void
384 TimingPanel::trim_start_to_playhead_clicked ()
385 {
386         DCPTime const ph = _viewer->position ();
387         BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
388                 if (i->position() < ph && ph < i->end ()) {
389                         FrameRateChange const frc = _parent->film()->active_frame_rate_change (i->position ());
390                         i->set_trim_start (i->trim_start() + ContentTime (ph - i->position (), frc));
391                 }
392         }
393 }
394
395 void
396 TimingPanel::trim_end_to_playhead_clicked ()
397 {
398         DCPTime const ph = _viewer->position ();
399         BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
400                 if (i->position() < ph && ph < i->end ()) {
401                         FrameRateChange const frc = _parent->film()->active_frame_rate_change (i->position ());
402                         i->set_trim_end (ContentTime (i->position() + i->full_length() - ph, frc) - i->trim_start());
403                 }
404
405         }
406 }