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