Reasonably straightforward stuff; main things are adding
[dcpomatic.git] / src / wx / timeline.cc
1 /*
2     Copyright (C) 2013-2016 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 "film_editor.h"
21 #include "timeline.h"
22 #include "timeline_time_axis_view.h"
23 #include "timeline_reels_view.h"
24 #include "timeline_labels_view.h"
25 #include "timeline_video_content_view.h"
26 #include "timeline_audio_content_view.h"
27 #include "timeline_subtitle_content_view.h"
28 #include "content_panel.h"
29 #include "wx_util.h"
30 #include "lib/film.h"
31 #include "lib/playlist.h"
32 #include "lib/image_content.h"
33 #include "lib/timer.h"
34 #include "lib/audio_content.h"
35 #include "lib/subtitle_content.h"
36 #include <wx/graphics.h>
37 #include <boost/weak_ptr.hpp>
38 #include <boost/foreach.hpp>
39 #include <list>
40 #include <iostream>
41
42 using std::list;
43 using std::cout;
44 using std::max;
45 using boost::shared_ptr;
46 using boost::weak_ptr;
47 using boost::dynamic_pointer_cast;
48 using boost::bind;
49 using boost::optional;
50
51 Timeline::Timeline (wxWindow* parent, ContentPanel* cp, shared_ptr<Film> film)
52         : wxPanel (parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxFULL_REPAINT_ON_RESIZE)
53         , _content_panel (cp)
54         , _film (film)
55         , _time_axis_view (new TimelineTimeAxisView (*this, 64))
56         , _reels_view (new TimelineReelsView (*this, 32))
57         , _labels_view (new TimelineLabelsView (*this))
58         , _tracks (0)
59         , _left_down (false)
60         , _down_view_position (0)
61         , _first_move (false)
62         , _menu (this)
63         , _snap (true)
64 {
65 #ifndef __WXOSX__
66         SetDoubleBuffered (true);
67 #endif
68
69         Bind (wxEVT_PAINT,      boost::bind (&Timeline::paint,       this));
70         Bind (wxEVT_LEFT_DOWN,  boost::bind (&Timeline::left_down,   this, _1));
71         Bind (wxEVT_LEFT_UP,    boost::bind (&Timeline::left_up,     this, _1));
72         Bind (wxEVT_RIGHT_DOWN, boost::bind (&Timeline::right_down,  this, _1));
73         Bind (wxEVT_MOTION,     boost::bind (&Timeline::mouse_moved, this, _1));
74         Bind (wxEVT_SIZE,       boost::bind (&Timeline::resized,     this));
75
76         film_changed (Film::CONTENT);
77
78         SetMinSize (wxSize (640, tracks() * track_height() + 96));
79
80         _tracks_position = Position<int> (_labels_view->bbox().width, 32);
81
82         _film_changed_connection = film->Changed.connect (bind (&Timeline::film_changed, this, _1));
83         _film_content_changed_connection = film->ContentChanged.connect (bind (&Timeline::film_content_changed, this, _2, _3));
84 }
85
86 void
87 Timeline::paint ()
88 {
89         wxPaintDC dc (this);
90
91         wxGraphicsContext* gc = wxGraphicsContext::Create (dc);
92         if (!gc) {
93                 return;
94         }
95
96         gc->SetAntialiasMode (wxANTIALIAS_DEFAULT);
97
98         BOOST_FOREACH (shared_ptr<TimelineView> i, _views) {
99
100                 shared_ptr<TimelineContentView> ic = dynamic_pointer_cast<TimelineContentView> (i);
101
102                 /* Find areas of overlap */
103                 list<dcpomatic::Rect<int> > overlaps;
104                 BOOST_FOREACH (shared_ptr<TimelineView> j, _views) {
105                         shared_ptr<TimelineContentView> jc = dynamic_pointer_cast<TimelineContentView> (j);
106                         /* No overlap with non-content views, views no different tracks, audio views or non-active views */
107                         if (!ic || !jc || i == j || ic->track() != jc->track() || ic->track().get_value_or(2) >= 2 || !ic->active() || !jc->active()) {
108                                 continue;
109                         }
110
111                         optional<dcpomatic::Rect<int> > r = j->bbox().intersection (i->bbox());
112                         if (r) {
113                                 overlaps.push_back (r.get ());
114                         }
115                 }
116
117                 i->paint (gc, overlaps);
118         }
119
120         delete gc;
121 }
122
123 void
124 Timeline::film_changed (Film::Property p)
125 {
126         if (p == Film::CONTENT || p == Film::REEL_TYPE || p == Film::REEL_LENGTH) {
127                 ensure_ui_thread ();
128                 recreate_views ();
129         } else if (p == Film::CONTENT_ORDER) {
130                 Refresh ();
131         }
132 }
133
134 void
135 Timeline::recreate_views ()
136 {
137         shared_ptr<const Film> film = _film.lock ();
138         if (!film) {
139                 return;
140         }
141
142         _views.clear ();
143         _views.push_back (_time_axis_view);
144         _views.push_back (_reels_view);
145         _views.push_back (_labels_view);
146
147         BOOST_FOREACH (shared_ptr<Content> i, film->content ()) {
148                 if (i->video) {
149                         _views.push_back (shared_ptr<TimelineView> (new TimelineVideoContentView (*this, i)));
150                 }
151
152                 shared_ptr<AudioContent> ac = dynamic_pointer_cast<AudioContent> (i);
153                 if (ac && !ac->audio_mapping().mapped_output_channels().empty ()) {
154                         _views.push_back (shared_ptr<TimelineView> (new TimelineAudioContentView (*this, i)));
155                 }
156
157                 shared_ptr<SubtitleContent> sc = dynamic_pointer_cast<SubtitleContent> (i);
158                 if (sc && sc->has_subtitles ()) {
159                         _views.push_back (shared_ptr<TimelineView> (new TimelineSubtitleContentView (*this, sc)));
160                 }
161         }
162
163         assign_tracks ();
164         setup_pixels_per_second ();
165         Refresh ();
166 }
167
168 void
169 Timeline::film_content_changed (int property, bool frequent)
170 {
171         ensure_ui_thread ();
172
173         if (property == AudioContentProperty::AUDIO_STREAMS) {
174                 recreate_views ();
175         } else if (!frequent) {
176                 setup_pixels_per_second ();
177                 Refresh ();
178         }
179 }
180
181 void
182 Timeline::assign_tracks ()
183 {
184         for (TimelineViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
185                 shared_ptr<TimelineContentView> c = dynamic_pointer_cast<TimelineContentView> (*i);
186                 if (c) {
187                         c->unset_track ();
188                 }
189         }
190
191         for (TimelineViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
192                 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (*i);
193                 if (!cv) {
194                         continue;
195                 }
196
197                 if (dynamic_pointer_cast<TimelineVideoContentView> (*i)) {
198                         /* Video on tracks 0 and 1 (left and right eye) */
199                         shared_ptr<VideoContent> vc = dynamic_pointer_cast<VideoContent> (cv->content ());
200                         cv->set_track (vc->video_frame_type() == VIDEO_FRAME_TYPE_3D_RIGHT ? 1 : 0);
201                         _tracks = max (_tracks, 2);
202                         continue;
203                 } else if (dynamic_pointer_cast<TimelineSubtitleContentView> (*i)) {
204                         /* Subtitles on track 2 */
205                         cv->set_track (2);
206                         _tracks = max (_tracks, 3);
207                         continue;
208                 }
209
210                 /* Audio on tracks 3 and up */
211                 int t = 3;
212
213                 shared_ptr<Content> content = cv->content();
214                 DCPTimePeriod content_period (content->position(), content->end());
215
216                 while (true) {
217                         TimelineViewList::iterator j = _views.begin();
218                         while (j != _views.end()) {
219                                 shared_ptr<TimelineAudioContentView> test = dynamic_pointer_cast<TimelineAudioContentView> (*j);
220                                 if (!test) {
221                                         ++j;
222                                         continue;
223                                 }
224
225                                 shared_ptr<Content> test_content = test->content();
226
227                                 if (test && test->track() && test->track().get() == t) {
228                                         if (content_period.overlaps (DCPTimePeriod(test_content->position(), test_content->end()))) {
229                                                 /* we have an overlap on track `t' */
230                                                 ++t;
231                                                 break;
232                                         }
233                                 }
234
235                                 ++j;
236                         }
237
238                         if (j == _views.end ()) {
239                                 /* no overlap on `t' */
240                                 break;
241                         }
242                 }
243
244                 cv->set_track (t);
245                 _tracks = max (_tracks, t + 1);
246         }
247
248         _time_axis_view->set_y (tracks() * track_height() + 64);
249         _reels_view->set_y (8);
250 }
251
252 int
253 Timeline::tracks () const
254 {
255         return _tracks;
256 }
257
258 void
259 Timeline::setup_pixels_per_second ()
260 {
261         shared_ptr<const Film> film = _film.lock ();
262         if (!film || film->length() == DCPTime ()) {
263                 return;
264         }
265
266         _pixels_per_second = static_cast<double>(width() - tracks_position().x * 2) / film->length().seconds ();
267 }
268
269 shared_ptr<TimelineView>
270 Timeline::event_to_view (wxMouseEvent& ev)
271 {
272         /* Search backwards through views so that we find the uppermost one first */
273         TimelineViewList::reverse_iterator i = _views.rbegin();
274         Position<int> const p (ev.GetX(), ev.GetY());
275         while (i != _views.rend() && !(*i)->bbox().contains (p)) {
276                 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (*i);
277                 ++i;
278         }
279
280         if (i == _views.rend ()) {
281                 return shared_ptr<TimelineView> ();
282         }
283
284         return *i;
285 }
286
287 void
288 Timeline::left_down (wxMouseEvent& ev)
289 {
290         shared_ptr<TimelineView> view = event_to_view (ev);
291         shared_ptr<TimelineContentView> content_view = dynamic_pointer_cast<TimelineContentView> (view);
292
293         _down_view.reset ();
294
295         if (content_view) {
296                 _down_view = content_view;
297                 _down_view_position = content_view->content()->position ();
298         }
299
300         for (TimelineViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
301                 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (*i);
302                 if (!cv) {
303                         continue;
304                 }
305
306                 if (!ev.ShiftDown ()) {
307                         cv->set_selected (view == *i);
308                 }
309         }
310
311         if (content_view && ev.ShiftDown ()) {
312                 content_view->set_selected (!content_view->selected ());
313         }
314
315         _left_down = true;
316         _down_point = ev.GetPosition ();
317         _first_move = false;
318
319         if (_down_view) {
320                 /* Pre-compute the points that we might snap to */
321                 for (TimelineViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
322                         shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (*i);
323                         if (!cv || cv == _down_view || cv->content() == _down_view->content()) {
324                                 continue;
325                         }
326
327                         _start_snaps.push_back (cv->content()->position());
328                         _end_snaps.push_back (cv->content()->position());
329                         _start_snaps.push_back (cv->content()->end());
330                         _end_snaps.push_back (cv->content()->end());
331
332                         BOOST_FOREACH (DCPTime i, cv->content()->reel_split_points()) {
333                                 _start_snaps.push_back (i);
334                         }
335                 }
336
337                 /* Tell everyone that things might change frequently during the drag */
338                 _down_view->content()->set_change_signals_frequent (true);
339         }
340 }
341
342 void
343 Timeline::left_up (wxMouseEvent& ev)
344 {
345         _left_down = false;
346
347         if (_down_view) {
348                 _down_view->content()->set_change_signals_frequent (false);
349                 _content_panel->set_selection (_down_view->content ());
350         }
351
352         set_position_from_event (ev);
353
354         /* Clear up up the stuff we don't do during drag */
355         assign_tracks ();
356         setup_pixels_per_second ();
357         Refresh ();
358
359         _start_snaps.clear ();
360         _end_snaps.clear ();
361 }
362
363 void
364 Timeline::mouse_moved (wxMouseEvent& ev)
365 {
366         if (!_left_down) {
367                 return;
368         }
369
370         set_position_from_event (ev);
371 }
372
373 void
374 Timeline::right_down (wxMouseEvent& ev)
375 {
376         shared_ptr<TimelineView> view = event_to_view (ev);
377         shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (view);
378         if (!cv) {
379                 return;
380         }
381
382         if (!cv->selected ()) {
383                 clear_selection ();
384                 cv->set_selected (true);
385         }
386
387         _menu.popup (_film, selected_content (), selected_views (), ev.GetPosition ());
388 }
389
390 void
391 Timeline::maybe_snap (DCPTime a, DCPTime b, optional<DCPTime>& nearest_distance) const
392 {
393         DCPTime const d = a - b;
394         if (!nearest_distance || d.abs() < nearest_distance.get().abs()) {
395                 nearest_distance = d;
396         }
397 }
398
399 void
400 Timeline::set_position_from_event (wxMouseEvent& ev)
401 {
402         if (!_pixels_per_second) {
403                 return;
404         }
405
406         double const pps = _pixels_per_second.get ();
407
408         wxPoint const p = ev.GetPosition();
409
410         if (!_first_move) {
411                 /* We haven't moved yet; in that case, we must move the mouse some reasonable distance
412                    before the drag is considered to have started.
413                 */
414                 int const dist = sqrt (pow (p.x - _down_point.x, 2) + pow (p.y - _down_point.y, 2));
415                 if (dist < 8) {
416                         return;
417                 }
418                 _first_move = true;
419         }
420
421         if (!_down_view) {
422                 return;
423         }
424
425         DCPTime new_position = _down_view_position + DCPTime::from_seconds ((p.x - _down_point.x) / pps);
426
427         if (_snap) {
428
429                 DCPTime const new_end = new_position + _down_view->content()->length_after_trim();
430                 /* Signed `distance' to nearest thing (i.e. negative is left on the timeline,
431                    positive is right).
432                 */
433                 optional<DCPTime> nearest_distance;
434
435                 /* Find the nearest snap point */
436
437                 BOOST_FOREACH (DCPTime i, _start_snaps) {
438                         maybe_snap (i, new_position, nearest_distance);
439                 }
440
441                 BOOST_FOREACH (DCPTime i, _end_snaps) {
442                         maybe_snap (i, new_end, nearest_distance);
443                 }
444
445                 if (nearest_distance) {
446                         /* Snap if it's close; `close' means within a proportion of the time on the timeline */
447                         if (nearest_distance.get().abs() < DCPTime::from_seconds ((width() / pps) / 64)) {
448                                 new_position += nearest_distance.get ();
449                         }
450                 }
451         }
452
453         if (new_position < DCPTime ()) {
454                 new_position = DCPTime ();
455         }
456
457         _down_view->content()->set_position (new_position);
458
459         shared_ptr<Film> film = _film.lock ();
460         DCPOMATIC_ASSERT (film);
461         film->set_sequence (false);
462 }
463
464 void
465 Timeline::force_redraw (dcpomatic::Rect<int> const & r)
466 {
467         RefreshRect (wxRect (r.x, r.y, r.width, r.height), false);
468 }
469
470 shared_ptr<const Film>
471 Timeline::film () const
472 {
473         return _film.lock ();
474 }
475
476 void
477 Timeline::resized ()
478 {
479         setup_pixels_per_second ();
480 }
481
482 void
483 Timeline::clear_selection ()
484 {
485         for (TimelineViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
486                 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (*i);
487                 if (cv) {
488                         cv->set_selected (false);
489                 }
490         }
491 }
492
493 TimelineContentViewList
494 Timeline::selected_views () const
495 {
496         TimelineContentViewList sel;
497
498         for (TimelineViewList::const_iterator i = _views.begin(); i != _views.end(); ++i) {
499                 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (*i);
500                 if (cv && cv->selected()) {
501                         sel.push_back (cv);
502                 }
503         }
504
505         return sel;
506 }
507
508 ContentList
509 Timeline::selected_content () const
510 {
511         ContentList sel;
512         TimelineContentViewList views = selected_views ();
513
514         for (TimelineContentViewList::const_iterator i = views.begin(); i != views.end(); ++i) {
515                 sel.push_back ((*i)->content ());
516         }
517
518         return sel;
519 }
520
521 void
522 Timeline::set_selection (ContentList selection)
523 {
524         for (TimelineViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
525                 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (*i);
526                 if (cv) {
527                         cv->set_selected (find (selection.begin(), selection.end(), cv->content ()) != selection.end ());
528                 }
529         }
530 }