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