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