Fix drawing.
[dcpomatic.git] / src / wx / timeline.cc
1 /*
2     Copyright (C) 2013-2018 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::min;
49 using std::max;
50 using boost::shared_ptr;
51 using boost::weak_ptr;
52 using boost::dynamic_pointer_cast;
53 using boost::bind;
54 using boost::optional;
55
56 Timeline::Timeline (wxWindow* parent, ContentPanel* cp, shared_ptr<Film> film)
57         : wxPanel (parent, wxID_ANY)
58         , _labels_panel (new wxPanel (this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxFULL_REPAINT_ON_RESIZE))
59         , _main_canvas (new wxScrolledCanvas (this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxFULL_REPAINT_ON_RESIZE))
60         , _content_panel (cp)
61         , _film (film)
62         , _time_axis_view (new TimelineTimeAxisView (*this, 64))
63         , _reels_view (new TimelineReelsView (*this, 32))
64         , _labels_view (new TimelineLabelsView (*this))
65         , _tracks (0)
66         , _left_down (false)
67         , _down_view_position (0)
68         , _first_move (false)
69         , _menu (this)
70         , _snap (true)
71         , _tool (SELECT)
72         , _x_scroll_rate (16)
73         , _y_scroll_rate (16)
74 {
75 #ifndef __WXOSX__
76         _labels_panel->SetDoubleBuffered (true);
77         _main_canvas->SetDoubleBuffered (true);
78 #endif
79
80         wxSizer* sizer = new wxBoxSizer (wxHORIZONTAL);
81         sizer->Add (_labels_panel, 0, wxEXPAND);
82         _labels_panel->SetMinSize (wxSize (_labels_view->bbox().width, -1));
83         sizer->Add (_main_canvas, 1, wxEXPAND);
84         SetSizer (sizer);
85
86         _labels_panel->Bind (wxEVT_PAINT,      boost::bind (&Timeline::paint_labels, this));
87         _main_canvas->Bind  (wxEVT_PAINT,      boost::bind (&Timeline::paint_main,   this));
88         _main_canvas->Bind  (wxEVT_LEFT_DOWN,  boost::bind (&Timeline::left_down,    this, _1));
89         _main_canvas->Bind  (wxEVT_LEFT_UP,    boost::bind (&Timeline::left_up,      this, _1));
90         _main_canvas->Bind  (wxEVT_RIGHT_DOWN, boost::bind (&Timeline::right_down,   this, _1));
91         _main_canvas->Bind  (wxEVT_MOTION,     boost::bind (&Timeline::mouse_moved,  this, _1));
92         _main_canvas->Bind  (wxEVT_SIZE,       boost::bind (&Timeline::resized,      this));
93
94         film_changed (Film::CONTENT);
95
96         SetMinSize (wxSize (640, 4 * track_height() + 96));
97
98         _film_changed_connection = film->Changed.connect (bind (&Timeline::film_changed, this, _1));
99         _film_content_changed_connection = film->ContentChanged.connect (bind (&Timeline::film_content_changed, this, _2, _3));
100
101         _pixels_per_second = max (0.01, static_cast<double>(640) / film->length().seconds ());
102
103         setup_scrollbars ();
104         _main_canvas->EnableScrolling (true, true);
105 }
106
107 void
108 Timeline::paint_labels ()
109 {
110         wxPaintDC dc (_labels_panel);
111
112         wxGraphicsContext* gc = wxGraphicsContext::Create (dc);
113         if (!gc) {
114                 return;
115         }
116
117         _labels_view->paint (gc, list<dcpomatic::Rect<int> >());
118
119         delete gc;
120 }
121
122 void
123 Timeline::paint_main ()
124 {
125         wxPaintDC dc (_main_canvas);
126         _main_canvas->DoPrepareDC (dc);
127
128         wxGraphicsContext* gc = wxGraphicsContext::Create (dc);
129         if (!gc) {
130                 return;
131         }
132
133         int vsx, vsy;
134         _main_canvas->GetViewStart (&vsx, &vsy);
135         gc->Translate (-vsx * _x_scroll_rate, -vsy * _y_scroll_rate);
136
137         gc->SetAntialiasMode (wxANTIALIAS_DEFAULT);
138
139         BOOST_FOREACH (shared_ptr<TimelineView> i, _views) {
140
141                 shared_ptr<TimelineContentView> ic = dynamic_pointer_cast<TimelineContentView> (i);
142
143                 /* Find areas of overlap with other content views, so that we can plot them */
144                 list<dcpomatic::Rect<int> > overlaps;
145                 BOOST_FOREACH (shared_ptr<TimelineView> j, _views) {
146                         shared_ptr<TimelineContentView> jc = dynamic_pointer_cast<TimelineContentView> (j);
147                         /* No overlap with non-content views, views no different tracks, audio views or non-active views */
148                         if (!ic || !jc || i == j || ic->track() != jc->track() || ic->track().get_value_or(2) >= 2 || !ic->active() || !jc->active()) {
149                                 continue;
150                         }
151
152                         optional<dcpomatic::Rect<int> > r = j->bbox().intersection (i->bbox());
153                         if (r) {
154                                 overlaps.push_back (r.get ());
155                         }
156                 }
157
158                 i->paint (gc, overlaps);
159         }
160
161         if (_zoom_point) {
162                 /* Translate back as _down_point and _zoom_point do not take scroll into account */
163                 gc->Translate (vsx * _x_scroll_rate, vsy * _y_scroll_rate);
164                 gc->SetPen (*wxBLACK_PEN);
165                 gc->SetBrush (*wxTRANSPARENT_BRUSH);
166                 gc->DrawRectangle (
167                         min (_down_point.x, _zoom_point->x),
168                         min (_down_point.y, _zoom_point->y),
169                         fabs (_down_point.x - _zoom_point->x),
170                         fabs (_down_point.y - _zoom_point->y)
171                         );
172         }
173
174         delete gc;
175 }
176
177 void
178 Timeline::film_changed (Film::Property p)
179 {
180         if (p == Film::CONTENT || p == Film::REEL_TYPE || p == Film::REEL_LENGTH) {
181                 ensure_ui_thread ();
182                 recreate_views ();
183         } else if (p == Film::CONTENT_ORDER) {
184                 Refresh ();
185         }
186 }
187
188 void
189 Timeline::recreate_views ()
190 {
191         shared_ptr<const Film> film = _film.lock ();
192         if (!film) {
193                 return;
194         }
195
196         _views.clear ();
197         _views.push_back (_time_axis_view);
198         _views.push_back (_reels_view);
199
200         BOOST_FOREACH (shared_ptr<Content> i, film->content ()) {
201                 if (i->video) {
202                         _views.push_back (shared_ptr<TimelineView> (new TimelineVideoContentView (*this, i)));
203                 }
204
205                 if (i->audio && !i->audio->mapping().mapped_output_channels().empty ()) {
206                         _views.push_back (shared_ptr<TimelineView> (new TimelineAudioContentView (*this, i)));
207                 }
208
209                 if (i->subtitle) {
210                         _views.push_back (shared_ptr<TimelineView> (new TimelineSubtitleContentView (*this, i)));
211                 }
212
213                 if (dynamic_pointer_cast<AtmosMXFContent> (i)) {
214                         _views.push_back (shared_ptr<TimelineView> (new TimelineAtmosContentView (*this, i)));
215                 }
216         }
217
218         assign_tracks ();
219         setup_scrollbars ();
220         Refresh ();
221 }
222
223 void
224 Timeline::film_content_changed (int property, bool frequent)
225 {
226         ensure_ui_thread ();
227
228         if (property == AudioContentProperty::STREAMS) {
229                 recreate_views ();
230         } else if (!frequent) {
231                 setup_scrollbars ();
232                 Refresh ();
233         }
234 }
235
236 template <class T>
237 int
238 place (TimelineViewList& views, int& tracks)
239 {
240         int const base = tracks;
241
242         BOOST_FOREACH (shared_ptr<TimelineView> i, views) {
243                 if (!dynamic_pointer_cast<T>(i)) {
244                         continue;
245                 }
246
247                 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (i);
248
249                 int t = base;
250
251                 shared_ptr<Content> content = cv->content();
252                 DCPTimePeriod const content_period (content->position(), content->end());
253
254                 while (true) {
255                         TimelineViewList::iterator j = views.begin();
256                         while (j != views.end()) {
257                                 shared_ptr<T> test = dynamic_pointer_cast<T> (*j);
258                                 if (!test) {
259                                         ++j;
260                                         continue;
261                                 }
262
263                                 shared_ptr<Content> test_content = test->content();
264                                 if (
265                                         test->track() && test->track().get() == t &&
266                                         content_period.overlap(DCPTimePeriod(test_content->position(), test_content->end()))) {
267                                         /* we have an overlap on track `t' */
268                                         ++t;
269                                         break;
270                                 }
271
272                                 ++j;
273                         }
274
275                         if (j == views.end ()) {
276                                 /* no overlap on `t' */
277                                 break;
278                         }
279                 }
280
281                 cv->set_track (t);
282                 tracks = max (tracks, t + 1);
283         }
284
285         return tracks - base;
286 }
287
288 void
289 Timeline::assign_tracks ()
290 {
291         /* Tracks are:
292            Video (mono or left-eye)
293            Video (right-eye)
294            Subtitle 1
295            Subtitle 2
296            Subtitle N
297            Atmos
298            Audio 1
299            Audio 2
300            Audio N
301         */
302
303         _tracks = 0;
304
305         for (TimelineViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
306                 shared_ptr<TimelineContentView> c = dynamic_pointer_cast<TimelineContentView> (*i);
307                 if (c) {
308                         c->unset_track ();
309                 }
310         }
311
312         /* Video */
313
314         bool have_3d = false;
315         BOOST_FOREACH (shared_ptr<TimelineView> i, _views) {
316                 shared_ptr<TimelineVideoContentView> cv = dynamic_pointer_cast<TimelineVideoContentView> (i);
317                 if (!cv) {
318                         continue;
319                 }
320
321                 /* Video on tracks 0 and maybe 1 (left and right eye) */
322                 if (cv->content()->video->frame_type() == VIDEO_FRAME_TYPE_3D_RIGHT) {
323                         cv->set_track (1);
324                         _tracks = max (_tracks, 2);
325                         have_3d = true;
326                 } else {
327                         cv->set_track (0);
328                 }
329         }
330
331         _tracks = max (_tracks, 1);
332
333         /* Subtitle */
334
335         int const subtitle_tracks = place<TimelineSubtitleContentView> (_views, _tracks);
336
337         /* Atmos */
338
339         bool have_atmos = false;
340         BOOST_FOREACH (shared_ptr<TimelineView> i, _views) {
341                 shared_ptr<TimelineVideoContentView> cv = dynamic_pointer_cast<TimelineVideoContentView> (i);
342                 if (!cv) {
343                         continue;
344                 }
345                 if (dynamic_pointer_cast<TimelineAtmosContentView> (i)) {
346                         cv->set_track (_tracks - 1);
347                         have_atmos = true;
348                 }
349         }
350
351         if (have_atmos) {
352                 ++_tracks;
353         }
354
355         /* Audio */
356
357         place<TimelineAudioContentView> (_views, _tracks);
358
359         _labels_view->set_3d (have_3d);
360         _labels_view->set_subtitle_tracks (subtitle_tracks);
361         _labels_view->set_atmos (have_atmos);
362
363         _time_axis_view->set_y (tracks() * track_height() + 64);
364         _reels_view->set_y (8);
365 }
366
367 int
368 Timeline::tracks () const
369 {
370         return _tracks;
371 }
372
373 void
374 Timeline::setup_scrollbars ()
375 {
376         shared_ptr<const Film> film = _film.lock ();
377         if (!film || !_pixels_per_second) {
378                 return;
379         }
380         _main_canvas->SetVirtualSize (*_pixels_per_second * film->length().seconds(), tracks() * track_height() + 96);
381         _main_canvas->SetScrollRate (_x_scroll_rate, _y_scroll_rate);
382 }
383
384 shared_ptr<TimelineView>
385 Timeline::event_to_view (wxMouseEvent& ev)
386 {
387         /* Search backwards through views so that we find the uppermost one first */
388         TimelineViewList::reverse_iterator i = _views.rbegin();
389         Position<int> const p (ev.GetX(), ev.GetY());
390         while (i != _views.rend() && !(*i)->bbox().contains (p)) {
391                 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (*i);
392                 ++i;
393         }
394
395         if (i == _views.rend ()) {
396                 return shared_ptr<TimelineView> ();
397         }
398
399         return *i;
400 }
401
402 void
403 Timeline::left_down (wxMouseEvent& ev)
404 {
405         _left_down = true;
406         _down_point = ev.GetPosition ();
407
408         switch (_tool) {
409         case SELECT:
410                 left_down_select (ev);
411                 break;
412         case ZOOM:
413                 /* Nothing to do */
414                 break;
415         }
416 }
417
418 void
419 Timeline::left_down_select (wxMouseEvent& ev)
420 {
421         shared_ptr<TimelineView> view = event_to_view (ev);
422         shared_ptr<TimelineContentView> content_view = dynamic_pointer_cast<TimelineContentView> (view);
423
424         _down_view.reset ();
425
426         if (content_view) {
427                 _down_view = content_view;
428                 _down_view_position = content_view->content()->position ();
429         }
430
431         for (TimelineViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
432                 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (*i);
433                 if (!cv) {
434                         continue;
435                 }
436
437                 if (!ev.ShiftDown ()) {
438                         cv->set_selected (view == *i);
439                 }
440         }
441
442         if (content_view && ev.ShiftDown ()) {
443                 content_view->set_selected (!content_view->selected ());
444         }
445
446         _first_move = false;
447
448         if (_down_view) {
449                 /* Pre-compute the points that we might snap to */
450                 for (TimelineViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
451                         shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (*i);
452                         if (!cv || cv == _down_view || cv->content() == _down_view->content()) {
453                                 continue;
454                         }
455
456                         _start_snaps.push_back (cv->content()->position());
457                         _end_snaps.push_back (cv->content()->position());
458                         _start_snaps.push_back (cv->content()->end());
459                         _end_snaps.push_back (cv->content()->end());
460
461                         BOOST_FOREACH (DCPTime i, cv->content()->reel_split_points()) {
462                                 _start_snaps.push_back (i);
463                         }
464                 }
465
466                 /* Tell everyone that things might change frequently during the drag */
467                 _down_view->content()->set_change_signals_frequent (true);
468         }
469 }
470
471 void
472 Timeline::left_up (wxMouseEvent& ev)
473 {
474         _left_down = false;
475
476         switch (_tool) {
477         case SELECT:
478                 left_up_select (ev);
479                 break;
480         case ZOOM:
481                 left_up_zoom (ev);
482                 break;
483         }
484 }
485
486 void
487 Timeline::left_up_select (wxMouseEvent& ev)
488 {
489         if (_down_view) {
490                 _down_view->content()->set_change_signals_frequent (false);
491         }
492
493         _content_panel->set_selection (selected_content ());
494         set_position_from_event (ev);
495
496         /* Clear up up the stuff we don't do during drag */
497         assign_tracks ();
498         setup_scrollbars ();
499         Refresh ();
500
501         _start_snaps.clear ();
502         _end_snaps.clear ();
503 }
504
505 void
506 Timeline::left_up_zoom (wxMouseEvent& ev)
507 {
508         _zoom_point = ev.GetPosition ();
509
510         int vsx, vsy;
511         _main_canvas->GetViewStart (&vsx, &vsy);
512
513         wxPoint top_left(min(_down_point.x, _zoom_point->x), min(_down_point.y, _zoom_point->y));
514         wxPoint bottom_right(max(_down_point.x, _zoom_point->x), max(_down_point.y, _zoom_point->y));
515
516         DCPTime time_left = DCPTime::from_seconds((top_left.x + vsx) / *_pixels_per_second);
517         DCPTime time_right = DCPTime::from_seconds((bottom_right.x + vsx) / *_pixels_per_second);
518         _pixels_per_second = GetSize().GetWidth() / (time_right.seconds() - time_left.seconds());
519         setup_scrollbars ();
520         _main_canvas->Scroll (time_left.seconds() * *_pixels_per_second / _x_scroll_rate, wxDefaultCoord);
521
522         _zoom_point = optional<wxPoint> ();
523         Refresh ();
524 }
525
526 void
527 Timeline::mouse_moved (wxMouseEvent& ev)
528 {
529         switch (_tool) {
530         case SELECT:
531                 mouse_moved_select (ev);
532                 break;
533         case ZOOM:
534                 mouse_moved_zoom (ev);
535                 break;
536         }
537 }
538
539 void
540 Timeline::mouse_moved_select (wxMouseEvent& ev)
541 {
542         if (!_left_down) {
543                 return;
544         }
545
546         set_position_from_event (ev);
547 }
548
549 void
550 Timeline::mouse_moved_zoom (wxMouseEvent& ev)
551 {
552         if (!_left_down) {
553                 return;
554         }
555
556         _zoom_point = ev.GetPosition ();
557         Refresh ();
558 }
559
560 void
561 Timeline::right_down (wxMouseEvent& ev)
562 {
563         switch (_tool) {
564         case SELECT:
565                 right_down_select (ev);
566                 break;
567         case ZOOM:
568                 /* Zoom out */
569                 _pixels_per_second = *_pixels_per_second / 2;
570                 setup_scrollbars ();
571                 break;
572         }
573 }
574
575 void
576 Timeline::right_down_select (wxMouseEvent& ev)
577 {
578         shared_ptr<TimelineView> view = event_to_view (ev);
579         shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (view);
580         if (!cv) {
581                 return;
582         }
583
584         if (!cv->selected ()) {
585                 clear_selection ();
586                 cv->set_selected (true);
587         }
588
589         _menu.popup (_film, selected_content (), selected_views (), ev.GetPosition ());
590 }
591
592 void
593 Timeline::maybe_snap (DCPTime a, DCPTime b, optional<DCPTime>& nearest_distance) const
594 {
595         DCPTime const d = a - b;
596         if (!nearest_distance || d.abs() < nearest_distance.get().abs()) {
597                 nearest_distance = d;
598         }
599 }
600
601 void
602 Timeline::set_position_from_event (wxMouseEvent& ev)
603 {
604         if (!_pixels_per_second) {
605                 return;
606         }
607
608         double const pps = _pixels_per_second.get ();
609
610         wxPoint const p = ev.GetPosition();
611
612         if (!_first_move) {
613                 /* We haven't moved yet; in that case, we must move the mouse some reasonable distance
614                    before the drag is considered to have started.
615                 */
616                 int const dist = sqrt (pow (p.x - _down_point.x, 2) + pow (p.y - _down_point.y, 2));
617                 if (dist < 8) {
618                         return;
619                 }
620                 _first_move = true;
621         }
622
623         if (!_down_view) {
624                 return;
625         }
626
627         DCPTime new_position = _down_view_position + DCPTime::from_seconds ((p.x - _down_point.x) / pps);
628
629         if (_snap) {
630
631                 DCPTime const new_end = new_position + _down_view->content()->length_after_trim();
632                 /* Signed `distance' to nearest thing (i.e. negative is left on the timeline,
633                    positive is right).
634                 */
635                 optional<DCPTime> nearest_distance;
636
637                 /* Find the nearest snap point */
638
639                 BOOST_FOREACH (DCPTime i, _start_snaps) {
640                         maybe_snap (i, new_position, nearest_distance);
641                 }
642
643                 BOOST_FOREACH (DCPTime i, _end_snaps) {
644                         maybe_snap (i, new_end, nearest_distance);
645                 }
646
647                 if (nearest_distance) {
648                         /* Snap if it's close; `close' means within a proportion of the time on the timeline */
649                         if (nearest_distance.get().abs() < DCPTime::from_seconds ((width() / pps) / 64)) {
650                                 new_position += nearest_distance.get ();
651                         }
652                 }
653         }
654
655         if (new_position < DCPTime ()) {
656                 new_position = DCPTime ();
657         }
658
659         _down_view->content()->set_position (new_position);
660
661         shared_ptr<Film> film = _film.lock ();
662         DCPOMATIC_ASSERT (film);
663         film->set_sequence (false);
664 }
665
666 void
667 Timeline::force_redraw (dcpomatic::Rect<int> const & r)
668 {
669         RefreshRect (wxRect (r.x, r.y, r.width, r.height), false);
670 }
671
672 shared_ptr<const Film>
673 Timeline::film () const
674 {
675         return _film.lock ();
676 }
677
678 void
679 Timeline::resized ()
680 {
681         setup_scrollbars ();
682 }
683
684 void
685 Timeline::clear_selection ()
686 {
687         for (TimelineViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
688                 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (*i);
689                 if (cv) {
690                         cv->set_selected (false);
691                 }
692         }
693 }
694
695 TimelineContentViewList
696 Timeline::selected_views () const
697 {
698         TimelineContentViewList sel;
699
700         for (TimelineViewList::const_iterator i = _views.begin(); i != _views.end(); ++i) {
701                 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (*i);
702                 if (cv && cv->selected()) {
703                         sel.push_back (cv);
704                 }
705         }
706
707         return sel;
708 }
709
710 ContentList
711 Timeline::selected_content () const
712 {
713         ContentList sel;
714         TimelineContentViewList views = selected_views ();
715
716         for (TimelineContentViewList::const_iterator i = views.begin(); i != views.end(); ++i) {
717                 sel.push_back ((*i)->content ());
718         }
719
720         return sel;
721 }
722
723 void
724 Timeline::set_selection (ContentList selection)
725 {
726         for (TimelineViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
727                 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (*i);
728                 if (cv) {
729                         cv->set_selected (find (selection.begin(), selection.end(), cv->content ()) != selection.end ());
730                 }
731         }
732 }